MediaWiki master
UnionQueryBuilder.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\Rdbms;
4
6
18 public const SORT_ASC = 'ASC';
19
21 public const SORT_DESC = 'DESC';
22
26 private $sqbs = [];
27
29 private $db;
30
31 private $all = IReadableDatabase::UNION_DISTINCT;
32
33 private $options = [];
34
38 private $caller = __CLASS__;
39
45 public function __construct( IDatabase $db ) {
46 $this->db = $db;
47 }
48
54 public function add( SelectQueryBuilder $selectQueryBuilder ) {
55 $this->sqbs[] = $selectQueryBuilder;
56 return $this;
57 }
58
64 public function all() {
65 $this->all = $this->db::UNION_ALL;
66 return $this;
67 }
68
80 public function limit( $limit ) {
81 if ( !$this->db->unionSupportsOrderAndLimit() ) {
82 return $this;
83 }
84 $this->options['LIMIT'] = $limit;
85 return $this;
86 }
87
99 public function offset( $offset ) {
100 if ( !$this->db->unionSupportsOrderAndLimit() ) {
101 return $this;
102 }
103 $this->options['OFFSET'] = $offset;
104 return $this;
105 }
106
121 public function orderBy( $fields, $direction = null ) {
122 if ( !$this->db->unionSupportsOrderAndLimit() ) {
123 return $this;
124 }
125 if ( $direction === null ) {
126 $this->mergeOption( 'ORDER BY', $fields );
127 } elseif ( is_array( $fields ) ) {
128 $fieldsWithDirection = [];
129 foreach ( $fields as $field ) {
130 $fieldsWithDirection[] = "$field $direction";
131 }
132 $this->mergeOption( 'ORDER BY', $fieldsWithDirection );
133 } else {
134 $this->mergeOption( 'ORDER BY', "$fields $direction" );
135 }
136 return $this;
137 }
138
145 private function mergeOption( $name, $newArrayOrValue ) {
146 $value = isset( $this->options[$name] )
147 ? (array)$this->options[$name] : [];
148 if ( is_array( $newArrayOrValue ) ) {
149 $value = array_merge( $value, $newArrayOrValue );
150 } else {
151 $value[] = $newArrayOrValue;
152 }
153 $this->options[$name] = $value;
154 }
155
163 public function caller( $fname ) {
164 $this->caller = $fname;
165 return $this;
166 }
167
173 public function fetchResultSet() {
174 $query = new Query( $this->getSQL(), ISQLPlatform::QUERY_CHANGE_NONE, 'SELECT' );
175 return $this->db->query( $query, $this->caller );
176 }
177
188 public function fetchField() {
189 $this->limit( 1 );
190 foreach ( $this->fetchResultSet() as $row ) {
191 $row = (array)$row;
192 if ( count( $row ) !== 1 ) {
193 throw new \UnexpectedValueException(
194 __METHOD__ . ' expects the query to have only one field' );
195 }
196 return $row[ array_key_first( $row ) ];
197 }
198 return false;
199 }
200
210 public function fetchFieldValues() {
211 $values = [];
212 foreach ( $this->fetchResultSet() as $row ) {
213 $row = (array)$row;
214 if ( count( $row ) !== 1 ) {
215 throw new \UnexpectedValueException(
216 __METHOD__ . ' expects the query to have only one field' );
217 }
218 $values[] = $row[ array_key_first( $row ) ];
219 }
220 return $values;
221 }
222
231 public function fetchRow() {
232 $this->limit( 1 );
233 foreach ( $this->fetchResultSet() as $row ) {
234 return $row;
235 }
236 return false;
237 }
238
245 public function getSQL() {
246 $sqls = [];
247 foreach ( $this->sqbs as $sqb ) {
248 $sqls[] = $sqb->getSQL();
249 }
250 return $this->db->unionQueries( $sqls, $this->all, $this->options );
251 }
252
253}
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.
fetchFieldValues()
Run the constructed UNION query, and extract a single field from each result row, returning an array ...
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.
fetchField()
Run the constructed UNION query, and return a single field extracted from the first result row.
all()
Enable UNION_ALL option, the default is UNION_DISTINCT.
fetchRow()
Run the constructed UNION query, and return the first result row.
caller( $fname)
Set the method name to be included in an SQL comment.
getSQL()
Get the SQL query string which would be used by fetchResultSet().
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.