2013-02-18 03:48:21 +01:00
|
|
|
<?php
|
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
namespace PicoDb\Driver;
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
use PDO;
|
2015-08-15 03:33:39 +02:00
|
|
|
use PDOException;
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Sqlite Driver
|
|
|
|
*
|
2016-07-31 00:41:42 +02:00
|
|
|
* @package PicoDb\Driver
|
|
|
|
* @author Frederic Guillot
|
2015-08-15 03:33:39 +02:00
|
|
|
*/
|
|
|
|
class Sqlite extends Base
|
2014-12-24 03:28:26 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* List of required settings options
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-07-31 00:41:42 +02:00
|
|
|
protected $requiredAttributes = array('filename');
|
2014-09-15 13:23:55 +02:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Create a new PDO connection
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $settings
|
|
|
|
*/
|
|
|
|
public function createConnection(array $settings)
|
|
|
|
{
|
|
|
|
$this->pdo = new PDO('sqlite:'.$settings['filename']);
|
|
|
|
$this->enableForeignKeys();
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Enable foreign keys
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function enableForeignKeys()
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
$this->pdo->exec('PRAGMA foreign_keys = ON');
|
|
|
|
}
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Disable foreign keys
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function disableForeignKeys()
|
|
|
|
{
|
|
|
|
$this->pdo->exec('PRAGMA foreign_keys = OFF');
|
|
|
|
}
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Return true if the error code is a duplicate key
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $code
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isDuplicateKeyError($code)
|
|
|
|
{
|
|
|
|
return $code == 23000;
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Escape identifier
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $identifier
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function escape($identifier)
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
return '"'.$identifier.'"';
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Get non standard operator
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $operator
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getOperator($operator)
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
if ($operator === 'LIKE' || $operator === 'ILIKE') {
|
|
|
|
return 'LIKE';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Get last inserted id
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getLastId()
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
return $this->pdo->lastInsertId();
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
2014-12-29 22:52:36 +01:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Get current schema version
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getSchemaVersion()
|
2014-12-29 22:52:36 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
$rq = $this->pdo->prepare('PRAGMA user_version');
|
|
|
|
$rq->execute();
|
|
|
|
|
|
|
|
return (int) $rq->fetchColumn();
|
2014-12-29 22:52:36 +01:00
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Set current schema version
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $version
|
|
|
|
*/
|
|
|
|
public function setSchemaVersion($version)
|
2014-12-29 22:52:36 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
$this->pdo->exec('PRAGMA user_version='.$version);
|
2014-12-29 22:52:36 +01:00
|
|
|
}
|
2015-01-07 01:08:10 +01:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Upsert for a key/value variable
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $table
|
|
|
|
* @param string $keyColumn
|
|
|
|
* @param string $valueColumn
|
|
|
|
* @param array $dictionary
|
|
|
|
* @return bool False on failure
|
|
|
|
*/
|
|
|
|
public function upsert($table, $keyColumn, $valueColumn, array $dictionary)
|
2015-01-07 01:08:10 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
try {
|
|
|
|
$this->pdo->beginTransaction();
|
|
|
|
|
|
|
|
foreach ($dictionary as $key => $value) {
|
|
|
|
|
|
|
|
$sql = sprintf(
|
|
|
|
'INSERT OR REPLACE INTO %s (%s, %s) VALUES (?, ?)',
|
|
|
|
$this->escape($table),
|
|
|
|
$this->escape($keyColumn),
|
|
|
|
$this->escape($valueColumn)
|
|
|
|
);
|
|
|
|
|
|
|
|
$rq = $this->pdo->prepare($sql);
|
|
|
|
$rq->execute(array($key, $value));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->pdo->commit();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (PDOException $e) {
|
2016-07-31 00:41:42 +02:00
|
|
|
$this->pdo->rollBack();
|
2015-08-15 03:33:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-07 01:08:10 +01:00
|
|
|
}
|
2016-07-31 00:41:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run EXPLAIN command
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $sql
|
|
|
|
* @param array $values
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function explain($sql, array $values)
|
|
|
|
{
|
|
|
|
return $this->getConnection()->query('EXPLAIN QUERY PLAN '.$this->getSqlFromPreparedStatement($sql, $values))->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get database version
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDatabaseVersion()
|
|
|
|
{
|
|
|
|
return $this->getConnection()->query('SELECT sqlite_version()')->fetchColumn();
|
|
|
|
}
|
2015-08-15 03:33:39 +02:00
|
|
|
}
|