Merge "adjust method comment for Ia19f1011"
[lhc/web/wiklou.git] / includes / db / DatabaseSqlite.php
index be8e781..15d1ad0 100644 (file)
@@ -3,6 +3,21 @@
  * This is the SQLite database abstraction layer.
  * See maintenance/sqlite/README for development notes and other specific information
  *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup Database
  */
@@ -19,15 +34,25 @@ class DatabaseSqlite extends DatabaseBase {
        var $mDatabaseFile;
        var $mName;
 
+       /**
+        * @var PDO
+        */
+       protected $mConn;
+
        /**
         * Constructor.
         * Parameters $server, $user and $password are not used.
+        * @param $server string
+        * @param $user string
+        * @param $password string
+        * @param $dbName string
+        * @param $flags int
         */
        function __construct( $server = false, $user = false, $password = false, $dbName = false, $flags = 0 ) {
                $this->mName = $dbName;
                parent::__construct( $server, $user, $password, $dbName, $flags );
                // parent doesn't open when $user is false, but we can work with $dbName
-               if( !$user && $dbName ) {
+               if( $dbName ) {
                        global $wgSharedDB;
                        if( $this->open( $server, $user, $password, $dbName ) && $wgSharedDB ) {
                                $this->attachDatabase( $wgSharedDB );
@@ -35,17 +60,31 @@ class DatabaseSqlite extends DatabaseBase {
                }
        }
 
+       /**
+        * @return string
+        */
        function getType() {
                return 'sqlite';
        }
 
        /**
         * @todo: check if it should be true like parent class
+        *
+        * @return bool
         */
-       function implicitGroupby()   { return false; }
+       function implicitGroupby() {
+               return false;
+       }
 
        /** Open an SQLite database and return a resource handle to it
         *  NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases
+        *
+        * @param $server
+        * @param $user
+        * @param $pass
+        * @param $dbName
+        *
+        * @return PDO
         */
        function open( $server, $user, $pass, $dbName ) {
                global $wgSQLiteDataDir;
@@ -61,7 +100,10 @@ class DatabaseSqlite extends DatabaseBase {
 
        /**
         * Opens a database file
-        * @return SQL connection or false if failed
+        *
+        * @param $fileName string
+        *
+        * @return PDO|bool SQL connection or false if failed
         */
        function openFile( $fileName ) {
                $this->mDatabaseFile = $fileName;
@@ -88,16 +130,11 @@ class DatabaseSqlite extends DatabaseBase {
        }
 
        /**
-        * Close an SQLite database
-        *
+        * Does not actually close the connection, just destroys the reference for GC to do its work
         * @return bool
         */
-       function close() {
-               $this->mOpened = false;
-               if ( is_object( $this->mConn ) ) {
-                       if ( $this->trxLevel() ) $this->commit();
-                       $this->mConn = null;
-               }
+       protected function closeConnection() {
+               $this->mConn = null;
                return true;
        }
 
@@ -113,7 +150,7 @@ class DatabaseSqlite extends DatabaseBase {
 
        /**
         * Check if the searchindext table is FTS enabled.
-        * @return false if not enabled.
+        * @return bool False if not enabled.
         */
        function checkForEnabledSearch() {
                if ( self::$fulltextEnabled === null ) {
@@ -152,9 +189,12 @@ class DatabaseSqlite extends DatabaseBase {
        /**
         * Attaches external database to our connection, see http://sqlite.org/lang_attach.html
         * for details.
+        *
         * @param $name String: database name to be used in queries like SELECT foo FROM dbname.table
         * @param $file String: database file name. If omitted, will be generated using $name and $wgSQLiteDataDir
         * @param $fname String: calling function name
+        *
+        * @return ResultWrapper
         */
        function attachDatabase( $name, $file = false, $fname = 'DatabaseSqlite::attachDatabase' ) {
                global $wgSQLiteDataDir;
@@ -168,6 +208,8 @@ class DatabaseSqlite extends DatabaseBase {
        /**
         * @see DatabaseBase::isWriteQuery()
         *
+        * @param $sql string
+        *
         * @return bool
         */
        function isWriteQuery( $sql ) {
@@ -176,8 +218,12 @@ class DatabaseSqlite extends DatabaseBase {
 
        /**
         * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
+        *
+        * @param $sql string
+        *
+        * @return ResultWrapper
         */
-       function doQuery( $sql ) {
+       protected function doQuery( $sql ) {
                $res = $this->mConn->query( $sql );
                if ( $res === false ) {
                        return false;
@@ -189,6 +235,9 @@ class DatabaseSqlite extends DatabaseBase {
                return $res;
        }
 
+       /**
+        * @param $res ResultWrapper
+        */
        function freeResult( $res ) {
                if ( $res instanceof ResultWrapper ) {
                        $res->result = null;
@@ -197,6 +246,10 @@ class DatabaseSqlite extends DatabaseBase {
                }
        }
 
+       /**
+        * @param $res ResultWrapper
+        * @return
+        */
        function fetchObject( $res ) {
                if ( $res instanceof ResultWrapper ) {
                        $r =& $res->result;
@@ -219,6 +272,10 @@ class DatabaseSqlite extends DatabaseBase {
                return false;
        }
 
+       /**
+        * @param $res ResultWrapper
+        * @return bool|mixed
+        */
        function fetchRow( $res ) {
                if ( $res instanceof ResultWrapper ) {
                        $r =& $res->result;
@@ -235,37 +292,60 @@ class DatabaseSqlite extends DatabaseBase {
 
        /**
         * The PDO::Statement class implements the array interface so count() will work
+        *
+        * @param $res ResultWrapper
+        *
+        * @return int
         */
        function numRows( $res ) {
                $r = $res instanceof ResultWrapper ? $res->result : $res;
                return count( $r );
        }
 
+       /**
+        * @param $res ResultWrapper
+        * @return int
+        */
        function numFields( $res ) {
                $r = $res instanceof ResultWrapper ? $res->result : $res;
                return is_array( $r ) ? count( $r[0] ) : 0;
        }
 
+       /**
+        * @param $res ResultWrapper
+        * @param $n 
+        * @return bool
+        */
        function fieldName( $res, $n ) {
                $r = $res instanceof ResultWrapper ? $res->result : $res;
                if ( is_array( $r ) ) {
                        $keys = array_keys( $r[0] );
                        return $keys[$n];
                }
-               return  false;
+               return false;
        }
 
        /**
         * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
+        *
+        * @param $name
+        * @param $format String
+        * @return string
         */
-       function tableName( $name, $quoted = true ) {
+       function tableName( $name, $format = 'quoted' ) {
                // table names starting with sqlite_ are reserved
-               if ( strpos( $name, 'sqlite_' ) === 0 ) return $name;
-               return str_replace( '"', '', parent::tableName( $name, $quoted ) );
+               if ( strpos( $name, 'sqlite_' ) === 0 ) {
+                       return $name;
+               }
+               return str_replace( '"', '', parent::tableName( $name, $format ) );
        }
 
        /**
         * Index names have DB scope
+        *
+        * @param $index string
+        *
+        * @return string
         */
        function indexName( $index ) {
                return $index;
@@ -273,11 +353,18 @@ class DatabaseSqlite extends DatabaseBase {
 
        /**
         * This must be called after nextSequenceVal
+        *
+        * @return int
         */
        function insertId() {
-               return $this->mConn->lastInsertId();
+               // PDO::lastInsertId yields a string :(
+               return intval( $this->mConn->lastInsertId() );
        }
 
+       /**
+        * @param $res ResultWrapper
+        * @param $row
+        */
        function dataSeek( $res, $row ) {
                if ( $res instanceof ResultWrapper ) {
                        $r =& $res->result;
@@ -292,6 +379,9 @@ class DatabaseSqlite extends DatabaseBase {
                }
        }
 
+       /**
+        * @return string
+        */
        function lastError() {
                if ( !is_object( $this->mConn ) ) {
                        return "Cannot return last error, no db connection";
@@ -300,6 +390,9 @@ class DatabaseSqlite extends DatabaseBase {
                return isset( $e[2] ) ? $e[2] : '';
        }
 
+       /**
+        * @return string
+        */
        function lastErrno() {
                if ( !is_object( $this->mConn ) ) {
                        return "Cannot return last error, no db connection";
@@ -309,6 +402,9 @@ class DatabaseSqlite extends DatabaseBase {
                }
        }
 
+       /**
+        * @return int
+        */
        function affectedRows() {
                return $this->mAffectedRows;
        }
@@ -317,6 +413,8 @@ class DatabaseSqlite extends DatabaseBase {
         * Returns information about an index
         * Returns false if the index does not exist
         * - if errors are explicitly ignored, returns NULL on failure
+        *
+        * @return array
         */
        function indexInfo( $table, $index, $fname = 'DatabaseSqlite::indexExists' ) {
                $sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')';
@@ -334,6 +432,12 @@ class DatabaseSqlite extends DatabaseBase {
                return $info;
        }
 
+       /**
+        * @param $table
+        * @param $index
+        * @param $fname string
+        * @return bool|null
+        */
        function indexUnique( $table, $index, $fname = 'DatabaseSqlite::indexUnique' ) {
                $row = $this->selectRow( 'sqlite_master', '*',
                        array(
@@ -356,6 +460,10 @@ class DatabaseSqlite extends DatabaseBase {
 
        /**
         * Filter the options used in SELECT statements
+        *
+        * @param $options array
+        *
+        * @return array
         */
        function makeSelectOptions( $options ) {
                foreach ( $options as $k => $v ) {
@@ -400,6 +508,7 @@ class DatabaseSqlite extends DatabaseBase {
 
        /**
         * Based on generic method (parent) with some prior SQLite-sepcific adjustments
+        * @return bool
         */
        function insert( $table, $a, $fname = 'DatabaseSqlite::insert', $options = array() ) {
                if ( !count( $a ) ) {
@@ -421,6 +530,13 @@ class DatabaseSqlite extends DatabaseBase {
                return $ret;
        }
 
+       /**
+        * @param $table
+        * @param $uniqueIndexes
+        * @param $rows
+        * @param $fname string
+        * @return bool|ResultWrapper
+        */
        function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabaseSqlite::replace' ) {
                if ( !count( $rows ) ) return true;
 
@@ -428,12 +544,12 @@ class DatabaseSqlite extends DatabaseBase {
                if ( isset( $rows[0] ) && is_array( $rows[0] ) ) {
                        $ret = true;
                        foreach ( $rows as $v ) {
-                               if ( !parent::replace( $table, $uniqueIndexes, $v, "$fname/multi-row" ) ) {
+                               if ( !$this->nativeReplace( $table, $v, "$fname/multi-row" ) ) {
                                        $ret = false;
                                }
                        }
                } else {
-                       $ret = parent::replace( $table, $uniqueIndexes, $rows, "$fname/single-row" );
+                       $ret = $this->nativeReplace( $table, $rows, "$fname/single-row" );
                }
 
                return $ret;
@@ -442,28 +558,47 @@ class DatabaseSqlite extends DatabaseBase {
        /**
         * Returns the size of a text field, or -1 for "unlimited"
         * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though.
+        *
+        * @return int
         */
        function textFieldSize( $table, $field ) {
                return -1;
        }
 
+       /**
+        * @return bool
+        */
        function unionSupportsOrderAndLimit() {
                return false;
        }
 
+       /**
+        * @param $sqls
+        * @param $all
+        * @return string
+        */
        function unionQueries( $sqls, $all ) {
                $glue = $all ? ' UNION ALL ' : ' UNION ';
                return implode( $glue, $sqls );
        }
 
+       /**
+        * @return bool
+        */
        function wasDeadlock() {
                return $this->lastErrno() == 5; // SQLITE_BUSY
        }
 
+       /**
+        * @return bool
+        */
        function wasErrorReissuable() {
                return $this->lastErrno() ==  17; // SQLITE_SCHEMA;
        }
 
+       /**
+        * @return bool
+        */
        function wasReadOnlyError() {
                return $this->lastErrno() == 8; // SQLITE_READONLY;
        }
@@ -494,7 +629,9 @@ class DatabaseSqlite extends DatabaseBase {
         * Get information about a given field
         * Returns false if the field does not exist.
         *
-        * @return SQLiteField|false
+        * @param $table string
+        * @param $field string
+        * @return SQLiteField|bool False on failure
         */
        function fieldInfo( $table, $field ) {
                $tableName = $this->tableName( $table );
@@ -509,35 +646,58 @@ class DatabaseSqlite extends DatabaseBase {
        }
 
        function begin( $fname = '' ) {
-               if ( $this->mTrxLevel == 1 ) $this->commit();
+               if ( $this->mTrxLevel == 1 ) {
+                       $this->commit( __METHOD__ );
+               }
                $this->mConn->beginTransaction();
                $this->mTrxLevel = 1;
        }
 
        function commit( $fname = '' ) {
-               if ( $this->mTrxLevel == 0 ) return;
+               if ( $this->mTrxLevel == 0 ) {
+                       return;
+               }
                $this->mConn->commit();
                $this->mTrxLevel = 0;
        }
 
        function rollback( $fname = '' ) {
-               if ( $this->mTrxLevel == 0 ) return;
+               if ( $this->mTrxLevel == 0 ) {
+                       return;
+               }
                $this->mConn->rollBack();
                $this->mTrxLevel = 0;
        }
 
+       /**
+        * @param  $sql
+        * @param  $num
+        * @return string
+        */
        function limitResultForUpdate( $sql, $num ) {
                return $this->limitResult( $sql, $num );
        }
 
+       /**
+        * @param $s string
+        * @return string
+        */
        function strencode( $s ) {
                return substr( $this->addQuotes( $s ), 1, - 1 );
        }
 
+       /**
+        * @param $b
+        * @return Blob
+        */
        function encodeBlob( $b ) {
                return new Blob( $b );
        }
 
+       /**
+        * @param $b Blob|string
+        * @return string
+        */
        function decodeBlob( $b ) {
                if ( $b instanceof Blob ) {
                        $b = $b->fetch();
@@ -545,6 +705,10 @@ class DatabaseSqlite extends DatabaseBase {
                return $b;
        }
 
+       /**
+        * @param $s Blob|string
+        * @return string
+        */
        function addQuotes( $s ) {
                if ( $s instanceof Blob ) {
                        return "x'" . bin2hex( $s->fetch() ) . "'";
@@ -553,6 +717,9 @@ class DatabaseSqlite extends DatabaseBase {
                }
        }
 
+       /**
+        * @return string
+        */
        function buildLike() {
                $params = func_get_args();
                if ( count( $params ) > 0 && is_array( $params[0] ) ) {
@@ -561,12 +728,16 @@ class DatabaseSqlite extends DatabaseBase {
                return parent::buildLike( $params ) . "ESCAPE '\' ";
        }
 
+       /**
+        * @return string
+        */
        public function getSearchEngine() {
                return "SearchSqlite";
        }
 
        /**
         * No-op version of deadlockLoop
+        * @return mixed
         */
        public function deadlockLoop( /*...*/ ) {
                $args = func_get_args();
@@ -623,12 +794,22 @@ class DatabaseSqlite extends DatabaseBase {
        /**
         * Build a concatenation list to feed into a SQL query
         *
+        * @param $stringList array
+        *
         * @return string
         */
        function buildConcat( $stringList ) {
                return '(' . implode( ') || (', $stringList ) . ')';
        }
 
+       /**
+        * @throws MWException
+        * @param $oldName
+        * @param $newName
+        * @param $temporary bool
+        * @param $fname string
+        * @return bool|ResultWrapper
+        */
        function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseSqlite::duplicateTableStructure' ) {
                $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name=" . $this->addQuotes( $oldName ) . " AND type='table'", $fname );
                $obj = $this->fetchObject( $res );
@@ -651,7 +832,7 @@ class DatabaseSqlite extends DatabaseBase {
        /**
         * List all tables on the database
         *
-        * @param $prefix Only show tables with this prefix, e.g. mw_
+        * @param $prefix string Only show tables with this prefix, e.g. mw_
         * @param $fname String: calling function name
         *
         * @return array
@@ -722,6 +903,9 @@ class SQLiteField implements Field {
                return $this->info->dflt_value;
        }
 
+       /**
+        * @return bool
+        */
        function isNullable() {
                return !$this->info->notnull;
        }