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;
173 $query =
new Query( $this->
getSQL(), ISQLPlatform::QUERY_CHANGE_NONE,
'SELECT' );
174 return $this->db->query( $query, $this->
caller );
191 if ( count( $row ) !== 1 ) {
192 throw new \UnexpectedValueException(
193 __METHOD__ .
' expects the query to have only one field' );
195 return $row[ array_key_first( $row ) ];
213 if ( count( $row ) !== 1 ) {
214 throw new \UnexpectedValueException(
215 __METHOD__ .
' expects the query to have only one field' );
217 $values[] = $row[ array_key_first( $row ) ];
246 foreach ( $this->sqbs as $sqb ) {
247 $sqls[] = $sqb->getSQL();
249 return $this->db->unionQueries( $sqls, $this->
all, $this->options );