Source for file ibase.php
Documentation is available at ibase.php
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Sterling Hughes <sterling@php.net> |
// | Maintainer: Daniel Convissor <danielc@php.net> |
// +----------------------------------------------------------------------+
// $Id: ibase.php,v 1.67 2004/03/25 17:12:03 danielc Exp $
// - If dbsyntax is not firebird, the limitQuery may fail
require_once 'DB/common.php';
* Database independent query interface definition for PHP's Interbase
* @version $Id: ibase.php,v 1.67 2004/03/25 17:12:03 danielc Exp $
* @author Sterling Hughes <sterling@php.net>
// just a few of the tons of Interbase error codes listed in the
// Language Reference section of the Interbase manual
function connect($dsninfo, $persistent = false )
$dbhost = $dsninfo['hostspec'] ?
($dsninfo['hostspec'] . ':' . $dsninfo['database']) :
$connect_function = $persistent ? 'ibase_pconnect' : 'ibase_connect';
$params[] = $dsninfo['username'] ? $dsninfo['username'] : null;
$params[] = $dsninfo['password'] ? $dsninfo['password'] : null;
$params[] = isset ($dsninfo['charset']) ? $dsninfo['charset'] : null;
$params[] = isset ($dsninfo['buffers']) ? $dsninfo['buffers'] : null;
$params[] = isset ($dsninfo['dialect']) ? $dsninfo['dialect'] : null;
$params[] = isset ($dsninfo['role']) ? $dsninfo['role'] : null;
if ($this->dsn['dbsyntax'] == 'firebird') {
$query = $this->modifyQuery ($query);
$result = @ibase_query ($this->connection, $query);
// Determine which queries that should return data, and which
// should return an error code only.
return $ismanip ? DB_OK : $result;
// {{{ modifyLimitQuery()
* This method is used by backends to alter limited queries
* Uses the new FIRST n SKIP n Firebird 1.0 syntax, so it is
* only compatible with Firebird 1.x
* @param string $query query to modify
* @param integer $from the row to start to fetching
* @param integer $count the numbers of rows to fetch
* @return the new (modified) query
* @author Ludovico Magnocavallo <ludo@sumatrasolutions.com>
function modifyLimitQuery ($query, $from, $count, $params = array ())
if ($this->dsn['dbsyntax'] == 'firebird') {
//$from++; // SKIP starts from 1, ie SKIP 1 starts from the first record
// (cox) Seems that SKIP starts in 0
" SELECT FIRST $count SKIP $from $1" , $query);
* Move the internal ibase result pointer to the next available result
* @param a valid fbsql result resource
* @return true if a result is available otherwise return false
* Fetch a row and insert the data into an existing array.
* Formating of the array and the data therein are configurable.
* See DB_result::fetchInto() for more information.
* @param resource $result query result identifier
* @param array $arr (reference) array where data from the row
* @param int $fetchmode how the resulting array should be indexed
* @param int $rownum the row number to fetch
* @return mixed DB_OK on success, null when end of result set is
* @see DB_result::fetchInto()
function fetchInto ($result, &$arr, $fetchmode, $rownum=null )
$arr = @ibase_fetch_assoc ($result);
$arr = @ibase_fetch_row ($result);
if ($errmsg = @ibase_errmsg ()) {
$this->_rtrimArrayValues ($arr);
$this->_convertNullArrayValuesToEmpty ($arr);
return @ibase_free_result ($result);
@ibase_free_query ($query);
$cols = @ibase_num_fields ($result);
* Prepares a query for multiple execution with execute().
* prepare() requires a generic query as string like <code>
* INSERT INTO numbers VALUES (?, ?, ?)
* </code>. The <kbd>?</kbd> characters are placeholders.
* Three types of placeholders can be used:
* + <kbd>?</kbd> a quoted scalar value, i.e. strings, integers
* + <kbd>!</kbd> value is inserted 'as is'
* + <kbd>&</kbd> requires a file name. The file's contents get
* inserted into the query (i.e. saving binary
* Use backslashes to escape placeholder characters if you don't want
* them to be interpreted as placeholders. Example: <code>
* "UPDATE foo SET col=? WHERE col='over \& under'"
* @param string $query query to be prepared
* @return mixed DB statement resource on success. DB_Error on failure.
$tokens = preg_split('/((?<!\\\)[&?!])/', $query, -1 ,
PREG_SPLIT_DELIM_CAPTURE );
foreach ($tokens as $key => $val) {
$newquery .= $tokens[$key] . '?';
$newquery = substr($newquery, 0 , -1 );
$newquery = $this->modifyQuery ($newquery);
$stmt = @ibase_prepare ($this->connection, $newquery);
* Executes a DB statement prepared with prepare().
* @param resource $stmt a DB statement resource returned from prepare()
* @param mixed $data array, string or numeric data to be used in
* execution of the statement. Quantity of items
* passed must match quantity of placeholders in
* query: meaning 1 for non-array items or the
* quantity of elements in the array.
* @return object a new DB_Result or a DB_Error when fail
* @see DB_ibase::prepare()
function &execute($stmt, $data = array ())
foreach ($data as $key => $value) {
* ibase doesn't seem to have the ability to pass a
* parameter along unchanged, so strip off quotes from start
* and end, plus turn two single quotes to one single quote,
* in order to avoid the quotes getting escaped by
* ibase and ending up in the database.
$data[$key] = preg_replace("/^'(.*)'$/", "\\1", $data[$key]);
$fp = @fopen($data[$key], 'rb');
if ($this->autocommit && $this->manip_query[(int)$stmt]) {
@ibase_commit($this->connection);
* Free the internal resources associated with a prepared query.
* @param $stmt The interbase_query resource type
* @return bool true on success, false if $result is invalid
@ibase_free_query ($stmt);
return $trans_args ? @ibase_trans ($trans_args, $this->connection) : @ibase_trans ();
* Returns the next free id in a sequence
* @param string $seq_name name of the sequence
* @param boolean $ondemand when true, the seqence is automatically
* created if it does not exist
* @return int the next id number in the sequence. DB_Error if problem.
* @see DB_common::nextID()
function nextId($seq_name, $ondemand = true )
$sqn = strtoupper($this->getSequenceName ($seq_name));
$this->pushErrorHandling (PEAR_ERROR_RETURN );
$result = & $this->query(" SELECT GEN_ID(${sqn}, 1) "
. " WHERE RDB\$GENERATOR_NAME='${sqn}'" );
$this->popErrorHandling ();
* @param string $seq_name the name of the sequence
* @return mixed DB_OK on success or DB error on error
$sqn = strtoupper($this->getSequenceName ($seq_name));
$this->pushErrorHandling (PEAR_ERROR_RETURN );
$result = $this->query(" CREATE GENERATOR ${sqn}" );
$this->popErrorHandling ();
* @param string $seq_name the name of the sequence
* @return mixed DB_OK on success or DB error on error
$sqn = strtoupper($this->getSequenceName ($seq_name));
return $this->query('DELETE FROM RDB$GENERATORS '
. " WHERE RDB\$GENERATOR_NAME='${sqn}'" );
// {{{ _ibaseFieldFlags()
* get the Flags of a Field
* @param string $field_name the name of the field
* @param string $table_name the name of the table
* @return string The flags of the field ("primary_key", "unique_key", "not_null"
* "default", "computed" and "blob" are supported)
function _ibaseFieldFlags ($field_name, $table_name)
$sql = 'SELECT R.RDB$CONSTRAINT_TYPE CTYPE'
. ' FROM RDB$INDEX_SEGMENTS I'
. ' JOIN RDB$RELATION_CONSTRAINTS R ON I.RDB$INDEX_NAME=R.RDB$INDEX_NAME'
. ' WHERE I.RDB$FIELD_NAME=\'' . $field_name . '\''
. ' AND UPPER(R.RDB$RELATION_NAME)=\'' . strtoupper($table_name) . '\'';
if ($obj = @ibase_fetch_object ($result)) {
@ibase_free_result ($result);
if (isset ($obj->CTYPE ) && trim($obj->CTYPE ) == 'PRIMARY KEY') {
$flags .= 'primary_key ';
if (isset ($obj->CTYPE ) && trim($obj->CTYPE ) == 'UNIQUE') {
$sql = 'SELECT R.RDB$NULL_FLAG AS NFLAG,'
. ' R.RDB$DEFAULT_SOURCE AS DSOURCE,'
. ' F.RDB$FIELD_TYPE AS FTYPE,'
. ' F.RDB$COMPUTED_SOURCE AS CSOURCE'
. ' FROM RDB$RELATION_FIELDS R '
. ' JOIN RDB$FIELDS F ON R.RDB$FIELD_SOURCE=F.RDB$FIELD_NAME'
. ' WHERE UPPER(R.RDB$RELATION_NAME)=\'' . strtoupper($table_name) . '\''
. ' AND R.RDB$FIELD_NAME=\'' . $field_name . '\'';
if ($obj = @ibase_fetch_object ($result)) {
@ibase_free_result ($result);
if (isset ($obj->NFLAG )) {
if (isset ($obj->DSOURCE )) {
if (isset ($obj->CSOURCE )) {
if (isset ($obj->FTYPE ) && $obj->FTYPE == 261 ) {
* Returns information about a table or a result set.
* NOTE: only supports 'table' and 'flags' if <var>$result</var>
* @param object|string $result DB_result object from a query or a
* string containing the name of a table
* @param int $mode a valid tableInfo mode
* @return array an associative array with the information requested
* or an error object if something is wrong
* @see DB_common::tableInfo()
if (isset ($result->result )) {
* Probably received a result object.
* Extract the result resource identifier.
* Probably received a table name.
* Create a result resource identifier.
" SELECT * FROM $result WHERE 1=0" );
* Probably received a result resource identifier.
* Depricated. Here for compatibility only.
$case_func = 'strtolower';
$count = @ibase_num_fields ($id);
// made this IF due to performance (one if is faster than $count if's)
for ($i=0; $i< $count; $i++ ) {
$info = @ibase_field_info ($id, $i);
$res[$i]['table'] = $got_string ? $case_func($result) : '';
$res[$i]['name'] = $case_func($info['name']);
$res[$i]['type'] = $info['type'];
$res[$i]['len'] = $info['length'];
$res[$i]['flags'] = ($got_string) ? $this->_ibaseFieldFlags ($info['name'], $result) : '';
$res['num_fields']= $count;
for ($i=0; $i< $count; $i++ ) {
$info = @ibase_field_info ($id, $i);
$res[$i]['table'] = $got_string ? $case_func($result) : '';
$res[$i]['name'] = $case_func($info['name']);
$res[$i]['type'] = $info['type'];
$res[$i]['len'] = $info['length'];
$res[$i]['flags'] = ($got_string) ? $this->_ibaseFieldFlags ($info['name'], $result) : '';
$res['order'][$res[$i]['name']] = $i;
$res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
// free the result only if we were called on a table
* Gather information about an error, then use that info to create a
* DB error object and finally return that object.
* @param integer $db_errno PEAR error number (usually a DB constant) if
* manually raising an error
* @param string $native_errmsg text of error message if known
* @return object DB error object
* @see DB_common::errorCode()
* @see DB_common::raiseError()
if ($native_errmsg === null ) {
$native_errmsg = @ibase_errmsg ();
// memo for the interbase php module hackers: we need something similar
// to mysql_errno() to retrieve error codes instead of this ugly hack
if (preg_match('/^([^0-9\-]+)([0-9\-]+)\s+(.*)$/', $native_errmsg, $m)) {
$native_errno = (int) $m[2 ];
// try to map the native error to the DB one
if ($db_errno === null ) {
// try to interpret Interbase error code (that's why we need ibase_errno()
// in the interbase module to return the real error code)
'/arithmetic exception, numeric overflow, or string truncation/' => DB_ERROR_DIVZERO
foreach ($error_regexps as $regexp => $code) {
$tmp = & $this->raiseError($db_errno, null , null , null , $native_errmsg);
Documentation generated on Mon, 11 Mar 2019 10:14:52 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|