X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Flibs%2Frdbms%2Fdatabase%2FDatabaseSqlite.php;h=58b6ef934c87d812877fe8a0ff4bd4d13f3dffb9;hb=cefb552df43fef1d641fa784d8f906dc0710c088;hp=9242414dfe0d785e6454edb934a4d34fc926d6e5;hpb=43a548a5c3436b5d07091b504bbf5eb80d1ed215;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/libs/rdbms/database/DatabaseSqlite.php b/includes/libs/rdbms/database/DatabaseSqlite.php index 9242414dfe..58b6ef934c 100644 --- a/includes/libs/rdbms/database/DatabaseSqlite.php +++ b/includes/libs/rdbms/database/DatabaseSqlite.php @@ -46,16 +46,19 @@ class DatabaseSqlite extends Database { protected $trxMode; /** @var int The number of rows affected as an integer */ - protected $mAffectedRows; + protected $lastAffectedRowCount; /** @var resource */ - protected $mLastResult; + protected $lastResultHandle; /** @var PDO */ - protected $mConn; + protected $conn; /** @var FSLockManager (hopefully on the same server as the DB) */ protected $lockMgr; + /** @var array List of shared database already attached to this connection */ + private $alreadyAttached = []; + /** * Additional params include: * - dbDirectory : directory containing the DB and the lock file directory @@ -76,22 +79,13 @@ class DatabaseSqlite extends Database { throw new InvalidArgumentException( "Need 'dbDirectory' or 'dbFilePath' parameter." ); } else { $this->dbDir = $p['dbDirectory']; - $this->mDBname = $p['dbname']; - $lockDomain = $this->mDBname; + $this->dbName = $p['dbname']; + $lockDomain = $this->dbName; // Stock wiki mode using standard file names per DB. parent::__construct( $p ); // Super doesn't open when $user is false, but we can work with $dbName if ( $p['dbname'] && !$this->isOpen() ) { - if ( $this->open( $p['host'], $p['user'], $p['password'], $p['dbname'] ) ) { - $done = []; - foreach ( $this->tableAliases as $params ) { - if ( isset( $done[$params['dbname']] ) ) { - continue; - } - $this->attachDatabase( $params['dbname'] ); - $done[$params['dbname']] = 1; - } - } + $this->open( $p['host'], $p['user'], $p['password'], $p['dbname'] ); } } @@ -159,12 +153,12 @@ class DatabaseSqlite extends Database { $this->close(); $fileName = self::generateFileName( $this->dbDir, $dbName ); if ( !is_readable( $fileName ) ) { - $this->mConn = false; + $this->conn = false; throw new DBConnectionError( $this, "SQLite database not accessible" ); } $this->openFile( $fileName ); - return (bool)$this->mConn; + return (bool)$this->conn; } /** @@ -179,29 +173,29 @@ class DatabaseSqlite extends Database { $this->dbPath = $fileName; try { - if ( $this->mFlags & self::DBO_PERSISTENT ) { - $this->mConn = new PDO( "sqlite:$fileName", '', '', + if ( $this->flags & self::DBO_PERSISTENT ) { + $this->conn = new PDO( "sqlite:$fileName", '', '', [ PDO::ATTR_PERSISTENT => true ] ); } else { - $this->mConn = new PDO( "sqlite:$fileName", '', '' ); + $this->conn = new PDO( "sqlite:$fileName", '', '' ); } } catch ( PDOException $e ) { $err = $e->getMessage(); } - if ( !$this->mConn ) { + if ( !$this->conn ) { $this->queryLogger->debug( "DB connection error: $err\n" ); throw new DBConnectionError( $this, $err ); } - $this->mOpened = !!$this->mConn; - if ( $this->mOpened ) { + $this->opened = !!$this->conn; + if ( $this->opened ) { # Set error codes only, don't raise exceptions - $this->mConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT ); + $this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT ); # Enforce LIKE to be case sensitive, just like MySQL $this->query( 'PRAGMA case_sensitive_like = 1' ); - return $this->mConn; + return $this->conn; } return false; @@ -224,7 +218,7 @@ class DatabaseSqlite extends Database { * @return bool */ protected function closeConnection() { - $this->mConn = null; + $this->conn = null; return true; } @@ -302,6 +296,14 @@ class DatabaseSqlite extends Database { return parent::isWriteQuery( $sql ) && !preg_match( '/^(ATTACH|PRAGMA)\b/i', $sql ); } + protected function isTransactableQuery( $sql ) { + return parent::isTransactableQuery( $sql ) && !in_array( + $this->getQueryVerb( $sql ), + [ 'ATTACH', 'PRAGMA' ], + true + ); + } + /** * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result * @@ -309,13 +311,13 @@ class DatabaseSqlite extends Database { * @return bool|ResultWrapper */ protected function doQuery( $sql ) { - $res = $this->mConn->query( $sql ); + $res = $this->conn->query( $sql ); if ( $res === false ) { return false; } $r = $res instanceof ResultWrapper ? $res->result : $res; - $this->mAffectedRows = $r->rowCount(); + $this->lastAffectedRowCount = $r->rowCount(); $res = new ResultWrapper( $this, $r->fetchAll() ); return $res; @@ -445,7 +447,7 @@ class DatabaseSqlite extends Database { */ function insertId() { // PDO::lastInsertId yields a string :( - return intval( $this->mConn->lastInsertId() ); + return intval( $this->conn->lastInsertId() ); } /** @@ -470,10 +472,10 @@ class DatabaseSqlite extends Database { * @return string */ function lastError() { - if ( !is_object( $this->mConn ) ) { + if ( !is_object( $this->conn ) ) { return "Cannot return last error, no db connection"; } - $e = $this->mConn->errorInfo(); + $e = $this->conn->errorInfo(); return isset( $e[2] ) ? $e[2] : ''; } @@ -482,10 +484,10 @@ class DatabaseSqlite extends Database { * @return string */ function lastErrno() { - if ( !is_object( $this->mConn ) ) { + if ( !is_object( $this->conn ) ) { return "Cannot return last error, no db connection"; } else { - $info = $this->mConn->errorInfo(); + $info = $this->conn->errorInfo(); return $info[1]; } @@ -494,8 +496,8 @@ class DatabaseSqlite extends Database { /** * @return int */ - function affectedRows() { - return $this->mAffectedRows; + protected function fetchAffectedRowCount() { + return $this->lastAffectedRowCount; } /** @@ -567,7 +569,7 @@ class DatabaseSqlite extends Database { /** * @param array $options - * @return string + * @return array */ protected function makeUpdateOptionsArray( $options ) { $options = parent::makeUpdateOptionsArray( $options ); @@ -718,7 +720,7 @@ class DatabaseSqlite extends Database { * @return string Version information from the database */ function getServerVersion() { - $ver = $this->mConn->getAttribute( PDO::ATTR_SERVER_VERSION ); + $ver = $this->conn->getAttribute( PDO::ATTR_SERVER_VERSION ); return $ver; } @@ -750,7 +752,7 @@ class DatabaseSqlite extends Database { } else { $this->query( 'BEGIN', $fname ); } - $this->mTrxLevel = 1; + $this->trxLevel = 1; } /** @@ -790,7 +792,7 @@ class DatabaseSqlite extends Database { return "x'" . bin2hex( $s->fetch() ) . "'"; } elseif ( is_bool( $s ) ) { return (int)$s; - } elseif ( strpos( $s, "\0" ) !== false ) { + } elseif ( strpos( (string)$s, "\0" ) !== false ) { // SQLite doesn't support \0 in strings, so use the hex representation as a workaround. // This is a known limitation of SQLite's mprintf function which PDO // should work around, but doesn't. I have reported this to php.net as bug #63419: @@ -806,9 +808,9 @@ class DatabaseSqlite extends Database { 'For consistency all binary data should have been ' . 'first processed with self::encodeBlob()' ); - return "x'" . bin2hex( $s ) . "'"; + return "x'" . bin2hex( (string)$s ) . "'"; } else { - return $this->mConn->quote( $s ); + return $this->conn->quote( (string)$s ); } } @@ -1033,6 +1035,17 @@ class DatabaseSqlite extends Database { return $this->query( $sql, $fName ); } + public function setTableAliases( array $aliases ) { + parent::setTableAliases( $aliases ); + foreach ( $this->tableAliases as $params ) { + if ( isset( $this->alreadyAttached[$params['dbname']] ) ) { + continue; + } + $this->attachDatabase( $params['dbname'] ); + $this->alreadyAttached[$params['dbname']] = true; + } + } + protected function requiresDatabaseUser() { return false; // just a file } @@ -1041,7 +1054,7 @@ class DatabaseSqlite extends Database { * @return string */ public function __toString() { - return 'SQLite ' . (string)$this->mConn->getAttribute( PDO::ATTR_SERVER_VERSION ); + return 'SQLite ' . (string)$this->conn->getAttribute( PDO::ATTR_SERVER_VERSION ); } }