Overview

Namespaces

  • zsql
  • PHP

Classes

  • Database
  • Delete
  • Expression
  • ExtendedQuery
  • Insert
  • Model
  • Query
  • Result
  • Select
  • Update

Exceptions

  • Exception
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace zsql;
  4: 
  5: /**
  6:  * Abstract class for queries that support select-like functionality (delete, 
  7:  * select, update)
  8:  */
  9: abstract class ExtendedQuery extends Query
 10: {
 11:   /**
 12:    * Order direction
 13:    * 
 14:    * @var string
 15:    */
 16:   protected $direction;
 17:   
 18:   /**
 19:    * Group by column
 20:    * 
 21:    * @var string
 22:    */
 23:   protected $group;
 24:   
 25:   /**
 26:    * Limit number
 27:    * 
 28:    * @var integer
 29:    */
 30:   protected $limit;
 31:   
 32:   /**
 33:    * Offset number
 34:    * 
 35:    * @var integer
 36:    */
 37:   protected $offset;
 38:   
 39:   /**
 40:    * Order by column
 41:    * 
 42:    * @var string
 43:    */
 44:   protected $order;
 45:   
 46:   /**
 47:    * Where expressions
 48:    * 
 49:    * @var array
 50:    */
 51:   protected $where;
 52:   
 53:   /**
 54:    * Set group by
 55:    * 
 56:    * @param string $column
 57:    * @return \zsql\ExtendedQuery
 58:    */
 59:   public function group($column)
 60:   {
 61:     $this->group = $column;
 62:     return $this;
 63:   }
 64:   
 65:   /**
 66:    * Set limit
 67:    * 
 68:    * @param integer $limit
 69:    * @param integer $offset
 70:    * @return \zsql\ExtendedQuery
 71:    */
 72:   public function limit($limit, $offset = null)
 73:   {
 74:     $this->limit = (int) $limit;
 75:     if( null !== $offset ) {
 76:       $this->offset = (int) $offset;
 77:     }
 78:     return $this;
 79:   }
 80:   
 81:   /**
 82:    * Set offset
 83:    * 
 84:    * @param integer $offset
 85:    * @param integer $limit
 86:    * @return \zsql\ExtendedQuery
 87:    */
 88:   public function offset($offset, $limit = null)
 89:   {
 90:     $this->offset = (int) $offset;
 91:     if( null !== $limit ) {
 92:       $this->limit = (int) $limit;
 93:     }
 94:     return $this;
 95:   }
 96:   
 97:   /**
 98:    * Set order
 99:    * 
100:    * @param string $order
101:    * @param string $direction
102:    * @return \zsql\ExtendedQuery
103:    */
104:   public function order($order, $direction = 'ASC')
105:   {
106:     $this->order = $order;
107:     $this->direction = $direction;
108:     return $this;
109:   }
110:   
111:   /**
112:    * Push limit clause onto parts
113:    * 
114:    * @return \zsql\ExtendedQuery
115:    */
116:   protected function pushLimit()
117:   {
118:     if( null !== $this->limit ) {
119:       if( null !== $this->offset ) {
120:         $this->parts[] = 'LIMIT ?, ?';
121:         $this->params[] = $this->offset;
122:         $this->params[] = $this->limit;
123:       } else {
124:         $this->parts[] = 'LIMIT ?';
125:         $this->params[] = $this->limit;
126:       }
127:     }
128:     return $this;
129:   }
130:   
131:   /**
132:    * Push group by clause onto parts
133:    * 
134:    * @return \zsql\ExtendedQuery
135:    */
136:   protected function pushGroup()
137:   {
138:     if( $this->group ) {
139:       $this->parts[] = 'GROUP BY';
140:       $this->parts[] = $this->quoteIdentifierIfNotExpression($this->group);
141:     }
142:     return $this;
143:   }
144:   
145:   /**
146:    * Push order by clause onto parts
147:    * 
148:    * @return \zsql\ExtendedQuery
149:    */
150:   protected function pushOrder()
151:   {
152:     if( $this->order ) {
153:       $this->parts[] = 'ORDER BY';
154:       $this->parts[] = $this->quoteIdentifierIfNotExpression($this->order);
155:       $this->parts[] = $this->direction == 'DESC' ? 'DESC' : 'ASC';
156:     }
157:     return $this;
158:   }
159:   
160:   /**
161:    * Push where clause onto parts
162:    * 
163:    * @return \zsql\ExtendedQuery
164:    */
165:   protected function pushWhere()
166:   {
167:     if( empty($this->where) ) {
168:       return $this;
169:     }
170:     
171:     $this->parts[] = 'WHERE';
172:     foreach( $this->where as $w ) {
173:       $where = $w[0];
174:       $value = isset($w[1]) ? $w[1] : null;
175:       $type = isset($w[2]) ? $w[2] : null;
176:       if( $where instanceof Expression ) {
177:         $this->parts[] = (string) $where;
178:       } else if( count($w) == 3 ) {
179:         $this->parts[] = $this->quoteIdentifierIfNotExpression($where);
180:         $this->parts[] = $type;
181:         $tmp = '';
182:         foreach( $value as $val ) {
183:           $tmp .= '?, ';
184:           $this->params[] = $val;
185:         }
186:         $this->parts[] = '(' . substr($tmp, 0, -2) . ')';
187:       } else if( false !== strpos($where, '?') ) {
188:         $this->parts[] = $where; //$this->quoteIdentifierIfNotExpression($where);
189:         $this->params[] = $value;
190:       } else {
191:         $this->parts[] = $this->quoteIdentifierIfNotExpression($where);
192:         $this->parts[] = '=';
193:         if( $value instanceof Expression ) {
194:           $this->parts[] = (string) $value;
195:         } else {
196:           $this->parts[] = '?';
197:           $this->params[] = $value;
198:         }
199:       }
200:       $this->parts[] = '&&';
201:     }
202:     array_pop($this->parts);
203:     return $this;
204:   }
205:   
206:   /**
207:    * Set where
208:    * 
209:    * @param mixed $where
210:    * @param mixed $value
211:    * @return \zsql\ExtendedQuery
212:    */
213:   public function where($where, $value = null)
214:   {
215:     $nArgs = func_num_args();
216:     if( $nArgs >= 2 ) {
217:       $this->where[] = array($where, $value);
218:     } else if( $nArgs >= 1 ) {
219:       $this->where[] = array($where);
220:     }
221:     return $this;
222:   }
223:   
224:   /**
225:    * Set where expression. Shorthand for:
226:    * where(new Expression('string'))
227:    * 
228:    * @param string $where
229:    * @return \zsql\ExtendedQuery
230:    */
231:   public function whereExpr($where)
232:   {
233:     $this->where[] = array(new Expression((string) $where));
234:     return $this;
235:   }
236:   
237:   /**
238:    * Set where in
239:    * 
240:    * @param mixed $where
241:    * @param array $value
242:    * @return \zsql\ExtendedQuery
243:    */
244:   public function whereIn($where, $value)
245:   {
246:     if( !is_array($value) ) {
247:       $value = (array) $value;
248:     }
249:     if( count($value) <= 0 ) {
250:       $this->where[] = array(new Expression('FALSE'));
251:     } else {
252:       $this->where[] = array($where, $value, 'IN');
253:     }
254:     return $this;
255:   }
256: }
API documentation generated by ApiGen 2.8.0