22 if ( $this->functionTest ) {
24 $this->functionTest =
false;
26 $this->db->restoreFlags( IDatabase::RESTORE_INITIAL );
34 if ( $this->db->getType() ===
'sqlite' || $this->db->getType() ===
'oracle' ) {
37 $this->assertEquals( $check, $this->db->addQuotes(
null ) );
41 # returning just "1234" should be ok too, though...
45 $this->db->addQuotes( 1234 ) );
49 # returning just "1234.5678" would be ok too, though
52 $this->db->addQuotes( 1234.5678 ) );
58 $this->db->addQuotes(
'string' ) );
62 $check =
"'string''s cause trouble'";
63 if ( $this->db->getType() ===
'mysql' ) {
64 $check =
"'string\'s cause trouble'";
68 $this->db->addQuotes(
"string's cause trouble" ) );
74 $this->db->setTableAliases( [
76 'dbname' => $database,
82 $ret = $this->db->tableName( $table, $format );
84 $this->db->setTableAliases( array_fill_keys(
96 private function prefixAndQuote( $table, $database =
null, $prefix =
null, $format =
'quoted' ) {
97 if ( $this->db->getType() ===
'sqlite' || $format !==
'quoted' ) {
99 } elseif ( $this->db->getType() ===
'mysql' ) {
101 } elseif ( $this->db->getType() ===
'oracle' ) {
107 if ( $database !==
null ) {
108 if ( $this->db->getType() ===
'oracle' ) {
109 $database = $quote . $database .
'.';
111 $database = $quote . $database . $quote .
'.';
115 if ( $prefix ===
null ) {
119 if ( $this->db->getType() ===
'oracle' ) {
120 return strtoupper( $database . $quote . $prefix . $table );
122 return $database . $quote . $prefix . $table . $quote;
129 $this->db->tableName(
'tablename' )
136 $this->db->tableName(
'tablename',
'raw' )
154 $this->
prefixAndQuote(
'tablename',
'sharedatabase',
'sh_',
'raw' ),
159 $this->
prefixAndQuote(
'tablename',
'sharedatabase',
null,
'raw' ),
167 $this->db->tableName(
'databasename.tablename' )
174 $this->db->tableName(
'databasename.tablename',
'raw' )
179 if ( !in_array(
wfGetDB(
DB_MASTER )->getType(), [
'mysql',
'postgres' ] ) ) {
180 $this->markTestSkipped(
'MySQL or Postgres required' );
184 $this->functionTest =
true;
186 $this->db->sourceFile(
"$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
188 $res = $this->db->query(
'SELECT mw_test_function() AS test', __METHOD__ );
189 $this->assertEquals( 42,
$res->fetchObject()->test );
193 $this->db->query(
'DROP FUNCTION IF EXISTS mw_test_function'
194 . ( $this->db->getType() ==
'postgres' ?
'()' :
'' )
199 $res = $this->db->select(
'page',
'*', [
'page_id' => 1 ] );
200 $this->assertFalse( $this->db->tableExists(
'foobarbaz' ) );
201 $this->assertInternalType(
'int',
$res->numRows() );
217 $this->assertFalse( $flagSet,
'DBO_TRX off in callback' );
219 $this->assertTrue(
$called,
'Callback reached' );
224 function ()
use (
$db, &$flagSet ) {
229 $this->assertFalse( $flagSet,
'DBO_TRX off in callback' );
254 $this->assertTrue(
$called,
'Callback reached' );
263 $db->
rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
265 $this->assertTrue(
$called,
'Callback reached' );
281 $this->assertTrue(
$called,
'Callback reached' );
286 $this->assertTrue(
$called,
'Callback still reached' );
291 $this->assertTrue(
$called,
'Callback reached' );
297 $this->assertFalse(
$called,
'Callback not reached' );
310 $db->
query(
'SELECT 1', __METHOD__ );
311 $this->assertTrue( (
bool)
$db->
trxLevel(),
"Transaction started." );
315 $this->assertFalse( (
bool)
$db->
trxLevel(),
"Transaction cleared." );
324 }
catch ( RunTimeException
$e ) {
325 $this->assertTrue(
$db->
trxLevel() > 0,
"Transaction not committed." );
328 $db->
rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
333 }
catch ( RunTimeException
$e ) {
334 $this->assertTrue(
$db->
trxLevel() > 0,
"Transaction not committed." );
336 $db->
rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
343 throw new RunTimeException(
"Uh oh!" );
349 throw new RunTimeException(
"Uh oh!" );
397 $old = $this->db->tablePrefix();
398 $this->
assertType(
'string', $old,
'Prefix is string' );
399 $this->assertEquals( $old, $this->db->tablePrefix(),
"Prefix unchanged" );
400 $this->assertEquals( $old, $this->db->tablePrefix(
'xxx' ) );
401 $this->assertEquals(
'xxx', $this->db->tablePrefix(),
"Prefix set" );
402 $this->db->tablePrefix( $old );
403 $this->assertNotEquals(
'xxx', $this->db->tablePrefix() );
405 $old = $this->db->dbSchema();
406 $this->
assertType(
'string', $old,
'Schema is string' );
407 $this->assertEquals( $old, $this->db->dbSchema(),
"Schema unchanged" );
408 $this->assertEquals( $old, $this->db->dbSchema(
'xxx' ) );
409 $this->assertEquals(
'xxx', $this->db->dbSchema(),
"Schema set" );
410 $this->db->dbSchema( $old );
411 $this->assertNotEquals(
'xxx', $this->db->dbSchema() );
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgSharedDB
Shared database for multiple wikis.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
badLockingMethodImplicit(IDatabase $db)
badLockingMethodExplicit(IDatabase $db)
testTransactionListener()
Database::setTransactionListener()
testAddQuotesStringQuote()
testMutators()
Database::tablePrefix() Database::dbSchema()
testFlagSetting()
Database::getFlag( Database::setFlag() Database::restoreFlags()
testFlushSnapshot()
Database::flushSnapshot()
testTransactionResolution()
testAddQuotesNull()
Database::dropTable.
testUnknownTableCorruptsResults()
getSharedTableName( $table, $database, $prefix, $format='quoted')
prefixAndQuote( $table, $database=null, $prefix=null, $format='quoted')
testTableNameRawForeign()
Relational database abstraction object.
onTransactionResolution(callable $callback, $fname=__METHOD__)
Run a callback as soon as the current transaction commits or rolls back.
flushSnapshot( $fname=__METHOD__)
Commit any transaction but error out if writes or callbacks are pending.
getFlag( $flag)
Returns a boolean whether the flag $flag is set for this connection.
restoreFlags( $state=self::RESTORE_PRIOR)
Restore the flags to their prior state before the last setFlag/clearFlag call.
onTransactionIdle(callable $callback, $fname=__METHOD__)
Run a callback as soon as there is no transaction pending.
setTransactionListener( $name, callable $callback=null)
Run a callback each time any transaction commits or rolls back.
getScopedLockAndFlush( $lockKey, $fname, $timeout)
Acquire a named lock, flush any transaction, and return an RAII style unlocker object.
commit( $fname=__METHOD__, $flush='')
Commits a transaction previously started using begin().
query( $sql, $fname=__METHOD__, $tempIgnore=false)
Run an SQL query and return the result.
trxLevel()
Gets the current transaction level.
setFlag( $flag, $remember=self::REMEMBER_NOTHING)
Set a flag for this connection.
rollback( $fname=__METHOD__, $flush='')
Rollback a transaction previously started using begin().
begin( $fname=__METHOD__, $mode=self::TRANSACTION_EXPLICIT)
Begin a transaction.
clearFlag( $flag, $remember=self::REMEMBER_NOTHING)
Clear a flag for this connection.
lockIsFree( $lockName, $method)
Check to see if a named lock is available (non-blocking)
when a variable name is used in a it is silently declared as a new local masking the global
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
returning false will NOT prevent logging $e
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Basic database interface for live and lazy-loaded relation database handles.