| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\Rdbms; |
| 4 |
|
| 5 |
use DatabaseTestHelper; |
| 6 |
use LogicException; |
| 7 |
use MediaWikiUnitTestCase; |
| 8 |
use Wikimedia\Rdbms\IResultWrapper; |
| 9 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 10 |
use Wikimedia\Rdbms\Subquery; |
| 11 |
use Wikimedia\TestingAccessWrapper; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class SelectQueryBuilderTest extends MediaWikiUnitTestCase { |
| 18 |
|
| 19 |
|
| 20 |
private $db; |
| 21 |
|
| 22 |
|
| 23 |
private $sqb; |
| 24 |
|
| 25 |
protected function setUp(): void { |
| 26 |
$this->db = new DatabaseTestHelper( __CLASS__ . '::' . $this->getName() ); |
| 27 |
$this->sqb = $this->db->newSelectQueryBuilder(); |
| 28 |
} |
| 29 |
|
| 30 |
private function assertSQL( $expected ) { |
| 31 |
$actual = $this->sqb->getSQL(); |
| 32 |
$actual = preg_replace( '/ +/', ' ', $actual ); |
| 33 |
$actual = rtrim( $actual, " " ); |
| 34 |
$this->assertEquals( $expected, $actual ); |
| 35 |
} |
| 36 |
|
| 37 |
public function testNoTable() { |
| 38 |
$this->sqb |
| 39 |
->select( '1' ); |
| 40 |
$this->assertSQL( 'SELECT 1' ); |
| 41 |
} |
| 42 |
|
| 43 |
public function testCondsEtc() { |
| 44 |
$this->sqb |
| 45 |
->table( 'a' ) |
| 46 |
->where( '1' ) |
| 47 |
->andWhere( '2' ) |
| 48 |
->conds( '3' ) |
| 49 |
->field( 'f' ); |
| 50 |
$this->assertSQL( 'SELECT f FROM a WHERE (1) AND (2) AND (3)' ); |
| 51 |
} |
| 52 |
|
| 53 |
public function testConflictingConds() { |
| 54 |
|
| 55 |
$this->sqb |
| 56 |
->select( '1' ) |
| 57 |
->from( 'a' ) |
| 58 |
->where( [ 'k' => 'v1' ] ) |
| 59 |
->andWhere( [ 'k' => 'v2' ] ); |
| 60 |
$this->assertSQL( 'SELECT 1 FROM a WHERE k = \'v1\' AND (k = \'v2\')' ); |
| 61 |
} |
| 62 |
|
| 63 |
public function testTableAlias() { |
| 64 |
$this->sqb |
| 65 |
->table( 't', 'a' ) |
| 66 |
->field( 'f' ); |
| 67 |
$this->assertSQL( 'SELECT f FROM t a' ); |
| 68 |
} |
| 69 |
|
| 70 |
public function testTableIndex() { |
| 71 |
$this->sqb |
| 72 |
->table( 't', null ) |
| 73 |
->useIndex( 'i' ) |
| 74 |
->field( 'f' ); |
| 75 |
$this->assertSQL( 'SELECT f FROM t FORCE INDEX (i)' ); |
| 76 |
} |
| 77 |
|
| 78 |
public function testTableAliasIndex() { |
| 79 |
$this->sqb |
| 80 |
->table( 't', 'a' ) |
| 81 |
->useIndex( 'i' ) |
| 82 |
->field( 'f' ); |
| 83 |
$this->assertSQL( 'SELECT f FROM t a FORCE INDEX (i)' ); |
| 84 |
} |
| 85 |
|
| 86 |
public function testIgnoreIndex() { |
| 87 |
$this->sqb |
| 88 |
->table( 't' ) |
| 89 |
->ignoreIndex( 'i' ) |
| 90 |
->field( 'f' ); |
| 91 |
$this->assertSQL( 'SELECT f FROM t IGNORE INDEX (i)' ); |
| 92 |
} |
| 93 |
|
| 94 |
public function testSubquery() { |
| 95 |
$this->sqb |
| 96 |
->table( |
| 97 |
$this->sqb->newSubquery() |
| 98 |
->field( 'f' ) |
| 99 |
->from( 't' ) |
| 100 |
->useIndex( 'i' ), |
| 101 |
'sq' |
| 102 |
) |
| 103 |
->field( 'sq.f' ); |
| 104 |
$this->assertSQL( 'SELECT sq.f FROM (SELECT f FROM t FORCE INDEX (i) ) sq' ); |
| 105 |
} |
| 106 |
|
| 107 |
public function testSubqueryAsObject() { |
| 108 |
$this->sqb |
| 109 |
->table( |
| 110 |
new Subquery( $this->sqb->newSubquery() |
| 111 |
->field( 'f' ) |
| 112 |
->from( 'ta' ) |
| 113 |
->useIndex( 'i' ) |
| 114 |
->getSQL() ), |
| 115 |
'sq' |
| 116 |
) |
| 117 |
->field( 'sq.f' ); |
| 118 |
$this->assertSQL( 'SELECT sq.f FROM (SELECT f FROM ta FORCE INDEX (i) ) sq' ); |
| 119 |
} |
| 120 |
|
| 121 |
public function testTablesFields() { |
| 122 |
$this->sqb |
| 123 |
->tables( [ 'a' => 'b', 'c' ] ) |
| 124 |
->useIndex( 'ic' ) |
| 125 |
->useIndex( [ 'a' => 'ia' ] ) |
| 126 |
->fields( [ 'a', 'b' ] ); |
| 127 |
$this->assertSQL( 'SELECT a,b FROM b a FORCE INDEX (ia),c FORCE INDEX (ic)' ); |
| 128 |
} |
| 129 |
|
| 130 |
public function testRawTables() { |
| 131 |
$this->sqb |
| 132 |
->select( 'f' ) |
| 133 |
->rawTables( [ 'a' => [ 't1', 't2' ] ] ); |
| 134 |
$this->assertSQL( "SELECT f FROM (t1,t2 )" ); |
| 135 |
} |
| 136 |
|
| 137 |
public function testJoin() { |
| 138 |
$this->sqb |
| 139 |
->table( 'a' ) |
| 140 |
->join( 'b', 'b', 'aa=bb' ) |
| 141 |
->field( '*' ); |
| 142 |
$this->assertSQL( 'SELECT * FROM a JOIN b ON ((aa=bb))' ); |
| 143 |
} |
| 144 |
|
| 145 |
public function testLeftJoin() { |
| 146 |
$this->sqb |
| 147 |
->table( 'a' ) |
| 148 |
->leftJoin( 'b', 'b', 'aa=bb' ) |
| 149 |
->fields( '*' ); |
| 150 |
$this->assertSQL( 'SELECT * FROM a LEFT JOIN b ON ((aa=bb))' ); |
| 151 |
} |
| 152 |
|
| 153 |
public function testStraightJoin() { |
| 154 |
$this->sqb |
| 155 |
->table( 'a' ) |
| 156 |
->straightJoin( 'b', 'b', 'aa=bb' ) |
| 157 |
->fields( '*' ); |
| 158 |
$this->assertSQL( 'SELECT * FROM a JOIN b ON ((aa=bb))' ); |
| 159 |
} |
| 160 |
|
| 161 |
public function testAutoAliasedJoin() { |
| 162 |
$this->sqb |
| 163 |
->table( 'a' ) |
| 164 |
->join( 'b' ) |
| 165 |
->field( '*' ); |
| 166 |
$this->assertSQL( 'SELECT * FROM a JOIN b' ); |
| 167 |
} |
| 168 |
|
| 169 |
public function testAutoAliasedLeftJoin() { |
| 170 |
$this->sqb |
| 171 |
->table( 'a' ) |
| 172 |
->leftJoin( 'b', null, 'aa=bb' ) |
| 173 |
->field( '*' ); |
| 174 |
$this->assertSQL( 'SELECT * FROM a LEFT JOIN b ON ((aa=bb))' ); |
| 175 |
} |
| 176 |
|
| 177 |
public function testLeftJoinGroup() { |
| 178 |
$this->sqb |
| 179 |
->table( 'a' ) |
| 180 |
->field( 'f' ) |
| 181 |
->leftJoin( |
| 182 |
$this->sqb->newJoinGroup() |
| 183 |
->table( 'b' ) |
| 184 |
->leftJoin( 'c', 'c', 'bb=cc' ), |
| 185 |
null, |
| 186 |
'bb=aa' |
| 187 |
); |
| 188 |
$this->assertSQL( 'SELECT f FROM a LEFT JOIN (b LEFT JOIN c ON ((bb=cc))) ON ((bb=aa))' ); |
| 189 |
} |
| 190 |
|
| 191 |
public function testInnerJoinGroup() { |
| 192 |
$this->sqb |
| 193 |
->table( 'a' ) |
| 194 |
->field( 'f' ) |
| 195 |
->join( |
| 196 |
$this->sqb->newJoinGroup() |
| 197 |
->table( 'b' ) |
| 198 |
->join( 'c', 'c', 'bb=cc' ), |
| 199 |
null, |
| 200 |
'bb=aa' |
| 201 |
); |
| 202 |
$this->assertSQL( 'SELECT f FROM a JOIN (b JOIN c ON ((bb=cc))) ON ((bb=aa))' ); |
| 203 |
} |
| 204 |
|
| 205 |
public function testDoubleJoinGroup() { |
| 206 |
$this->sqb |
| 207 |
->table( 'a' ) |
| 208 |
->field( 'f' ) |
| 209 |
->join( |
| 210 |
$this->sqb->newJoinGroup() |
| 211 |
->table( 'b' ) |
| 212 |
->join( |
| 213 |
$this->sqb->newJoinGroup() |
| 214 |
->table( 'c' ) |
| 215 |
->join( 'd', 'd', [] ), |
| 216 |
null, [] ), |
| 217 |
null, |
| 218 |
[] |
| 219 |
); |
| 220 |
$this->assertSQL( 'SELECT f FROM a JOIN (b JOIN (c JOIN d))' ); |
| 221 |
} |
| 222 |
|
| 223 |
public function testInitialJoinGroup() { |
| 224 |
$this->sqb |
| 225 |
->table( |
| 226 |
$this->sqb->newJoinGroup() |
| 227 |
->table( 'a' ) |
| 228 |
->table( 'b' ) |
| 229 |
) |
| 230 |
->field( 'f' ); |
| 231 |
$this->assertSQL( 'SELECT f FROM (a,b )' ); |
| 232 |
} |
| 233 |
|
| 234 |
public function testInitialDoubleJoinGroup() { |
| 235 |
$this->sqb |
| 236 |
->table( $this->sqb->newJoinGroup() |
| 237 |
->table( |
| 238 |
$this->sqb->newJoinGroup() |
| 239 |
->table( 'a' ) |
| 240 |
->table( 'b' ) |
| 241 |
) |
| 242 |
->table( |
| 243 |
$this->sqb->newJoinGroup() |
| 244 |
->table( 'c' ) |
| 245 |
->table( 'd' ) |
| 246 |
) |
| 247 |
) |
| 248 |
->field( 'f' ); |
| 249 |
$this->assertSQL( 'SELECT f FROM ((a,b ),(c,d ) )' ); |
| 250 |
} |
| 251 |
|
| 252 |
public function testDegenerateJoinGroup() { |
| 253 |
$this->sqb |
| 254 |
->table( $this->sqb->newJoinGroup()->table( 'a' ) ) |
| 255 |
->field( 'f' ); |
| 256 |
$this->assertSQL( 'SELECT f FROM a' ); |
| 257 |
} |
| 258 |
|
| 259 |
public function testSubqueryInJoinGroup() { |
| 260 |
$this->sqb |
| 261 |
->field( 'f' ) |
| 262 |
->table( |
| 263 |
$this->sqb->newJoinGroup() |
| 264 |
->table( 'a' ) |
| 265 |
->join( $this->sqb->newSubquery()->select( '1' ) ) |
| 266 |
); |
| 267 |
$this->assertSQL( 'SELECT f FROM (a,(SELECT 1 ) sqb1_0 )' ); |
| 268 |
} |
| 269 |
|
| 270 |
public function testConditionsOnGroup() { |
| 271 |
$this->sqb |
| 272 |
->field( 'f' ) |
| 273 |
->table( 'a' ) |
| 274 |
->join( |
| 275 |
$this->sqb->newJoinGroup() |
| 276 |
->table( 'b' ) |
| 277 |
->table( 'c' ), |
| 278 |
null, |
| 279 |
'aa=bb' ); |
| 280 |
$this->assertSQL( 'SELECT f FROM a JOIN (b,c ) ON ((aa=bb))' ); |
| 281 |
} |
| 282 |
|
| 283 |
public function testJoinToEmpty() { |
| 284 |
$this->expectException( LogicException::class ); |
| 285 |
$this->sqb->join( 'a', 'a', [] ); |
| 286 |
} |
| 287 |
|
| 288 |
public function testJoinToEmptyInJoinGroup() { |
| 289 |
$this->expectException( LogicException::class ); |
| 290 |
$this->sqb->newJoinGroup()->join( 'a', 'a', [] ); |
| 291 |
} |
| 292 |
|
| 293 |
public function testConflictingAlias() { |
| 294 |
$this->expectException( LogicException::class ); |
| 295 |
$this->sqb |
| 296 |
->table( 'a' ) |
| 297 |
->join( 'b' ) |
| 298 |
->join( 'b' ); |
| 299 |
} |
| 300 |
|
| 301 |
public function testConflictingAliasInGroup() { |
| 302 |
$this->expectException( LogicException::class ); |
| 303 |
$this->sqb->newJoinGroup() |
| 304 |
->table( 'a' ) |
| 305 |
->join( 'b' ) |
| 306 |
->join( 'b' ); |
| 307 |
} |
| 308 |
|
| 309 |
public function testJoinConds() { |
| 310 |
$this->sqb |
| 311 |
->field( '*' ) |
| 312 |
->tables( [ 'a', 'b' => 'b' ] ) |
| 313 |
->joinConds( [ 'b' => [ 'LEFT JOIN', 'aa=bb' ] ] ); |
| 314 |
$this->assertSQL( 'SELECT * FROM a LEFT JOIN b ON ((aa=bb))' ); |
| 315 |
} |
| 316 |
|
| 317 |
public function testJoinSubquery() { |
| 318 |
$this->sqb |
| 319 |
->select( 'sq.a' ) |
| 320 |
->from( 't1' ) |
| 321 |
->join( |
| 322 |
$this->sqb->newSubquery()->select( 'a' )->from( 't2' ), |
| 323 |
'sq', |
| 324 |
[] |
| 325 |
); |
| 326 |
$this->assertSQL( 'SELECT sq.a FROM t1 JOIN (SELECT a FROM t2 ) sq' ); |
| 327 |
} |
| 328 |
|
| 329 |
public function testLeftJoinSubquery() { |
| 330 |
$this->sqb |
| 331 |
->select( 'sq.a' ) |
| 332 |
->from( 't1' ) |
| 333 |
->leftJoin( |
| 334 |
$this->sqb->newSubquery()->select( 'a' )->from( 't2' ), |
| 335 |
'sq', |
| 336 |
[ 'aa' => null ] |
| 337 |
); |
| 338 |
$this->assertSQL( 'SELECT sq.a FROM t1 LEFT JOIN (SELECT a FROM t2 ) sq ON (aa IS NULL)' ); |
| 339 |
} |
| 340 |
|
| 341 |
public function testOffsetLimit() { |
| 342 |
$this->sqb |
| 343 |
->select( 'f' ) |
| 344 |
->from( 't' ) |
| 345 |
->offset( 1 ) |
| 346 |
->limit( 2 ); |
| 347 |
$this->assertSQL( 'SELECT f FROM t LIMIT 1,2' ); |
| 348 |
} |
| 349 |
|
| 350 |
public function testLockInShareMode() { |
| 351 |
$this->sqb |
| 352 |
->select( 'f' ) |
| 353 |
->from( 't' ) |
| 354 |
->lockInShareMode(); |
| 355 |
$this->assertSQL( 'SELECT f FROM t LOCK IN SHARE MODE' ); |
| 356 |
} |
| 357 |
|
| 358 |
public function testForUpdate() { |
| 359 |
$this->sqb |
| 360 |
->select( 'f' ) |
| 361 |
->from( 't' ) |
| 362 |
->forUpdate(); |
| 363 |
$this->assertSQL( 'SELECT f FROM t FOR UPDATE' ); |
| 364 |
} |
| 365 |
|
| 366 |
public function testDistinct() { |
| 367 |
$this->sqb |
| 368 |
->select( 'f' ) |
| 369 |
->from( 't' ) |
| 370 |
->distinct(); |
| 371 |
$this->assertSQL( 'SELECT DISTINCT f FROM t' ); |
| 372 |
} |
| 373 |
|
| 374 |
public function testGroupBy() { |
| 375 |
$this->sqb |
| 376 |
->select( 'f' ) |
| 377 |
->from( 't' ) |
| 378 |
->groupBy( [ '1', '2' ] ); |
| 379 |
$this->assertSQL( 'SELECT f FROM t GROUP BY 1,2' ); |
| 380 |
} |
| 381 |
|
| 382 |
public function testHaving() { |
| 383 |
$this->sqb |
| 384 |
->select( 'f' ) |
| 385 |
->from( 't' ) |
| 386 |
->having( [ 'a' => 1 ] ); |
| 387 |
$this->assertSQL( 'SELECT f FROM t HAVING a = 1' ); |
| 388 |
} |
| 389 |
|
| 390 |
public function testOrderBy1() { |
| 391 |
$this->sqb |
| 392 |
->select( [ 'a', 'b', 'c' ] ) |
| 393 |
->from( 't' ) |
| 394 |
->orderBy( 'a' ) |
| 395 |
->orderBy( 'b', 'DESC' ) |
| 396 |
->orderBy( 'c' ); |
| 397 |
$this->assertSQL( 'SELECT a,b,c FROM t ORDER BY a,b DESC,c' ); |
| 398 |
} |
| 399 |
|
| 400 |
public function testOrderBy2() { |
| 401 |
$this->sqb |
| 402 |
->select( [ 'a', 'b', 'c' ] ) |
| 403 |
->from( 't' ) |
| 404 |
->orderBy( [ 'a', 'b', 'c' ] ); |
| 405 |
$this->assertSQL( 'SELECT a,b,c FROM t ORDER BY a,b,c' ); |
| 406 |
} |
| 407 |
|
| 408 |
public function testOrderBy3() { |
| 409 |
$this->sqb |
| 410 |
->select( [ 'a', 'b', 'c' ] ) |
| 411 |
->from( 't' ) |
| 412 |
->orderBy( [ 'a', 'b', 'c' ], 'DESC' ); |
| 413 |
$this->assertSQL( 'SELECT a,b,c FROM t ORDER BY a DESC,b DESC,c DESC' ); |
| 414 |
} |
| 415 |
|
| 416 |
public function testOrderBy4() { |
| 417 |
$this->sqb |
| 418 |
->select( [ 'a', 'b', 'c' ] ) |
| 419 |
->from( 't' ) |
| 420 |
->orderBy( 'a' ) |
| 421 |
->orderBy( [ 'b', 'c' ] ); |
| 422 |
$this->assertSQL( 'SELECT a,b,c FROM t ORDER BY a,b,c' ); |
| 423 |
} |
| 424 |
|
| 425 |
public function testOrderBy5() { |
| 426 |
$this->sqb |
| 427 |
->select( [ 'a', 'b', 'c' ] ) |
| 428 |
->from( 't' ) |
| 429 |
->option( 'ORDER BY', 'a' ) |
| 430 |
->orderBy( [ 'b', 'c' ] ); |
| 431 |
$this->assertSQL( 'SELECT a,b,c FROM t ORDER BY a,b,c' ); |
| 432 |
} |
| 433 |
|
| 434 |
public function testExplain() { |
| 435 |
$this->sqb |
| 436 |
->explain() |
| 437 |
->select( '*' ) |
| 438 |
->from( 't' ); |
| 439 |
$this->assertSQL( 'EXPLAIN SELECT * FROM t' ); |
| 440 |
} |
| 441 |
|
| 442 |
public function testStraightJoinOption() { |
| 443 |
$this->sqb |
| 444 |
->straightJoinOption() |
| 445 |
->select( '1' ) |
| 446 |
->from( 't' ); |
| 447 |
$this->assertSQL( 'SELECT /*! STRAIGHT_JOIN */ 1 FROM t' ); |
| 448 |
} |
| 449 |
|
| 450 |
public function testBigResult() { |
| 451 |
$this->sqb |
| 452 |
->bigResult() |
| 453 |
->select( '1' ) |
| 454 |
->from( 't' ); |
| 455 |
$this->assertSQL( 'SELECT SQL_BIG_RESULT 1 FROM t' ); |
| 456 |
} |
| 457 |
|
| 458 |
public function testSmallResult() { |
| 459 |
$this->sqb |
| 460 |
->smallResult() |
| 461 |
->select( '1' ) |
| 462 |
->from( 't' ); |
| 463 |
$this->assertSQL( 'SELECT SQL_SMALL_RESULT 1 FROM t' ); |
| 464 |
} |
| 465 |
|
| 466 |
public function testCalcFoundRows() { |
| 467 |
$this->sqb |
| 468 |
->calcFoundRows() |
| 469 |
->select( '1' ) |
| 470 |
->from( 't' ); |
| 471 |
$this->assertSQL( 'SELECT SQL_CALC_FOUND_ROWS 1 FROM t' ); |
| 472 |
} |
| 473 |
|
| 474 |
public function testOption() { |
| 475 |
$this->sqb |
| 476 |
->select( 'f' ) |
| 477 |
->from( 't' ) |
| 478 |
->option( 'ORDER BY', 'a' ); |
| 479 |
$this->assertSQL( 'SELECT f FROM t ORDER BY a' ); |
| 480 |
} |
| 481 |
|
| 482 |
public function testOptions() { |
| 483 |
$this->sqb |
| 484 |
->select( '1' ) |
| 485 |
->options( [ 'ORDER BY' => '1', 'GROUP BY' => '2' ] ); |
| 486 |
$this->assertSQL( 'SELECT 1 GROUP BY 2 ORDER BY 1' ); |
| 487 |
} |
| 488 |
|
| 489 |
public function testFetchResultSet() { |
| 490 |
$this->sqb->select( '1' )->caller( __METHOD__ ); |
| 491 |
$res = $this->sqb->fetchResultSet(); |
| 492 |
$this->assertEquals( 'SELECT 1', $this->db->getLastSqls() ); |
| 493 |
$this->assertInstanceOf( IResultWrapper::class, $res ); |
| 494 |
} |
| 495 |
|
| 496 |
public function testFetchField() { |
| 497 |
$this->sqb->select( '1' )->caller( __METHOD__ ); |
| 498 |
$this->sqb->fetchField(); |
| 499 |
$this->assertEquals( 'SELECT 1 LIMIT 1', $this->db->getLastSqls() ); |
| 500 |
} |
| 501 |
|
| 502 |
public function testFetchFieldValues() { |
| 503 |
$this->sqb->select( '1' )->caller( __METHOD__ ); |
| 504 |
$res = $this->sqb->fetchFieldValues(); |
| 505 |
$this->assertEquals( 'SELECT 1 AS value', $this->db->getLastSqls() ); |
| 506 |
$this->assertIsArray( $res ); |
| 507 |
} |
| 508 |
|
| 509 |
public function testFetchRow() { |
| 510 |
$this->sqb->select( '1' )->caller( __METHOD__ ); |
| 511 |
$this->sqb->fetchRow(); |
| 512 |
$this->assertEquals( 'SELECT 1 LIMIT 1', $this->db->getLastSqls() ); |
| 513 |
} |
| 514 |
|
| 515 |
public function testFetchRowCount() { |
| 516 |
$this->sqb->table( 't' )->caller( __METHOD__ ); |
| 517 |
$this->sqb->fetchRowCount(); |
| 518 |
$this->assertEquals( 'SELECT COUNT(*) AS rowcount FROM (SELECT 1 FROM t ) tmp_count', |
| 519 |
$this->db->getLastSqls() ); |
| 520 |
} |
| 521 |
|
| 522 |
public function testFetchRowCountWithField() { |
| 523 |
$this->sqb->table( 't' )->field( 'f' )->caller( __METHOD__ ); |
| 524 |
$this->sqb->fetchRowCount(); |
| 525 |
$this->assertEquals( 'SELECT COUNT(*) AS rowcount FROM (SELECT 1 FROM t WHERE (f IS NOT NULL) ) tmp_count', |
| 526 |
$this->db->getLastSqls() ); |
| 527 |
} |
| 528 |
|
| 529 |
public function testEstimateRowCount() { |
| 530 |
$this->sqb |
| 531 |
->table( 't' ) |
| 532 |
->conds( [ 'a' => 'b' ] ) |
| 533 |
->caller( __METHOD__ ); |
| 534 |
$this->sqb->estimateRowCount(); |
| 535 |
$this->assertEquals( 'SELECT COUNT(*) AS rowcount FROM t WHERE a = \'b\'', |
| 536 |
$this->db->getLastSqls() ); |
| 537 |
} |
| 538 |
|
| 539 |
public function testBuildGroupConcatField() { |
| 540 |
$this->sqb |
| 541 |
->select( 'f' ) |
| 542 |
->from( 't' ) |
| 543 |
->caller( __METHOD__ ); |
| 544 |
$res = $this->sqb->buildGroupConcatField( '|' ); |
| 545 |
$this->assertEquals( '(SELECT GROUP_CONCAT(f SEPARATOR \'|\') FROM t )', |
| 546 |
$res ); |
| 547 |
} |
| 548 |
|
| 549 |
public function testGetSQL() { |
| 550 |
$this->sqb |
| 551 |
->select( 'f' ) |
| 552 |
->from( 't' ) |
| 553 |
->caller( __METHOD__ ); |
| 554 |
$res = $this->sqb->getSQL(); |
| 555 |
$this->assertEquals( 'SELECT f FROM t ', $res ); |
| 556 |
} |
| 557 |
|
| 558 |
public function testGetQueryInfo() { |
| 559 |
$this->sqb |
| 560 |
->select( 'f' ) |
| 561 |
->from( 't' ) |
| 562 |
->conds( [ 'a' => 'b' ] ) |
| 563 |
->join( 'u', 'u', 'tt=uu' ) |
| 564 |
->limit( 1 ) |
| 565 |
->caller( 'foo' ); |
| 566 |
$this->assertEquals( |
| 567 |
[ |
| 568 |
'tables' => [ 't', 'u' => 'u' ], |
| 569 |
'fields' => [ 'f' ], |
| 570 |
'conds' => [ 'a' => 'b' ], |
| 571 |
'options' => [ 'LIMIT' => 1 ], |
| 572 |
'join_conds' => [ 'u' => [ 'JOIN', 'tt=uu' ] ], |
| 573 |
'caller' => 'foo', |
| 574 |
], |
| 575 |
$this->sqb->getQueryInfo() ); |
| 576 |
} |
| 577 |
|
| 578 |
public function testQueryInfo() { |
| 579 |
$this->sqb->queryInfo( |
| 580 |
[ |
| 581 |
'tables' => [ 't', 'u' => 'u' ], |
| 582 |
'fields' => [ 'f' ], |
| 583 |
'conds' => [ 'a' => 'b' ], |
| 584 |
'options' => [ 'LIMIT' => 1 ], |
| 585 |
'join_conds' => [ 'u' => [ 'JOIN', 'tt=uu' ] ] |
| 586 |
] |
| 587 |
); |
| 588 |
$this->assertSQL( "SELECT f FROM t JOIN u ON ((tt=uu)) WHERE a = 'b' LIMIT 1" ); |
| 589 |
} |
| 590 |
|
| 591 |
public function testGetQueryInfoJoins() { |
| 592 |
$this->sqb |
| 593 |
->select( 'f' ) |
| 594 |
->from( 't' ) |
| 595 |
->conds( [ 'a' => 'b' ] ) |
| 596 |
->join( 'u', 'u', 'tt=uu' ) |
| 597 |
->limit( 1 ) |
| 598 |
->caller( 'foo' ); |
| 599 |
$this->assertEquals( |
| 600 |
[ |
| 601 |
'tables' => [ 't', 'u' => 'u' ], |
| 602 |
'fields' => [ 'f' ], |
| 603 |
'conds' => [ 'a' => 'b' ], |
| 604 |
'options' => [ 'LIMIT' => 1 ], |
| 605 |
'joins' => [ 'u' => [ 'JOIN', 'tt=uu' ] ], |
| 606 |
'caller' => 'foo', |
| 607 |
], |
| 608 |
$this->sqb->getQueryInfo( 'joins' ) ); |
| 609 |
} |
| 610 |
|
| 611 |
public function testQueryInfoJoins() { |
| 612 |
$this->sqb->queryInfo( |
| 613 |
[ |
| 614 |
'tables' => [ 't', 'u' => 'u' ], |
| 615 |
'fields' => [ 'f' ], |
| 616 |
'conds' => [ 'a' => 'b' ], |
| 617 |
'options' => [ 'LIMIT' => 1 ], |
| 618 |
'joins' => [ 'u' => [ 'JOIN', 'tt=uu' ] ] |
| 619 |
] |
| 620 |
); |
| 621 |
$this->assertSQL( "SELECT f FROM t JOIN u ON ((tt=uu)) WHERE a = 'b' LIMIT 1" ); |
| 622 |
} |
| 623 |
|
| 624 |
public function testQueryInfoMerge() { |
| 625 |
$this->sqb |
| 626 |
->select( [ 'a', 'b' => 'c' ] ) |
| 627 |
->from( 't' ) |
| 628 |
->where( [ 'a' => '1' ] ) |
| 629 |
->orderBy( 'a' ) |
| 630 |
->queryInfo( [ |
| 631 |
'tables' => [ 'u' => 'u' ], |
| 632 |
'fields' => [ 'd' ], |
| 633 |
'conds' => [ 'a' => '2' ], |
| 634 |
'options' => [ 'LIMIT' => 1 ], |
| 635 |
'joins' => [ 'u' => [ 'JOIN', 'tt=uu' ] ] |
| 636 |
] ); |
| 637 |
$this->assertSQL( "SELECT a,c AS b,d FROM t JOIN u ON ((tt=uu)) WHERE a = '1' AND (a = '2') ORDER BY a LIMIT 1" ); |
| 638 |
} |
| 639 |
|
| 640 |
public function testMerge() { |
| 641 |
$this->sqb |
| 642 |
->select( [ 'a', 'b' => 'c' ] ) |
| 643 |
->from( 't' ) |
| 644 |
->where( [ 'a' => '1' ] ) |
| 645 |
->orderBy( 'a' ) |
| 646 |
->merge( |
| 647 |
( new SelectQueryBuilder( $this->db ) ) |
| 648 |
->select( 'd' ) |
| 649 |
->from( 'u' ) |
| 650 |
->join( 'v', 'v', 'uu=vv' ) |
| 651 |
->where( [ 'a' => '2' ] ) |
| 652 |
->limit( 1 ) |
| 653 |
); |
| 654 |
$this->assertSQL( "SELECT a,c AS b,d FROM t,u JOIN v ON ((uu=vv)) WHERE a = '1' AND (a = '2') ORDER BY a LIMIT 1" ); |
| 655 |
} |
| 656 |
|
| 657 |
public function testMergeCaller() { |
| 658 |
$tsqb = TestingAccessWrapper::newFromObject( $this->sqb ); |
| 659 |
$this->sqb->caller( 'A' ); |
| 660 |
$this->assertSame( 'A', $tsqb->caller ); |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
$this->sqb->merge( new SelectQueryBuilder( $this->db ) ); |
| 665 |
$this->assertSame( 'A', $tsqb->caller ); |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
$this->sqb->merge( |
| 671 |
( new SelectQueryBuilder( $this->db ) ) |
| 672 |
->caller( 'B' ) |
| 673 |
); |
| 674 |
$this->assertSame( 'B', $tsqb->caller ); |
| 675 |
} |
| 676 |
|
| 677 |
public function testAcquireRowLocks() { |
| 678 |
$this->sqb |
| 679 |
->table( 't' ) |
| 680 |
->conds( [ 'a' => 'b' ] ) |
| 681 |
->forUpdate() |
| 682 |
->caller( __METHOD__ ) |
| 683 |
->acquireRowLocks(); |
| 684 |
$this->assertEquals( 'SELECT 1 FROM t WHERE a = \'b\' FOR UPDATE', |
| 685 |
$this->db->getLastSqls() ); |
| 686 |
} |
| 687 |
|
| 688 |
public function testClearFields() { |
| 689 |
$this->sqb |
| 690 |
->fields( [ 'a', 'b' ] ) |
| 691 |
->clearFields() |
| 692 |
->fields( [ 'c', 'd' ] ); |
| 693 |
$this->assertSQL( "SELECT c,d" ); |
| 694 |
} |
| 695 |
} |