miniflux-legacy/vendor/fguillot/picodb/lib/PicoDb/Driver/Postgres.php

173 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace PicoDb\Driver;
use PDO;
2015-08-15 03:33:39 +02:00
use PDOException;
/**
* Postgres Driver
*
* @author Frederic Guillot
*/
class Postgres extends Base
{
2015-08-15 03:33:39 +02:00
/**
* List of required settings options
*
* @access protected
* @var array
*/
protected $requiredAtttributes = array(
'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)
{
$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']);
if (isset($settings['schema_table'])) {
2015-08-15 03:33:39 +02:00
$this->schemaTable = $settings['schema_table'];
}
}
2015-08-15 03:33:39 +02:00
/**
* Enable foreign keys
*
* @access public
*/
public function enableForeignKeys()
{
2015-08-15 03:33:39 +02:00
}
2015-08-15 03:33:39 +02:00
/**
* Disable foreign keys
*
* @access public
*/
public function disableForeignKeys()
{
}
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)
{
2015-08-15 03:33:39 +02:00
return $code == 23505 || $code == 23503;
}
2015-08-15 03:33:39 +02:00
/**
* Escape identifier
*
* @access public
* @param string $identifier
* @return string
*/
public function escape($identifier)
{
2015-08-15 03:33:39 +02:00
return '"'.$identifier.'"';
}
2015-08-15 03:33:39 +02:00
/**
* Get non standard operator
*
* @access public
* @param string $operator
* @return string
*/
public function getOperator($operator)
{
2015-08-15 03:33:39 +02:00
if ($operator === 'LIKE') {
return 'LIKE';
}
else if ($operator === 'ILIKE') {
return 'ILIKE';
}
return '';
}
2015-08-15 03:33:39 +02:00
/**
* Get last inserted id
*
* @access public
* @return integer
*/
public function getLastId()
{
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;
}
}
2015-08-15 03:33:39 +02:00
/**
* Get current schema version
*
* @access public
* @return integer
*/
public function getSchemaVersion()
{
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;
}
2015-08-15 03:33:39 +02:00
/**
* Set current schema version
*
* @access public
* @param integer $version
*/
public function setSchemaVersion($version)
{
2015-08-15 03:33:39 +02:00
$rq = $this->pdo->prepare('UPDATE '.$this->schemaTable.' SET version=?');
$rq->execute(array($version));
}
2015-08-15 03:33:39 +02:00
}