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
28 private IDatabase $db;
29
32
34 private $options = [];
35
39 private $caller = __CLASS__;
40
44 public function __construct( IDatabase $db ) {
45 $this->db = $db;
46 }
47
53 public function add( SelectQueryBuilder $selectQueryBuilder ) {
54 $this->sqbs[] = $selectQueryBuilder;
55 return $this;
56 }
57
63 public function all() {
64 $this->all = $this->db::UNION_ALL;
65 return $this;
66 }
67
79 public function limit( $limit ) {
80 if ( !$this->db->unionSupportsOrderAndLimit() ) {
81 return $this;
82 }
83 $this->options['LIMIT'] = $limit;
84 return $this;
85 }
86
98 public function offset( $offset ) {
99 if ( !$this->db->unionSupportsOrderAndLimit() ) {
100 return $this;
101 }
102 $this->options['OFFSET'] = $offset;
103 return $this;
104 }
105
120 public function orderBy( $fields, $direction = null ) {
121 if ( !$this->db->unionSupportsOrderAndLimit() ) {
122 return $this;
123 }
124 if ( $direction === null ) {
125 $this->mergeOption( 'ORDER BY', $fields );
126 } elseif ( is_array( $fields ) ) {
127 $fieldsWithDirection = [];
128 foreach ( $fields as $field ) {
129 $fieldsWithDirection[] = "$field $direction";
130 }
131 $this->mergeOption( 'ORDER BY', $fieldsWithDirection );
132 } else {
133 $this->mergeOption( 'ORDER BY', "$fields $direction" );
134 }
135 return $this;
136 }
137
144 private function mergeOption( $name, $newArrayOrValue ) {
145 $value = isset( $this->options[$name] )
146 ? (array)$this->options[$name] : [];
147 if ( is_array( $newArrayOrValue ) ) {
148 $value = array_merge( $value, $newArrayOrValue );
149 } else {
150 $value[] = $newArrayOrValue;
151 }
152 $this->options[$name] = $value;
153 }
154
162 public function caller( $fname ) {
163 $this->caller = $fname;
164 return $this;
165 }
166
172 public function fetchResultSet() {
173 $query = new Query( $this->getSQL(), ISQLPlatform::QUERY_CHANGE_NONE, 'SELECT' );
174 return $this->db->query( $query, $this->caller );
175 }
176
187 public function fetchField() {
188 $this->limit( 1 );
189 foreach ( $this->fetchResultSet() as $row ) {
190 $row = (array)$row;
191 if ( count( $row ) !== 1 ) {
192 throw new \UnexpectedValueException(
193 __METHOD__ . ' expects the query to have only one field' );
194 }
195 return $row[ array_key_first( $row ) ];
196 }
197 return false;
198 }
199
209 public function fetchFieldValues() {
210 $values = [];
211 foreach ( $this->fetchResultSet() as $row ) {
212 $row = (array)$row;
213 if ( count( $row ) !== 1 ) {
214 throw new \UnexpectedValueException(
215 __METHOD__ . ' expects the query to have only one field' );
216 }
217 $values[] = $row[ array_key_first( $row ) ];
218 }
219 return $values;
220 }
221
230 public function fetchRow() {
231 $this->limit( 1 );
232 foreach ( $this->fetchResultSet() as $row ) {
233 return $row;
234 }
235 return false;
236 }
237
244 public function getSQL() {
245 $sqls = [];
246 foreach ( $this->sqbs as $sqb ) {
247 $sqls[] = $sqb->getSQL();
248 }
249 return $this->db->unionQueries( $sqls, $this->all, $this->options );
250 }
251
252}
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.
const SORT_DESC
sort the results in descending order
__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.
const SORT_ASC
sort the results in ascending order
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.
Interface to a relational database.
Definition IDatabase.php:45
const UNION_DISTINCT
Parameter to unionQueries() for UNION DISTINCT.
Interface for query language.