Source for file ibase.php
Documentation is available at ibase.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* The PEAR DB driver for PHP's interbase extension
* for interacting with Interbase and Firebird databases
* While this class works with PHP 4, PHP's InterBase extension is
* unstable in PHP 4. Use PHP 5.
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
* @author Sterling Hughes <sterling@php.net>
* @author Daniel Convissor <danielc@php.net>
* @copyright 1997-2007 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: ibase.php,v 1.116 2007/09/21 13:40:41 aharvey Exp $
* @link http://pear.php.net/package/DB
* Obtain the DB_common class so it can be extended from
require_once 'DB/common.php';
* The methods PEAR DB uses to interact with PHP's interbase extension
* for interacting with Interbase and Firebird databases
* These methods overload the ones declared in DB_common.
* While this class works with PHP 4, PHP's InterBase extension is
* unstable in PHP 4. Use PHP 5.
* NOTICE: limitQuery() only works for Firebird.
* @author Sterling Hughes <sterling@php.net>
* @author Daniel Convissor <danielc@php.net>
* @copyright 1997-2007 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version Release: 1.7.14RC1
* @link http://pear.php.net/package/DB
* @since Class became stable in Release 1.7.0
* The DB driver type (mysql, oci8, odbc, etc.)
* The database syntax variant to be used (db2, access, etc.), if any
* The capabilities of this DB implementation
* The 'new_link' element contains the PHP version that first provided
* new_link support for this DBMS. Contains false if it's unsupported.
* Meaning of the 'limit' element:
* + 'emulate' = emulate with fetch row by number
* + 'alter' = alter the query
* NOTE: only firebird supports limit.
* A mapping of native error codes to DB error codes
-150 => DB_ERROR_ACCESS_VIOLATION ,
-151 => DB_ERROR_ACCESS_VIOLATION ,
-155 => DB_ERROR_NOSUCHTABLE ,
-157 => DB_ERROR_NOSUCHFIELD ,
-158 => DB_ERROR_VALUE_COUNT_ON_ROW ,
-170 => DB_ERROR_MISMATCH ,
-171 => DB_ERROR_MISMATCH ,
-172 => DB_ERROR_INVALID ,
// -204 => // Covers too many errors, need to use regex on msg
-205 => DB_ERROR_NOSUCHFIELD ,
-206 => DB_ERROR_NOSUCHFIELD ,
-208 => DB_ERROR_INVALID ,
-219 => DB_ERROR_NOSUCHTABLE ,
-297 => DB_ERROR_CONSTRAINT ,
-303 => DB_ERROR_INVALID ,
-413 => DB_ERROR_INVALID_NUMBER ,
-530 => DB_ERROR_CONSTRAINT ,
-551 => DB_ERROR_ACCESS_VIOLATION ,
-552 => DB_ERROR_ACCESS_VIOLATION ,
// -607 => // Covers too many errors, need to use regex on msg
-625 => DB_ERROR_CONSTRAINT_NOT_NULL ,
-803 => DB_ERROR_CONSTRAINT ,
-804 => DB_ERROR_VALUE_COUNT_ON_ROW ,
// -902 => // Covers too many errors, need to use regex on msg
-904 => DB_ERROR_CONNECT_FAILED ,
-922 => DB_ERROR_NOSUCHDB ,
-923 => DB_ERROR_CONNECT_FAILED ,
-924 => DB_ERROR_CONNECT_FAILED
* The raw database connection created by PHP
* The DSN information for connecting to a database
* The number of rows affected by a data manipulation query
* Should data manipulation queries be committed automatically?
* The prepared statement handle from the most recently executed statement
* {@internal Mainly here because the InterBase/Firebird API is only
* able to retrieve data from result sets if the statemnt handle is
* Is the given prepared statement a data manipulation query?
var $manip_query = array ();
* This constructor calls <kbd>$this->DB_common()</kbd>
* Connect to the database server, log in and open the database
* Don't call this method directly. Use DB::connect() instead.
* PEAR DB's ibase driver supports the following extra DSN options:
* + buffers The number of database buffers to allocate for the
* + charset The default character set for a database.
* + dialect The default SQL dialect for any statement
* executed within a connection. Defaults to the
* highest one supported by client libraries.
* Functional only with InterBase 6 and up.
* + role Functional only with InterBase 5 and up.
* @param array $dsn the data source name
* @param bool $persistent should the connection be persistent?
* @return int DB_OK on success. A DB_Error object on failure.
function connect($dsn, $persistent = false )
if (!PEAR ::loadExtension ('interbase')) {
? ($dsn['hostspec'] . ':' . $dsn['database'])
$dsn['username'] ? $dsn['username'] : null ,
$dsn['password'] ? $dsn['password'] : null ,
isset ($dsn['charset']) ? $dsn['charset'] : null ,
isset ($dsn['buffers']) ? $dsn['buffers'] : null ,
isset ($dsn['dialect']) ? $dsn['dialect'] : null ,
isset ($dsn['role']) ? $dsn['role'] : null ,
$connect_function = $persistent ? 'ibase_pconnect' : 'ibase_connect';
* Disconnects from the database server
* @return bool TRUE on success, FALSE on failure
* Sends a query to the database server
* @param string the SQL query string
* @return mixed + a PHP result resrouce for successful SELECT queries
* + the DB_OK constant for other successful queries
* + a DB_Error object on failure
$result = @ibase_query ($this->connection, $query);
if ($this->autocommit && $ismanip) {
$this->affected = $result;
// {{{ modifyLimitQuery()
* Adds LIMIT clauses to a query string according to current DBMS standards
* Only works with Firebird.
* @param string $query the query to modify
* @param int $from the row to start to fetching (0 = the first row)
* @param int $count the numbers of rows to fetch
* @param mixed $params 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 placeholder for non-array
* parameters or 1 placeholder per array element.
* @return string the query string with LIMIT clauses added
if ($this->dsn['dbsyntax'] == 'firebird') {
" SELECT FIRST $count SKIP $from" , $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
* Places a row from the result set into the given array
* Formating of the array and the data therein are configurable.
* See DB_result::fetchInto() for more information.
* This method is not meant to be called directly. Use
* DB_result::fetchInto() instead. It can't be declared "protected"
* because DB_result is a separate object.
* @param resource $result the query result resource
* @param array $arr the referenced array to put the data in
* @param int $fetchmode how the resulting array should be indexed
* @param int $rownum the row number to fetch (0 = first row)
* @return mixed DB_OK on success, NULL when the end of a result set is
* @see DB_result::fetchInto()
function fetchInto($result, &$arr, $fetchmode, $rownum = null )
$arr = @ibase_fetch_assoc ($result);
$arr = @ibase_fetch_row ($result);
* Deletes the result set and frees the memory occupied by the result set
* This method is not meant to be called directly. Use
* DB_result::free() instead. It can't be declared "protected"
* because DB_result is a separate object.
* @param resource $result PHP's query result resource
* @return bool TRUE on success, FALSE if $result is invalid
return is_resource($result) ? ibase_free_result ($result) : false;
return is_resource($query) ? ibase_free_query ($query) : false;
* Determines the number of rows affected by a data maniuplation query
* 0 is returned for queries that don't manipulate data.
* @return int the number of rows. A DB_Error object on failure.
* Gets the number of columns in a result set
* This method is not meant to be called directly. Use
* DB_result::numCols() instead. It can't be declared "protected"
* because DB_result is a separate object.
* @param resource $result PHP's query result resource
* @return int the number of columns. A DB_Error object on failure.
* @see DB_result::numCols()
$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 );
$stmt = @ibase_prepare ($this->connection, $newquery);
$this->manip_query[(int) $stmt] = DB::isManip($query);
* 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);
* Frees the internal resources associated with a prepared query
* @param resource $stmt the prepared statement's PHP resource
* @param bool $free_resource should the PHP resource be freed too?
* Use false if you need to get data
* from the result set later.
* @return bool TRUE on success, FALSE if $result is invalid
* @see DB_ibase::prepare()
@ibase_free_query ($stmt);
unset ($this->manip_query[(int) $stmt]);
* Enables or disables automatic commits
* @param bool $onoff true turns it on, false turns it off
* @return int DB_OK on success. A DB_Error object if the driver
* doesn't support auto-committing transactions.
$this->autocommit = $onoff ? 1 : 0;
|