| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Rdbms; |
| 4 |
|
| 5 |
use Wikimedia\Rdbms\Platform\ISQLPlatform; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class UnionQueryBuilder { |
| 17 |
|
| 18 |
public const SORT_ASC = 'ASC'; |
| 19 |
|
| 20 |
|
| 21 |
public const SORT_DESC = 'DESC'; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
private $sqbs = []; |
| 27 |
|
| 28 |
|
| 29 |
private $db; |
| 30 |
|
| 31 |
|
| 32 |
private $all = IReadableDatabase::UNION_DISTINCT; |
| 33 |
|
| 34 |
|
| 35 |
private $options = []; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
private $caller = __CLASS__; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function __construct( IDatabase $db ) { |
| 48 |
$this->db = $db; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function add( SelectQueryBuilder $selectQueryBuilder ) { |
| 57 |
$this->sqbs[] = $selectQueryBuilder; |
| 58 |
return $this; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function all() { |
| 67 |
$this->all = $this->db::UNION_ALL; |
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function limit( $limit ) { |
| 83 |
if ( !$this->db->unionSupportsOrderAndLimit() ) { |
| 84 |
return $this; |
| 85 |
} |
| 86 |
$this->options['LIMIT'] = $limit; |
| 87 |
return $this; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function offset( $offset ) { |
| 102 |
if ( !$this->db->unionSupportsOrderAndLimit() ) { |
| 103 |
return $this; |
| 104 |
} |
| 105 |
$this->options['OFFSET'] = $offset; |
| 106 |
return $this; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function orderBy( $fields, $direction = null ) { |
| 124 |
if ( !$this->db->unionSupportsOrderAndLimit() ) { |
| 125 |
return $this; |
| 126 |
} |
| 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"; |
| 133 |
} |
| 134 |
$this->mergeOption( 'ORDER BY', $fieldsWithDirection ); |
| 135 |
} else { |
| 136 |
$this->mergeOption( 'ORDER BY', "$fields $direction" ); |
| 137 |
} |
| 138 |
return $this; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 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 ); |
| 152 |
} else { |
| 153 |
$value[] = $newArrayOrValue; |
| 154 |
} |
| 155 |
$this->options[$name] = $value; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function caller( $fname ) { |
| 166 |
$this->caller = $fname; |
| 167 |
return $this; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function fetchResultSet() { |
| 176 |
$query = new Query( $this->getSQL(), ISQLPlatform::QUERY_CHANGE_NONE, 'SELECT' ); |
| 177 |
return $this->db->query( $query, $this->caller ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function fetchField() { |
| 191 |
$this->limit( 1 ); |
| 192 |
foreach ( $this->fetchResultSet() as $row ) { |
| 193 |
$row = (array)$row; |
| 194 |
if ( count( $row ) !== 1 ) { |
| 195 |
throw new \UnexpectedValueException( |
| 196 |
__METHOD__ . ' expects the query to have only one field' ); |
| 197 |
} |
| 198 |
return $row[ array_key_first( $row ) ]; |
| 199 |
} |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public function fetchFieldValues() { |
| 213 |
$values = []; |
| 214 |
foreach ( $this->fetchResultSet() as $row ) { |
| 215 |
$row = (array)$row; |
| 216 |
if ( count( $row ) !== 1 ) { |
| 217 |
throw new \UnexpectedValueException( |
| 218 |
__METHOD__ . ' expects the query to have only one field' ); |
| 219 |
} |
| 220 |
$values[] = $row[ array_key_first( $row ) ]; |
| 221 |
} |
| 222 |
return $values; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function fetchRow() { |
| 234 |
$this->limit( 1 ); |
| 235 |
foreach ( $this->fetchResultSet() as $row ) { |
| 236 |
return $row; |
| 237 |
} |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public function getSQL() { |
| 248 |
$sqls = []; |
| 249 |
foreach ( $this->sqbs as $sqb ) { |
| 250 |
$sqls[] = $sqb->getSQL(); |
| 251 |
} |
| 252 |
return $this->db->unionQueries( $sqls, $this->all, $this->options ); |
| 253 |
} |
| 254 |
|
| 255 |
} |