MediaWiki  master
SelectQueryBuilder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
9  private $fields = [];
10 
14  private $conds = [];
15 
19  private $caller = __CLASS__;
20 
24  private $options = [];
25 
29  private $nextAutoAlias = 1;
30 
32  private $db;
33 
39  public function __construct( IDatabase $db ) {
40  $this->db = $db;
41  }
42 
50  public function connection( IDatabase $db ) {
51  if ( $this->db->getType() !== $db->getType() ) {
52  throw new \InvalidArgumentException( __METHOD__ .
53  ' cannot switch to a database of a different type.' );
54  }
55  $this->db = $db;
56  return $this;
57  }
58 
76  public function queryInfo( $info ) {
77  if ( isset( $info['tables'] ) ) {
78  $this->rawTables( $info['tables'] );
79  }
80  if ( isset( $info['fields'] ) ) {
81  $this->fields( $info['fields'] );
82  }
83  if ( isset( $info['conds'] ) ) {
84  $this->where( $info['conds'] );
85  }
86  if ( isset( $info['options'] ) ) {
87  $this->options( (array)$info['options'] );
88  }
89  if ( isset( $info['join_conds'] ) ) {
90  $this->joinConds( (array)$info['join_conds'] );
91  }
92  return $this;
93  }
94 
107  public function rawTables( $tables ) {
108  if ( is_array( $tables ) ) {
109  $this->tables = array_merge( $this->tables, $tables );
110  } elseif ( is_string( $tables ) ) {
111  $this->tables[] = $tables;
112  } else {
113  throw new \InvalidArgumentException( __METHOD__ .
114  ': $tables must be a string or array' );
115  }
116  return $this;
117  }
118 
124  public function newSubquery() {
125  return new self( $this->db );
126  }
127 
135  public function from( $table, $alias = null ) {
136  return $this->table( $table, $alias );
137  }
138 
145  public function tables( $tables ) {
146  foreach ( $tables as $alias => $table ) {
147  if ( is_string( $alias ) ) {
148  $this->table( $table, $alias );
149  } else {
150  $this->table( $table );
151  }
152  }
153  return $this;
154  }
155 
166  public function fields( $fields ) {
167  if ( is_array( $fields ) ) {
168  $this->fields = array_merge( $this->fields, $fields );
169  } else {
170  $this->fields[] = $fields;
171  }
172  return $this;
173  }
174 
181  public function select( $fields ) {
182  return $this->fields( $fields );
183  }
184 
193  public function field( $field, $alias = null ) {
194  if ( $alias === null ) {
195  $this->fields[] = $field;
196  } else {
197  $this->fields[$alias] = $field;
198  }
199  return $this;
200  }
201 
238  public function where( $conds ) {
239  if ( is_array( $conds ) ) {
240  $this->conds = array_merge( $this->conds, $conds );
241  } else {
242  $this->conds[] = $conds;
243  }
244  return $this;
245  }
246 
253  public function andWhere( $conds ) {
254  return $this->where( $conds );
255  }
256 
263  public function conds( $conds ) {
264  return $this->where( $conds );
265  }
266 
275  public function joinConds( array $joinConds ) {
276  $this->joinConds = array_merge( $this->joinConds, $joinConds );
277  return $this;
278  }
279 
285  protected function getAutoAlias() {
286  return 'sqb' . ( $this->nextAutoAlias++ );
287  }
288 
295  public function newJoinGroup() {
296  return new JoinGroup( $this->getAutoAlias() );
297  }
298 
309  public function offset( $offset ) {
310  $this->options['OFFSET'] = $offset;
311  return $this;
312  }
313 
324  public function limit( $limit ) {
325  $this->options['LIMIT'] = $limit;
326  return $this;
327  }
328 
336  public function lockInShareMode() {
337  $this->options[] = 'LOCK IN SHARE MODE';
338  return $this;
339  }
340 
348  public function forUpdate() {
349  $this->options[] = 'FOR UPDATE';
350  return $this;
351  }
352 
358  public function distinct() {
359  $this->options[] = 'DISTINCT';
360  return $this;
361  }
362 
372  public function groupBy( $group ) {
373  $this->mergeOption( 'GROUP BY', $group );
374  return $this;
375  }
376 
388  public function having( $having ) {
389  $this->mergeOption( 'HAVING', $having );
390  return $this;
391  }
392 
402  public function orderBy( $fields, $direction = null ) {
403  if ( $direction === null ) {
404  $this->mergeOption( 'ORDER BY', $fields );
405  } elseif ( is_array( $fields ) ) {
406  $fieldsWithDirection = [];
407  foreach ( $fields as $field ) {
408  $fieldsWithDirection[] = "$field $direction";
409  }
410  $this->mergeOption( 'ORDER BY', $fieldsWithDirection );
411  } else {
412  $this->mergeOption( 'ORDER BY', "$fields $direction" );
413  }
414  return $this;
415  }
416 
423  private function mergeOption( $name, $newArrayOrValue ) {
424  $value = isset( $this->options[$name] )
425  ? (array)$this->options[$name] : [];
426  if ( is_array( $newArrayOrValue ) ) {
427  $value = array_merge( $value, $newArrayOrValue );
428  } else {
429  $value[] = $newArrayOrValue;
430  }
431  $this->options[$name] = $value;
432  }
433 
446  public function useIndex( $index ) {
447  $this->setIndexHint( 'USE INDEX', $index );
448  return $this;
449  }
450 
463  public function ignoreIndex( $index ) {
464  $this->setIndexHint( 'IGNORE INDEX', $index );
465  return $this;
466  }
467 
474  private function setIndexHint( $type, $value ) {
475  if ( !isset( $this->options[$type] ) ) {
476  $this->options[$type] = [];
477  } elseif ( !is_array( $this->options[$type] ) ) {
478  throw new \UnexpectedValueException(
479  __METHOD__ . ": The $type option cannot be appended to " .
480  'because it is not an array. This may have been caused by a prior ' .
481  'call to option() or options().' );
482  }
483  if ( is_array( $value ) ) {
484  $this->options[$type] = array_merge( $this->options[$type], $value );
485  } elseif ( $this->lastAlias === null ) {
486  throw new \UnexpectedValueException(
487  __METHOD__ . ': Cannot append index value since there is no' .
488  'prior table' );
489  } else {
490  $this->options[$type][$this->lastAlias] = $value;
491  }
492  }
493 
499  public function explain() {
500  $this->options['EXPLAIN'] = true;
501  return $this;
502  }
503 
509  public function straightJoin() {
510  $this->options[] = 'STRAIGHT_JOIN';
511  return $this;
512  }
513 
519  public function bigResult() {
520  $this->options[] = 'SQL_BIG_RESULT';
521  return $this;
522  }
523 
529  public function bufferResult() {
530  $this->options[] = 'SQL_BUFFER_RESULT';
531  return $this;
532  }
533 
539  public function smallResult() {
540  $this->options[] = 'SQL_SMALL_RESULT';
541  return $this;
542  }
543 
549  public function calcFoundRows() {
550  $this->options[] = 'SQL_CALC_FOUND_ROWS';
551  return $this;
552  }
553 
562  public function option( $name, $value = null ) {
563  if ( $value === null ) {
564  $this->options[] = $name;
565  } else {
566  $this->options[$name] = $value;
567  }
568  return $this;
569  }
570 
578  public function options( array $options ) {
579  $this->options = array_merge( $this->options, $options );
580  return $this;
581  }
582 
589  public function caller( $fname ) {
590  $this->caller = $fname;
591  return $this;
592  }
593 
599  public function fetchResultSet() {
600  return $this->db->select( $this->tables, $this->fields, $this->conds, $this->caller,
601  $this->options, $this->joinConds );
602  }
603 
611  public function fetchField() {
612  if ( count( $this->fields ) !== 1 ) {
613  throw new \UnexpectedValueException(
614  __METHOD__ . ' expects the query to have only one field' );
615  }
616  $field = reset( $this->fields );
617  return $this->db->selectField( $this->tables, $field, $this->conds, $this->caller,
618  $this->options, $this->joinConds );
619  }
620 
628  public function fetchFieldValues() {
629  if ( count( $this->fields ) !== 1 ) {
630  throw new \UnexpectedValueException(
631  __METHOD__ . ' expects the query to have only one field' );
632  }
633  $field = reset( $this->fields );
634  return $this->db->selectFieldValues( $this->tables, $field, $this->conds, $this->caller,
635  $this->options, $this->joinConds );
636  }
637 
644  public function fetchRow() {
645  return $this->db->selectRow( $this->tables, $this->fields, $this->conds, $this->caller,
646  $this->options, $this->joinConds );
647  }
648 
656  public function fetchRowCount() {
657  return $this->db->selectRowCount( $this->tables, $this->getRowCountVar(), $this->conds,
658  $this->caller, $this->options, $this->joinConds );
659  }
660 
671  public function estimateRowCount() {
672  return $this->db->estimateRowCount( $this->tables, $this->getRowCountVar(), $this->conds,
673  $this->caller, $this->options, $this->joinConds );
674  }
675 
682  private function getRowCountVar() {
683  if ( count( $this->fields ) === 0 ) {
684  return '*';
685  } elseif ( count( $this->fields ) === 1 ) {
686  return reset( $this->fields );
687  } else {
688  throw new \UnexpectedValueException(
689  __METHOD__ . ' expects the query to have at most one field' );
690  }
691  }
692 
698  public function lockForUpdate() {
699  return $this->db->lockForUpdate( $this->tables, $this->conds, $this->caller,
700  $this->options, $this->joinConds );
701  }
702 
714  public function buildGroupConcatField( $delim ) {
715  if ( count( $this->fields ) !== 1 ) {
716  throw new \UnexpectedValueException(
717  __METHOD__ . ' expects the query to have only one field' );
718  }
719  $field = reset( $this->fields );
720  return $this->db->buildGroupConcatField( $delim, $this->tables, $field,
721  $this->conds, $this->joinConds );
722  }
723 
729  public function getSQL() {
730  return $this->db->selectSQLText( $this->tables, $this->fields, $this->conds, $this->caller,
731  $this->options, $this->joinConds );
732  }
733 
745  public function getQueryInfo() {
746  return [
747  'tables' => $this->tables,
748  'fields' => $this->fields,
749  'conds' => $this->conds,
750  'options' => $this->options,
751  'join_conds' => $this->joinConds
752  ];
753  }
754 }
Wikimedia\Rdbms\JoinGroupBase\$joinConds
array $joinConds
Definition: JoinGroupBase.php:14
Wikimedia\Rdbms\SelectQueryBuilder\newSubquery
newSubquery()
Get an empty SelectQueryBuilder which can be used to build a subquery of this query.
Definition: SelectQueryBuilder.php:124
Wikimedia\Rdbms\SelectQueryBuilder\calcFoundRows
calcFoundRows()
Enable the SQL_CALC_FOUND_ROWS option.
Definition: SelectQueryBuilder.php:549
Wikimedia\Rdbms\SelectQueryBuilder\joinConds
joinConds(array $joinConds)
Manually append to the $join_conds array which will be passed to IDatabase::select().
Definition: SelectQueryBuilder.php:275
Wikimedia\Rdbms\SelectQueryBuilder\$conds
array $conds
The conditions to be passed to IDatabase::select()
Definition: SelectQueryBuilder.php:14
Wikimedia\Rdbms\SelectQueryBuilder\mergeOption
mergeOption( $name, $newArrayOrValue)
Add a value to an option which may be not set or a string or array.
Definition: SelectQueryBuilder.php:423
Wikimedia\Rdbms\SelectQueryBuilder\getRowCountVar
getRowCountVar()
Private helper which extracts a field suitable for row counting from the fields array.
Definition: SelectQueryBuilder.php:682
Wikimedia\Rdbms\SelectQueryBuilder\limit
limit( $limit)
Set the query limit.
Definition: SelectQueryBuilder.php:324
Wikimedia\Rdbms\SelectQueryBuilder\options
options(array $options)
Manually set multiple options in the $options array to be passed to IDatabase::select().
Definition: SelectQueryBuilder.php:578
Wikimedia\Rdbms\SelectQueryBuilder\rawTables
rawTables( $tables)
Given a table or table array as might be passed to Database::select(), append it to the existing tabl...
Definition: SelectQueryBuilder.php:107
Wikimedia\Rdbms\SelectQueryBuilder\useIndex
useIndex( $index)
Set a USE INDEX option.
Definition: SelectQueryBuilder.php:446
Wikimedia\Rdbms\SelectQueryBuilder\connection
connection(IDatabase $db)
Change the IDatabase object the query builder is bound to.
Definition: SelectQueryBuilder.php:50
Wikimedia\Rdbms\SelectQueryBuilder\fetchResultSet
fetchResultSet()
Run the constructed SELECT query and return all results.
Definition: SelectQueryBuilder.php:599
Wikimedia\Rdbms\SelectQueryBuilder\tables
tables( $tables)
Add multiple tables.
Definition: SelectQueryBuilder.php:145
Wikimedia\Rdbms\JoinGroup
An object representing a parenthesized group of tables and their join types and conditions.
Definition: JoinGroup.php:9
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\SelectQueryBuilder\getAutoAlias
getAutoAlias()
Get a table alias which is unique to this SelectQueryBuilder.
Definition: SelectQueryBuilder.php:285
Wikimedia\Rdbms\SelectQueryBuilder\$caller
string $caller
The caller (function name) to be passed to IDatabase::select()
Definition: SelectQueryBuilder.php:19
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
Wikimedia\Rdbms\SelectQueryBuilder\bufferResult
bufferResult()
Enable the SQL_BUFFER_RESULT option.
Definition: SelectQueryBuilder.php:529
Wikimedia\Rdbms\SelectQueryBuilder\distinct
distinct()
Enable the DISTINCT option.
Definition: SelectQueryBuilder.php:358
Wikimedia\Rdbms\JoinGroupBase
A class for code shared between SelectQueryBuilder and JoinGroup.
Definition: JoinGroupBase.php:9
Wikimedia\Rdbms\SelectQueryBuilder\__construct
__construct(IDatabase $db)
Definition: SelectQueryBuilder.php:39
Wikimedia\Rdbms\SelectQueryBuilder\newJoinGroup
newJoinGroup()
Create a parenthesized group of joins which can be added to the object like a table.
Definition: SelectQueryBuilder.php:295
Wikimedia\Rdbms\SelectQueryBuilder\caller
caller( $fname)
Set the method name to be included in an SQL comment.
Definition: SelectQueryBuilder.php:589
Wikimedia\Rdbms\SelectQueryBuilder
Definition: SelectQueryBuilder.php:5
Wikimedia\Rdbms\SelectQueryBuilder\forUpdate
forUpdate()
Enable the FOR UPDATE option.
Definition: SelectQueryBuilder.php:348
Wikimedia\Rdbms\JoinGroupBase\$lastAlias
$lastAlias
Definition: JoinGroupBase.php:16
Wikimedia\Rdbms\SelectQueryBuilder\straightJoin
straightJoin()
Enable the STRAIGHT_JOIN option.
Definition: SelectQueryBuilder.php:509
Wikimedia\Rdbms\SelectQueryBuilder\fetchRowCount
fetchRowCount()
Run the SELECT query, and return the number of results.
Definition: SelectQueryBuilder.php:656
Wikimedia\Rdbms\SelectQueryBuilder\fields
fields( $fields)
Add a field or an array of fields to the query.
Definition: SelectQueryBuilder.php:166
Wikimedia\Rdbms\SelectQueryBuilder\lockInShareMode
lockInShareMode()
Enable the LOCK IN SHARE MODE option.
Definition: SelectQueryBuilder.php:336
Wikimedia\Rdbms\SelectQueryBuilder\option
option( $name, $value=null)
Manually set an option in the $options array to be passed to IDatabase::select()
Definition: SelectQueryBuilder.php:562
Wikimedia\Rdbms\SelectQueryBuilder\offset
offset( $offset)
Set the offset.
Definition: SelectQueryBuilder.php:309
Wikimedia\Rdbms\SelectQueryBuilder\$options
array $options
The options to be passed to IDatabase::select()
Definition: SelectQueryBuilder.php:24
Wikimedia\Rdbms\SelectQueryBuilder\fetchRow
fetchRow()
Run the constructed SELECT query, and return the first result row.
Definition: SelectQueryBuilder.php:644
Wikimedia\Rdbms\SelectQueryBuilder\queryInfo
queryInfo( $info)
Set the query parameters to the given values, appending to the values which were already set.
Definition: SelectQueryBuilder.php:76
Wikimedia\Rdbms\SelectQueryBuilder\fetchFieldValues
fetchFieldValues()
Run the constructed SELECT query, and extract a single field from each result row,...
Definition: SelectQueryBuilder.php:628
Wikimedia\Rdbms\JoinGroupBase\$tables
array $tables
Definition: JoinGroupBase.php:11
Wikimedia\Rdbms\SelectQueryBuilder\$nextAutoAlias
int $nextAutoAlias
An integer used to assign automatic aliases to tables and groups.
Definition: SelectQueryBuilder.php:29
Wikimedia\Rdbms\SelectQueryBuilder\lockForUpdate
lockForUpdate()
Run the SELECT query with the FOR UPDATE option.
Definition: SelectQueryBuilder.php:698
Wikimedia\Rdbms\SelectQueryBuilder\getQueryInfo
getQueryInfo()
Get an associative array describing the query in terms of its raw parameters to Database::select().
Definition: SelectQueryBuilder.php:745
Wikimedia\Rdbms\SelectQueryBuilder\conds
conds( $conds)
Add conditions to the query.
Definition: SelectQueryBuilder.php:263
Wikimedia\Rdbms\SelectQueryBuilder\andWhere
andWhere( $conds)
Add conditions to the query.
Definition: SelectQueryBuilder.php:253
Wikimedia\Rdbms\SelectQueryBuilder\bigResult
bigResult()
Enable the SQL_BIG_RESULT option.
Definition: SelectQueryBuilder.php:519
Wikimedia\Rdbms\SelectQueryBuilder\buildGroupConcatField
buildGroupConcatField( $delim)
Build a GROUP_CONCAT or equivalent statement for a query.
Definition: SelectQueryBuilder.php:714
Wikimedia\Rdbms\SelectQueryBuilder\explain
explain()
Make the query be an EXPLAIN SELECT query instead of a SELECT query.
Definition: SelectQueryBuilder.php:499
Wikimedia\Rdbms\SelectQueryBuilder\having
having( $having)
Add a HAVING clause.
Definition: SelectQueryBuilder.php:388
Wikimedia\Rdbms\IDatabase\getType
getType()
Get the type of the DBMS (e.g.
Wikimedia\Rdbms\SelectQueryBuilder\ignoreIndex
ignoreIndex( $index)
Set the IGNORE INDEX option.
Definition: SelectQueryBuilder.php:463
Wikimedia\Rdbms\SelectQueryBuilder\smallResult
smallResult()
Enable the SQL_SMALL_RESULT option.
Definition: SelectQueryBuilder.php:539
Wikimedia\Rdbms\SelectQueryBuilder\setIndexHint
setIndexHint( $type, $value)
Private helper for methods that set index hints.
Definition: SelectQueryBuilder.php:474
Wikimedia\Rdbms\SelectQueryBuilder\where
where( $conds)
Add conditions to the query.
Definition: SelectQueryBuilder.php:238
Wikimedia\Rdbms\SelectQueryBuilder\field
field( $field, $alias=null)
Add a single field to the query, optionally with an alias.
Definition: SelectQueryBuilder.php:193
Wikimedia\Rdbms\SelectQueryBuilder\orderBy
orderBy( $fields, $direction=null)
Set the ORDER BY clause.
Definition: SelectQueryBuilder.php:402
Wikimedia\Rdbms\SelectQueryBuilder\select
select( $fields)
Add a field or an array of fields to the query.
Definition: SelectQueryBuilder.php:181
Wikimedia\Rdbms\SelectQueryBuilder\$db
IDatabase $db
Definition: SelectQueryBuilder.php:32
Wikimedia\Rdbms\SelectQueryBuilder\fetchField
fetchField()
Run the constructed SELECT query, and return a single field extracted from the first result row.
Definition: SelectQueryBuilder.php:611
Wikimedia\Rdbms\SelectQueryBuilder\getSQL
getSQL()
Get the SQL query string which would be used by fetchResultSet().
Definition: SelectQueryBuilder.php:729
Wikimedia\Rdbms\SelectQueryBuilder\groupBy
groupBy( $group)
Add a GROUP BY clause.
Definition: SelectQueryBuilder.php:372
Wikimedia\Rdbms\SelectQueryBuilder\$fields
array $fields
The fields to be passed to IDatabase::select()
Definition: SelectQueryBuilder.php:9
Wikimedia\Rdbms\SelectQueryBuilder\from
from( $table, $alias=null)
Add a single table to the SELECT query.
Definition: SelectQueryBuilder.php:135
Wikimedia\Rdbms\SelectQueryBuilder\estimateRowCount
estimateRowCount()
Estimate the number of rows in dataset.
Definition: SelectQueryBuilder.php:671
Wikimedia\Rdbms\JoinGroupBase\table
table( $table, $alias=null)
Add a single table or a single parenthesized group.
Definition: JoinGroupBase.php:28
$type
$type
Definition: testCompression.php:52