| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
o |
| 23 |
s |
| 24 |
|
| 25 |
|
| 26 |
namespace Wikimedia\Tests\Rdbms; |
| 27 |
|
| 28 |
use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock; |
| 29 |
use MediaWikiCoversValidator; |
| 30 |
use PHPUnit\Framework\TestCase; |
| 31 |
use Wikimedia\Rdbms\DatabaseDomain; |
| 32 |
use Wikimedia\Rdbms\DatabaseMySQL; |
| 33 |
use Wikimedia\Rdbms\IDatabase; |
| 34 |
use Wikimedia\Rdbms\IDatabaseForOwner; |
| 35 |
use Wikimedia\Rdbms\MySQLPrimaryPos; |
| 36 |
use Wikimedia\Rdbms\Platform\MySQLPlatform; |
| 37 |
use Wikimedia\Rdbms\Replication\MysqlReplicationReporter; |
| 38 |
use Wikimedia\TestingAccessWrapper; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class DatabaseMySQLTest extends TestCase { |
| 44 |
|
| 45 |
use MediaWikiCoversValidator; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function testBinLogName() { |
| 51 |
$pos = new MySQLPrimaryPos( "db1052.2424/4643", 1 ); |
| 52 |
|
| 53 |
$this->assertEquals( "db1052.2424", $pos->getLogFile() ); |
| 54 |
$this->assertEquals( [ 2424, 4643 ], $pos->getLogPosition() ); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function testHasReached( |
| 62 |
MySQLPrimaryPos $lowerPos, MySQLPrimaryPos $higherPos, $match, $hetero |
| 63 |
) { |
| 64 |
if ( $match ) { |
| 65 |
if ( $hetero ) { |
| 66 |
|
| 67 |
$this->assertFalse( $higherPos->hasReached( $lowerPos ) ); |
| 68 |
} else { |
| 69 |
$this->assertTrue( $higherPos->hasReached( $lowerPos ) ); |
| 70 |
} |
| 71 |
$this->assertTrue( $lowerPos->hasReached( $lowerPos ) ); |
| 72 |
$this->assertTrue( $higherPos->hasReached( $higherPos ) ); |
| 73 |
$this->assertFalse( $lowerPos->hasReached( $higherPos ) ); |
| 74 |
} else { |
| 75 |
|
| 76 |
$this->assertFalse( $higherPos->hasReached( $lowerPos ) ); |
| 77 |
$this->assertFalse( $lowerPos->hasReached( $higherPos ) ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public static function provideComparePositions() { |
| 82 |
$now = microtime( true ); |
| 83 |
|
| 84 |
return [ |
| 85 |
|
| 86 |
[ |
| 87 |
new MySQLPrimaryPos( 'db1034-bin.000976/843431247', $now ), |
| 88 |
new MySQLPrimaryPos( 'db1034-bin.000976/843431248', $now ), |
| 89 |
true, |
| 90 |
false |
| 91 |
], |
| 92 |
[ |
| 93 |
new MySQLPrimaryPos( 'db1034-bin.000976/999', $now ), |
| 94 |
new MySQLPrimaryPos( 'db1034-bin.000976/1000', $now ), |
| 95 |
true, |
| 96 |
false |
| 97 |
], |
| 98 |
[ |
| 99 |
new MySQLPrimaryPos( 'db1034-bin.000976/999', $now ), |
| 100 |
new MySQLPrimaryPos( 'db1035-bin.000976/1000', $now ), |
| 101 |
false, |
| 102 |
false |
| 103 |
], |
| 104 |
|
| 105 |
[ |
| 106 |
new MySQLPrimaryPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-23', $now ), |
| 107 |
new MySQLPrimaryPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-24', $now ), |
| 108 |
true, |
| 109 |
false |
| 110 |
], |
| 111 |
[ |
| 112 |
new MySQLPrimaryPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-99', $now ), |
| 113 |
new MySQLPrimaryPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ), |
| 114 |
true, |
| 115 |
false |
| 116 |
], |
| 117 |
[ |
| 118 |
new MySQLPrimaryPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-99', $now ), |
| 119 |
new MySQLPrimaryPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ), |
| 120 |
false, |
| 121 |
false |
| 122 |
], |
| 123 |
|
| 124 |
[ |
| 125 |
new MySQLPrimaryPos( '255-11-23', $now ), |
| 126 |
new MySQLPrimaryPos( '255-11-24', $now ), |
| 127 |
true, |
| 128 |
false |
| 129 |
], |
| 130 |
[ |
| 131 |
new MySQLPrimaryPos( '255-11-99', $now ), |
| 132 |
new MySQLPrimaryPos( '255-11-100', $now ), |
| 133 |
true, |
| 134 |
false |
| 135 |
], |
| 136 |
[ |
| 137 |
new MySQLPrimaryPos( '255-11-999', $now ), |
| 138 |
new MySQLPrimaryPos( '254-11-1000', $now ), |
| 139 |
false, |
| 140 |
false |
| 141 |
], |
| 142 |
[ |
| 143 |
new MySQLPrimaryPos( '255-11-23,256-12-50', $now ), |
| 144 |
new MySQLPrimaryPos( '255-11-24', $now ), |
| 145 |
true, |
| 146 |
false |
| 147 |
], |
| 148 |
[ |
| 149 |
new MySQLPrimaryPos( '255-11-99,256-12-50,257-12-50', $now ), |
| 150 |
new MySQLPrimaryPos( '255-11-1000', $now ), |
| 151 |
true, |
| 152 |
false |
| 153 |
], |
| 154 |
[ |
| 155 |
new MySQLPrimaryPos( '255-11-23,256-12-50', $now ), |
| 156 |
new MySQLPrimaryPos( '255-11-24,155-52-63', $now ), |
| 157 |
true, |
| 158 |
false |
| 159 |
], |
| 160 |
[ |
| 161 |
new MySQLPrimaryPos( '255-11-99,256-12-50,257-12-50', $now ), |
| 162 |
new MySQLPrimaryPos( '255-11-1000,256-12-51', $now ), |
| 163 |
true, |
| 164 |
false |
| 165 |
], |
| 166 |
[ |
| 167 |
new MySQLPrimaryPos( '255-11-99,256-12-50', $now ), |
| 168 |
new MySQLPrimaryPos( '255-13-1000,256-14-49', $now ), |
| 169 |
true, |
| 170 |
true |
| 171 |
], |
| 172 |
[ |
| 173 |
new MySQLPrimaryPos( '253-11-999,255-11-999', $now ), |
| 174 |
new MySQLPrimaryPos( '254-11-1000', $now ), |
| 175 |
false, |
| 176 |
false |
| 177 |
], |
| 178 |
]; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function testGetRelevantActiveGTIDs( MySQLPrimaryPos $pos, MySQLPrimaryPos $ref, $gtids ) { |
| 186 |
$this->assertEquals( $gtids, MySQLPrimaryPos::getRelevantActiveGTIDs( $pos, $ref ) ); |
| 187 |
} |
| 188 |
|
| 189 |
public static function provideCommonDomainGTIDs() { |
| 190 |
return [ |
| 191 |
[ |
| 192 |
new MySQLPrimaryPos( '255-13-99,256-12-50,257-14-50', 1 ), |
| 193 |
new MySQLPrimaryPos( '255-11-1000', 1 ), |
| 194 |
[ '255-13-99' ] |
| 195 |
], |
| 196 |
[ |
| 197 |
( new MySQLPrimaryPos( '255-13-99,256-12-50,257-14-50', 1 ) ) |
| 198 |
->setActiveDomain( 257 ), |
| 199 |
new MySQLPrimaryPos( '255-11-1000,257-14-30', 1 ), |
| 200 |
[ '257-14-50' ] |
| 201 |
], |
| 202 |
[ |
| 203 |
new MySQLPrimaryPos( |
| 204 |
'2E11FA47-71CA-11E1-9E33-C80AA9429562:1-5,' . |
| 205 |
'3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99,' . |
| 206 |
'7E11FA47-71CA-11E1-9E33-C80AA9429562:1-30', |
| 207 |
1 |
| 208 |
), |
| 209 |
new MySQLPrimaryPos( |
| 210 |
'1E11FA47-71CA-11E1-9E33-C80AA9429562:30-100,' . |
| 211 |
'3E11FA47-71CA-11E1-9E33-C80AA9429562:30-66', |
| 212 |
1 |
| 213 |
), |
| 214 |
[ '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99' ] |
| 215 |
] |
| 216 |
]; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public function testPtHeartbeat( $lag ) { |
| 223 |
|
| 224 |
$db = $this->getMockBuilder( IDatabaseForOwner::class ) |
| 225 |
->disableOriginalConstructor() |
| 226 |
->getMock(); |
| 227 |
$db->setLBInfo( 'replica', true ); |
| 228 |
|
| 229 |
$replicationReporter = $this->getMockBuilder( MysqlReplicationReporter::class ) |
| 230 |
->disableOriginalConstructor() |
| 231 |
->onlyMethods( [ 'fetchSecondsSinceHeartbeat' ] ) |
| 232 |
->getMock(); |
| 233 |
|
| 234 |
TestingAccessWrapper::newFromObject( $replicationReporter )->lagDetectionMethod = 'pt-heartbeat'; |
| 235 |
$replicationReporter->method( 'fetchSecondsSinceHeartbeat' )->willReturn( $lag ); |
| 236 |
|
| 237 |
$lagEst = $replicationReporter->getLag( $db ); |
| 238 |
|
| 239 |
$this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" ); |
| 240 |
$this->assertLessThan( $lag + 0.010, $lagEst, "Correct heatbeat lag" ); |
| 241 |
} |
| 242 |
|
| 243 |
public static function provideLagAmounts() { |
| 244 |
return [ |
| 245 |
[ 0 ], |
| 246 |
[ 0.3 ], |
| 247 |
[ 6.5 ], |
| 248 |
[ 10.1 ], |
| 249 |
[ 200.2 ], |
| 250 |
[ 400.7 ], |
| 251 |
[ 600.22 ], |
| 252 |
[ 1000.77 ], |
| 253 |
]; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public function testServerGtidTable( $gtable, $rBLtable, $mBLtable, $rGTIDs, $mGTIDs ) { |
| 262 |
$db = $this->getMockBuilder( IDatabase::class ) |
| 263 |
->disableOriginalConstructor() |
| 264 |
->getMock(); |
| 265 |
$replicationReporter = $this->getMockBuilder( MysqlReplicationReporter::class ) |
| 266 |
->disableOriginalConstructor() |
| 267 |
->onlyMethods( [ |
| 268 |
'useGTIDs', |
| 269 |
'getServerGTIDs', |
| 270 |
'getServerRoleStatus', |
| 271 |
'getServerId', |
| 272 |
'getServerUUID' |
| 273 |
] ) |
| 274 |
->getMock(); |
| 275 |
|
| 276 |
$replicationReporter->method( 'useGTIDs' )->willReturn( true ); |
| 277 |
$replicationReporter->method( 'getServerGTIDs' )->willReturn( $gtable ); |
| 278 |
$replicationReporter->method( 'getServerRoleStatus' )->willReturnCallback( |
| 279 |
static function ( $db, $role ) use ( $rBLtable, $mBLtable ) { |
| 280 |
if ( $role === 'SLAVE' ) { |
| 281 |
return $rBLtable; |
| 282 |
} elseif ( $role === 'MASTER' ) { |
| 283 |
return $mBLtable; |
| 284 |
} |
| 285 |
|
| 286 |
return null; |
| 287 |
} |
| 288 |
); |
| 289 |
$replicationReporter->method( 'getServerId' )->willReturn( 1 ); |
| 290 |
$replicationReporter->method( 'getServerUUID' )->willReturn( '2E11FA47-71CA-11E1-9E33-C80AA9429562' ); |
| 291 |
|
| 292 |
|
| 293 |
if ( is_array( $rGTIDs ) ) { |
| 294 |
$this->assertEquals( $rGTIDs, $replicationReporter->getReplicaPos( $db )->getGTIDs() ); |
| 295 |
} else { |
| 296 |
$this->assertFalse( $replicationReporter->getReplicaPos( $db ) ); |
| 297 |
} |
| 298 |
if ( is_array( $mGTIDs ) ) { |
| 299 |
$this->assertEquals( $mGTIDs, $replicationReporter->getPrimaryPos( $db )->getGTIDs() ); |
| 300 |
} else { |
| 301 |
$this->assertFalse( $replicationReporter->getPrimaryPos( $db ) ); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
public static function provideGtidData() { |
| 306 |
return [ |
| 307 |
|
| 308 |
[ |
| 309 |
[ |
| 310 |
'gtid_domain_id' => 100, |
| 311 |
'gtid_current_pos' => '100-13-77', |
| 312 |
'gtid_binlog_pos' => '100-13-77', |
| 313 |
'gtid_slave_pos' => null |
| 314 |
], |
| 315 |
[ |
| 316 |
'Relay_Master_Log_File' => 'host.1600', |
| 317 |
'Exec_Master_Log_Pos' => '77' |
| 318 |
], |
| 319 |
[ |
| 320 |
'File' => 'host.1600', |
| 321 |
'Position' => '77' |
| 322 |
], |
| 323 |
[], |
| 324 |
[ '100' => '100-13-77' ] |
| 325 |
], |
| 326 |
[ |
| 327 |
[ |
| 328 |
'gtid_domain_id' => 100, |
| 329 |
'gtid_current_pos' => '100-13-77', |
| 330 |
'gtid_binlog_pos' => '100-13-77', |
| 331 |
'gtid_slave_pos' => '100-13-77' |
| 332 |
], |
| 333 |
[ |
| 334 |
'Relay_Master_Log_File' => 'host.1600', |
| 335 |
'Exec_Master_Log_Pos' => '77' |
| 336 |
], |
| 337 |
[], |
| 338 |
[ '100' => '100-13-77' ], |
| 339 |
[ '100' => '100-13-77' ] |
| 340 |
], |
| 341 |
[ |
| 342 |
[ |
| 343 |
'gtid_current_pos' => '100-13-77', |
| 344 |
'gtid_binlog_pos' => '100-13-77', |
| 345 |
'gtid_slave_pos' => '100-13-77' |
| 346 |
], |
| 347 |
[ |
| 348 |
'Relay_Master_Log_File' => 'host.1600', |
| 349 |
'Exec_Master_Log_Pos' => '77' |
| 350 |
], |
| 351 |
[], |
| 352 |
[ '100' => '100-13-77' ], |
| 353 |
[ '100' => '100-13-77' ] |
| 354 |
], |
| 355 |
|
| 356 |
[ |
| 357 |
[ |
| 358 |
'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' |
| 359 |
], |
| 360 |
[ |
| 361 |
'Relay_Master_Log_File' => 'host.1600', |
| 362 |
'Exec_Master_Log_Pos' => '77' |
| 363 |
], |
| 364 |
[], |
| 365 |
[ '2E11FA47-71CA-11E1-9E33-C80AA9429562' |
| 366 |
=> '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ], |
| 367 |
|
| 368 |
[ '2E11FA47-71CA-11E1-9E33-C80AA9429562' |
| 369 |
=> '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ], |
| 370 |
], |
| 371 |
[ |
| 372 |
[ |
| 373 |
'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-49,' . |
| 374 |
'2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' |
| 375 |
], |
| 376 |
[ |
| 377 |
'Relay_Master_Log_File' => 'host.1600', |
| 378 |
'Exec_Master_Log_Pos' => '77' |
| 379 |
], |
| 380 |
[], |
| 381 |
[ '2E11FA47-71CA-11E1-9E33-C80AA9429562' |
| 382 |
=> '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ], |
| 383 |
|
| 384 |
[ '2E11FA47-71CA-11E1-9E33-C80AA9429562' |
| 385 |
=> '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ], |
| 386 |
], |
| 387 |
[ |
| 388 |
[ |
| 389 |
'gtid_executed' => null, |
| 390 |
'gtid_binlog_pos' => null |
| 391 |
], |
| 392 |
[ |
| 393 |
'Relay_Master_Log_File' => 'host.1600', |
| 394 |
'Exec_Master_Log_Pos' => '77' |
| 395 |
], |
| 396 |
[], |
| 397 |
[], |
| 398 |
false |
| 399 |
], |
| 400 |
[ |
| 401 |
[ |
| 402 |
'gtid_executed' => null, |
| 403 |
'gtid_binlog_pos' => null |
| 404 |
], |
| 405 |
[], |
| 406 |
[], |
| 407 |
false, |
| 408 |
false |
| 409 |
] |
| 410 |
]; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public function testSerialize() { |
| 417 |
$pos = new MySQLPrimaryPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 ); |
| 418 |
$roundtripPos = unserialize( serialize( $pos ) ); |
| 419 |
|
| 420 |
$this->assertEquals( $pos, $roundtripPos ); |
| 421 |
|
| 422 |
$pos = new MySQLPrimaryPos( '255-11-23', 53636363 ); |
| 423 |
$roundtripPos = unserialize( serialize( $pos ) ); |
| 424 |
|
| 425 |
$this->assertEquals( $pos, $roundtripPos ); |
| 426 |
} |
| 427 |
|
| 428 |
public function testBuildIntegerCast() { |
| 429 |
$db = $this->createPartialMock( DatabaseMySQL::class, [] ); |
| 430 |
TestingAccessWrapper::newFromObject( $db )->platform = new MySQLPlatform( new AddQuoterMock() ); |
| 431 |
|
| 432 |
|
| 433 |
$output = $db->buildIntegerCast( 'fieldName' ); |
| 434 |
$this->assertSame( 'CAST( fieldName AS SIGNED )', $output ); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
public function testNormalizeJoinType() { |
| 441 |
$platform = new MySQLPlatform( new AddQuoterMock() ); |
| 442 |
$sql = $platform->selectSQLText( |
| 443 |
[ 'a', 'b' ], |
| 444 |
'aa', |
| 445 |
[], |
| 446 |
'', |
| 447 |
[], |
| 448 |
[ 'b' => [ 'STRAIGHT_JOIN', 'bb=aa' ] ] |
| 449 |
); |
| 450 |
$this->assertSame( |
| 451 |
'SELECT aa FROM `a` STRAIGHT_JOIN `b` ON ((bb=aa)) ', |
| 452 |
$sql |
| 453 |
); |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
public function testNormalizeJoinTypeSqb() { |
| 460 |
$db = $this->createPartialMock( DatabaseMySQL::class, [] ); |
| 461 |
|
| 462 |
TestingAccessWrapper::newFromObject( $db )->currentDomain = |
| 463 |
new DatabaseDomain( null, null, '' ); |
| 464 |
TestingAccessWrapper::newFromObject( $db )->platform = |
| 465 |
new MySQLPlatform( new AddQuoterMock() ); |
| 466 |
|
| 467 |
|
| 468 |
$sql = $db->newSelectQueryBuilder() |
| 469 |
->select( 'aa' ) |
| 470 |
->from( 'a' ) |
| 471 |
->straightJoin( 'b', null, [ 'bb=aa' ] ) |
| 472 |
->getSQL(); |
| 473 |
$this->assertSame( |
| 474 |
'SELECT aa FROM `a` STRAIGHT_JOIN `b` ON ((bb=aa)) ', |
| 475 |
$sql |
| 476 |
); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
public function testIndexAliases() { |
| 485 |
$db = $this->getMockBuilder( DatabaseMySQL::class ) |
| 486 |
->disableOriginalConstructor() |
| 487 |
->onlyMethods( [ 'strencode', 'dbSchema', 'tablePrefix' ] ) |
| 488 |
->getMock(); |
| 489 |
$db->method( 'strencode' )->willReturnCallback( |
| 490 |
static function ( $s ) { |
| 491 |
return str_replace( "'", "\\'", $s ); |
| 492 |
} |
| 493 |
); |
| 494 |
$wdb = TestingAccessWrapper::newFromObject( $db ); |
| 495 |
$wdb->platform = new MySQLPlatform( new AddQuoterMock() ); |
| 496 |
|
| 497 |
|
| 498 |
$db->setIndexAliases( [ 'a_b_idx' => 'a_c_idx' ] ); |
| 499 |
$sql = $db->newSelectQueryBuilder() |
| 500 |
->select( 'field' ) |
| 501 |
->from( 'zend' ) |
| 502 |
->where( [ 'a' => 'x' ] ) |
| 503 |
->useIndex( 'a_b_idx' ) |
| 504 |
->caller( __METHOD__ )->getSQL(); |
| 505 |
|
| 506 |
$this->assertSameSql( |
| 507 |
"SELECT field FROM `zend` FORCE INDEX (a_c_idx) WHERE a = 'x' ", |
| 508 |
$sql |
| 509 |
); |
| 510 |
|
| 511 |
$db->setIndexAliases( [] ); |
| 512 |
$sql = $db->newSelectQueryBuilder() |
| 513 |
->select( 'field' ) |
| 514 |
->from( 'zend' ) |
| 515 |
->where( [ 'a' => 'x' ] ) |
| 516 |
->useIndex( 'a_b_idx' ) |
| 517 |
->caller( __METHOD__ )->getSQL(); |
| 518 |
|
| 519 |
$this->assertSameSql( |
| 520 |
"SELECT field FROM `zend` FORCE INDEX (a_b_idx) WHERE a = 'x'", |
| 521 |
$sql |
| 522 |
); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
public function testTableAliases() { |
| 531 |
$db = $this->getMockBuilder( DatabaseMySQL::class ) |
| 532 |
->disableOriginalConstructor() |
| 533 |
->onlyMethods( [ 'strencode', 'dbSchema', 'tablePrefix' ] ) |
| 534 |
->getMock(); |
| 535 |
$db->method( 'strencode' )->willReturnCallback( |
| 536 |
static function ( $s ) { |
| 537 |
return str_replace( "'", "\\'", $s ); |
| 538 |
} |
| 539 |
); |
| 540 |
$wdb = TestingAccessWrapper::newFromObject( $db ); |
| 541 |
$wdb->platform = new MySQLPlatform( new AddQuoterMock() ); |
| 542 |
|
| 543 |
|
| 544 |
$db->setTableAliases( [ |
| 545 |
'meow' => [ 'dbname' => 'feline', 'schema' => null, 'prefix' => '' ] |
| 546 |
] ); |
| 547 |
$sql = $db->newSelectQueryBuilder() |
| 548 |
->select( 'field' ) |
| 549 |
->from( 'meow' ) |
| 550 |
->where( [ 'a' => 'x' ] ) |
| 551 |
->caller( __METHOD__ )->getSQL(); |
| 552 |
|
| 553 |
$this->assertSameSql( |
| 554 |
"SELECT field FROM `feline`.`meow` `meow` WHERE a = 'x' ", |
| 555 |
$sql |
| 556 |
); |
| 557 |
|
| 558 |
$db->setTableAliases( [ |
| 559 |
'meow' => [ 'dbname' => 'feline', 'schema' => null, 'prefix' => 'cat_' ] |
| 560 |
] ); |
| 561 |
$sql = $db->newSelectQueryBuilder() |
| 562 |
->select( 'field' ) |
| 563 |
->from( 'meow' ) |
| 564 |
->where( [ 'a' => 'x' ] ) |
| 565 |
->caller( __METHOD__ )->getSQL(); |
| 566 |
|
| 567 |
$this->assertSameSql( |
| 568 |
"SELECT field FROM `feline`.`cat_meow` `meow` WHERE a = 'x' ", |
| 569 |
$sql |
| 570 |
); |
| 571 |
|
| 572 |
$db->setTableAliases( [] ); |
| 573 |
$sql = $db->newSelectQueryBuilder() |
| 574 |
->select( 'field' ) |
| 575 |
->from( 'meow' ) |
| 576 |
->where( [ 'a' => 'x' ] ) |
| 577 |
->caller( __METHOD__ )->getSQL(); |
| 578 |
|
| 579 |
$this->assertSameSql( |
| 580 |
"SELECT field FROM `meow` WHERE a = 'x' ", |
| 581 |
$sql |
| 582 |
); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
public function testMaxExecutionTime() { |
| 591 |
$db = $this->getMockBuilder( DatabaseMySQL::class ) |
| 592 |
->disableOriginalConstructor() |
| 593 |
->onlyMethods( [ 'getServerVersion', 'dbSchema', 'tablePrefix' ] ) |
| 594 |
->getMock(); |
| 595 |
$db->method( 'getServerVersion' )->willReturn( '10.4.28-MariaDB-log' ); |
| 596 |
$wdb = TestingAccessWrapper::newFromObject( $db ); |
| 597 |
$wdb->platform = new MySQLPlatform( new AddQuoterMock() ); |
| 598 |
|
| 599 |
|
| 600 |
$sql = $wdb->selectSQLText( 'image', |
| 601 |
'img_metadata', |
| 602 |
'*', |
| 603 |
'', |
| 604 |
[ 'MAX_EXECUTION_TIME' => 1 ] |
| 605 |
); |
| 606 |
|
| 607 |
$this->assertSameSql( |
| 608 |
"SET STATEMENT max_statement_time=0.001 FOR SELECT img_metadata FROM `image` ", |
| 609 |
$sql |
| 610 |
); |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
public function testStreamStatementEnd() { |
| 618 |
|
| 619 |
$db = $this->getMockForAbstractClass( DatabaseMySQL::class, [], '', false ); |
| 620 |
$sql = ''; |
| 621 |
|
| 622 |
$newLine = "delimiter\n!! ?"; |
| 623 |
$this->assertFalse( $db->streamStatementEnd( $sql, $newLine ) ); |
| 624 |
$this->assertSame( '', $newLine ); |
| 625 |
|
| 626 |
$newLine = 'JUST A TEST!!!'; |
| 627 |
$this->assertTrue( $db->streamStatementEnd( $sql, $newLine ) ); |
| 628 |
$this->assertSame( 'JUST A TEST!', $newLine ); |
| 629 |
} |
| 630 |
|
| 631 |
private function assertSameSql( $expected, $actual, $message = '' ) { |
| 632 |
$this->assertSame( trim( $expected ), trim( $actual ), $message ); |
| 633 |
} |
| 634 |
} |