34 private $options = [];
39 private $caller = __CLASS__;
54 $this->sqbs[] = $selectQueryBuilder;
63 public function all() {
64 $this->
all = $this->db::UNION_ALL;
79 public function limit( $limit ) {
80 if ( !$this->db->unionSupportsOrderAndLimit() ) {
83 $this->options[
'LIMIT'] = $limit;
99 if ( !$this->db->unionSupportsOrderAndLimit() ) {
102 $this->options[
'OFFSET'] = $offset;
120 public function orderBy( $fields, $direction =
null ) {
121 if ( !$this->db->unionSupportsOrderAndLimit() ) {
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";
131 $this->mergeOption(
'ORDER BY', $fieldsWithDirection );
133 $this->mergeOption(
'ORDER BY',
"$fields $direction" );
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 );
150 $value[] = $newArrayOrValue;
152 $this->options[$name] = $value;
174 if ( defined(
'MW_PHPUNIT_TEST' ) && str_contains( $this->db->getSoftwareLink(),
'MySQL' ) ) {
177 $resultSets = array_map(
static fn ( $qb ) => $qb->fetchResultSet(), $this->sqbs );
179 foreach ( $resultSets as $resultSet ) {
180 $res = array_merge( $res, iterator_to_array( $resultSet ) );
182 if ( $this->
all === $this->db::UNION_DISTINCT ) {
183 $res = array_unique( $res );
188 $query =
new Query( $this->
getSQL(), ISQLPlatform::QUERY_CHANGE_NONE,
'SELECT' );
189 return $this->db->query( $query, $this->
caller );
206 if ( count( $row ) !== 1 ) {
207 throw new \UnexpectedValueException(
208 __METHOD__ .
' expects the query to have only one field' );
210 return $row[ array_key_first( $row ) ];
228 if ( count( $row ) !== 1 ) {
229 throw new \UnexpectedValueException(
230 __METHOD__ .
' expects the query to have only one field' );
232 $values[] = $row[ array_key_first( $row ) ];
261 foreach ( $this->sqbs as $sqb ) {
262 $sqls[] = $sqb->getSQL();
264 return $this->db->unionQueries( $sqls, $this->
all, $this->options );