| 1 |
<?php |
| 2 |
|
| 3 |
use Psr\Log\NullLogger; |
| 4 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 5 |
use Wikimedia\Rdbms\Blob; |
| 6 |
use Wikimedia\Rdbms\Database; |
| 7 |
use Wikimedia\Rdbms\DatabaseSqlite; |
| 8 |
use Wikimedia\Rdbms\IDatabase; |
| 9 |
use Wikimedia\Rdbms\IResultWrapper; |
| 10 |
use Wikimedia\Rdbms\Query; |
| 11 |
use Wikimedia\Rdbms\ResultWrapper; |
| 12 |
use Wikimedia\Rdbms\TransactionProfiler; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class DatabaseSqliteTest extends \MediaWikiIntegrationTestCase { |
| 23 |
|
| 24 |
protected $db; |
| 25 |
|
| 26 |
|
| 27 |
protected $currentTableInfo; |
| 28 |
|
| 29 |
protected function setUp(): void { |
| 30 |
parent::setUp(); |
| 31 |
|
| 32 |
if ( !Sqlite::isPresent() ) { |
| 33 |
$this->markTestSkipped( 'No SQLite support detected' ); |
| 34 |
} |
| 35 |
$this->db = $this->newMockDb(); |
| 36 |
if ( version_compare( $this->getDb()->getServerVersion(), '3.6.0', '<' ) ) { |
| 37 |
$this->markTestSkipped( "SQLite at least 3.6 required, {$this->getDb()->getServerVersion()} found" ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private function newMockDb( $version = null, &$sqlDump = null ) { |
| 47 |
$mock = $this->getMockBuilder( DatabaseSqlite::class ) |
| 48 |
->setConstructorArgs( [ [ |
| 49 |
'dbFilePath' => ':memory:', |
| 50 |
'dbname' => 'Foo', |
| 51 |
'schema' => null, |
| 52 |
'host' => false, |
| 53 |
'user' => false, |
| 54 |
'password' => false, |
| 55 |
'tablePrefix' => '', |
| 56 |
'cliMode' => true, |
| 57 |
'agent' => 'unit-tests', |
| 58 |
'serverName' => null, |
| 59 |
'flags' => DBO_DEFAULT, |
| 60 |
'variables' => [ 'synchronous' => 'NORMAL', 'temp_store' => 'MEMORY' ], |
| 61 |
'profiler' => null, |
| 62 |
'topologyRole' => Database::ROLE_STREAMING_MASTER, |
| 63 |
'trxProfiler' => new TransactionProfiler(), |
| 64 |
'errorLogger' => null, |
| 65 |
'deprecationLogger' => new NullLogger(), |
| 66 |
'srvCache' => new HashBagOStuff(), |
| 67 |
] ] )->onlyMethods( array_merge( |
| 68 |
[ 'query' ], |
| 69 |
$version ? [ 'getServerVersion' ] : [] |
| 70 |
) )->getMock(); |
| 71 |
|
| 72 |
$mock->initConnection(); |
| 73 |
|
| 74 |
$sqlDump = ''; |
| 75 |
$mock->method( 'query' )->willReturnCallback( static function ( $sql ) use ( &$sqlDump ) { |
| 76 |
if ( $sql instanceof Query ) { |
| 77 |
$sql = $sql->getSQL(); |
| 78 |
} |
| 79 |
$sqlDump .= "$sql;"; |
| 80 |
|
| 81 |
return true; |
| 82 |
} ); |
| 83 |
|
| 84 |
if ( $version ) { |
| 85 |
$mock->method( 'getServerVersion' )->willReturn( $version ); |
| 86 |
} |
| 87 |
|
| 88 |
return $mock; |
| 89 |
} |
| 90 |
|
| 91 |
private function assertResultIs( $expected, $res ) { |
| 92 |
$this->assertNotNull( $res ); |
| 93 |
$i = 0; |
| 94 |
foreach ( $res as $row ) { |
| 95 |
foreach ( $expected[$i] as $key => $value ) { |
| 96 |
$this->assertTrue( isset( $row->$key ) ); |
| 97 |
$this->assertEquals( $value, $row->$key ); |
| 98 |
} |
| 99 |
$i++; |
| 100 |
} |
| 101 |
$this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' ); |
| 102 |
} |
| 103 |
|
| 104 |
public static function provideAddQuotes() { |
| 105 |
return [ |
| 106 |
[ |
| 107 |
'', "''" |
| 108 |
], |
| 109 |
[ |
| 110 |
'foo bar', "'foo bar'" |
| 111 |
], |
| 112 |
[ |
| 113 |
'foo\'bar', "'foo''bar'" |
| 114 |
], |
| 115 |
|
| 116 |
[ |
| 117 |
"x\0y", |
| 118 |
"x'780079'", |
| 119 |
], |
| 120 |
[ |
| 121 |
new Blob( "hello" ), |
| 122 |
"x'68656c6c6f'", |
| 123 |
], |
| 124 |
[ |
| 125 |
null, |
| 126 |
"''", |
| 127 |
], |
| 128 |
]; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function testAddQuotes( $value, $expected ) { |
| 135 |
|
| 136 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 137 |
$this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' ); |
| 138 |
|
| 139 |
|
| 140 |
$re = $db->query( 'select ' . $db->addQuotes( $value ) ); |
| 141 |
|
| 142 |
$this->assertInstanceOf( IResultWrapper::class, $re, 'query failed' ); |
| 143 |
|
| 144 |
$row = $re->fetchRow(); |
| 145 |
if ( $row ) { |
| 146 |
if ( $value instanceof Blob ) { |
| 147 |
$value = $value->fetch(); |
| 148 |
} |
| 149 |
|
| 150 |
$this->assertEquals( $value, $row[0], 'string mangled by the database' ); |
| 151 |
} else { |
| 152 |
$this->fail( 'query returned no result' ); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
public function testDuplicateTableStructure() { |
| 157 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 158 |
$db->query( 'CREATE TABLE foo(foo, barfoo)' ); |
| 159 |
$db->query( 'CREATE INDEX index1 ON foo(foo)' ); |
| 160 |
$db->query( 'CREATE UNIQUE INDEX index2 ON foo(barfoo)' ); |
| 161 |
|
| 162 |
$db->duplicateTableStructure( 'foo', 'bar' ); |
| 163 |
$this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)', |
| 164 |
$db->newSelectQueryBuilder() |
| 165 |
->select( 'sql' ) |
| 166 |
->from( 'sqlite_master' ) |
| 167 |
->where( [ 'name' => 'bar' ] ) |
| 168 |
->caller( __METHOD__ )->fetchField(), |
| 169 |
'Normal table duplication' |
| 170 |
); |
| 171 |
$indexList = $db->query( 'PRAGMA INDEX_LIST("bar")' ); |
| 172 |
$index = $indexList->fetchObject(); |
| 173 |
$this->assertEquals( 'bar_index1', $index->name ); |
| 174 |
$this->assertSame( '0', (string)$index->unique ); |
| 175 |
$index = $indexList->fetchObject(); |
| 176 |
$this->assertEquals( 'bar_index2', $index->name ); |
| 177 |
$this->assertSame( '1', (string)$index->unique ); |
| 178 |
|
| 179 |
$db->duplicateTableStructure( 'foo', 'baz', true ); |
| 180 |
$this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)', |
| 181 |
$db->newSelectQueryBuilder() |
| 182 |
->select( 'sql' ) |
| 183 |
->from( 'sqlite_temp_master' ) |
| 184 |
->where( [ 'name' => 'baz' ] ) |
| 185 |
->caller( __METHOD__ )->fetchField(), |
| 186 |
'Creation of temporary duplicate' |
| 187 |
); |
| 188 |
$indexList = $db->query( 'PRAGMA INDEX_LIST("baz")' ); |
| 189 |
$index = $indexList->fetchObject(); |
| 190 |
$this->assertEquals( 'baz_index1', $index->name ); |
| 191 |
$this->assertSame( '0', (string)$index->unique ); |
| 192 |
$index = $indexList->fetchObject(); |
| 193 |
$this->assertEquals( 'baz_index2', $index->name ); |
| 194 |
$this->assertSame( '1', (string)$index->unique ); |
| 195 |
$this->assertSame( '0', |
| 196 |
(string)$db->newSelectQueryBuilder() |
| 197 |
->select( 'COUNT(*)' ) |
| 198 |
->from( 'sqlite_master' ) |
| 199 |
->where( [ 'name' => 'baz' ] ) |
| 200 |
->caller( __METHOD__ )->fetchField(), |
| 201 |
'Create a temporary duplicate only' |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
public function testDuplicateTableStructureVirtual() { |
| 206 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 207 |
if ( $db->getFulltextSearchModule() != 'FTS3' ) { |
| 208 |
$this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' ); |
| 209 |
} |
| 210 |
$db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' ); |
| 211 |
|
| 212 |
$db->duplicateTableStructure( 'foo', 'bar' ); |
| 213 |
$this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)', |
| 214 |
$db->newSelectQueryBuilder() |
| 215 |
->select( 'sql' ) |
| 216 |
->from( 'sqlite_master' ) |
| 217 |
->where( [ 'name' => 'bar' ] ) |
| 218 |
->caller( __METHOD__ )->fetchField(), |
| 219 |
'Duplication of virtual tables' |
| 220 |
); |
| 221 |
|
| 222 |
$db->duplicateTableStructure( 'foo', 'baz', true ); |
| 223 |
$this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)', |
| 224 |
$db->newSelectQueryBuilder() |
| 225 |
->select( 'sql' ) |
| 226 |
->from( 'sqlite_master' ) |
| 227 |
->where( [ 'name' => 'baz' ] ) |
| 228 |
->caller( __METHOD__ )->fetchField(), |
| 229 |
"Can't create temporary virtual tables, should fall back to non-temporary duplication" |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
public function testDeleteJoin() { |
| 234 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 235 |
$db->query( 'CREATE TABLE a (a_1)', __METHOD__ ); |
| 236 |
$db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ ); |
| 237 |
$db->insert( 'a', [ |
| 238 |
[ 'a_1' => 1 ], |
| 239 |
[ 'a_1' => 2 ], |
| 240 |
[ 'a_1' => 3 ], |
| 241 |
], |
| 242 |
__METHOD__ |
| 243 |
); |
| 244 |
$db->insert( 'b', [ |
| 245 |
[ 'b_1' => 2, 'b_2' => 'a' ], |
| 246 |
[ 'b_1' => 3, 'b_2' => 'b' ], |
| 247 |
], |
| 248 |
__METHOD__ |
| 249 |
); |
| 250 |
$db->deleteJoin( 'a', 'b', 'a_1', 'b_1', [ 'b_2' => 'a' ], __METHOD__ ); |
| 251 |
$res = $db->query( "SELECT * FROM a", __METHOD__ ); |
| 252 |
$this->assertResultIs( [ |
| 253 |
[ 'a_1' => 1 ], |
| 254 |
[ 'a_1' => 3 ], |
| 255 |
], |
| 256 |
$res |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public function testEntireSchema() { |
| 264 |
global $IP; |
| 265 |
|
| 266 |
$result = Sqlite::checkSqlSyntax( "$IP/maintenance/sqlite/tables-generated.sql" ); |
| 267 |
|
| 268 |
$this->assertTrue( $result, $result ); |
| 269 |
} |
| 270 |
|
| 271 |
public function testInsertIdType() { |
| 272 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 273 |
|
| 274 |
$databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ ); |
| 275 |
$this->assertInstanceOf( ResultWrapper::class, $databaseCreation, "Database creation" ); |
| 276 |
|
| 277 |
$db->insert( 'a', [ 'a_1' => 10 ], __METHOD__ ); |
| 278 |
$this->assertIsInt( $db->insertId(), "Actual typecheck" ); |
| 279 |
$this->assertTrue( $db->close(), "closing database" ); |
| 280 |
} |
| 281 |
|
| 282 |
public function testInsertAffectedRows() { |
| 283 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 284 |
$db->query( 'CREATE TABLE testInsertAffectedRows ( foo )', __METHOD__ ); |
| 285 |
|
| 286 |
$db->insert( |
| 287 |
'testInsertAffectedRows', |
| 288 |
[ |
| 289 |
[ 'foo' => 10 ], |
| 290 |
[ 'foo' => 12 ], |
| 291 |
[ 'foo' => 1555 ], |
| 292 |
], |
| 293 |
__METHOD__ |
| 294 |
); |
| 295 |
|
| 296 |
$this->assertSame( 3, $db->affectedRows() ); |
| 297 |
$this->assertTrue( $db->close(), "closing database" ); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public function testCaseInsensitiveLike() { |
| 304 |
|
| 305 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 306 |
$res = $db->query( 'SELECT "a" LIKE "A" AS a' ); |
| 307 |
$row = $res->fetchRow(); |
| 308 |
$this->assertFalse( (bool)$row['a'] ); |
| 309 |
} |
| 310 |
|
| 311 |
public function testToString() { |
| 312 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 313 |
|
| 314 |
$toString = (string)$db; |
| 315 |
|
| 316 |
$this->assertStringContainsString( 'sqlite object', $toString ); |
| 317 |
} |
| 318 |
|
| 319 |
public function testsAttributes() { |
| 320 |
$dbFactory = $this->getServiceContainer()->getDatabaseFactory(); |
| 321 |
$this->assertTrue( $dbFactory->attributesFromType( 'sqlite' )[Database::ATTR_DB_LEVEL_LOCKING] ); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
public function testNativeInsertSupport( $version, $table, $rows, $expectedSql ) { |
| 332 |
$sqlDump = ''; |
| 333 |
$db = $this->newMockDb( $version, $sqlDump ); |
| 334 |
$db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ ); |
| 335 |
|
| 336 |
$sqlDump = ''; |
| 337 |
$db->insert( $table, $rows, __METHOD__ ); |
| 338 |
$this->assertEquals( $expectedSql, $sqlDump ); |
| 339 |
} |
| 340 |
|
| 341 |
public static function provideNativeInserts() { |
| 342 |
return [ |
| 343 |
[ |
| 344 |
'3.8.0', |
| 345 |
'a', |
| 346 |
[ 'a_1' => 1 ], |
| 347 |
'INSERT INTO "a" (a_1) VALUES (1);' |
| 348 |
], |
| 349 |
[ |
| 350 |
'3.8.0', |
| 351 |
'a', |
| 352 |
[ |
| 353 |
[ 'a_1' => 2 ], |
| 354 |
[ 'a_1' => 3 ] |
| 355 |
], |
| 356 |
'INSERT INTO "a" (a_1) VALUES (2),(3);' |
| 357 |
], |
| 358 |
]; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
public function testNativeReplaceSupport( $version, $table, $ukeys, $rows, $expectedSql ) { |
| 370 |
$sqlDump = ''; |
| 371 |
$db = $this->newMockDb( $version, $sqlDump ); |
| 372 |
$db->query( 'CREATE TABLE a ( a_1 PRIMARY KEY, a_2 )', __METHOD__ ); |
| 373 |
|
| 374 |
$sqlDump = ''; |
| 375 |
$db->replace( $table, $ukeys, $rows, __METHOD__ ); |
| 376 |
$this->assertEquals( $expectedSql, $sqlDump ); |
| 377 |
} |
| 378 |
|
| 379 |
public static function provideNativeReplaces() { |
| 380 |
return [ |
| 381 |
[ |
| 382 |
'3.8.0', |
| 383 |
'a', |
| 384 |
[ 'a_1' ], |
| 385 |
[ 'a_1' => 1, 'a_2' => 'x' ], |
| 386 |
'REPLACE INTO "a" (a_1,a_2) VALUES (1,\'x\');' |
| 387 |
], |
| 388 |
[ |
| 389 |
'3.8.0', |
| 390 |
'a', |
| 391 |
[ 'a_1' ], |
| 392 |
[ |
| 393 |
[ 'a_1' => 2, 'a_2' => 'x' ], |
| 394 |
[ 'a_1' => 3, 'a_2' => 'y' ] |
| 395 |
], |
| 396 |
'REPLACE INTO "a" (a_1,a_2) VALUES (2,\'x\'),(3,\'y\');' |
| 397 |
], |
| 398 |
]; |
| 399 |
} |
| 400 |
|
| 401 |
public function testInsertIdAfterInsert() { |
| 402 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 403 |
$dTable = $this->createDestTable( $db ); |
| 404 |
|
| 405 |
$rows = [ [ 'k' => 'Luca', 'v' => mt_rand( 1, 100 ), 't' => time() ] ]; |
| 406 |
|
| 407 |
$db->insert( $dTable, $rows, __METHOD__ ); |
| 408 |
$this->assertSame( 1, $db->affectedRows() ); |
| 409 |
$this->assertSame( 1, $db->insertId() ); |
| 410 |
|
| 411 |
$this->assertNWhereKEqualsLuca( 1, $dTable, $db ); |
| 412 |
$this->assertSame( 0, $db->affectedRows() ); |
| 413 |
$this->assertSame( 0, $db->insertId() ); |
| 414 |
} |
| 415 |
|
| 416 |
public function testInsertIdAfterInsertIgnore() { |
| 417 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 418 |
$dTable = $this->createDestTable( $db ); |
| 419 |
|
| 420 |
$rows = [ [ 'k' => 'Luca', 'v' => mt_rand( 1, 100 ), 't' => time() ] ]; |
| 421 |
|
| 422 |
$db->insert( $dTable, $rows, __METHOD__, 'IGNORE' ); |
| 423 |
$this->assertSame( 1, $db->affectedRows() ); |
| 424 |
$this->assertSame( 1, $db->insertId() ); |
| 425 |
$this->assertNWhereKEqualsLuca( 1, $dTable, $db ); |
| 426 |
|
| 427 |
$db->insert( $dTable, $rows, __METHOD__, 'IGNORE' ); |
| 428 |
$this->assertSame( 0, $db->affectedRows() ); |
| 429 |
$this->assertSame( 0, $db->insertId() ); |
| 430 |
|
| 431 |
$this->assertNWhereKEqualsLuca( 1, $dTable, $db ); |
| 432 |
$this->assertSame( 0, $db->affectedRows() ); |
| 433 |
$this->assertSame( 0, $db->insertId() ); |
| 434 |
} |
| 435 |
|
| 436 |
public function testInsertIdAfterReplace() { |
| 437 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 438 |
$dTable = $this->createDestTable( $db ); |
| 439 |
|
| 440 |
$rows = [ [ 'k' => 'Luca', 'v' => mt_rand( 1, 100 ), 't' => time() ] ]; |
| 441 |
|
| 442 |
$db->replace( $dTable, 'k', $rows, __METHOD__ ); |
| 443 |
$this->assertSame( 1, $db->affectedRows() ); |
| 444 |
$this->assertSame( 1, $db->insertId() ); |
| 445 |
$this->assertNWhereKEqualsLuca( 1, $dTable, $db ); |
| 446 |
|
| 447 |
$db->replace( $dTable, 'k', $rows, __METHOD__ ); |
| 448 |
$this->assertSame( 1, $db->affectedRows() ); |
| 449 |
$this->assertSame( 2, $db->insertId() ); |
| 450 |
|
| 451 |
$this->assertNWhereKEqualsLuca( 2, $dTable, $db ); |
| 452 |
$this->assertSame( 0, $db->affectedRows() ); |
| 453 |
$this->assertSame( 0, $db->insertId() ); |
| 454 |
} |
| 455 |
|
| 456 |
public function testInsertIdAfterUpsert() { |
| 457 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 458 |
$dTable = $this->createDestTable( $db ); |
| 459 |
|
| 460 |
$rows = [ [ 'k' => 'Luca', 'v' => mt_rand( 1, 100 ), 't' => time() ] ]; |
| 461 |
$otherRows = [ [ 'k' => 'Skylar', 'v' => mt_rand( 1, 100 ), 't' => time() ] ]; |
| 462 |
$set = [ |
| 463 |
'v = ' . $db->buildExcludedValue( 'v' ), |
| 464 |
't = ' . $db->buildExcludedValue( 't' ) . ' + 1' |
| 465 |
]; |
| 466 |
|
| 467 |
$db->upsert( $dTable, $rows, 'k', $set, __METHOD__ ); |
| 468 |
$this->assertSame( 1, $db->affectedRows() ); |
| 469 |
$this->assertSame( 1, $db->insertId() ); |
| 470 |
$this->assertNWhereKEqualsLuca( 1, $dTable, $db ); |
| 471 |
|
| 472 |
$db->upsert( $dTable, $otherRows, 'k', $set, __METHOD__ ); |
| 473 |
$this->assertSame( 1, $db->affectedRows() ); |
| 474 |
$this->assertSame( 2, $db->insertId() ); |
| 475 |
|
| 476 |
$db->upsert( $dTable, $rows, 'k', $set, __METHOD__ ); |
| 477 |
$this->assertSame( 1, $db->affectedRows() ); |
| 478 |
$this->assertSame( 1, $db->insertId() ); |
| 479 |
|
| 480 |
$this->assertNWhereKEqualsLuca( 1, $dTable, $db ); |
| 481 |
$this->assertSame( 0, $db->affectedRows() ); |
| 482 |
$this->assertSame( 0, $db->insertId() ); |
| 483 |
} |
| 484 |
|
| 485 |
public function testInsertIdAfterInsertSelect() { |
| 486 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 487 |
$sTable = $this->createSourceTable( $db ); |
| 488 |
$dTable = $this->createDestTable( $db ); |
| 489 |
|
| 490 |
$rows = [ [ 'sk' => 'Luca', 'sv' => mt_rand( 1, 100 ), 'st' => time() ] ]; |
| 491 |
$db->insert( $sTable, $rows, __METHOD__, 'IGNORE' ); |
| 492 |
$this->assertSame( 1, $db->affectedRows() ); |
| 493 |
$this->assertSame( 1, $db->insertId() ); |
| 494 |
$this->assertSame( 1, (int)$db->newSelectQueryBuilder() |
| 495 |
->select( 'sn' ) |
| 496 |
->from( $sTable ) |
| 497 |
->where( [ 'sk' => 'Luca' ] ) |
| 498 |
->fetchField() ); |
| 499 |
|
| 500 |
$db->insertSelect( |
| 501 |
$dTable, |
| 502 |
$sTable, |
| 503 |
[ 'k' => 'sk', 'v' => 'sv', 't' => 'st' ], |
| 504 |
[ 'sk' => 'Luca' ], |
| 505 |
__METHOD__, |
| 506 |
'IGNORE' |
| 507 |
); |
| 508 |
$this->assertSame( 1, $db->affectedRows() ); |
| 509 |
$this->assertSame( 1, $db->insertId() ); |
| 510 |
|
| 511 |
$this->assertNWhereKEqualsLuca( 1, $dTable, $db ); |
| 512 |
$this->assertSame( 0, $db->affectedRows() ); |
| 513 |
$this->assertSame( 0, $db->insertId() ); |
| 514 |
} |
| 515 |
|
| 516 |
public function testInsertIdAfterInsertSelectIgnore() { |
| 517 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 518 |
$sTable = $this->createSourceTable( $db ); |
| 519 |
$dTable = $this->createDestTable( $db ); |
| 520 |
|
| 521 |
$rows = [ [ 'sk' => 'Luca', 'sv' => mt_rand( 1, 100 ), 'st' => time() ] ]; |
| 522 |
$db->insert( $sTable, $rows, __METHOD__, 'IGNORE' ); |
| 523 |
$this->assertSame( 1, $db->affectedRows() ); |
| 524 |
$this->assertSame( 1, $db->insertId() ); |
| 525 |
$this->assertSame( 1, (int)$db->newSelectQueryBuilder() |
| 526 |
->select( 'sn' ) |
| 527 |
->from( $sTable ) |
| 528 |
->where( [ 'sk' => 'Luca' ] ) |
| 529 |
->fetchField() ); |
| 530 |
|
| 531 |
$db->insertSelect( |
| 532 |
$dTable, |
| 533 |
$sTable, |
| 534 |
[ 'k' => 'sk', 'v' => 'sv', 't' => 'st' ], |
| 535 |
[ 'sk' => 'Luca' ], |
| 536 |
__METHOD__, |
| 537 |
'IGNORE' |
| 538 |
); |
| 539 |
$this->assertSame( 1, $db->affectedRows() ); |
| 540 |
$this->assertSame( 1, $db->insertId() ); |
| 541 |
$this->assertNWhereKEqualsLuca( 1, $dTable, $db ); |
| 542 |
|
| 543 |
$db->insertSelect( |
| 544 |
$dTable, |
| 545 |
$sTable, |
| 546 |
[ 'k' => 'sk', 'v' => 'sv', 't' => 'st' ], |
| 547 |
[ 'sk' => 'Luca' ], |
| 548 |
__METHOD__, |
| 549 |
'IGNORE' |
| 550 |
); |
| 551 |
$this->assertSame( 0, $db->affectedRows() ); |
| 552 |
$this->assertSame( 0, $db->insertId() ); |
| 553 |
|
| 554 |
$this->assertNWhereKEqualsLuca( 1, $dTable, $db ); |
| 555 |
$this->assertSame( 0, $db->affectedRows() ); |
| 556 |
$this->assertSame( 0, $db->insertId() ); |
| 557 |
} |
| 558 |
|
| 559 |
public function testFieldAndIndexInfo() { |
| 560 |
$db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
| 561 |
$db->query( |
| 562 |
"CREATE TABLE tmp_schema_tbl (" . |
| 563 |
"n integer not null primary key autoincrement, " . |
| 564 |
"k text, " . |
| 565 |
"v integer, " . |
| 566 |
"t integer" . |
| 567 |
")" |
| 568 |
); |
| 569 |
$db->query( "CREATE UNIQUE INDEX tmp_schema_tbl_k ON tmp_schema_tbl (k)" ); |
| 570 |
$db->query( "CREATE INDEX tmp_schema_tbl_t ON tmp_schema_tbl (t)" ); |
| 571 |
|
| 572 |
$this->assertTrue( $db->fieldExists( 'tmp_schema_tbl', 'n' ) ); |
| 573 |
$this->assertTrue( $db->fieldExists( 'tmp_schema_tbl', 'k' ) ); |
| 574 |
$this->assertTrue( $db->fieldExists( 'tmp_schema_tbl', 'v' ) ); |
| 575 |
$this->assertTrue( $db->fieldExists( 'tmp_schema_tbl', 't' ) ); |
| 576 |
$this->assertFalse( $db->fieldExists( 'tmp_schema_tbl', 'x' ) ); |
| 577 |
|
| 578 |
$this->assertTrue( $db->indexExists( 'tmp_schema_tbl', 'tmp_schema_tbl_k' ) ); |
| 579 |
$this->assertTrue( $db->indexExists( 'tmp_schema_tbl', 'tmp_schema_tbl_t' ) ); |
| 580 |
$this->assertFalse( $db->indexExists( 'tmp_schema_tbl', 'tmp_schema_tbl_x' ) ); |
| 581 |
$this->assertFalse( $db->indexExists( 'tmp_schema_tbl', 'PRIMARY' ) ); |
| 582 |
|
| 583 |
$this->assertTrue( $db->indexUnique( 'tmp_schema_tbl', 'tmp_schema_tbl_k' ) ); |
| 584 |
$this->assertFalse( $db->indexUnique( 'tmp_schema_tbl', 'tmp_schema_tbl_t' ) ); |
| 585 |
$this->assertNull( $db->indexUnique( 'tmp_schema_tbl', 'tmp_schema_tbl_x' ) ); |
| 586 |
$this->assertNull( $db->indexUnique( 'tmp_schema_tbl', 'PRIMARY' ) ); |
| 587 |
} |
| 588 |
|
| 589 |
private function createSourceTable( IDatabase $db ) { |
| 590 |
$db->query( "DROP TABLE IF EXISTS tmp_src_tbl" ); |
| 591 |
$db->query( |
| 592 |
"CREATE TABLE tmp_src_tbl (" . |
| 593 |
"sn integer not null primary key autoincrement, " . |
| 594 |
"sk text, " . |
| 595 |
"sv integer, " . |
| 596 |
"st integer" . |
| 597 |
")" |
| 598 |
); |
| 599 |
$db->query( "CREATE UNIQUE INDEX tmp_src_tbl_sk ON tmp_src_tbl (sk)" ); |
| 600 |
|
| 601 |
return "tmp_src_tbl"; |
| 602 |
} |
| 603 |
|
| 604 |
private function createDestTable( IDatabase $db ) { |
| 605 |
$db->query( "DROP TABLE IF EXISTS tmp_dst_tbl" ); |
| 606 |
$db->query( |
| 607 |
"CREATE TABLE tmp_dst_tbl (" . |
| 608 |
"n integer not null primary key autoincrement, " . |
| 609 |
"k text, " . |
| 610 |
"v integer, " . |
| 611 |
"t integer" . |
| 612 |
")" |
| 613 |
); |
| 614 |
$db->query( "CREATE UNIQUE INDEX tmp_dst_tbl_k ON tmp_dst_tbl (k)" ); |
| 615 |
|
| 616 |
return "tmp_dst_tbl"; |
| 617 |
} |
| 618 |
|
| 619 |
private function assertNWhereKEqualsLuca( $expected, $table, $db ) { |
| 620 |
$this->assertSame( $expected, (int)$db->newSelectQueryBuilder() |
| 621 |
->select( 'n' ) |
| 622 |
->from( $table ) |
| 623 |
->where( [ 'k' => 'Luca' ] ) |
| 624 |
->fetchField() ); |
| 625 |
} |
| 626 |
} |