ObjectQuery.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2005-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: ObjectQuery.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00024     final class ObjectQuery
00025     {
00026         const SORT_ASC      = 0x0001;
00027         const SORT_DESC     = 0x0002;
00028         const SORT_IS_NULL  = 0x0003;
00029         const SORT_NOT_NULL = 0x0004;
00030         
00031         private $sort       = array();
00032         private $logic      = array();
00033 
00034         private $current    = null;
00035 
00036         private $limit      = null;
00037         private $offset     = null;
00038         
00042         public static function create()
00043         {
00044             return new self;
00045         }
00046         
00050         public function sort($name)
00051         {
00052             if ($this->current)
00053                 $this->sort[$this->current] = self::SORT_ASC;
00054 
00055             $this->current = $name;     
00056 
00057             return $this;
00058         }
00059         
00063         public function dropSort()
00064         {
00065             $this->current = null;
00066             $this->sort = array();
00067             
00068             return $this;
00069         }
00070         
00074         public function asc()
00075         {
00076             return $this->direction(self::SORT_ASC);
00077         }
00078         
00082         public function desc()
00083         {
00084             return $this->direction(self::SORT_DESC);
00085         }
00086         
00090         public function isNull()
00091         {
00092             return $this->direction(self::SORT_IS_NULL);
00093         }
00094         
00098         public function notNull()
00099         {
00100             return $this->direction(self::SORT_NOT_NULL);
00101         }
00102         
00103         public function getLimit()
00104         {
00105             return $this->limit;
00106         }
00107         
00111         public function setLimit($limit)
00112         {
00113             $this->limit = $limit;
00114             
00115             return $this;
00116         }
00117         
00118         public function getOffset()
00119         {
00120             return $this->offset;
00121         }
00122         
00126         public function setOffset($offset)
00127         {
00128             $this->offset = $offset;
00129             
00130             return $this;
00131         }
00132         
00133         public function getLogic()
00134         {
00135             return $this->logic;
00136         }
00137         
00141         public function addLogic(LogicalObject $exp)
00142         {
00143             $this->logic[] = $exp;
00144             
00145             return $this;
00146         }
00147         
00151         public function toSelectQuery(StorableDAO $dao)
00152         {
00153             // cleanup
00154             if ($this->current) {
00155                 $this->sort[$this->current] = self::SORT_ASC;
00156                 $this->current = null;
00157             }
00158             
00159             $query = $dao->makeSelectHead();
00160             
00161             foreach ($this->logic as $exp) {
00162                 $query->andWhere(
00163                     $exp->toMapped($dao, $query)
00164                 );
00165             }
00166             
00167             foreach ($this->sort as $property => $direction) {
00168                 switch ($direction) {
00169                     case self::SORT_ASC:
00170                         
00171                         $query->orderBy(
00172                             OrderBy::create($property)->
00173                             asc()->
00174                             toMapped($dao, $query)
00175                         );
00176                         
00177                         break;
00178                     
00179                     case self::SORT_DESC:
00180                         
00181                         $query->orderBy(
00182                             OrderBy::create($property)->
00183                             desc()->
00184                             toMapped($dao, $query)
00185                         );
00186                         
00187                         break;
00188                     
00189                     case self::SORT_IS_NULL:
00190                         
00191                         $query->orderBy(
00192                             Expression::isNull($property)->toMapped($dao, $query)
00193                         );
00194                         
00195                         break;
00196                     
00197                     case self::SORT_NOT_NULL:
00198                         
00199                         $query->orderBy(
00200                             Expression::notNull($property)->toMapped($dao, $query)
00201                         );
00202                         
00203                         break;
00204                     
00205                     default:
00206                         
00207                         throw new WrongStateException(
00208                             'unknown or unsupported '.
00209                             "direction '{$direction}'"
00210                         );
00211                 }
00212             }
00213             
00214             return $query->limit($this->limit, $this->offset);
00215         }
00216         
00220         private function direction($constant)
00221         {
00222             if (!$this->current)
00223                 throw new WrongStateException(
00224                     'specify property name first'
00225                 );
00226             
00227             $this->sort[$this->current] = $constant;
00228             
00229             $this->current = null;
00230             
00231             return $this;
00232         }
00233     }
00234 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4