MediaWiki  master
UnionQueryBuilder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
6 
20  private $sqbs = [];
21 
23  private $db;
24 
25  private $all = IReadableDatabase::UNION_DISTINCT;
26 
27  private $options = [];
28 
32  private $caller = __CLASS__;
33 
39  public function __construct( IDatabase $db ) {
40  $this->db = $db;
41  }
42 
48  public function add( SelectQueryBuilder $selectQueryBuilder ) {
49  $this->sqbs[] = $selectQueryBuilder;
50  return $this;
51  }
52 
58  public function all() {
59  $this->all = $this->db::UNION_ALL;
60  return $this;
61  }
62 
74  public function limit( $limit ) {
75  if ( !$this->db->unionSupportsOrderAndLimit() ) {
76  return $this;
77  }
78  $this->options['LIMIT'] = $limit;
79  return $this;
80  }
81 
93  public function offset( $offset ) {
94  if ( !$this->db->unionSupportsOrderAndLimit() ) {
95  return $this;
96  }
97  $this->options['OFFSET'] = $offset;
98  return $this;
99  }
100 
113  public function orderBy( $fields, $direction = null ) {
114  if ( !$this->db->unionSupportsOrderAndLimit() ) {
115  return $this;
116  }
117  if ( $direction === null ) {
118  $this->mergeOption( 'ORDER BY', $fields );
119  } elseif ( is_array( $fields ) ) {
120  $fieldsWithDirection = [];
121  foreach ( $fields as $field ) {
122  $fieldsWithDirection[] = "$field $direction";
123  }
124  $this->mergeOption( 'ORDER BY', $fieldsWithDirection );
125  } else {
126  $this->mergeOption( 'ORDER BY', "$fields $direction" );
127  }
128  return $this;
129  }
130 
137  private function mergeOption( $name, $newArrayOrValue ) {
138  $value = isset( $this->options[$name] )
139  ? (array)$this->options[$name] : [];
140  if ( is_array( $newArrayOrValue ) ) {
141  $value = array_merge( $value, $newArrayOrValue );
142  } else {
143  $value[] = $newArrayOrValue;
144  }
145  $this->options[$name] = $value;
146  }
147 
154  public function caller( $fname ) {
155  $this->caller = $fname;
156  return $this;
157  }
158 
164  public function fetchResultSet() {
165  $sqls = [];
166  $tables = [];
167  foreach ( $this->sqbs as $sqb ) {
168  $sqls[] = $sqb->getSQL();
169  $tables = array_merge( $tables, $sqb->getQueryInfo()['tables'] );
170  }
171  $sql = $this->db->unionQueries( $sqls, $this->all, $this->options );
172  $query = new Query( $sql, ISQLPlatform::QUERY_CHANGE_NONE, 'SELECT', $tables );
173  return $this->db->query( $query, $this->caller );
174  }
175 }
Holds information on Query to be executed.
Definition: Query.php:31
Build SELECT queries with a fluent interface.
A query builder for UNION queries takes SelectQueryBuilder objects.
orderBy( $fields, $direction=null)
Set the ORDER BY clause.
__construct(IDatabase $db)
To create a UnionQueryBuilder instance, use $db->newUnionQueryBuilder() instead.
fetchResultSet()
Run the constructed UNION query and return all results.
all()
Enable UNION_ALL option, the default is UNION_DISTINCT.
offset( $offset)
Set the offset.
caller( $fname)
Set the method name to be included in an SQL comment.
add(SelectQueryBuilder $selectQueryBuilder)
Add a select query builder object to the list of union.
limit( $limit)
Set the query limit.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:36
Interface for query language.