24 if ( $this->functionTest ) {
26 $this->functionTest =
false;
28 $this->db->restoreFlags( IDatabase::RESTORE_INITIAL );
36 if ( $this->db->getType() ===
'sqlite' || $this->db->getType() ===
'oracle' ) {
39 $this->assertEquals( $check, $this->db->addQuotes(
null ) );
43 # returning just "1234" should be ok too, though...
47 $this->db->addQuotes( 1234 ) );
51 # returning just "1234.5678" would be ok too, though
54 $this->db->addQuotes( 1234.5678 ) );
60 $this->db->addQuotes(
'string' ) );
64 $check =
"'string''s cause trouble'";
65 if ( $this->db->getType() ===
'mysql' ) {
66 $check =
"'string\'s cause trouble'";
70 $this->db->addQuotes(
"string's cause trouble" ) );
76 $this->db->setTableAliases( [
78 'dbname' => $database,
84 $ret = $this->db->tableName( $table, $format );
86 $this->db->setTableAliases( array_fill_keys(
98 private function prefixAndQuote( $table, $database =
null, $prefix =
null, $format =
'quoted' ) {
99 if ( $this->db->getType() ===
'sqlite' || $format !==
'quoted' ) {
101 } elseif ( $this->db->getType() ===
'mysql' ) {
103 } elseif ( $this->db->getType() ===
'oracle' ) {
109 if ( $database !==
null ) {
110 if ( $this->db->getType() ===
'oracle' ) {
111 $database = $quote . $database .
'.';
113 $database = $quote . $database . $quote .
'.';
117 if ( $prefix ===
null ) {
121 if ( $this->db->getType() ===
'oracle' ) {
122 return strtoupper( $database . $quote . $prefix . $table );
124 return $database . $quote . $prefix . $table . $quote;
131 $this->db->tableName(
'tablename' )
138 $this->db->tableName(
'tablename',
'raw' )
156 $this->
prefixAndQuote(
'tablename',
'sharedatabase',
'sh_',
'raw' ),
161 $this->
prefixAndQuote(
'tablename',
'sharedatabase',
null,
'raw' ),
169 $this->db->tableName(
'databasename.tablename' )
176 $this->db->tableName(
'databasename.tablename',
'raw' )
181 if ( !in_array(
wfGetDB(
DB_MASTER )->getType(), [
'mysql',
'postgres' ] ) ) {
182 $this->markTestSkipped(
'MySQL or Postgres required' );
186 $this->functionTest =
true;
188 $this->db->sourceFile(
"$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
190 $res = $this->db->query(
'SELECT mw_test_function() AS test', __METHOD__ );
191 $this->assertEquals( 42,
$res->fetchObject()->test );
195 $this->db->query(
'DROP FUNCTION IF EXISTS mw_test_function'
196 . ( $this->db->getType() ==
'postgres' ?
'()' :
'' )
201 $res = $this->db->select(
'page',
'*', [
'page_id' => 1 ] );
202 $this->assertFalse( $this->db->tableExists(
'foobarbaz' ) );
203 $this->assertInternalType(
'int',
$res->numRows() );
212 $db->onTransactionIdle(
219 $this->assertFalse( $flagSet,
'DBO_TRX off in callback' );
220 $this->assertTrue(
$db->getFlag(
DBO_TRX ),
'DBO_TRX restored to default' );
221 $this->assertTrue(
$called,
'Callback reached' );
225 $db->onTransactionIdle(
226 function ()
use (
$db, &$flagSet ) {
231 $this->assertFalse( $flagSet,
'DBO_TRX off in callback' );
232 $this->assertFalse(
$db->getFlag(
DBO_TRX ),
'DBO_TRX restored to default' );
235 $db->onTransactionIdle(
241 $this->assertFalse(
$db->getFlag(
DBO_TRX ),
'DBO_TRX restored to default' );
248 $db->begin( __METHOD__ );
254 $db->commit( __METHOD__ );
255 $this->assertFalse(
$db->getFlag(
DBO_TRX ),
'DBO_TRX restored to default' );
256 $this->assertTrue(
$called,
'Callback reached' );
259 $db->begin( __METHOD__ );
265 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
266 $this->assertFalse(
$db->getFlag(
DBO_TRX ),
'DBO_TRX restored to default' );
267 $this->assertTrue(
$called,
'Callback reached' );
281 $db->begin( __METHOD__ );
282 $db->commit( __METHOD__ );
283 $this->assertTrue(
$called,
'Callback reached' );
286 $db->begin( __METHOD__ );
287 $db->commit( __METHOD__ );
288 $this->assertTrue(
$called,
'Callback still reached' );
291 $db->begin( __METHOD__ );
292 $db->rollback( __METHOD__ );
293 $this->assertTrue(
$called,
'Callback reached' );
295 $db->setTransactionListener(
'ping',
null );
297 $db->begin( __METHOD__ );
298 $db->commit( __METHOD__ );
299 $this->assertFalse(
$called,
'Callback not reached' );
308 $db->flushSnapshot( __METHOD__ );
309 $db->flushSnapshot( __METHOD__ );
312 $db->query(
'SELECT 1', __METHOD__ );
313 $this->assertTrue( (
bool)
$db->trxLevel(),
"Transaction started." );
314 $db->flushSnapshot( __METHOD__ );
315 $db->restoreFlags( $db::RESTORE_PRIOR );
317 $this->assertFalse( (
bool)
$db->trxLevel(),
"Transaction cleared." );
326 }
catch ( RunTimeException
$e ) {
327 $this->assertTrue(
$db->trxLevel() > 0,
"Transaction not committed." );
330 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
331 $this->assertTrue(
$db->lockIsFree(
'meow', __METHOD__ ) );
335 }
catch ( RunTimeException
$e ) {
336 $this->assertTrue(
$db->trxLevel() > 0,
"Transaction not committed." );
338 $db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
339 $this->assertTrue(
$db->lockIsFree(
'meow', __METHOD__ ) );
343 $lock =
$db->getScopedLockAndFlush(
'meow', __METHOD__, 1 );
344 $db->query(
"SELECT 1" );
345 throw new RunTimeException(
"Uh oh!" );
349 $lock =
$db->getScopedLockAndFlush(
'meow', __METHOD__, 1 );
350 $db->begin( __METHOD__ );
351 throw new RunTimeException(
"Uh oh!" );
365 ?
$db->clearFlag(
DBO_TRX, $db::REMEMBER_PRIOR )
366 :
$db->setFlag(
DBO_TRX, $db::REMEMBER_PRIOR );
367 $this->assertEquals( !$origTrx,
$db->getFlag(
DBO_TRX ) );
370 ?
$db->clearFlag(
DBO_SSL, $db::REMEMBER_PRIOR )
371 :
$db->setFlag(
DBO_SSL, $db::REMEMBER_PRIOR );
372 $this->assertEquals( !$origSsl,
$db->getFlag(
DBO_SSL ) );
374 $db->restoreFlags( $db::RESTORE_INITIAL );
375 $this->assertEquals( $origTrx,
$db->getFlag(
DBO_TRX ) );
376 $this->assertEquals( $origSsl,
$db->getFlag(
DBO_SSL ) );
379 ?
$db->clearFlag(
DBO_TRX, $db::REMEMBER_PRIOR )
380 :
$db->setFlag(
DBO_TRX, $db::REMEMBER_PRIOR );
382 ?
$db->clearFlag(
DBO_SSL, $db::REMEMBER_PRIOR )
383 :
$db->setFlag(
DBO_SSL, $db::REMEMBER_PRIOR );
386 $this->assertEquals( $origSsl,
$db->getFlag(
DBO_SSL ) );
387 $this->assertEquals( !$origTrx,
$db->getFlag(
DBO_TRX ) );
390 $this->assertEquals( $origSsl,
$db->getFlag(
DBO_SSL ) );
391 $this->assertEquals( $origTrx,
$db->getFlag(
DBO_TRX ) );
399 $old = $this->db->tablePrefix();
400 $this->
assertType(
'string', $old,
'Prefix is string' );
401 $this->assertEquals( $old, $this->db->tablePrefix(),
"Prefix unchanged" );
402 $this->assertEquals( $old, $this->db->tablePrefix(
'xxx' ) );
403 $this->assertEquals(
'xxx', $this->db->tablePrefix(),
"Prefix set" );
404 $this->db->tablePrefix( $old );
405 $this->assertNotEquals(
'xxx', $this->db->tablePrefix() );
407 $old = $this->db->dbSchema();
408 $this->
assertType(
'string', $old,
'Schema is string' );
409 $this->assertEquals( $old, $this->db->dbSchema(),
"Schema unchanged" );
410 $this->assertEquals( $old, $this->db->dbSchema(
'xxx' ) );
411 $this->assertEquals(
'xxx', $this->db->dbSchema(),
"Schema set" );
412 $this->db->dbSchema( $old );
413 $this->assertNotEquals(
'xxx', $this->db->dbSchema() );