2008-05-07 23:40:14 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2010-08-01 21:13:44 +00:00
|
|
|
* This is the SQLite database abstraction layer.
|
2008-05-13 00:01:00 +00:00
|
|
|
* See maintenance/sqlite/README for development notes and other specific information
|
2010-08-01 21:13:44 +00:00
|
|
|
*
|
2012-04-26 08:47:10 +00:00
|
|
|
* 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
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
2010-08-01 21:13:44 +00:00
|
|
|
* @ingroup Database
|
2008-05-07 23:40:14 +00:00
|
|
|
*/
|
2017-02-07 04:49:57 +00:00
|
|
|
namespace Wikimedia\Rdbms;
|
|
|
|
|
|
|
|
|
|
use PDO;
|
|
|
|
|
use PDOException;
|
2018-10-26 20:17:34 +00:00
|
|
|
use Exception;
|
2017-02-07 04:49:57 +00:00
|
|
|
use LockManager;
|
|
|
|
|
use FSLockManager;
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
use RuntimeException;
|
|
|
|
|
use stdClass;
|
2008-05-07 23:40:14 +00:00
|
|
|
|
|
|
|
|
/**
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Database
|
2008-05-07 23:40:14 +00:00
|
|
|
*/
|
2016-09-28 23:08:15 +00:00
|
|
|
class DatabaseSqlite extends Database {
|
2013-12-26 23:45:36 +00:00
|
|
|
/** @var bool Whether full text is enabled */
|
2010-06-16 18:26:47 +00:00
|
|
|
private static $fulltextEnabled = null;
|
|
|
|
|
|
2015-03-07 12:31:08 +00:00
|
|
|
/** @var string Directory */
|
|
|
|
|
protected $dbDir;
|
2013-12-26 23:45:36 +00:00
|
|
|
/** @var string File name for SQLite database file */
|
2015-03-07 12:31:08 +00:00
|
|
|
protected $dbPath;
|
2015-02-24 00:32:00 +00:00
|
|
|
/** @var string Transaction mode */
|
|
|
|
|
protected $trxMode;
|
|
|
|
|
|
2013-12-26 23:45:36 +00:00
|
|
|
/** @var int The number of rows affected as an integer */
|
2018-02-16 19:16:10 +00:00
|
|
|
protected $lastAffectedRowCount;
|
2013-12-26 23:45:36 +00:00
|
|
|
/** @var resource */
|
2018-02-16 19:16:10 +00:00
|
|
|
protected $lastResultHandle;
|
2008-05-07 23:40:14 +00:00
|
|
|
|
2016-12-07 00:06:39 +00:00
|
|
|
/** @var PDO */
|
2018-02-13 06:58:57 +00:00
|
|
|
protected $conn;
|
2011-05-28 17:52:12 +00:00
|
|
|
|
2014-01-31 23:32:52 +00:00
|
|
|
/** @var FSLockManager (hopefully on the same server as the DB) */
|
|
|
|
|
protected $lockMgr;
|
|
|
|
|
|
2018-01-02 17:56:32 +00:00
|
|
|
/** @var array List of shared database already attached to this connection */
|
|
|
|
|
private $alreadyAttached = [];
|
|
|
|
|
|
2015-02-24 00:32:00 +00:00
|
|
|
/**
|
|
|
|
|
* Additional params include:
|
2015-03-07 12:31:08 +00:00
|
|
|
* - dbDirectory : directory containing the DB and the lock file directory
|
|
|
|
|
* [defaults to $wgSQLiteDataDir]
|
|
|
|
|
* - dbFilePath : use this to force the path of the DB file
|
|
|
|
|
* - trxMode : one of (deferred, immediate, exclusive)
|
2015-02-24 00:32:00 +00:00
|
|
|
* @param array $p
|
|
|
|
|
*/
|
2015-02-20 22:10:26 +00:00
|
|
|
function __construct( array $p ) {
|
2015-03-07 12:31:08 +00:00
|
|
|
if ( isset( $p['dbFilePath'] ) ) {
|
2018-02-28 20:56:34 +00:00
|
|
|
$this->dbPath = $p['dbFilePath'];
|
|
|
|
|
$lockDomain = md5( $this->dbPath );
|
2018-08-14 23:44:41 +00:00
|
|
|
// Use "X" for things like X.sqlite and ":memory:" for RAM-only DBs
|
|
|
|
|
if ( !isset( $p['dbname'] ) || !strlen( $p['dbname'] ) ) {
|
|
|
|
|
$p['dbname'] = preg_replace( '/\.sqlite\d?$/', '', basename( $this->dbPath ) );
|
|
|
|
|
}
|
2018-02-28 20:56:34 +00:00
|
|
|
} elseif ( isset( $p['dbDirectory'] ) ) {
|
2016-09-18 03:39:28 +00:00
|
|
|
$this->dbDir = $p['dbDirectory'];
|
2018-02-28 20:56:34 +00:00
|
|
|
$lockDomain = $p['dbname'];
|
|
|
|
|
} else {
|
|
|
|
|
throw new InvalidArgumentException( "Need 'dbDirectory' or 'dbFilePath' parameter." );
|
2010-06-26 12:10:47 +00:00
|
|
|
}
|
2014-01-31 23:32:52 +00:00
|
|
|
|
2015-02-24 00:32:00 +00:00
|
|
|
$this->trxMode = isset( $p['trxMode'] ) ? strtoupper( $p['trxMode'] ) : null;
|
2015-03-07 12:31:08 +00:00
|
|
|
if ( $this->trxMode &&
|
2016-02-17 09:09:32 +00:00
|
|
|
!in_array( $this->trxMode, [ 'DEFERRED', 'IMMEDIATE', 'EXCLUSIVE' ] )
|
2015-03-07 12:31:08 +00:00
|
|
|
) {
|
2015-02-24 00:32:00 +00:00
|
|
|
$this->trxMode = null;
|
2016-09-18 03:39:28 +00:00
|
|
|
$this->queryLogger->warning( "Invalid SQLite transaction mode provided." );
|
2015-02-24 00:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-18 03:48:23 +00:00
|
|
|
$this->lockMgr = new FSLockManager( [
|
|
|
|
|
'domain' => $lockDomain,
|
|
|
|
|
'lockDirectory' => "{$this->dbDir}/locks"
|
|
|
|
|
] );
|
2018-02-28 20:56:34 +00:00
|
|
|
|
|
|
|
|
parent::__construct( $p );
|
2015-03-07 12:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
2018-02-28 00:00:05 +00:00
|
|
|
protected static function getAttributes() {
|
|
|
|
|
return [ self::ATTR_DB_LEVEL_LOCKING => true ];
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-07 12:31:08 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $filename
|
|
|
|
|
* @param array $p Options map; supports:
|
|
|
|
|
* - flags : (same as __construct counterpart)
|
|
|
|
|
* - trxMode : (same as __construct counterpart)
|
|
|
|
|
* - dbDirectory : (same as __construct counterpart)
|
|
|
|
|
* @return DatabaseSqlite
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public static function newStandaloneInstance( $filename, array $p = [] ) {
|
2015-03-07 12:31:08 +00:00
|
|
|
$p['dbFilePath'] = $filename;
|
2018-08-14 23:44:41 +00:00
|
|
|
$p['schema'] = null;
|
2015-04-27 19:17:52 +00:00
|
|
|
$p['tablePrefix'] = '';
|
2017-02-07 04:49:57 +00:00
|
|
|
/** @var DatabaseSqlite $db */
|
|
|
|
|
$db = Database::factory( 'sqlite', $p );
|
2015-03-07 12:31:08 +00:00
|
|
|
|
2017-02-07 04:49:57 +00:00
|
|
|
return $db;
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2018-02-28 20:56:34 +00:00
|
|
|
protected function doInitConnection() {
|
|
|
|
|
if ( $this->dbPath !== null ) {
|
|
|
|
|
// Standalone .sqlite file mode.
|
2018-08-14 23:44:41 +00:00
|
|
|
$this->openFile(
|
|
|
|
|
$this->dbPath,
|
|
|
|
|
$this->connectionParams['dbname'],
|
|
|
|
|
$this->connectionParams['tablePrefix']
|
|
|
|
|
);
|
2018-02-28 20:56:34 +00:00
|
|
|
} elseif ( $this->dbDir !== null ) {
|
|
|
|
|
// Stock wiki mode using standard file names per DB
|
|
|
|
|
if ( strlen( $this->connectionParams['dbname'] ) ) {
|
|
|
|
|
$this->open(
|
|
|
|
|
$this->connectionParams['host'],
|
|
|
|
|
$this->connectionParams['user'],
|
|
|
|
|
$this->connectionParams['password'],
|
2018-08-14 23:44:41 +00:00
|
|
|
$this->connectionParams['dbname'],
|
|
|
|
|
$this->connectionParams['schema'],
|
|
|
|
|
$this->connectionParams['tablePrefix']
|
2018-02-28 20:56:34 +00:00
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
// Caller will manually call open() later?
|
|
|
|
|
$this->connLogger->debug( __METHOD__ . ': no database opened.' );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new InvalidArgumentException( "Need 'dbDirectory' or 'dbFilePath' parameter." );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-01-08 00:31:24 +00:00
|
|
|
function getType() {
|
|
|
|
|
return 'sqlite';
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
/**
|
2013-05-15 01:12:35 +00:00
|
|
|
* @todo Check if it should be true like parent class
|
2011-05-28 17:52:12 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2008-05-07 23:40:14 +00:00
|
|
|
*/
|
2011-05-28 17:52:12 +00:00
|
|
|
function implicitGroupby() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2008-05-07 23:40:14 +00:00
|
|
|
|
2018-08-14 23:44:41 +00:00
|
|
|
protected function open( $server, $user, $pass, $dbName, $schema, $tablePrefix ) {
|
2013-10-17 04:16:46 +00:00
|
|
|
$this->close();
|
2015-03-07 12:31:08 +00:00
|
|
|
$fileName = self::generateFileName( $this->dbDir, $dbName );
|
2010-01-22 21:30:23 +00:00
|
|
|
if ( !is_readable( $fileName ) ) {
|
2018-02-13 06:58:57 +00:00
|
|
|
$this->conn = false;
|
2010-06-11 12:51:23 +00:00
|
|
|
throw new DBConnectionError( $this, "SQLite database not accessible" );
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
2018-08-22 01:34:51 +00:00
|
|
|
// Only $dbName is used, the other parameters are irrelevant for SQLite databases
|
2018-08-14 23:44:41 +00:00
|
|
|
$this->openFile( $fileName, $dbName, $tablePrefix );
|
2018-02-28 20:56:34 +00:00
|
|
|
|
2018-02-13 06:58:57 +00:00
|
|
|
return (bool)$this->conn;
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-22 21:30:23 +00:00
|
|
|
/**
|
|
|
|
|
* Opens a database file
|
2011-05-28 17:52:12 +00:00
|
|
|
*
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $fileName
|
2018-04-13 08:39:22 +00:00
|
|
|
* @param string $dbName
|
2018-08-14 23:44:41 +00:00
|
|
|
* @param string $tablePrefix
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws DBConnectionError
|
2012-02-09 17:42:35 +00:00
|
|
|
* @return PDO|bool SQL connection or false if failed
|
2010-01-22 21:30:23 +00:00
|
|
|
*/
|
2018-08-14 23:44:41 +00:00
|
|
|
protected function openFile( $fileName, $dbName, $tablePrefix ) {
|
2014-01-06 18:23:05 +00:00
|
|
|
$err = false;
|
|
|
|
|
|
2015-03-07 12:31:08 +00:00
|
|
|
$this->dbPath = $fileName;
|
2010-01-22 21:30:23 +00:00
|
|
|
try {
|
2018-02-13 06:58:57 +00:00
|
|
|
if ( $this->flags & self::DBO_PERSISTENT ) {
|
|
|
|
|
$this->conn = new PDO( "sqlite:$fileName", '', '',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ PDO::ATTR_PERSISTENT => true ] );
|
2010-01-22 21:30:23 +00:00
|
|
|
} else {
|
2018-02-13 06:58:57 +00:00
|
|
|
$this->conn = new PDO( "sqlite:$fileName", '', '' );
|
2010-01-22 21:30:23 +00:00
|
|
|
}
|
|
|
|
|
} catch ( PDOException $e ) {
|
|
|
|
|
$err = $e->getMessage();
|
|
|
|
|
}
|
2013-12-27 01:54:51 +00:00
|
|
|
|
2018-02-13 06:58:57 +00:00
|
|
|
if ( !$this->conn ) {
|
2016-09-18 03:39:28 +00:00
|
|
|
$this->queryLogger->debug( "DB connection error: $err\n" );
|
2010-10-24 20:48:48 +00:00
|
|
|
throw new DBConnectionError( $this, $err );
|
2010-01-22 21:30:23 +00:00
|
|
|
}
|
2013-12-27 01:54:51 +00:00
|
|
|
|
2018-02-28 20:56:34 +00:00
|
|
|
$this->opened = is_object( $this->conn );
|
2018-02-13 06:58:57 +00:00
|
|
|
if ( $this->opened ) {
|
2018-08-14 23:44:41 +00:00
|
|
|
$this->currentDomain = new DatabaseDomain( $dbName, null, $tablePrefix );
|
2015-02-19 01:05:20 +00:00
|
|
|
# Set error codes only, don't raise exceptions
|
2018-02-13 06:58:57 +00:00
|
|
|
$this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
|
2013-02-18 14:39:29 +00:00
|
|
|
# Enforce LIKE to be case sensitive, just like MySQL
|
2012-12-24 20:10:22 +00:00
|
|
|
$this->query( 'PRAGMA case_sensitive_like = 1' );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2018-02-13 06:58:57 +00:00
|
|
|
return $this->conn;
|
2010-02-03 19:55:56 +00:00
|
|
|
}
|
2014-01-06 18:32:50 +00:00
|
|
|
|
|
|
|
|
return false;
|
2010-01-22 21:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-07 12:31:08 +00:00
|
|
|
/**
|
|
|
|
|
* @return string SQLite DB file path
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
public function getDbFilePath() {
|
|
|
|
|
return $this->dbPath;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
/**
|
2012-02-28 14:42:08 +00:00
|
|
|
* Does not actually close the connection, just destroys the reference for GC to do its work
|
2011-05-11 23:11:41 +00:00
|
|
|
* @return bool
|
2008-05-07 23:40:14 +00:00
|
|
|
*/
|
2012-02-28 14:42:08 +00:00
|
|
|
protected function closeConnection() {
|
2018-02-13 06:58:57 +00:00
|
|
|
$this->conn = null;
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-20 20:23:14 +00:00
|
|
|
/**
|
|
|
|
|
* Generates a database file name. Explicitly public for installer.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $dir Directory where database resides
|
|
|
|
|
* @param string $dbName Database name
|
2013-12-27 01:54:51 +00:00
|
|
|
* @return string
|
2009-10-20 20:23:14 +00:00
|
|
|
*/
|
|
|
|
|
public static function generateFileName( $dir, $dbName ) {
|
|
|
|
|
return "$dir/$dbName.sqlite";
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-16 18:26:47 +00:00
|
|
|
/**
|
|
|
|
|
* Check if the searchindext table is FTS enabled.
|
2012-02-10 15:37:33 +00:00
|
|
|
* @return bool False if not enabled.
|
2010-06-16 18:26:47 +00:00
|
|
|
*/
|
|
|
|
|
function checkForEnabledSearch() {
|
|
|
|
|
if ( self::$fulltextEnabled === null ) {
|
|
|
|
|
self::$fulltextEnabled = false;
|
2010-11-01 19:51:36 +00:00
|
|
|
$table = $this->tableName( 'searchindex' );
|
|
|
|
|
$res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name = '$table'", __METHOD__ );
|
2010-06-16 18:26:47 +00:00
|
|
|
if ( $res ) {
|
|
|
|
|
$row = $res->fetchRow();
|
2013-02-03 18:47:42 +00:00
|
|
|
self::$fulltextEnabled = stristr( $row['sql'], 'fts' ) !== false;
|
2010-06-16 18:26:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2010-06-16 18:26:47 +00:00
|
|
|
return self::$fulltextEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-29 16:19:35 +00:00
|
|
|
/**
|
|
|
|
|
* Returns version of currently supported SQLite fulltext search module or false if none present.
|
2013-12-27 01:54:51 +00:00
|
|
|
* @return string
|
2009-10-29 16:19:35 +00:00
|
|
|
*/
|
2011-04-11 17:16:41 +00:00
|
|
|
static function getFulltextSearchModule() {
|
2010-09-23 19:36:06 +00:00
|
|
|
static $cachedResult = null;
|
|
|
|
|
if ( $cachedResult !== null ) {
|
|
|
|
|
return $cachedResult;
|
|
|
|
|
}
|
|
|
|
|
$cachedResult = false;
|
2009-10-29 16:19:35 +00:00
|
|
|
$table = 'dummy_search_test';
|
2012-06-16 05:24:59 +00:00
|
|
|
|
2015-03-07 12:31:08 +00:00
|
|
|
$db = self::newStandaloneInstance( ':memory:' );
|
2011-04-11 17:16:41 +00:00
|
|
|
if ( $db->query( "CREATE VIRTUAL TABLE $table USING FTS3(dummy_field)", __METHOD__, true ) ) {
|
2010-09-23 19:36:06 +00:00
|
|
|
$cachedResult = 'FTS3';
|
2009-10-29 16:19:35 +00:00
|
|
|
}
|
2011-04-11 17:16:41 +00:00
|
|
|
$db->close();
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2010-09-23 19:36:06 +00:00
|
|
|
return $cachedResult;
|
2009-10-29 16:19:35 +00:00
|
|
|
}
|
|
|
|
|
|
2010-06-26 12:10:47 +00:00
|
|
|
/**
|
2016-10-13 05:34:26 +00:00
|
|
|
* Attaches external database to our connection, see https://sqlite.org/lang_attach.html
|
2010-06-26 12:10:47 +00:00
|
|
|
* for details.
|
2011-05-28 17:52:12 +00:00
|
|
|
*
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $name Database name to be used in queries like
|
2013-11-20 10:13:51 +00:00
|
|
|
* SELECT foo FROM dbname.table
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param bool|string $file Database file name. If omitted, will be generated
|
2015-03-07 12:31:08 +00:00
|
|
|
* using $name and configured data directory
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $fname Calling function name
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return ResultWrapper
|
2010-06-26 12:10:47 +00:00
|
|
|
*/
|
2013-05-06 13:20:40 +00:00
|
|
|
function attachDatabase( $name, $file = false, $fname = __METHOD__ ) {
|
2010-06-26 12:10:47 +00:00
|
|
|
if ( !$file ) {
|
2015-03-07 12:31:08 +00:00
|
|
|
$file = self::generateFileName( $this->dbDir, $name );
|
2010-06-26 12:10:47 +00:00
|
|
|
}
|
|
|
|
|
$file = $this->addQuotes( $file );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2010-06-26 12:10:47 +00:00
|
|
|
return $this->query( "ATTACH DATABASE $file AS $name", $fname );
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-28 23:42:43 +00:00
|
|
|
protected function isWriteQuery( $sql ) {
|
2015-08-20 00:47:49 +00:00
|
|
|
return parent::isWriteQuery( $sql ) && !preg_match( '/^(ATTACH|PRAGMA)\b/i', $sql );
|
2010-06-27 12:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
2018-01-02 17:56:32 +00:00
|
|
|
protected function isTransactableQuery( $sql ) {
|
|
|
|
|
return parent::isTransactableQuery( $sql ) && !in_array(
|
|
|
|
|
$this->getQueryVerb( $sql ),
|
|
|
|
|
[ 'ATTACH', 'PRAGMA' ],
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
/**
|
|
|
|
|
* SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
|
2011-05-28 17:52:12 +00:00
|
|
|
*
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $sql
|
|
|
|
|
* @return bool|ResultWrapper
|
2008-05-07 23:40:14 +00:00
|
|
|
*/
|
2011-06-20 12:09:22 +00:00
|
|
|
protected function doQuery( $sql ) {
|
2018-02-28 20:56:34 +00:00
|
|
|
$res = $this->getBindingHandle()->query( $sql );
|
2009-09-15 21:05:30 +00:00
|
|
|
if ( $res === false ) {
|
2009-01-15 06:56:58 +00:00
|
|
|
return false;
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2016-09-18 03:21:50 +00:00
|
|
|
$r = $res instanceof ResultWrapper ? $res->result : $res;
|
2018-02-16 19:16:10 +00:00
|
|
|
$this->lastAffectedRowCount = $r->rowCount();
|
2016-09-18 03:21:50 +00:00
|
|
|
$res = new ResultWrapper( $this, $r->fetchAll() );
|
|
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param ResultWrapper|mixed $res
|
2011-05-28 17:52:12 +00:00
|
|
|
*/
|
2009-09-15 21:05:30 +00:00
|
|
|
function freeResult( $res ) {
|
2010-08-25 15:38:32 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
2009-12-11 21:07:27 +00:00
|
|
|
$res->result = null;
|
2010-08-25 15:38:32 +00:00
|
|
|
} else {
|
2009-12-11 21:07:27 +00:00
|
|
|
$res = null;
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param ResultWrapper|array $res
|
|
|
|
|
* @return stdClass|bool
|
2011-05-28 17:52:12 +00:00
|
|
|
*/
|
2010-01-17 20:47:23 +00:00
|
|
|
function fetchObject( $res ) {
|
2010-08-25 15:38:32 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
2009-09-15 21:05:30 +00:00
|
|
|
$r =& $res->result;
|
2010-08-25 15:38:32 +00:00
|
|
|
} else {
|
2009-09-15 21:05:30 +00:00
|
|
|
$r =& $res;
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
2009-09-15 21:05:30 +00:00
|
|
|
|
|
|
|
|
$cur = current( $r );
|
|
|
|
|
if ( is_array( $cur ) ) {
|
|
|
|
|
next( $r );
|
2008-05-07 23:40:14 +00:00
|
|
|
$obj = new stdClass;
|
2010-08-25 15:38:32 +00:00
|
|
|
foreach ( $cur as $k => $v ) {
|
|
|
|
|
if ( !is_numeric( $k ) ) {
|
2009-09-15 21:05:30 +00:00
|
|
|
$obj->$k = $v;
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-09-15 21:05:30 +00:00
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
return $obj;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param ResultWrapper|mixed $res
|
2013-02-18 14:39:29 +00:00
|
|
|
* @return array|bool
|
2011-05-28 17:52:12 +00:00
|
|
|
*/
|
2010-01-17 20:47:23 +00:00
|
|
|
function fetchRow( $res ) {
|
2010-08-25 15:38:32 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
2009-09-15 21:05:30 +00:00
|
|
|
$r =& $res->result;
|
2010-08-25 15:38:32 +00:00
|
|
|
} else {
|
2009-09-15 21:05:30 +00:00
|
|
|
$r =& $res;
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
2010-01-17 20:47:23 +00:00
|
|
|
$cur = current( $r );
|
|
|
|
|
if ( is_array( $cur ) ) {
|
|
|
|
|
next( $r );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
return $cur;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The PDO::Statement class implements the array interface so count() will work
|
2011-05-28 17:52:12 +00:00
|
|
|
*
|
2018-07-02 11:19:23 +00:00
|
|
|
* @param ResultWrapper|array|false $res
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return int
|
2008-05-07 23:40:14 +00:00
|
|
|
*/
|
2009-09-15 21:05:30 +00:00
|
|
|
function numRows( $res ) {
|
2018-07-02 11:19:23 +00:00
|
|
|
// false does not implement Countable
|
2008-05-07 23:40:14 +00:00
|
|
|
$r = $res instanceof ResultWrapper ? $res->result : $res;
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2018-07-02 11:19:23 +00:00
|
|
|
return is_array( $r ) ? count( $r ) : 0;
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param ResultWrapper $res
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return int
|
|
|
|
|
*/
|
2009-09-15 21:05:30 +00:00
|
|
|
function numFields( $res ) {
|
2008-05-07 23:40:14 +00:00
|
|
|
$r = $res instanceof ResultWrapper ? $res->result : $res;
|
2014-07-19 21:12:10 +00:00
|
|
|
if ( is_array( $r ) && count( $r ) > 0 ) {
|
2018-10-04 07:06:00 +00:00
|
|
|
// The size of the result array is twice the number of fields. (T67578)
|
2014-07-19 21:12:10 +00:00
|
|
|
return count( $r[0] ) / 2;
|
2014-05-21 11:38:56 +00:00
|
|
|
} else {
|
|
|
|
|
// If the result is empty return 0
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param ResultWrapper $res
|
2014-04-19 11:55:27 +00:00
|
|
|
* @param int $n
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2009-09-15 21:05:30 +00:00
|
|
|
function fieldName( $res, $n ) {
|
2008-05-07 23:40:14 +00:00
|
|
|
$r = $res instanceof ResultWrapper ? $res->result : $res;
|
2009-09-15 21:05:30 +00:00
|
|
|
if ( is_array( $r ) ) {
|
|
|
|
|
$keys = array_keys( $r[0] );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
return $keys[$n];
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
return false;
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
|
2011-05-28 17:52:12 +00:00
|
|
|
*
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @param string $format
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return string
|
2008-05-07 23:40:14 +00:00
|
|
|
*/
|
2011-09-06 20:51:10 +00:00
|
|
|
function tableName( $name, $format = 'quoted' ) {
|
2010-09-19 15:19:38 +00:00
|
|
|
// table names starting with sqlite_ are reserved
|
2011-05-28 17:52:12 +00:00
|
|
|
if ( strpos( $name, 'sqlite_' ) === 0 ) {
|
|
|
|
|
return $name;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2011-09-06 20:51:10 +00:00
|
|
|
return str_replace( '"', '', parent::tableName( $name, $format ) );
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This must be called after nextSequenceVal
|
2011-05-28 17:52:12 +00:00
|
|
|
*
|
|
|
|
|
* @return int
|
2008-05-07 23:40:14 +00:00
|
|
|
*/
|
|
|
|
|
function insertId() {
|
2012-04-06 18:54:24 +00:00
|
|
|
// PDO::lastInsertId yields a string :(
|
2018-02-28 20:56:34 +00:00
|
|
|
return intval( $this->getBindingHandle()->lastInsertId() );
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param ResultWrapper|array $res
|
|
|
|
|
* @param int $row
|
2011-05-28 17:52:12 +00:00
|
|
|
*/
|
2009-09-15 21:05:30 +00:00
|
|
|
function dataSeek( $res, $row ) {
|
2010-08-25 15:38:32 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
2009-09-15 21:05:30 +00:00
|
|
|
$r =& $res->result;
|
2010-08-25 15:38:32 +00:00
|
|
|
} else {
|
2009-09-15 21:05:30 +00:00
|
|
|
$r =& $res;
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
2009-09-15 21:05:30 +00:00
|
|
|
reset( $r );
|
2010-08-25 15:38:32 +00:00
|
|
|
if ( $row > 0 ) {
|
|
|
|
|
for ( $i = 0; $i < $row; $i++ ) {
|
2009-09-15 21:05:30 +00:00
|
|
|
next( $r );
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2008-05-07 23:40:14 +00:00
|
|
|
function lastError() {
|
2018-02-13 06:58:57 +00:00
|
|
|
if ( !is_object( $this->conn ) ) {
|
2009-09-15 21:05:30 +00:00
|
|
|
return "Cannot return last error, no db connection";
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
2018-02-13 06:58:57 +00:00
|
|
|
$e = $this->conn->errorInfo();
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2017-10-06 22:17:58 +00:00
|
|
|
return $e[2] ?? '';
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2008-05-07 23:40:14 +00:00
|
|
|
function lastErrno() {
|
2018-02-13 06:58:57 +00:00
|
|
|
if ( !is_object( $this->conn ) ) {
|
2009-01-15 06:56:58 +00:00
|
|
|
return "Cannot return last error, no db connection";
|
|
|
|
|
} else {
|
2018-02-13 06:58:57 +00:00
|
|
|
$info = $this->conn->errorInfo();
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2009-01-15 06:56:58 +00:00
|
|
|
return $info[1];
|
|
|
|
|
}
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2018-01-28 14:10:39 +00:00
|
|
|
protected function fetchAffectedRowCount() {
|
2018-02-16 19:16:10 +00:00
|
|
|
return $this->lastAffectedRowCount;
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-06 21:38:47 +00:00
|
|
|
function tableExists( $table, $fname = __METHOD__ ) {
|
|
|
|
|
$tableRaw = $this->tableName( $table, 'raw' );
|
|
|
|
|
if ( isset( $this->sessionTempTables[$tableRaw] ) ) {
|
|
|
|
|
return true; // already known to exist
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$encTable = $this->addQuotes( $tableRaw );
|
|
|
|
|
$res = $this->query(
|
|
|
|
|
"SELECT 1 FROM sqlite_master WHERE type='table' AND name=$encTable" );
|
|
|
|
|
|
|
|
|
|
return $res->numRows() ? true : false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
/**
|
|
|
|
|
* Returns information about an index
|
2009-01-15 06:56:58 +00:00
|
|
|
* Returns false if the index does not exist
|
2008-05-07 23:40:14 +00:00
|
|
|
* - if errors are explicitly ignored, returns NULL on failure
|
2011-05-28 17:52:12 +00:00
|
|
|
*
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param string $index
|
|
|
|
|
* @param string $fname
|
2016-10-24 22:19:21 +00:00
|
|
|
* @return array|false
|
2008-05-07 23:40:14 +00:00
|
|
|
*/
|
2013-05-06 13:20:40 +00:00
|
|
|
function indexInfo( $table, $index, $fname = __METHOD__ ) {
|
2009-01-19 13:56:08 +00:00
|
|
|
$sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')';
|
2009-01-15 06:56:58 +00:00
|
|
|
$res = $this->query( $sql, $fname );
|
2016-10-24 22:19:21 +00:00
|
|
|
if ( !$res || $res->numRows() == 0 ) {
|
2009-01-15 06:56:58 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$info = [];
|
2009-01-15 06:56:58 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$info[] = $row->name;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2009-01-15 06:56:58 +00:00
|
|
|
return $info;
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param string $index
|
|
|
|
|
* @param string $fname
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return bool|null
|
|
|
|
|
*/
|
2013-05-06 13:20:40 +00:00
|
|
|
function indexUnique( $table, $index, $fname = __METHOD__ ) {
|
2009-09-15 21:05:30 +00:00
|
|
|
$row = $this->selectRow( 'sqlite_master', '*',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2009-01-15 06:56:58 +00:00
|
|
|
'type' => 'index',
|
2009-01-19 13:56:08 +00:00
|
|
|
'name' => $this->indexName( $index ),
|
2016-02-17 09:09:32 +00:00
|
|
|
], $fname );
|
2009-01-15 06:56:58 +00:00
|
|
|
if ( !$row || !isset( $row->sql ) ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// $row->sql will be of the form CREATE [UNIQUE] INDEX ...
|
|
|
|
|
$indexPos = strpos( $row->sql, 'INDEX' );
|
|
|
|
|
if ( $indexPos === false ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$firstPart = substr( $row->sql, 0, $indexPos );
|
|
|
|
|
$options = explode( ' ', $firstPart );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2009-01-15 06:56:58 +00:00
|
|
|
return in_array( 'UNIQUE', $options );
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Filter the options used in SELECT statements
|
2011-05-28 17:52:12 +00:00
|
|
|
*
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param array $options
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return array
|
2008-05-07 23:40:14 +00:00
|
|
|
*/
|
2009-09-15 21:05:30 +00:00
|
|
|
function makeSelectOptions( $options ) {
|
2010-08-25 15:38:32 +00:00
|
|
|
foreach ( $options as $k => $v ) {
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( is_numeric( $k ) && ( $v == 'FOR UPDATE' || $v == 'LOCK IN SHARE MODE' ) ) {
|
2009-09-15 21:05:30 +00:00
|
|
|
$options[$k] = '';
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2009-09-15 21:05:30 +00:00
|
|
|
return parent::makeSelectOptions( $options );
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param array $options
|
2017-08-04 17:59:46 +00:00
|
|
|
* @return array
|
2008-05-07 23:40:14 +00:00
|
|
|
*/
|
2014-03-12 12:25:20 +00:00
|
|
|
protected function makeUpdateOptionsArray( $options ) {
|
|
|
|
|
$options = parent::makeUpdateOptionsArray( $options );
|
2011-05-02 20:14:17 +00:00
|
|
|
$options = self::fixIgnore( $options );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2014-03-12 12:25:20 +00:00
|
|
|
return $options;
|
2011-05-02 20:14:17 +00:00
|
|
|
}
|
2008-05-07 23:40:14 +00:00
|
|
|
|
2011-05-02 20:14:17 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param array $options
|
2011-05-02 20:14:17 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
static function fixIgnore( $options ) {
|
2008-05-07 23:40:14 +00:00
|
|
|
# SQLite uses OR IGNORE not just IGNORE
|
2010-08-25 15:38:32 +00:00
|
|
|
foreach ( $options as $k => $v ) {
|
|
|
|
|
if ( $v == 'IGNORE' ) {
|
2009-09-15 21:05:30 +00:00
|
|
|
$options[$k] = 'OR IGNORE';
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2011-05-02 20:14:17 +00:00
|
|
|
return $options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param array $options
|
2011-05-02 20:14:17 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-05-02 21:38:48 +00:00
|
|
|
function makeInsertOptions( $options ) {
|
2011-05-02 20:14:17 +00:00
|
|
|
$options = self::fixIgnore( $options );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2011-05-02 20:14:17 +00:00
|
|
|
return parent::makeInsertOptions( $options );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Based on generic method (parent) with some prior SQLite-sepcific adjustments
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param array $a
|
|
|
|
|
* @param string $fname
|
|
|
|
|
* @param array $options
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return bool
|
2011-05-02 20:14:17 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
|
2011-05-02 20:14:17 +00:00
|
|
|
if ( !count( $a ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2008-05-07 23:40:14 +00:00
|
|
|
|
|
|
|
|
# SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts
|
2009-09-15 21:05:30 +00:00
|
|
|
if ( isset( $a[0] ) && is_array( $a[0] ) ) {
|
2018-10-26 20:17:34 +00:00
|
|
|
$affectedRowCount = 0;
|
|
|
|
|
try {
|
|
|
|
|
$this->startAtomic( $fname, self::ATOMIC_CANCELABLE );
|
|
|
|
|
foreach ( $a as $v ) {
|
|
|
|
|
parent::insert( $table, $v, "$fname/multi-row", $options );
|
2018-10-31 15:32:46 +00:00
|
|
|
$affectedRowCount += $this->affectedRows();
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
2018-10-26 20:17:34 +00:00
|
|
|
$this->endAtomic( $fname );
|
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
$this->cancelAtomic( $fname );
|
|
|
|
|
throw $e;
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
2018-10-26 20:17:34 +00:00
|
|
|
$this->affectedRowCount = $affectedRowCount;
|
2009-09-15 21:05:30 +00:00
|
|
|
} else {
|
2018-10-26 20:17:34 +00:00
|
|
|
parent::insert( $table, $a, "$fname/single-row", $options );
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-26 20:17:34 +00:00
|
|
|
return true;
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param array $uniqueIndexes Unused
|
|
|
|
|
* @param string|array $rows
|
|
|
|
|
* @param string $fname
|
2011-05-28 17:52:12 +00:00
|
|
|
*/
|
2013-05-06 13:20:40 +00:00
|
|
|
function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( !count( $rows ) ) {
|
2018-10-26 20:17:34 +00:00
|
|
|
return;
|
2013-04-17 14:52:47 +00:00
|
|
|
}
|
2010-06-16 18:26:47 +00:00
|
|
|
|
2010-01-10 17:27:46 +00:00
|
|
|
# SQLite can't handle multi-row replaces, so divide up into multiple single-row queries
|
|
|
|
|
if ( isset( $rows[0] ) && is_array( $rows[0] ) ) {
|
2018-10-26 20:17:34 +00:00
|
|
|
$affectedRowCount = 0;
|
|
|
|
|
try {
|
|
|
|
|
$this->startAtomic( $fname, self::ATOMIC_CANCELABLE );
|
|
|
|
|
foreach ( $rows as $v ) {
|
|
|
|
|
$this->nativeReplace( $table, $v, "$fname/multi-row" );
|
|
|
|
|
$affectedRowCount += $this->affectedRows();
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
2018-10-26 20:17:34 +00:00
|
|
|
$this->endAtomic( $fname );
|
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
$this->cancelAtomic( $fname );
|
|
|
|
|
throw $e;
|
2010-08-25 15:38:32 +00:00
|
|
|
}
|
2018-10-26 20:17:34 +00:00
|
|
|
$this->affectedRowCount = $affectedRowCount;
|
2010-01-10 17:27:46 +00:00
|
|
|
} else {
|
2018-10-26 20:17:34 +00:00
|
|
|
$this->nativeReplace( $table, $rows, "$fname/single-row" );
|
2010-01-10 17:27:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-15 06:56:58 +00:00
|
|
|
/**
|
|
|
|
|
* 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.
|
2011-05-28 17:52:12 +00:00
|
|
|
*
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param string $field
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return int
|
2009-01-15 06:56:58 +00:00
|
|
|
*/
|
2009-09-15 21:05:30 +00:00
|
|
|
function textFieldSize( $table, $field ) {
|
2011-01-11 14:30:03 +00:00
|
|
|
return -1;
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2009-10-17 12:23:23 +00:00
|
|
|
function unionSupportsOrderAndLimit() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2016-12-15 19:22:03 +00:00
|
|
|
* @param string[] $sqls
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param bool $all Whether to "UNION ALL" or not
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2009-10-17 12:23:23 +00:00
|
|
|
function unionQueries( $sqls, $all ) {
|
|
|
|
|
$glue = $all ? ' UNION ALL ' : ' UNION ';
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2009-10-17 12:23:23 +00:00
|
|
|
return implode( $glue, $sqls );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2008-05-07 23:40:14 +00:00
|
|
|
function wasDeadlock() {
|
2009-10-29 16:19:35 +00:00
|
|
|
return $this->lastErrno() == 5; // SQLITE_BUSY
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
* Converted BagOStuff.php from the style of memcached-client.php to the standard MediaWiki style, including camel case, using protected visibility instead of initial underscore, abstract functions instead of stubs, stylize.php.
* In SqlBagOStuff, ignore errors due to a read-only database, per my comments on CR r42796. Same for LocalisationCache.
* Merged SqlBagOStuff and MediaWikiBagOStuff, that proved to be an awkward and unnecessary generalisation. Use the standard quoting wrapper functions instead of $db->query().
* Implemented atomic incr() and decr() functions for SqlBagOStuff.
* Made incr() and decr() generally work roughly the same as it does in memcached, respecting negative steps instead of ignoring such operations. This allows decr() to be implemented in terms of incr().
* Per bug 11533, in MessageCache.php, don't retry 20 times on a cache failure, that's really memcached-specific and won't be useful for other cache types. It's not really very useful for memcached either.
* Moved MySQL-specific implementations of wasDeadlock() and wasErrorReissuable() to DatabaseMysql.
* Briefly tested page views with $wgReadOnly=read_only=1, fixed an error from Article::viewUpdates(). A CentralAuth fix will be in a subsequent commit.
2009-08-15 03:45:19 +00:00
|
|
|
function wasReadOnlyError() {
|
2009-10-29 16:19:35 +00:00
|
|
|
return $this->lastErrno() == 8; // SQLITE_READONLY;
|
* Converted BagOStuff.php from the style of memcached-client.php to the standard MediaWiki style, including camel case, using protected visibility instead of initial underscore, abstract functions instead of stubs, stylize.php.
* In SqlBagOStuff, ignore errors due to a read-only database, per my comments on CR r42796. Same for LocalisationCache.
* Merged SqlBagOStuff and MediaWikiBagOStuff, that proved to be an awkward and unnecessary generalisation. Use the standard quoting wrapper functions instead of $db->query().
* Implemented atomic incr() and decr() functions for SqlBagOStuff.
* Made incr() and decr() generally work roughly the same as it does in memcached, respecting negative steps instead of ignoring such operations. This allows decr() to be implemented in terms of incr().
* Per bug 11533, in MessageCache.php, don't retry 20 times on a cache failure, that's really memcached-specific and won't be useful for other cache types. It's not really very useful for memcached either.
* Moved MySQL-specific implementations of wasDeadlock() and wasErrorReissuable() to DatabaseMysql.
* Briefly tested page views with $wgReadOnly=read_only=1, fixed an error from Article::viewUpdates(). A CentralAuth fix will be in a subsequent commit.
2009-08-15 03:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-20 00:26:49 +00:00
|
|
|
public function wasConnectionError( $errno ) {
|
|
|
|
|
return $errno == 17; // SQLITE_SCHEMA;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-23 09:57:21 +00:00
|
|
|
protected function wasKnownStatementRollbackError() {
|
|
|
|
|
// ON CONFLICT ROLLBACK clauses make it so that SQLITE_CONSTRAINT error is
|
|
|
|
|
// ambiguous with regard to whether it implies a ROLLBACK or an ABORT happened.
|
|
|
|
|
// https://sqlite.org/lang_createtable.html#uniqueconst
|
|
|
|
|
// https://sqlite.org/lang_conflict.html
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
/**
|
2014-07-24 17:42:45 +00:00
|
|
|
* @return string Wikitext of a link to the server software's web site
|
2008-05-07 23:40:14 +00:00
|
|
|
*/
|
2013-05-15 04:13:02 +00:00
|
|
|
public function getSoftwareLink() {
|
2014-01-09 01:50:21 +00:00
|
|
|
return "[{{int:version-db-sqlite-url}} SQLite]";
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string Version information from the database
|
|
|
|
|
*/
|
|
|
|
|
function getServerVersion() {
|
2018-02-28 20:56:34 +00:00
|
|
|
$ver = $this->getBindingHandle()->getAttribute( PDO::ATTR_SERVER_VERSION );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2009-01-15 06:56:58 +00:00
|
|
|
return $ver;
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-15 06:56:58 +00:00
|
|
|
/**
|
|
|
|
|
* Get information about a given field
|
|
|
|
|
* Returns false if the field does not exist.
|
2011-05-11 23:11:41 +00:00
|
|
|
*
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param string $field
|
2012-02-10 15:37:33 +00:00
|
|
|
* @return SQLiteField|bool False on failure
|
2009-01-15 06:56:58 +00:00
|
|
|
*/
|
2009-09-15 21:05:30 +00:00
|
|
|
function fieldInfo( $table, $field ) {
|
2009-01-15 06:56:58 +00:00
|
|
|
$tableName = $this->tableName( $table );
|
|
|
|
|
$sql = 'PRAGMA table_info(' . $this->addQuotes( $tableName ) . ')';
|
|
|
|
|
$res = $this->query( $sql, __METHOD__ );
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
if ( $row->name == $field ) {
|
|
|
|
|
return new SQLiteField( $row, $tableName );
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2009-01-15 06:56:58 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2008-05-07 23:40:14 +00:00
|
|
|
|
2015-02-24 00:32:00 +00:00
|
|
|
protected function doBegin( $fname = '' ) {
|
|
|
|
|
if ( $this->trxMode ) {
|
|
|
|
|
$this->query( "BEGIN {$this->trxMode}", $fname );
|
|
|
|
|
} else {
|
|
|
|
|
$this->query( 'BEGIN', $fname );
|
|
|
|
|
}
|
2018-02-13 06:58:57 +00:00
|
|
|
$this->trxLevel = 1;
|
2015-02-24 00:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $s
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2009-09-15 21:05:30 +00:00
|
|
|
function strencode( $s ) {
|
2013-11-20 06:58:22 +00:00
|
|
|
return substr( $this->addQuotes( $s ), 1, -1 );
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2014-04-19 11:55:27 +00:00
|
|
|
* @param string $b
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return Blob
|
|
|
|
|
*/
|
2009-09-15 21:05:30 +00:00
|
|
|
function encodeBlob( $b ) {
|
2008-09-06 13:56:59 +00:00
|
|
|
return new Blob( $b );
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2014-04-19 11:55:27 +00:00
|
|
|
* @param Blob|string $b
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2009-09-15 21:05:30 +00:00
|
|
|
function decodeBlob( $b ) {
|
|
|
|
|
if ( $b instanceof Blob ) {
|
2008-09-06 13:56:59 +00:00
|
|
|
$b = $b->fetch();
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2008-05-07 23:40:14 +00:00
|
|
|
return $b;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2016-09-21 18:03:29 +00:00
|
|
|
* @param string|int|null|bool|Blob $s
|
|
|
|
|
* @return string|int
|
2011-05-28 17:52:12 +00:00
|
|
|
*/
|
2009-09-15 21:05:30 +00:00
|
|
|
function addQuotes( $s ) {
|
2008-09-06 13:56:59 +00:00
|
|
|
if ( $s instanceof Blob ) {
|
|
|
|
|
return "x'" . bin2hex( $s->fetch() ) . "'";
|
2013-09-17 22:54:59 +00:00
|
|
|
} elseif ( is_bool( $s ) ) {
|
|
|
|
|
return (int)$s;
|
2017-10-01 00:46:51 +00:00
|
|
|
} elseif ( strpos( (string)$s, "\0" ) !== false ) {
|
2012-10-31 14:27:57 +00:00
|
|
|
// SQLite doesn't support \0 in strings, so use the hex representation as a workaround.
|
2015-09-27 08:15:12 +00:00
|
|
|
// 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:
|
2012-10-31 14:27:57 +00:00
|
|
|
// https://bugs.php.net/bug.php?id=63419
|
|
|
|
|
// There was already a similar report for SQLite3::escapeString, bug #62361:
|
|
|
|
|
// https://bugs.php.net/bug.php?id=62361
|
2014-11-08 00:43:57 +00:00
|
|
|
// There is an additional bug regarding sorting this data after insert
|
|
|
|
|
// on older versions of sqlite shipped with ubuntu 12.04
|
2015-09-12 13:54:13 +00:00
|
|
|
// https://phabricator.wikimedia.org/T74367
|
2016-09-18 03:39:28 +00:00
|
|
|
$this->queryLogger->debug(
|
2015-09-27 08:15:12 +00:00
|
|
|
__FUNCTION__ .
|
2016-09-18 03:39:28 +00:00
|
|
|
': Quoting value containing null byte. ' .
|
|
|
|
|
'For consistency all binary data should have been ' .
|
|
|
|
|
'first processed with self::encodeBlob()'
|
2015-09-27 08:15:12 +00:00
|
|
|
);
|
2017-10-01 00:46:51 +00:00
|
|
|
return "x'" . bin2hex( (string)$s ) . "'";
|
2008-09-06 13:56:59 +00:00
|
|
|
} else {
|
2018-02-28 20:56:34 +00:00
|
|
|
return $this->getBindingHandle()->quote( (string)$s );
|
2008-09-06 13:56:59 +00:00
|
|
|
}
|
2008-05-07 23:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-04 13:23:39 +00:00
|
|
|
public function buildSubstring( $input, $startPosition, $length = null ) {
|
|
|
|
|
$this->assertBuildSubstringParams( $startPosition, $length );
|
|
|
|
|
$params = [ $input, $startPosition ];
|
|
|
|
|
if ( $length !== null ) {
|
|
|
|
|
$params[] = $length;
|
|
|
|
|
}
|
|
|
|
|
return 'SUBSTR(' . implode( ',', $params ) . ')';
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-25 20:24:42 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $field Field or column to cast
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
public function buildStringCast( $field ) {
|
|
|
|
|
return 'CAST ( ' . $field . ' AS TEXT )';
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-09 03:53:28 +00:00
|
|
|
/**
|
|
|
|
|
* No-op version of deadlockLoop
|
2013-12-27 01:54:51 +00:00
|
|
|
*
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return mixed
|
2009-01-09 03:53:28 +00:00
|
|
|
*/
|
|
|
|
|
public function deadlockLoop( /*...*/ ) {
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
$function = array_shift( $args );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2018-06-09 23:26:32 +00:00
|
|
|
return $function( ...$args );
|
2009-01-09 03:53:28 +00:00
|
|
|
}
|
2009-01-15 06:56:58 +00:00
|
|
|
|
2011-05-11 23:11:41 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $s
|
2011-05-11 23:11:41 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2009-01-15 06:56:58 +00:00
|
|
|
protected function replaceVars( $s ) {
|
|
|
|
|
$s = parent::replaceVars( $s );
|
2010-02-21 09:40:27 +00:00
|
|
|
if ( preg_match( '/^\s*(CREATE|ALTER) TABLE/i', $s ) ) {
|
2009-01-15 06:56:58 +00:00
|
|
|
// CREATE TABLE hacks to allow schema file sharing with MySQL
|
2009-09-15 21:05:30 +00:00
|
|
|
|
2009-01-15 06:56:58 +00:00
|
|
|
// binary/varbinary column type -> blob
|
2010-02-20 20:00:28 +00:00
|
|
|
$s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'BLOB', $s );
|
2009-01-15 06:56:58 +00:00
|
|
|
// no such thing as unsigned
|
2010-02-20 20:00:28 +00:00
|
|
|
$s = preg_replace( '/\b(un)?signed\b/i', '', $s );
|
|
|
|
|
// INT -> INTEGER
|
2010-10-07 16:13:54 +00:00
|
|
|
$s = preg_replace( '/\b(tiny|small|medium|big|)int(\s*\(\s*\d+\s*\)|\b)/i', 'INTEGER', $s );
|
|
|
|
|
// floating point types -> REAL
|
2013-11-20 10:13:51 +00:00
|
|
|
$s = preg_replace(
|
|
|
|
|
'/\b(float|double(\s+precision)?)(\s*\(\s*\d+\s*(,\s*\d+\s*)?\)|\b)/i',
|
|
|
|
|
'REAL',
|
|
|
|
|
$s
|
|
|
|
|
);
|
2010-02-20 20:00:28 +00:00
|
|
|
// varchar -> TEXT
|
2010-08-12 16:28:11 +00:00
|
|
|
$s = preg_replace( '/\b(var)?char\s*\(.*?\)/i', 'TEXT', $s );
|
2010-02-20 20:00:28 +00:00
|
|
|
// TEXT normalization
|
|
|
|
|
$s = preg_replace( '/\b(tiny|medium|long)text\b/i', 'TEXT', $s );
|
|
|
|
|
// BLOB normalization
|
|
|
|
|
$s = preg_replace( '/\b(tiny|small|medium|long|)blob\b/i', 'BLOB', $s );
|
|
|
|
|
// BOOL -> INTEGER
|
|
|
|
|
$s = preg_replace( '/\bbool(ean)?\b/i', 'INTEGER', $s );
|
|
|
|
|
// DATETIME -> TEXT
|
|
|
|
|
$s = preg_replace( '/\b(datetime|timestamp)\b/i', 'TEXT', $s );
|
2009-01-15 06:56:58 +00:00
|
|
|
// No ENUM type
|
2010-06-23 18:59:46 +00:00
|
|
|
$s = preg_replace( '/\benum\s*\([^)]*\)/i', 'TEXT', $s );
|
2009-01-15 06:56:58 +00:00
|
|
|
// binary collation type -> nothing
|
|
|
|
|
$s = preg_replace( '/\bbinary\b/i', '', $s );
|
|
|
|
|
// auto_increment -> autoincrement
|
2010-02-20 20:00:28 +00:00
|
|
|
$s = preg_replace( '/\bauto_increment\b/i', 'AUTOINCREMENT', $s );
|
2009-01-15 06:56:58 +00:00
|
|
|
// No explicit options
|
2010-02-20 20:00:28 +00:00
|
|
|
$s = preg_replace( '/\)[^);]*(;?)\s*$/', ')\1', $s );
|
2011-04-10 08:44:06 +00:00
|
|
|
// AUTOINCREMENT should immedidately follow PRIMARY KEY
|
|
|
|
|
$s = preg_replace( '/primary key (.*?) autoincrement/i', 'PRIMARY KEY AUTOINCREMENT $1', $s );
|
2009-01-15 06:56:58 +00:00
|
|
|
} elseif ( preg_match( '/^\s*CREATE (\s*(?:UNIQUE|FULLTEXT)\s+)?INDEX/i', $s ) ) {
|
|
|
|
|
// No truncated indexes
|
|
|
|
|
$s = preg_replace( '/\(\d+\)/', '', $s );
|
|
|
|
|
// No FULLTEXT
|
|
|
|
|
$s = preg_replace( '/\bfulltext\b/i', '', $s );
|
2013-10-15 19:46:39 +00:00
|
|
|
} elseif ( preg_match( '/^\s*DROP INDEX/i', $s ) ) {
|
|
|
|
|
// DROP INDEX is database-wide, not table-specific, so no ON <table> clause.
|
|
|
|
|
$s = preg_replace( '/\sON\s+[^\s]*/i', '', $s );
|
2014-09-08 19:16:02 +00:00
|
|
|
} elseif ( preg_match( '/^\s*INSERT IGNORE\b/i', $s ) ) {
|
|
|
|
|
// INSERT IGNORE --> INSERT OR IGNORE
|
|
|
|
|
$s = preg_replace( '/^\s*INSERT IGNORE\b/i', 'INSERT OR IGNORE', $s );
|
2009-01-15 06:56:58 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2009-01-15 06:56:58 +00:00
|
|
|
return $s;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 23:32:52 +00:00
|
|
|
public function lock( $lockName, $method, $timeout = 5 ) {
|
2015-03-07 12:31:08 +00:00
|
|
|
if ( !is_dir( "{$this->dbDir}/locks" ) ) { // create dir as needed
|
|
|
|
|
if ( !is_writable( $this->dbDir ) || !mkdir( "{$this->dbDir}/locks" ) ) {
|
2016-08-16 20:30:04 +00:00
|
|
|
throw new DBError( $this, "Cannot create directory \"{$this->dbDir}/locks\"." );
|
2014-01-31 23:32:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return $this->lockMgr->lock( [ $lockName ], LockManager::LOCK_EX, $timeout )->isOK();
|
2014-01-31 23:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function unlock( $lockName, $method ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return $this->lockMgr->unlock( [ $lockName ], LockManager::LOCK_EX )->isOK();
|
2014-01-31 23:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-21 19:35:16 +00:00
|
|
|
/**
|
2009-07-13 21:56:24 +00:00
|
|
|
* Build a concatenation list to feed into a SQL query
|
2011-05-11 23:11:41 +00:00
|
|
|
*
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string[] $stringList
|
2011-05-11 23:11:41 +00:00
|
|
|
* @return string
|
2009-07-13 21:56:24 +00:00
|
|
|
*/
|
|
|
|
|
function buildConcat( $stringList ) {
|
2009-07-14 00:55:17 +00:00
|
|
|
return '(' . implode( ') || (', $stringList ) . ')';
|
2009-07-13 21:56:24 +00:00
|
|
|
}
|
2009-06-26 15:38:43 +00:00
|
|
|
|
2013-11-15 07:23:36 +00:00
|
|
|
public function buildGroupConcatField(
|
2016-02-17 09:09:32 +00:00
|
|
|
$delim, $table, $field, $conds = '', $join_conds = []
|
2013-11-15 07:23:36 +00:00
|
|
|
) {
|
|
|
|
|
$fld = "group_concat($field," . $this->addQuotes( $delim ) . ')';
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
|
2013-11-15 07:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $oldName
|
|
|
|
|
* @param string $newName
|
|
|
|
|
* @param bool $temporary
|
|
|
|
|
* @param string $fname
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return bool|ResultWrapper
|
2016-09-15 01:37:35 +00:00
|
|
|
* @throws RuntimeException
|
2011-05-28 17:52:12 +00:00
|
|
|
*/
|
2013-05-06 13:20:40 +00:00
|
|
|
function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__ ) {
|
2013-11-20 10:13:51 +00:00
|
|
|
$res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name=" .
|
|
|
|
|
$this->addQuotes( $oldName ) . " AND type='table'", $fname );
|
2010-02-20 20:00:28 +00:00
|
|
|
$obj = $this->fetchObject( $res );
|
|
|
|
|
if ( !$obj ) {
|
2016-09-14 02:38:19 +00:00
|
|
|
throw new RuntimeException( "Couldn't retrieve structure for table $oldName" );
|
2010-02-20 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
$sql = $obj->sql;
|
2013-11-20 10:13:51 +00:00
|
|
|
$sql = preg_replace(
|
2018-09-01 12:39:56 +00:00
|
|
|
'/(?<=\W)"?' .
|
|
|
|
|
preg_quote( trim( $this->addIdentifierQuotes( $oldName ), '"' ), '/' ) .
|
|
|
|
|
'"?(?=\W)/',
|
2013-11-20 10:13:51 +00:00
|
|
|
$this->addIdentifierQuotes( $newName ),
|
|
|
|
|
$sql,
|
|
|
|
|
1
|
|
|
|
|
);
|
2011-02-26 16:45:35 +00:00
|
|
|
if ( $temporary ) {
|
2011-02-26 17:04:26 +00:00
|
|
|
if ( preg_match( '/^\\s*CREATE\\s+VIRTUAL\\s+TABLE\b/i', $sql ) ) {
|
2016-09-18 03:39:28 +00:00
|
|
|
$this->queryLogger->debug(
|
|
|
|
|
"Table $oldName is virtual, can't create a temporary duplicate.\n" );
|
2011-02-26 16:45:35 +00:00
|
|
|
} else {
|
|
|
|
|
$sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql );
|
|
|
|
|
}
|
2011-02-26 14:30:52 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2015-08-06 00:55:18 +00:00
|
|
|
$res = $this->query( $sql, $fname );
|
|
|
|
|
|
|
|
|
|
// Take over indexes
|
|
|
|
|
$indexList = $this->query( 'PRAGMA INDEX_LIST(' . $this->addQuotes( $oldName ) . ')' );
|
|
|
|
|
foreach ( $indexList as $index ) {
|
|
|
|
|
if ( strpos( $index->name, 'sqlite_autoindex' ) === 0 ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $index->unique ) {
|
|
|
|
|
$sql = 'CREATE UNIQUE INDEX';
|
|
|
|
|
} else {
|
|
|
|
|
$sql = 'CREATE INDEX';
|
|
|
|
|
}
|
|
|
|
|
// Try to come up with a new index name, given indexes have database scope in SQLite
|
|
|
|
|
$indexName = $newName . '_' . $index->name;
|
|
|
|
|
$sql .= ' ' . $indexName . ' ON ' . $newName;
|
|
|
|
|
|
|
|
|
|
$indexInfo = $this->query( 'PRAGMA INDEX_INFO(' . $this->addQuotes( $index->name ) . ')' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$fields = [];
|
2015-08-06 00:55:18 +00:00
|
|
|
foreach ( $indexInfo as $indexInfoRow ) {
|
2015-09-26 20:32:31 +00:00
|
|
|
$fields[$indexInfoRow->seqno] = $indexInfoRow->name;
|
2015-08-06 00:55:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sql .= '(' . implode( ',', $fields ) . ')';
|
|
|
|
|
|
|
|
|
|
$this->query( $sql );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $res;
|
2009-11-06 10:17:44 +00:00
|
|
|
}
|
2012-06-16 05:24:59 +00:00
|
|
|
|
2010-12-28 17:15:50 +00:00
|
|
|
/**
|
|
|
|
|
* List all tables on the database
|
|
|
|
|
*
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null $prefix Only show tables with this prefix, e.g. mw_
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $fname Calling function name
|
2011-05-11 23:11:41 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
2010-12-28 17:15:50 +00:00
|
|
|
*/
|
2013-05-06 13:20:40 +00:00
|
|
|
function listTables( $prefix = null, $fname = __METHOD__ ) {
|
2010-12-28 17:15:50 +00:00
|
|
|
$result = $this->select(
|
|
|
|
|
'sqlite_master',
|
|
|
|
|
'name',
|
2010-12-28 18:30:03 +00:00
|
|
|
"type='table'"
|
2010-12-28 17:15:50 +00:00
|
|
|
);
|
2012-06-16 05:24:59 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$endArray = [];
|
2012-06-16 05:24:59 +00:00
|
|
|
|
2013-04-17 14:52:47 +00:00
|
|
|
foreach ( $result as $table ) {
|
2013-02-03 18:47:42 +00:00
|
|
|
$vars = get_object_vars( $table );
|
2010-12-28 17:15:50 +00:00
|
|
|
$table = array_pop( $vars );
|
2012-06-16 05:24:59 +00:00
|
|
|
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
|
2010-12-30 17:30:35 +00:00
|
|
|
if ( strpos( $table, 'sqlite_' ) !== 0 ) {
|
|
|
|
|
$endArray[] = $table;
|
|
|
|
|
}
|
2010-12-28 17:15:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-06-16 05:24:59 +00:00
|
|
|
|
2010-12-28 17:15:50 +00:00
|
|
|
return $endArray;
|
|
|
|
|
}
|
2015-10-05 20:42:28 +00:00
|
|
|
|
2016-09-16 03:14:58 +00:00
|
|
|
/**
|
|
|
|
|
* Override due to no CASCADE support
|
|
|
|
|
*
|
|
|
|
|
* @param string $tableName
|
|
|
|
|
* @param string $fName
|
|
|
|
|
* @return bool|ResultWrapper
|
|
|
|
|
* @throws DBReadOnlyError
|
|
|
|
|
*/
|
|
|
|
|
public function dropTable( $tableName, $fName = __METHOD__ ) {
|
|
|
|
|
if ( !$this->tableExists( $tableName, $fName ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$sql = "DROP TABLE " . $this->tableName( $tableName );
|
|
|
|
|
|
|
|
|
|
return $this->query( $sql, $fName );
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 17:56:32 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 08:39:22 +00:00
|
|
|
public function resetSequenceForTable( $table, $fname = __METHOD__ ) {
|
|
|
|
|
$encTable = $this->addIdentifierQuotes( 'sqlite_sequence' );
|
|
|
|
|
$encName = $this->addQuotes( $this->tableName( $table, 'raw' ) );
|
|
|
|
|
$this->query( "DELETE FROM $encTable WHERE name = $encName", $fname );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function databasesAreIndependent() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 20:42:28 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function __toString() {
|
2018-02-28 20:56:34 +00:00
|
|
|
return is_object( $this->conn )
|
|
|
|
|
? 'SQLite ' . (string)$this->conn->getAttribute( PDO::ATTR_SERVER_VERSION )
|
|
|
|
|
: '(not connected)';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return PDO
|
|
|
|
|
*/
|
|
|
|
|
protected function getBindingHandle() {
|
|
|
|
|
return parent::getBindingHandle();
|
2015-10-05 20:42:28 +00:00
|
|
|
}
|
2016-09-18 03:21:50 +00:00
|
|
|
}
|
2017-02-07 04:49:57 +00:00
|
|
|
|
2018-05-29 16:21:31 +00:00
|
|
|
/**
|
|
|
|
|
* @deprecated since 1.29
|
|
|
|
|
*/
|
2017-02-07 04:49:57 +00:00
|
|
|
class_alias( DatabaseSqlite::class, 'DatabaseSqlite' );
|