From: Max Semenik Date: Fri, 22 Jan 2010 21:30:23 +0000 (+0000) Subject: Special delivery for Nikerabbit: ability to connect to a SQLite db file directly... X-Git-Tag: 1.31.0-rc.0~38154 X-Git-Url: http://git.cyclocoop.org/%28?a=commitdiff_plain;h=6c7bb573163c9144aae1933ecfaab1998997d61d;p=lhc%2Fweb%2Fwiklou.git Special delivery for Nikerabbit: ability to connect to a SQLite db file directly, no foreplay with global settings needed. --- diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 3837253f09..4504b1c10e 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -351,6 +351,7 @@ $wgAutoloadLocalClasses = array( 'DatabaseOracle' => 'includes/db/DatabaseOracle.php', 'DatabasePostgres' => 'includes/db/DatabasePostgres.php', 'DatabaseSqlite' => 'includes/db/DatabaseSqlite.php', + 'DatabaseSqliteStandalone' => 'includes/db/DatabaseSqlite.php', 'DBConnectionError' => 'includes/db/Database.php', 'DBError' => 'includes/db/Database.php', 'DBObject' => 'includes/db/Database.php', diff --git a/includes/db/DatabaseSqlite.php b/includes/db/DatabaseSqlite.php index 9a8e183a94..8a93c21569 100644 --- a/includes/db/DatabaseSqlite.php +++ b/includes/db/DatabaseSqlite.php @@ -22,12 +22,8 @@ class DatabaseSqlite extends DatabaseBase { * Parameters $server, $user and $password are not used. */ function __construct( $server = false, $user = false, $password = false, $dbName = false, $failFunction = false, $flags = 0 ) { - global $wgSQLiteDataDir; $this->mFailFunction = $failFunction; $this->mFlags = $flags; - $this->mDatabaseFile = self::generateFileName( $wgSQLiteDataDir, $dbName ); - if ( !is_readable( $this->mDatabaseFile ) ) - throw new DBConnectionError( $this, "SQLite database not accessible" ); $this->mName = $dbName; $this->open( $server, $user, $password, $dbName ); } @@ -49,35 +45,46 @@ class DatabaseSqlite extends DatabaseBase { * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases */ function open( $server, $user, $pass, $dbName ) { - $this->mConn = false; - if ( $dbName ) { - $file = $this->mDatabaseFile; - try { - if ( $this->mFlags & DBO_PERSISTENT ) { - $this->mConn = new PDO( "sqlite:$file", $user, $pass, - array( PDO::ATTR_PERSISTENT => true ) ); - } else { - $this->mConn = new PDO( "sqlite:$file", $user, $pass ); - } - } catch ( PDOException $e ) { - $err = $e->getMessage(); - } - if ( $this->mConn === false ) { - wfDebug( "DB connection error: $err\n" ); - if ( !$this->mFailFunction ) { - throw new DBConnectionError( $this, $err ); - } else { - return false; - } + global $wgSQLiteDataDir; - } - $this->mOpened = $this->mConn; - # set error codes only, don't raise exceptions - $this->mConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT ); + $fileName = self::generateFileName( $wgSQLiteDataDir, $dbName ); + if ( !is_readable( $fileName ) ) { + throw new DBConnectionError( $this, "SQLite database not accessible" ); $this->mConn = false; } + $this->openFile( $fileName ); return $this->mConn; } + /** + * Opens a database file + * @return SQL connection or false if failed + */ + function openFile( $fileName ) { + $this->mDatabaseFile = $fileName; + try { + if ( $this->mFlags & DBO_PERSISTENT ) { + $this->mConn = new PDO( "sqlite:$fileName", '', '', + array( PDO::ATTR_PERSISTENT => true ) ); + } else { + $this->mConn = new PDO( "sqlite:$fileName", '', '' ); + } + } catch ( PDOException $e ) { + $err = $e->getMessage(); + } + if ( $this->mConn === false ) { + wfDebug( "DB connection error: $err\n" ); + if ( !$this->mFailFunction ) { + throw new DBConnectionError( $this, $err ); + } else { + return false; + } + + } + $this->mOpened = $this->mConn; + # set error codes only, don't raise exceptions + $this->mConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT ); + } + /** * Close an SQLite database */ @@ -550,6 +557,17 @@ class DatabaseSqlite extends DatabaseBase { } // end DatabaseSqlite class +/** + * This class allows simple acccess to a SQLite database independently from main database settings + * @ingroup Database + */ +class DatabaseSqliteStandalone extends DatabaseSqlite { + public function __construct( $fileName, $flags = 0 ) { + $this->mFlags = $flags; + $this->openFile( $fileName ); + } +} + /** * @ingroup Database */