35 private $options = [];
40 private $caller = __CLASS__;
55 $this->sqbs[] = $selectQueryBuilder;
64 public function all() {
65 $this->
all = $this->db::UNION_ALL;
80 public function limit( $limit ) {
81 if ( !$this->db->unionSupportsOrderAndLimit() ) {
84 $this->options[
'LIMIT'] = $limit;
100 if ( !$this->db->unionSupportsOrderAndLimit() ) {
103 $this->options[
'OFFSET'] = $offset;
121 public function orderBy( $fields, $direction =
null ) {
122 if ( !$this->db->unionSupportsOrderAndLimit() ) {
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";
132 $this->mergeOption(
'ORDER BY', $fieldsWithDirection );
134 $this->mergeOption(
'ORDER BY',
"$fields $direction" );
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 );
151 $value[] = $newArrayOrValue;
153 $this->options[$name] = $value;
174 $query =
new Query( $this->
getSQL(), ISQLPlatform::QUERY_CHANGE_NONE,
'SELECT' );
175 return $this->db->query( $query, $this->
caller );
192 if ( count( $row ) !== 1 ) {
193 throw new \UnexpectedValueException(
194 __METHOD__ .
' expects the query to have only one field' );
196 return $row[ array_key_first( $row ) ];
214 if ( count( $row ) !== 1 ) {
215 throw new \UnexpectedValueException(
216 __METHOD__ .
' expects the query to have only one field' );
218 $values[] = $row[ array_key_first( $row ) ];
247 foreach ( $this->sqbs as $sqb ) {
248 $sqls[] = $sqb->getSQL();
250 return $this->db->unionQueries( $sqls, $this->
all, $this->options );