Source for file EDB_SQLRELAY.php
Documentation is available at EDB_SQLRELAY.php
* Project: EDB_SQLRELAY :: SQLRELAY abstraction layer
* File: EDB/EDB_SQLRELAY.php
* The EDB_SQLRELAY class is SQLRelay abstraction layer that used internally
* @subpackage EDB_ABSTRACT
* @author JoungKyun.Kim <http://oops.org>
* @copyright (c) 2018, JoungKyun.Kim
* @link http://pear.oops.org/package/EDB
* SQLRELAY engine for EDB API
* This class support abstracttion DB layer for SQLRELAY Engine
* db handler of EDB_SQLRELAY class
* The number of query parameter
* The current offset of result rows
* The last rownums of result rows
// {{{ (object) EDB_SQLRELAY::__construct ($host, $user, $pass)
* Instantiates an EDB_SQLRELAY object and opens an SQLRELAY database
* $db = new EDB_SQLRELAY ('sqlrelay://localhost', 'user', 'host');
* $db = new EDB_SQLRELAY ('sqlrelay://localhost:9000', 'user', 'host');
* $db = new EDB_SQLRELAY ('sqlrelay://localhost:/path/sock', 'user', 'host');
* If you add prefix 'p~' before host, you can connect with persistent
* $db = new EDB_SQLRELAY ('sqlrelay://p~localhost', 'user', 'host', 'database');
* @param string $hostname SQLRELAY host
* @param string $user SQLRELAY user
* @param string $password SQLRELAY password
* @param string $database SQLRELAY database
$argv = is_array ($_argv[0]) ? $_argv[0] : $_argv;;
throw new myException ('SQLRELAY extension is not loaded on PHP!', E_USER_ERROR);
if ( preg_match ('/([^:]+):(.*)/', $o->host, $matches) ) {
$this->db = sqlrcon_alloc ($o->host, $o->port, $o->sock, $o->user, $o->pass, 0, 1);
$this->result = sqlrcur_alloc ($this->db);
if ( ! sqlrcon_ping ($this->db) )
throw new myException (sqlrcur_errorMessage ($this->db), E_USER_ERROR);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_SQLRELAY::get_charset (void)
* Get character set of current database
* SQLRELAY extension don't support this function
* @return string Current character set name on DB
// {{{ (bool) EDB_SQLRELAY::set_charset ($charset)
* Set character set of current database
* This method is always returned true because SQLRELAY don't support
* @return bool always return true
* @param string name of character set that supported from database
// {{{ (string) EDB_SQLRELAY::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.
// {{{ (int) EDB_SQLRELAY::query ($query, $param_type, $param1, $param2 ...)
* Performs a query on the database
* @return integer The number of affected rows or false
* @param string $query The query strings
* @param string $type (optional) Bind parameter type. See also
* @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
// 얼마나 많은 라인을 받을 것인지.. 0은 무제한
//sqlrcur_setResultSetBufferSize ($this->result, 0);
if ( $this->pno++ == 0 ) // no bind query
$this->no_bind_query ($sql);
$this->bind_query ($sql, $argv);
if ( preg_match ('/^(update|insert|delete|replace)/i', trim ($sql)) ) {
/* Insert or update, or delete query */
$this->rownum = sqlrcur_affectedRows ($this->result);
$this->rownum = sqlrcur_rowCount ($this->result);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (int) EDB::SQLRELAY::lastId (void)
* 가장 마지막 입력 row ID를 반환한다.
return sqlrcon_getLastInsertId ($this->db);
// {{{ (bool) EDB_SQLRELAY::seek ($offset)
* Move the cursor in the result
* @param Number of units you want to move the cursor.
function seek ($offset) {
// {{{ (object) EDB_SQLRELAY::fetch (void)
* Fetch a result row as an associative object
* @return object|falseThe object of fetched a result row or false
* @param boolean (optional) fetch 수행 후 result를 free한다.
* (기본값: false) EDB >= 2.0.3
function fetch ($free = false) {
$r = sqlrcur_getRowAssoc ($this->result, $this->rowid++ );
return $r ? (object) $r : false;
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (array) EDB_SQLRELAY::fetch_all ($free = true)
* Fetch all result rows as an associative object
* @return array The fetched object result rows
* @param boolean (optional) free result set after fetch.
for ( $i= $start; $i< $this->rownum; $i++ ) {
$row[] = $this->fetch ();
// {{{ (bool) EDB_SQLRELAY::free_result (void)
* Frees stored result memory for the given statement handle
if ( ! $this->free ) return true;
// until current version (0.46)
// missing sqlrcur_closeResultSet api on sqlrelay php api.
// If you wnat to use this api, see also php-sqlrelay package
return sqlrcur_closeResultSet ($this->result);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_SQLRELAY::field_name ($index)
* Return the name of the specified field index
* @param integer The numerical field offset. The field_offset starts
* at 0. If field_offset does not exist, return false
* and an error of level E_WARNING is also issued.
return sqlrcur_getColumnName ($this->result, $index);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_SQLRELAY::field_type ($index)
* Get the type of the specified field in a result
* @param integer The numerical field offset. The field_offset starts
* at 0. If field_offset does not exist, return false
* and an error of level E_WARNING is also issued.
return sqlrcur_getColumnType ($this->result, $index);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (int) EDB_SQLRELAY::num_fields (void)
* Return the number of columns in the result set
* @return integer|falsereturn -1 if SQL sentence is not SELECT.
return sqlrcur_colCount ($this->result);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (void) EDB_SQLRELAY::trstart (void)
sqlrcon_autoCommitOff ($this->db);
// {{{ (void) EDB_SQLRELAY::trend ($v)
* @param bool false일경우 rollback을 수행한다.
function trend ($v = true) {
sqlrcon_rollback ($this->db);
sqlrcon_commit ($this->db);
sqlrcon_autoCommitOn ($this->db);
// {{{ (void) EDB_SQLRELAY::close (void)
sqlrcon_free ($this->db);
// {{{ private (int) EDB_SQLRELAY::no_bind_query ($sql)
* Performs a query on the database
* @return integer The number of affected rows or false
* @param string The query strings
private function no_bind_query ($sql) {
if ( ! sqlrcur_sendQuery ($this->result, $sql) ) {
sqlrcon_endSession ($this->db);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ private (int) EDB_SQLRELAY::bind_query ($sql, $parameters)
* Performs a bind query on the database
* @return integer The number of affected rows or false
* @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',
sqlrcur_prepareQuery ($this->result, $sql);
for ( $i= 1; $i< $this->pno; $i++ ) {
switch ($params[0][$i- 1]) {
// is strlen binary safe?
$params[$i]->data = $buf;
$params[$i]->len = strlen ($buf);
$func = ( $params[0][$i- 1] == 'b') ?
'sqlrcur_inputBindBlob' : 'sqlrcur_inputBindClob';
sqlrcur_inputBind ($this->result, $i, $params[$i]);
if ( ! sqlrcur_executeQuery ($this->result) ) {
sqlrcur_clearBinds ($this->result);
sqlrcon_endSession ($this->db);
sqlrcur_clearBinds ($this->result);
sqlrcon_endSession ($this->db);
} 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
|