2014-09-15 13:23:55 +02:00
|
|
|
<?php
|
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
namespace PicoDb\Driver;
|
2014-09-15 13:23:55 +02:00
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
use PDO;
|
2015-08-15 03:33:39 +02:00
|
|
|
use PDOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Postgres Driver
|
|
|
|
*
|
2016-07-31 00:41:42 +02:00
|
|
|
* @package PicoDb\Driver
|
|
|
|
* @author Frederic Guillot
|
2015-08-15 03:33:39 +02:00
|
|
|
*/
|
|
|
|
class Postgres 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(
|
2015-08-15 03:33:39 +02:00
|
|
|
'hostname',
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
'database',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Table to store the schema version
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $schemaTable = 'schema_version';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new PDO connection
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $settings
|
|
|
|
*/
|
|
|
|
public function createConnection(array $settings)
|
2014-09-15 13:23:55 +02:00
|
|
|
{
|
|
|
|
$dsn = 'pgsql:host='.$settings['hostname'].';dbname='.$settings['database'];
|
|
|
|
|
2015-06-21 15:56:36 +02:00
|
|
|
if (! empty($settings['port'])) {
|
|
|
|
$dsn .= ';port='.$settings['port'];
|
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
$this->pdo = new PDO($dsn, $settings['username'], $settings['password']);
|
2014-09-15 13:23:55 +02:00
|
|
|
|
|
|
|
if (isset($settings['schema_table'])) {
|
2015-08-15 03:33:39 +02:00
|
|
|
$this->schemaTable = $settings['schema_table'];
|
2014-09-15 13:23:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Enable foreign keys
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function enableForeignKeys()
|
2014-09-15 13:23:55 +02:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
}
|
2014-09-15 13:23:55 +02:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Disable foreign keys
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function disableForeignKeys()
|
|
|
|
{
|
2014-09-15 13:23:55 +02: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)
|
2014-09-15 13:23:55 +02:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
return $code == 23505 || $code == 23503;
|
2014-09-15 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Escape identifier
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $identifier
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function escape($identifier)
|
2014-09-15 13:23:55 +02:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
return '"'.$identifier.'"';
|
2014-09-15 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Get non standard operator
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $operator
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getOperator($operator)
|
2014-09-15 13:23:55 +02:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
if ($operator === 'LIKE') {
|
|
|
|
return 'LIKE';
|
|
|
|
}
|
|
|
|
else if ($operator === 'ILIKE') {
|
|
|
|
return 'ILIKE';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
2014-09-15 13:23:55 +02:00
|
|
|
}
|
2014-12-29 22:52:36 +01:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Get last inserted id
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getLastId()
|
2014-12-29 22:52:36 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
try {
|
|
|
|
$rq = $this->pdo->prepare('SELECT LASTVAL()');
|
|
|
|
$rq->execute();
|
|
|
|
|
|
|
|
return $rq->fetchColumn();
|
|
|
|
}
|
|
|
|
catch (PDOException $e) {
|
|
|
|
return 0;
|
|
|
|
}
|
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
|
|
|
$this->pdo->exec("CREATE TABLE IF NOT EXISTS ".$this->schemaTable." (version INTEGER DEFAULT 0)");
|
|
|
|
|
|
|
|
$rq = $this->pdo->prepare('SELECT "version" FROM "'.$this->schemaTable.'"');
|
|
|
|
$rq->execute();
|
|
|
|
$result = $rq->fetchColumn();
|
|
|
|
|
|
|
|
if ($result !== false) {
|
|
|
|
return (int) $result;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->pdo->exec('INSERT INTO '.$this->schemaTable.' VALUES(0)');
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-12-29 22:52:36 +01:00
|
|
|
}
|
2015-01-07 01:08:10 +01:00
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Set current schema version
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $version
|
|
|
|
*/
|
|
|
|
public function setSchemaVersion($version)
|
2015-01-07 01:08:10 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
$rq = $this->pdo->prepare('UPDATE '.$this->schemaTable.' SET version=?');
|
|
|
|
$rq->execute(array($version));
|
2015-01-07 01:08:10 +01:00
|
|
|
}
|
2016-07-31 00:41:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run EXPLAIN command
|
|
|
|
*
|
|
|
|
* @param string $sql
|
|
|
|
* @param array $values
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function explain($sql, array $values)
|
|
|
|
{
|
|
|
|
return $this->getConnection()->query('EXPLAIN (FORMAT YAML) '.$this->getSqlFromPreparedStatement($sql, $values))->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get database version
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDatabaseVersion()
|
|
|
|
{
|
|
|
|
return $this->getConnection()->query('SHOW server_version')->fetchColumn();
|
|
|
|
}
|
2015-08-15 03:33:39 +02:00
|
|
|
}
|