XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
Query.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) NAVER <http://www.navercorp.com> */
3 
9 class Query extends BaseObject
10 {
11 
16  var $queryID;
17 
22  var $action;
23 
28  var $priority;
29 
34  var $columns;
35 
40  var $tables;
41 
47 
52  var $groups;
53 
58  var $orderby;
59 
64  var $limit;
65 
70  var $arguments = NULL;
71 
76  var $columnList = NULL;
77 
83 
97  function __construct($queryID = NULL
98  , $action = NULL
99  , $columns = NULL
100  , $tables = NULL
101  , $conditions = NULL
102  , $groups = NULL
103  , $orderby = NULL
104  , $limit = NULL
105  , $priority = NULL)
106  {
107  $this->queryID = $queryID;
108  $this->action = $action;
109  $this->priority = $priority;
110 
111  if(!isset($tables))
112  {
113  return;
114  }
115 
116  $this->columns = $this->setColumns($columns);
117  $this->tables = $this->setTables($tables);
118  $this->conditions = $this->setConditions($conditions);
119  $this->groups = $this->setGroups($groups);
120  $this->orderby = $this->setOrder($orderby);
121  $this->limit = $this->setLimit($limit);
122  }
123 
124  function show()
125  {
126  return TRUE;
127  }
128 
130  {
131  $this->queryID = $queryID;
132  }
133 
134  function setAction($action)
135  {
136  $this->action = $action;
137  }
138 
140  {
141  $this->priority = $priority;
142  }
143 
145  {
146  $this->columnList = $columnList;
147  if(count($this->columnList) > 0)
148  {
149  $selectColumns = array();
150  $dbParser = DB::getParser();
151 
152  foreach($this->columnList as $columnName)
153  {
154  $columnName = $dbParser->escapeColumn($columnName);
155  $selectColumns[] = new SelectExpression($columnName);
156  }
157  unset($this->columns);
158  $this->columns = $selectColumns;
159  }
160  }
161 
163  {
164  if(!isset($columns) || count($columns) === 0)
165  {
166  $this->columns = array(new StarExpression());
167  return;
168  }
169 
170  if(!is_array($columns))
171  {
172  $columns = array($columns);
173  }
174 
175  $this->columns = $columns;
176  }
177 
178  function setTables($tables)
179  {
180  if(!isset($tables) || count($tables) === 0)
181  {
182  $this->setError(TRUE);
183  $this->setMessage("You must provide at least one table for the query.");
184  return;
185  }
186 
187  if(!is_array($tables))
188  {
189  $tables = array($tables);
190  }
191 
192  $this->tables = $tables;
193  }
194 
195  function setSubquery($subquery)
196  {
197  $this->subquery = $subquery;
198  }
199 
201  {
202  $this->conditions = array();
203  if(!isset($conditions) || count($conditions) === 0)
204  {
205  return;
206  }
207  if(!is_array($conditions))
208  {
209  $conditions = array($conditions);
210  }
211 
212  foreach($conditions as $conditionGroup)
213  {
214  if($conditionGroup->show())
215  {
216  $this->conditions[] = $conditionGroup;
217  }
218  }
219  }
220 
221  function setGroups($groups)
222  {
223  if(!isset($groups) || count($groups) === 0)
224  {
225  return;
226  }
227  if(!is_array($groups))
228  {
229  $groups = array($groups);
230  }
231 
232  $this->groups = $groups;
233  }
234 
235  function setOrder($order)
236  {
237  if(!isset($order) || count($order) === 0)
238  {
239  return;
240  }
241  if(!is_array($order))
242  {
243  $order = array($order);
244  }
245 
246  $this->orderby = $order;
247  }
248 
249  function getOrder()
250  {
251  return $this->orderby;
252  }
253 
254  function setLimit($limit = NULL)
255  {
256  if(!isset($limit))
257  {
258  return;
259  }
260  $this->limit = $limit;
261  }
262 
263  // START Fluent interface
269  function select($columns = NULL)
270  {
271  $this->action = 'select';
272  $this->setColumns($columns);
273  return $this;
274  }
275 
281  function from($tables)
282  {
283  $this->setTables($tables);
284  return $this;
285  }
286 
292  function where($conditions)
293  {
294  $this->setConditions($conditions);
295  return $this;
296  }
297 
303  function groupBy($groups)
304  {
305  $this->setGroups($groups);
306  return $this;
307  }
308 
314  function orderBy($order)
315  {
316  $this->setOrder($order);
317  return $this;
318  }
319 
325  function limit($limit)
326  {
327  $this->setLimit($limit);
328  return $this;
329  }
330 
331  // END Fluent interface
332 
333  function getAction()
334  {
335  return $this->action;
336  }
337 
338  function getPriority()
339  {
340  return $this->priority ? 'LOW_PRIORITY' : '';
341  }
342 
349  function usesClickCount()
350  {
351  return count($this->getClickCountColumns()) > 0;
352  }
353 
355  {
356  $click_count_columns = array();
357  foreach($this->columns as $column)
358  {
359  if($column->show() && is_a($column, 'ClickCountExpression'))
360  {
361  $click_count_columns[] = $column;
362  }
363  }
364  return $click_count_columns;
365  }
366 
372  function getSelectString($with_values = TRUE)
373  {
374  foreach($this->columns as $column)
375  {
376  if($column->show())
377  {
378  if($column->isSubquery())
379  {
380  $select[] = $column->toString($with_values) . ' as ' . $column->getAlias();
381  }
382  else
383  {
384  $select[] = $column->getExpression($with_values);
385  }
386  }
387  }
388  return trim(implode($select, ', '));
389  }
390 
396  function getUpdateString($with_values = TRUE)
397  {
398  foreach($this->columns as $column)
399  {
400  if($column->show())
401  {
402  $update[] = $column->getExpression($with_values);
403  }
404  }
405 
406  if(!$update) return;
407  return trim(implode($update, ', '));
408  }
409 
415  function getInsertString($with_values = TRUE)
416  {
417  $columnsList = '';
418  // means we have insert-select
419  if($this->subquery)
420  {
421  foreach($this->columns as $column)
422  {
423  $columnsList .= $column->getColumnName() . ', ';
424  }
425  $columnsList = substr($columnsList, 0, -2);
426  $selectStatement = $this->subquery->toString($with_values);
427  $selectStatement = substr($selectStatement, 1, -1);
428  return "($columnsList) \n $selectStatement";
429  }
430 
431  $valuesList = '';
432  foreach($this->columns as $column)
433  {
434  if($column->show())
435  {
436  $columnsList .= $column->getColumnName() . ', ';
437  $valuesList .= $column->getValue($with_values) . ', ';
438  }
439  }
440  $columnsList = substr($columnsList, 0, -2);
441  $valuesList = substr($valuesList, 0, -2);
442 
443  return "($columnsList) \n VALUES ($valuesList)";
444  }
445 
446  function getTables()
447  {
448  return $this->tables;
449  }
450 
459  function getFromString($with_values = TRUE)
460  {
461  $from = '';
462  $simple_table_count = 0;
463  foreach($this->tables as $table)
464  {
465  if($table->isJoinTable() || !$simple_table_count)
466  {
467  $from .= $table->toString($with_values) . ' ';
468  }
469  else
470  {
471  $from .= ', ' . $table->toString($with_values) . ' ';
472  }
473 
474  if(is_a($table, 'Subquery'))
475  {
476  $from .= $table->getAlias() ? ' as ' . $table->getAlias() . ' ' : ' ';
477  }
478 
479  $simple_table_count++;
480  }
481  if(trim($from) == '')
482  {
483  return '';
484  }
485  return $from;
486  }
487 
494  function getWhereString($with_values = TRUE, $with_optimization = TRUE)
495  {
496  $where = '';
497  $condition_count = 0;
498 
499  foreach($this->conditions as $conditionGroup)
500  {
501  if($condition_count === 0)
502  {
503  $conditionGroup->setPipe("");
504  }
505  $condition_string = $conditionGroup->toString($with_values);
506  $where .= $condition_string;
507  $condition_count++;
508  }
509 
510  if($with_optimization &&
511  (strstr($this->getOrderByString(), 'list_order') || strstr($this->getOrderByString(), 'update_order')))
512  {
513 
514  if($condition_count !== 0)
515  {
516  $where = '(' . $where . ') ';
517  }
518 
519  foreach($this->orderby as $order)
520  {
521  $colName = $order->getColumnName();
522  if(strstr($colName, 'list_order') || strstr($colName, 'update_order'))
523  {
524  $opt_condition = new ConditionWithoutArgument($colName, 2100000000, 'less', 'and');
525  if($condition_count === 0)
526  {
527  $opt_condition->setPipe("");
528  }
529  $where .= $opt_condition->toString($with_values) . ' ';
530  $condition_count++;
531  }
532  }
533  }
534 
535  return trim($where);
536  }
537 
542  function getGroupByString()
543  {
544  $groupBy = '';
545  if($this->groups)
546  {
547  if($this->groups[0] !== "")
548  {
549  $groupBy = implode(', ', $this->groups);
550  }
551  }
552  return $groupBy;
553  }
554 
559  function getOrderByString()
560  {
561  if(!$this->_orderByString)
562  {
563  if(count($this->orderby) === 0)
564  {
565  return '';
566  }
567  $orderBy = '';
568  foreach($this->orderby as $order)
569  {
570  $orderBy .= $order->toString() . ', ';
571  }
572  $orderBy = substr($orderBy, 0, -2);
573  $this->_orderByString = $orderBy;
574  }
575  return $this->_orderByString;
576  }
577 
578  function getLimit()
579  {
580  return $this->limit;
581  }
582 
587  function getLimitString()
588  {
589  $limit = '';
590  if(count($this->limit) > 0)
591  {
592  $limit = '';
593  $limit .= $this->limit->toString();
594  }
595  return $limit;
596  }
597 
598  function getFirstTableName()
599  {
600  return $this->tables[0]->getName();
601  }
602 
607  function getArguments()
608  {
609  if(!isset($this->arguments))
610  {
611  $this->arguments = array();
612 
613  // Join table arguments
614  if(count($this->tables) > 0)
615  {
616  foreach($this->tables as $table)
617  {
618  if($table->isJoinTable() || is_a($table, 'Subquery'))
619  {
620  $args = $table->getArguments();
621  if($args)
622  {
623  $this->arguments = array_merge($this->arguments, $args);
624  }
625  }
626  }
627  }
628 
629  // Column arguments
630  // The if is for delete statements, all others must have columns
631  if(count($this->columns) > 0)
632  {
633  foreach($this->columns as $column)
634  {
635  if($column->show())
636  {
637  $args = $column->getArguments();
638  if($args)
639  {
640  $this->arguments = array_merge($this->arguments, $args);
641  }
642  }
643  }
644  }
645 
646  // Condition arguments
647  if(count($this->conditions) > 0)
648  {
649  foreach($this->conditions as $conditionGroup)
650  {
651  $args = $conditionGroup->getArguments();
652  if(count($args) > 0)
653  {
654  $this->arguments = array_merge($this->arguments, $args);
655  }
656  }
657  }
658 
659  // Navigation arguments
660  if(count($this->orderby) > 0)
661  {
662  foreach($this->orderby as $order)
663  {
664  $args = $order->getArguments();
665  if(count($args) > 0)
666  {
667  $this->arguments = array_merge($this->arguments, $args);
668  }
669  }
670  }
671  }
672  return $this->arguments;
673  }
674 
675 }
676 /* End of file Query.class.php */
677 /* Location: ./classes/db/queryparts/Query.class.php */
setLimit($limit=NULL)
setSubquery($subquery)
limit($limit)
setConditions($conditions)
getTables()
getWhereString($with_values=TRUE, $with_optimization=TRUE)
where($conditions)
setGroups($groups)
__construct($queryID=NULL, $action=NULL, $columns=NULL, $tables=NULL, $conditions=NULL, $groups=NULL, $orderby=NULL, $limit=NULL, $priority=NULL)
Definition: Query.class.php:97
getOrder()
getUpdateString($with_values=TRUE)
setPriority($priority)
getParser($force=FALSE)
Definition: DB.class.php:1345
usesClickCount()
groupBy($groups)
setError($error=0)
$args
Definition: ko.install.php:185
getLimitString()
$_orderByString
Definition: Query.class.php:82
setTables($tables)
setColumnList($columnList)
getPriority()
setAction($action)
getLimit()
getInsertString($with_values=TRUE)
getSelectString($with_values=TRUE)
select($columns=NULL)
getFirstTableName()
from($tables)
getArguments()
getAction()
setOrder($order)
getOrderByString()
setMessage($message= 'success', $type=NULL)
getClickCountColumns()
getGroupByString()
orderBy($order)
setColumns($columns)
getFromString($with_values=TRUE)
setQueryId($queryID)