# OqlSelectQuery.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /****************************************************************************
    00003  *   Copyright (C) 2008-2009 by Vladlen Y. Koshelev                         *
    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
    00015     final class OqlSelectQuery extends OqlQuery
    00016     {
    00017         private $distinct       = false;
    00018         private $properties     = array();
    00019         private $where          = array();
    00020         private $whereLogic     = array();
    00021         private $groupChain     = array();
    00022         private $orderChain     = array();
    00023         private $havingChain    = array();
    00024         private $limit          = null;
    00025         private $offset         = null;
    00026
    00030         public static function create()
    00031         {
    00032             return new self;
    00033         }
    00034
    00035         public function isDistinct()
    00036         {
    00037             return $this->distinct;
    00038         }
    00039
    00043         public function setDistinct($orly = true)
    00044         {
    00045             $this->distinct = ($orly === true);
    00046
    00047             return $this;
    00048         }
    00049
    00050         public function getProperties()
    00051         {
    00052             return $this->properties;
    00053         }
    00054
    00058         public function addProperties(OqlSelectPropertiesClause $clause)
    00059         {
    00060             $this->properties[] = $clause;
    00061
    00062             return $this;
    00063         }
    00064
    00068         public function setProperties(OqlSelectPropertiesClause $clause)
    00069         {
    00070             $this->properties = array();
    00071             $this->properties[] = $clause;
    00072
    00073             return $this;
    00074         }
    00075
    00079         public function dropProperties()
    00080         {
    00081             $this->properties = array();
    00082
    00083             return $this;
    00084         }
    00085
    00086         public function getWhere()
    00087         {
    00088             return $this->where;
    00089         }
    00090
    00091         public function getWhereLogic()
    00092         {
    00093             return $this->whereLogic;
    00094         }
    00095
    00099         public function where(OqlWhereClause $clause, $logic = null)
    00100         {
    00101             if ($this->where && !$logic) {
    00102                 throw new WrongArgumentException(
    00103                     'you have to specify expression logic'
    00104                 );
    00105
    00106             } else {
    00107                 if (!$this->where && $logic)
    00108                     $logic = null;
    00109
    00110                 $this->where[] = $clause;
    00111                 $this->whereLogic[] = $logic;
    00112             }
    00113
    00114             return $this;
    00115         }
    00116
    00120         public function andWhere(OqlWhereClause $clause)
    00121         {
    00122             $this->where($clause, BinaryExpression::EXPRESSION_AND);
    00123
    00124             return $this;
    00125         }
    00126
    00130         public function orWhere(OqlWhereClause $clause)
    00131         {
    00132             $this->where($clause, BinaryExpression::EXPRESSION_OR);
    00133
    00134             return $this;
    00135         }
    00136
    00140         public function setWhere(OqlWhereClause $clause)
    00141         {
    00142             $this->where = array();
    00143             $this->whereLogic = array();
    00144             $this->where($clause);
    00145
    00146             return $this;
    00147         }
    00148
    00152         public function dropWhere()
    00153         {
    00154             $this->where = array();
    00155             $this->whereLogic = array();
    00156
    00157             return $this;
    00158         }
    00159
    00160         public function getGroupBy()
    00161         {
    00162             return $this->groupChain;
    00163         }
    00164
    00168         public function addGroupBy(OqlProjectionClause $clause)
    00169         {
    00170             $this->groupChain[] = $clause;
    00171
    00172             return $this;
    00173         }
    00174
    00178         public function setGroupBy(OqlProjectionClause $clause)
    00179         {
    00180             $this->groupChain = array();
    00181             $this->groupChain[] = $clause;
    00182
    00183             return $this;
    00184         }
    00185
    00189         public function dropGroupBy()
    00190         {
    00191             $this->groupChain = array();
    00192
    00193             return $this;
    00194         }
    00195
    00196         public function getOrderBy()
    00197         {
    00198             return $this->orderChain;
    00199         }
    00200
    00204         public function addOrderBy(OqlOrderByClause $clause)
    00205         {
    00206             $this->orderChain[] = $clause;
    00207
    00208             return $this;
    00209         }
    00210
    00214         public function setOrderBy(OqlOrderByClause $clause)
    00215         {
    00216             $this->orderChain = array();
    00217             $this->orderChain[] = $clause;
    00218
    00219             return $this;
    00220         }
    00221
    00225         public function dropOrderBy()
    00226         {
    00227             $this->orderChain = array();
    00228
    00229             return $this;
    00230         }
    00231
    00232         public function getHaving()
    00233         {
    00234             return $this->havingChain;
    00235         }
    00236
    00240         public function addHaving(OqlHavingClause $clause)
    00241         {
    00242             $this->havingChain[] = $clause;
    00243
    00244             return $this;
    00245         }
    00246
    00250         public function setHaving(OqlHavingClause $clause)
    00251         {
    00252             $this->havingChain = array();
    00253             $this->havingChain[] = $clause;
    00254
    00255             return $this;
    00256         }
    00257
    00261         public function dropHaving()
    00262         {
    00263             $this->havingChain = array();
    00264
    00265             return $this;
    00266         }
    00270         public function getLimit()
    00271         {
    00272             return $this->limit;
    00273         }
    00274
    00278         public function setLimit(OqlQueryParameter $limit)
    00279         {
    00280             $this->limit = $limit;
    00281
    00282             return $this;
    00283         }
    00284
    00288         public function getOffset()
    00289         {
    00290             return $this->offset;
    00291         }
    00292
    00296         public function setOffset(OqlQueryParameter $offset)
    00297         {
    00298             $this->offset = $offset;
    00299
    00300             return $this;
    00301         }
    00302
    00306         public function toCriteria()
    00307         {
    00308             $criteria = Criteria::create($this->dao)->
    00309                 setDistinct($this->distinct);
    00310
    00311             $projections = array_merge(
    00312                 $this->properties,
    00313                 $this->groupChain,
    00314                 $this->havingChain
    00315             );
    00316             foreach ($projections as $clause) {
    00317                 $criteria->addProjection(
    00318                     $clause->
    00319                         bindAll($this->parameters)->
    00320                         toProjection()
    00321                 );
    00322             }
    00323
    00324             if ($this->where) {
    00325                 if (count($this->where) == 1) {
    00326                     $clause = reset($this->where);
    00327
    00328                     $criteria->add(
    00329                         $clause->
    00330                             bindAll($this->parameters)->
    00331                             toLogic()
    00332                     );
    00333
    00334                 } else {
    00335                     $logic = Expression::chain();
    00336                     foreach ($this->where as $key => $clause) {
    00337                         $expression = $clause->
    00338                             bindAll($this->parameters)->
    00339                             toLogic();
    00340
    00341                         if (
    00342                             $this->whereLogic[$key]
    00343                             == BinaryExpression::EXPRESSION_AND
    00344                         ) {
    00345                             $logic->expAnd($expression);
    00346                         } else {
    00347                             $logic->expOr($expression);
    00348                         }
    00349                     }
    00350
    00351                     $criteria->add($logic);
    00352                 }
    00353             }
    00354
    00355             foreach ($this->orderChain as $clause) {
    00356                 $criteria->addOrder(
    00357                     $clause->
    00358                         bindAll($this->parameters)->
    00359                         toOrder()
    00360                 );
    00361             }
    00362
    00363             if ($this->limit)
    00364                 $criteria->setLimit(
    00365                     $this->limit->evaluate($this->parameters)
    00366                 );
    00367
    00368             if ($this->offset)
    00369                 $criteria->setOffset(
    00370                     $this->offset->evaluate($this->parameters)
    00371                 );
    00372
    00373             return $criteria;
    00374         }
    00375     }
    00376 ?>