35 private $options = [];
40 private $caller = __CLASS__;
57 $this->sqbs[] = $selectQueryBuilder;
66 public function all() {
67 $this->
all = $this->db::UNION_ALL;
82 public function limit( $limit ) {
83 if ( !$this->db->unionSupportsOrderAndLimit() ) {
86 $this->options[
'LIMIT'] = $limit;
102 if ( !$this->db->unionSupportsOrderAndLimit() ) {
105 $this->options[
'OFFSET'] = $offset;
123 public function orderBy( $fields, $direction =
null ) {
124 if ( !$this->db->unionSupportsOrderAndLimit() ) {
127 if ( $direction ===
null ) {
128 $this->mergeOption(
'ORDER BY', $fields );
129 } elseif ( is_array( $fields ) ) {
130 $fieldsWithDirection = [];
131 foreach ( $fields as $field ) {
132 $fieldsWithDirection[] =
"$field $direction";
134 $this->mergeOption(
'ORDER BY', $fieldsWithDirection );
136 $this->mergeOption(
'ORDER BY',
"$fields $direction" );
147 private function mergeOption( $name, $newArrayOrValue ) {
148 $value = isset( $this->options[$name] )
149 ? (array)$this->options[$name] : [];
150 if ( is_array( $newArrayOrValue ) ) {
151 $value = array_merge( $value, $newArrayOrValue );
153 $value[] = $newArrayOrValue;
155 $this->options[$name] = $value;
176 $query =
new Query( $this->
getSQL(), ISQLPlatform::QUERY_CHANGE_NONE,
'SELECT' );
177 return $this->db->query( $query, $this->
caller );
194 if ( count( $row ) !== 1 ) {
195 throw new \UnexpectedValueException(
196 __METHOD__ .
' expects the query to have only one field' );
198 return $row[ array_key_first( $row ) ];
216 if ( count( $row ) !== 1 ) {
217 throw new \UnexpectedValueException(
218 __METHOD__ .
' expects the query to have only one field' );
220 $values[] = $row[ array_key_first( $row ) ];
249 foreach ( $this->sqbs as $sqb ) {
250 $sqls[] = $sqb->getSQL();
252 return $this->db->unionQueries( $sqls, $this->
all, $this->options );