Source for file EDB_PGSQL.php
Documentation is available at EDB_PGSQL.php
* Project: EDB_PGSQL :: PostgreSQL abstraction layer<br>
* File: EDB/EDB_PGSQL.php
* EDB_PGSQL class는 EDB 패키지가 PosgreSQL을 처리하기 위한
* @subpackage EDB_ABSTRACT
* @author JoungKyun.Kim <http://oops.org>
* @copyright (c) 2018, JoungKyun.Kim
* @link http://pear.oops.org/package/EDB
* PosgreSQL engine for EDB API
* PostgreSQL 엔진을 위한 DB 추상 계층을 제공
* db handler of EDB_PGSQL class
* The number of query parameter
* The number of query parameter
private $field = array ();
// {{{ (object) EDB_PGSQL::__construct ($host, $user, $pass, $db)
* EDB_PGSQL 객체를 인스턴스화 하고 PostgreSQL 데이터베이스를
* $db = new EDB_PGSQL ('pgsql://localhost', 'user', 'host', 'database');
* $db = new EDB_PGSQL ('pgsql://localhost:3306', 'user', 'host', 'database');
* 'pgsql:///var/run/postgresql',
* 'user', 'host', 'database'
* 'pgsql:///var/run/postgresql',
* 'user', 'host', 'database', 'options'
* options parameter는 다음의 객체로 지정한다.
* 'connect_timeout' => 2,
* 'options' => '--client_encoding=UTF8',
* {@link http://www.postgresql.org/docs/8.3/static/libpq-connect.html}를
* 만약 persistent connection을 사용하고 싶다면 host 앞에 'p~' prefix를
* $db = new EDB_PGSQL ('pgsql://p~localhost', 'user', 'host', 'database');
* @param string $hostname pgsql host[:port] 또는 unix socket 경로
* @param string $user pgsql DB 계정
* @param string $password pgsql DB 암호
* @param string $database pgsql DB 이름
* @param object $options pgsql 옵션
$argv = is_array ($_argv[0]) ? $_argv[0] : $_argv;;
throw new myException ('pgsql extension is not loaded on PHP!', E_USER_ERROR);
foreach ( $o as $key => $val ) {
// 파일이 존재하면 host를 unix socket으로 지정
$cstring = sprintf ('host=%s', $val);
if ( $key == 'options' && is_object ($val) ) {
foreach ( $val as $k => $v ) {
$this->db = $func ($cstring, PGSQL_CONNECT_FORCE_NEW);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_PGSQL::get_charset (void)
* Get character set of current database
* @return string Current character set name on DB
return pg_client_encoding ($this->db);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (bool) EDB_PGSQL::set_charset ($charset)
* Set character set of current database
* Postgresql don't support set characterset and always returns true
* @return bool always returns true
* @param string name of character set that supported from database
$r = pg_set_client_encoding ($this->db, $char);
return $r ? false : true;
// {{{ (string) EDB_PGSQL::escape ($string)
* Escape special characters in a string for use in an SQL statement
* Attention! This method always returns original string.
* @param string The string that is to be escaped.
function escape ($buf, $type = 's') {
#return pg_escape_bytea ($buf);
return pg_escape_identifier ($buf);
* bind query시에 ::bytea가 먹지를 않는다 --;
if ( preg_match ('/::bytea$/', $buf) ) {
array ('/\'\'/', '/::bytea/'),
return pg_unescape_bytea (stripslashes ($buf));
return pg_unescape_bytea ($buf);
$pgver = pg_version ($this->db);
if ( version_compare ('8.3', $pgver['client'], '<') )
return pg_escape_literal ($buf);
return pg_escape_string ($buf);
// {{{ (int) EDB_PGSQL::query ($query, $param_type, $param1, $param2 ...)
* Performs a query on the database
* @return integer|falseThe number of affected rows
* @param string $query The query strings
* @param string $type (optional) Bind parameter type. See also
* {@link http://php.net/manual/en/mysqli-stmt.bind-param.php mysqli_stmt::bind_param}.
* @param mixed $param1 (optional) Bind parameter 1
* @param mixed $param2,... (optional) Bind parameter 2 ..
$argv = is_array ($_argv[0]) ? $_argv[0] : $_argv;;
// store query in log variable
$r = $this->no_bind_query ($sql);
$r = $this->bind_query ($sql, $argv);
if ( preg_match ('/^(update|insert|delete|replace)/i', trim ($sql)) ) {
/* Insert or update, or delete query */
return pg_affected_rows ($this->result);
for ( $i= 0; $i< $fno; $i++ ) {
$this->lob = array ($lob);
return pg_num_rows ($this->result);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_PGSQL::lastId (void)
* 가장 마지막 입력 row의 OID를 반환한다.
return pg_last_oid ($this->db);
// {{{ (bool) EDB_PGSQL::seek ($offset)
* Adjusts the result pointer to an arbitrary row in the result
* @param integer Must be between zero and the total number of rows minus one
function seek ($offset) {
return pg_result_seek ($this->result, $offset);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (object) EDB_PGSQL::fetch (void)
* Fetch a result row as an associative object
* @return object The object of fetched a result row or false
* @param boolean (optional) fetch 수행 후 result를 free한다.
* (기본값: false) EDB >= 2.0.3
function fetch ($free = false) {
$r = pg_fetch_object ($this->result);
foreach ( $this->lob as $keyname )
$r->$keyname = $this->escape ($r->$keyname, 'u');
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (array) EDB_PGSQL::fetch_all ($free = true)
* Fetch all result rows as an associative object
* @return array The fetched result rows
* @param boolean (optional) free result set after fetch.
while ( ($row = $this->fetch ()) !== false )
// {{{ (bool) EDB_PGSQL::free_result (void)
* Frees stored result memory for the given statement handle
if ( ! $this->free ) return true;
return pg_free_result ($this->result);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_PGSQL::field_name ($index)
* Get the name of the specified field in a result
* @param integer Field number, starting from 0.
* @see http://php.net/manual/en/function.pg-field-name.php pg_field_name()
return pg_field_name ($this->result, $index);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_PGSQL::field_type ($index)
* Returns the type name for the corresponding field number
* returns a string containing the base type name of the given
* field_number in the given PostgreSQL result resource.
* @param integer Field number, starting from 0.
return pg_field_type ($this->result, $index);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (int) EDB_PGSQL::num_fields (void)
* Returns the number of fields in a result
* @see http://php.net/manual/en/function.pg-num-fields.php pg_num_fields()
$r = pg_num_fields ($this->result);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
return ($r != - 1) ? $r : false;
// {{{ (void) EDB_PGSQL::trstart (void)
$this->db->query ('BEGIN');
// {{{ (void) EDB_PGSQL::trend ($v)
* @param bool false일경우 rollback을 수행한다.
function trend ($v = true) {
$sql = ($v === false) ? 'ROLLBACK' : 'COMMIT';
// {{{ (void) EDB_PGSQL::close (void)
// {{{ private (bool) EDB_PGSQL::no_bind_query ($sql)
* Performs a query on the database
* @param string The query strings
private function no_bind_query ($sql) {
if ( ($this->result = pg_query ($this->db, $sql)) === false ) {
throw new myException (pg_last_error ($this->db), E_USER_WARNING);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode (), $e);
// {{{ private (bool) EDB_PGSQL::bind_query ($sql, $parameters)
* Performs a bind query on the database
* @param string The query strings
* @param array (optional) Bind parameter type
private function bind_query ($sql, $params) {
if ( $this->pno != count ($params) || $this->check_param ($params) === false ) {
'Number of elements in query doesn\'t match number of bind variables',
foreach ($params as $key => $v) {
$params[$key] = $v->data;
if ( $type[$key] == 'b' || $type[$key] == 'c' )
$params[$key] = $this->escape ($params[$key], 'b');
$this->result = pg_query_params ($this->db, $sql, $params);
throw new myExeption (pg_last_error ($this->db), E_USER_WARNING);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode (), $e);
} catch ( Exception $e ) { }
* vim: set filetype=php noet sw=4 ts=4 fdm=marker:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
|