yf_db_driver_pdo_pgsql class

Fileplugins/db/classes/db/yf_db_driver_pdo_pgsql.class.php
Use_class('db_driver_pdo_pgsql', 'classes/db/')
Extendsyf_db_driver_pdo → yf_db_driver

Public properties

A site sets them in its config with $PROJECT_CONF['db_driver_pdo_pgsql']['NAME'] = ...;

PropertyDefaultDescription
$DEF_PORT
mixed
null Port left out of the DSN when it equals this (same as the mysqli driver)
$_connect_error null
$db_connect_id
@conf_skip
null
$params null

Inherited properties

from yf_db_driver_pdo: $_last_query_error, $_last_query_id, $_query_error

from yf_db_driver: $ESCAPE_BUILDER_VALUES

Public methods 13

public function auto_limit_one()

LIMIT 1 is not appended: a trailing OFFSET would end up before it

    public function auto_limit_one()
    {
        return false;
    }

connect

public function connect()
    public function connect()
    {
        $dsn = 'pgsql:host=' . $this->params['host'];
        if ($this->params['port'] && $this->params['port'] != $this->DEF_PORT) {
            $dsn .= ';port=' . $this->params['port'];
        }
        $dsn .= ';dbname=' . (($this->params['name'] ?? null) ?: 'template1');
        $attrs = [];
        $attrs[PDO::ATTR_TIMEOUT] = 2;
        if ($this->params['persist']) {
            $attrs[PDO::ATTR_PERSISTENT] = true;
        }
        $this->db_connect_id = new PDO($dsn, $this->params['user'], $this->params['pswd'], $attrs);
        $pdo = &$this->db_connect_id;

        if ( ! $this->db_connect_id) {
            $this->_connect_error = 'cannot_connect_to_server';
            return $this->db_connect_id;
        }
        return $this->db_connect_id;
    }

error

public function error()

The SQLSTATE (errorInfo[0], 42P01 undefined table) is added for the installer repair.

    public function error()
    {
        $error = parent::error();
        if ($error && $this->db_connect_id) {
            $error['sqlstate'] = (string) ($this->db_connect_id->errorInfo()[0] ?? '');
        }
        return $error;
    }

escape_key

public function escape_key($data)
    public function escape_key($data)
    {
        return '"' . str_replace('"', '""', trim($data, '"')) . '"';
    }

escape_val

public function escape_val($data)
    public function escape_val($data)
    {
        if ($data === null) {
            return 'NULL';
        }
        if ($this->ESCAPE_BUILDER_VALUES) {
            if ($this->db_connect_id) {
                return $this->db_connect_id->quote((string) $data);
            }
            $data = str_replace('\'', '\'\'', $data);
        }
        return '\'' . $data . '\'';
    }
public function is_follow_up_error($db_error)

SQLSTATE 25P02: the query only failed because an earlier one aborted the transaction

    public function is_follow_up_error($db_error)
    {
        return ($db_error['sqlstate'] ?? '') === '25P02';
    }
public function is_reconnect_error($db_error, array $codes = [])

Connection exception (class 08) and server shutdown (57P01-57P03).

  • $codes array
    public function is_reconnect_error($db_error, array $codes = [])
    {
        $sqlstate = (string) ($db_error['sqlstate'] ?? '');
        return substr($sqlstate, 0, 2) === '08' || in_array($sqlstate, ['57P01', '57P02', '57P03'], true);
    }

limit

public function limit($count, $offset)
    public function limit($count, $offset)
    {
        if ($count > 0) {
            return 'LIMIT ' . $count . ($offset > 0 ? ' OFFSET ' . $offset : '');
        }
        return false;
    }

no_cache_hint

public function no_cache_hint()
    public function no_cache_hint()
    {
        return '';
    }
public function real_escape_string($string)
    public function real_escape_string($string)
    {
        if ($string === null) {
            return 'NULL';
        }
        $quoted = $this->db_connect_id ? $this->db_connect_id->quote((string) $string) : false;
        if ($quoted !== false) {
            return substr($quoted, 1, -1);
        }
        // no connection, or PDO refused the bytes (invalid encoding): the server reports the encoding error
        return str_replace("'", "''", (string) $string);
    }
public function repairable_error_codes()

Null: every error goes to the installer repair, which decides by SQLSTATE

    public function repairable_error_codes()
    {
        return null;
    }
public function supports_upsert()
    public function supports_upsert()
    {
        return true;
    }

Methods starting with _ hooks and helpers; not routable as page actions

__construct

public function __construct(array $params)
  • $params array
    public function __construct(array $params)
    {
        if ( ! extension_loaded('pdo_pgsql')) {
            throw new RuntimeException('YF PDO PgSQL db driver require missing php extension pdo_pgsql');
            return false;
        }
        $params['port'] = ($params['port'] ?? null) ?: 5432;
        $this->params = $params;
        $this->connect();
        return $this->db_connect_id;
    }

Inherited methods

from yf_db_driver: __call()

from yf_db_driver_pdo: affected_rows(), begin(), close(), commit(), fetch_array(), fetch_assoc(), fetch_object(), fetch_row(), free_result(), get_host_info(), get_server_version(), insert_id(), num_rows(), query(), rollback(), select_db()