25 private $all = IReadableDatabase::UNION_DISTINCT;
27 private $options = [];
32 private $caller = __CLASS__;
49 $this->sqbs[] = $selectQueryBuilder;
58 public function all() {
59 $this->
all = $this->db::UNION_ALL;
74 public function limit( $limit ) {
75 if ( !$this->db->unionSupportsOrderAndLimit() ) {
78 $this->options[
'LIMIT'] = $limit;
94 if ( !$this->db->unionSupportsOrderAndLimit() ) {
97 $this->options[
'OFFSET'] = $offset;
113 public function orderBy( $fields, $direction =
null ) {
114 if ( !$this->db->unionSupportsOrderAndLimit() ) {
117 if ( $direction ===
null ) {
118 $this->mergeOption(
'ORDER BY', $fields );
119 } elseif ( is_array( $fields ) ) {
120 $fieldsWithDirection = [];
121 foreach ( $fields as $field ) {
122 $fieldsWithDirection[] =
"$field $direction";
124 $this->mergeOption(
'ORDER BY', $fieldsWithDirection );
126 $this->mergeOption(
'ORDER BY',
"$fields $direction" );
137 private function mergeOption( $name, $newArrayOrValue ) {
138 $value = isset( $this->options[$name] )
139 ? (array)$this->options[$name] : [];
140 if ( is_array( $newArrayOrValue ) ) {
141 $value = array_merge( $value, $newArrayOrValue );
143 $value[] = $newArrayOrValue;
145 $this->options[$name] = $value;
167 foreach ( $this->sqbs as $sqb ) {
168 $sqls[] = $sqb->getSQL();
169 $tables = array_merge( $tables, $sqb->getQueryInfo()[
'tables'] );
171 $sql = $this->db->unionQueries( $sqls, $this->
all, $this->options );
172 $query =
new Query( $sql, ISQLPlatform::QUERY_CHANGE_NONE,
'SELECT', $tables );
173 return $this->db->query( $query, $this->
caller );