r61396 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61395‎ | r61396 | r61397 >
Date:21:30, 22 January 2010
Author:maxsem
Status:ok (Comments)
Tags:
Comment:
Special delivery for Nikerabbit: ability to connect to a SQLite db file directly, no foreplay with global settings needed.
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseSqlite.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/DatabaseSqlite.php
@@ -22,12 +22,8 @@
2323 * Parameters $server, $user and $password are not used.
2424 */
2525 function __construct( $server = false, $user = false, $password = false, $dbName = false, $failFunction = false, $flags = 0 ) {
26 - global $wgSQLiteDataDir;
2726 $this->mFailFunction = $failFunction;
2827 $this->mFlags = $flags;
29 - $this->mDatabaseFile = self::generateFileName( $wgSQLiteDataDir, $dbName );
30 - if ( !is_readable( $this->mDatabaseFile ) )
31 - throw new DBConnectionError( $this, "SQLite database not accessible" );
3228 $this->mName = $dbName;
3329 $this->open( $server, $user, $password, $dbName );
3430 }
@@ -49,36 +45,47 @@
5046 * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases
5147 */
5248 function open( $server, $user, $pass, $dbName ) {
53 - $this->mConn = false;
54 - if ( $dbName ) {
55 - $file = $this->mDatabaseFile;
56 - try {
57 - if ( $this->mFlags & DBO_PERSISTENT ) {
58 - $this->mConn = new PDO( "sqlite:$file", $user, $pass,
59 - array( PDO::ATTR_PERSISTENT => true ) );
60 - } else {
61 - $this->mConn = new PDO( "sqlite:$file", $user, $pass );
62 - }
63 - } catch ( PDOException $e ) {
64 - $err = $e->getMessage();
65 - }
66 - if ( $this->mConn === false ) {
67 - wfDebug( "DB connection error: $err\n" );
68 - if ( !$this->mFailFunction ) {
69 - throw new DBConnectionError( $this, $err );
70 - } else {
71 - return false;
72 - }
 49+ global $wgSQLiteDataDir;
7350
74 - }
75 - $this->mOpened = $this->mConn;
76 - # set error codes only, don't raise exceptions
77 - $this->mConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
 51+ $fileName = self::generateFileName( $wgSQLiteDataDir, $dbName );
 52+ if ( !is_readable( $fileName ) ) {
 53+ throw new DBConnectionError( $this, "SQLite database not accessible" ); $this->mConn = false;
7854 }
 55+ $this->openFile( $fileName );
7956 return $this->mConn;
8057 }
8158
8259 /**
 60+ * Opens a database file
 61+ * @return SQL connection or false if failed
 62+ */
 63+ function openFile( $fileName ) {
 64+ $this->mDatabaseFile = $fileName;
 65+ try {
 66+ if ( $this->mFlags & DBO_PERSISTENT ) {
 67+ $this->mConn = new PDO( "sqlite:$fileName", '', '',
 68+ array( PDO::ATTR_PERSISTENT => true ) );
 69+ } else {
 70+ $this->mConn = new PDO( "sqlite:$fileName", '', '' );
 71+ }
 72+ } catch ( PDOException $e ) {
 73+ $err = $e->getMessage();
 74+ }
 75+ if ( $this->mConn === false ) {
 76+ wfDebug( "DB connection error: $err\n" );
 77+ if ( !$this->mFailFunction ) {
 78+ throw new DBConnectionError( $this, $err );
 79+ } else {
 80+ return false;
 81+ }
 82+
 83+ }
 84+ $this->mOpened = $this->mConn;
 85+ # set error codes only, don't raise exceptions
 86+ $this->mConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
 87+ }
 88+
 89+ /**
8390 * Close an SQLite database
8491 */
8592 function close() {
@@ -551,8 +558,19 @@
552559 } // end DatabaseSqlite class
553560
554561 /**
 562+ * This class allows simple acccess to a SQLite database independently from main database settings
555563 * @ingroup Database
556564 */
 565+class DatabaseSqliteStandalone extends DatabaseSqlite {
 566+ public function __construct( $fileName, $flags = 0 ) {
 567+ $this->mFlags = $flags;
 568+ $this->openFile( $fileName );
 569+ }
 570+}
 571+
 572+/**
 573+ * @ingroup Database
 574+ */
557575 class SQLiteField {
558576 private $info, $tableName;
559577 function __construct( $info, $tableName ) {
Index: trunk/phase3/includes/AutoLoader.php
@@ -351,6 +351,7 @@
352352 'DatabaseOracle' => 'includes/db/DatabaseOracle.php',
353353 'DatabasePostgres' => 'includes/db/DatabasePostgres.php',
354354 'DatabaseSqlite' => 'includes/db/DatabaseSqlite.php',
 355+ 'DatabaseSqliteStandalone' => 'includes/db/DatabaseSqlite.php',
355356 'DBConnectionError' => 'includes/db/Database.php',
356357 'DBError' => 'includes/db/Database.php',
357358 'DBObject' => 'includes/db/Database.php',

Comments

#Comment by Bryan (talk | contribs)   21:54, 22 January 2010

Your commit summary is remarkable :)

#Comment by Tim Starling (talk | contribs)   23:49, 28 January 2010

In the long term I'd like to see all the Database*::__construct() functions take an associative array of DBMS-specific parameters, so you'd do something like:

$db = new DatabaseSqlite( array( 'file' => $fileName ) );

or better still

$db = Database::factory( array( 'type' => 'sqlite', 'file' => $fileName ) );

Status & tagging log