Source for file ibase.php
Documentation is available at ibase.php
// vim: set et ts=4 sw=4 fdm=marker:
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith, Lorenzo Alberton |
// | All rights reserved. |
// +----------------------------------------------------------------------+
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
// | API as well as database abstraction for PHP applications. |
// | This LICENSE is in the BSD license style. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution. |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
// | Lukas Smith nor the names of his contributors may be used to endorse |
// | or promote products derived from this software without specific prior|
// | written permission. |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+
// | Author: Lorenzo Alberton <l.alberton@quipo.it> |
// +----------------------------------------------------------------------+
// $Id: ibase.php,v 1.205 2007/05/03 11:56:58 quipo Exp $
* MDB2 FireBird/InterBase driver
* @author Lorenzo Alberton <l.alberton@quipo.it>
var $string_quoting = array ('start' => "'", 'end' => "'", 'escape' => "'", 'escape_pattern' => '\\');
$this->phptype = 'ibase';
$this->dbsyntax = 'ibase';
$this->supported['sequences'] = true;
$this->supported['indexes'] = true;
$this->supported['affected_rows'] = function_exists('ibase_affected_rows');
$this->supported['summary_functions'] = true;
$this->supported['order_by_text'] = true;
$this->supported['transactions'] = true;
$this->supported['savepoints'] = true;
$this->supported['current_id'] = true;
$this->supported['limit_queries'] = 'emulated';
$this->supported['LOBs'] = true;
$this->supported['replace'] = false;
$this->supported['sub_selects'] = true;
$this->supported['auto_increment'] = true;
$this->supported['primary_key'] = true;
$this->supported['result_introspection'] = true;
$this->supported['prepared_statements'] = true;
$this->supported['identifier_quoting'] = false;
$this->supported['pattern_escaping'] = true;
$this->supported['new_link'] = false;
$this->options['DBA_username'] = false;
$this->options['DBA_password'] = false;
$this->options['database_path'] = '';
$this->options['database_extension'] = '.gdb';
$this->options['server_version'] = '';
* This method is used to collect information about an error
$native_msg = @ibase_errmsg ();
$native_code = @ibase_errcode ();
// 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_msg, $m)) {
$native_code = (int) $m[2 ];
// try to interpret Interbase error code (that's why we need ibase_errno()
// in the interbase module to return the real error code)
if (isset ($m[3 ]) && is_int(strpos($m[3 ], 'Table unknown'))) {
$errno = MDB2_ERROR_NOSUCHTABLE;
-104 => MDB2_ERROR_SYNTAX ,
-150 => MDB2_ERROR_ACCESS_VIOLATION ,
-151 => MDB2_ERROR_ACCESS_VIOLATION ,
-155 => MDB2_ERROR_NOSUCHTABLE ,
-157 => MDB2_ERROR_NOSUCHFIELD ,
-158 => MDB2_ERROR_VALUE_COUNT_ON_ROW ,
-170 => MDB2_ERROR_MISMATCH ,
-171 => MDB2_ERROR_MISMATCH ,
-172 => MDB2_ERROR_INVALID ,
// -204 => // Covers too many errors, need to use regex on msg
-205 => MDB2_ERROR_NOSUCHFIELD ,
-206 => MDB2_ERROR_NOSUCHFIELD ,
-208 => MDB2_ERROR_INVALID ,
-219 => MDB2_ERROR_NOSUCHTABLE ,
-297 => MDB2_ERROR_CONSTRAINT ,
-303 => MDB2_ERROR_INVALID ,
-413 => MDB2_ERROR_INVALID_NUMBER ,
-530 => MDB2_ERROR_CONSTRAINT ,
-551 => MDB2_ERROR_ACCESS_VIOLATION ,
-552 => MDB2_ERROR_ACCESS_VIOLATION ,
// -607 => // Covers too many errors, need to use regex on msg
-625 => MDB2_ERROR_CONSTRAINT_NOT_NULL ,
-803 => MDB2_ERROR_CONSTRAINT ,
-804 => MDB2_ERROR_VALUE_COUNT_ON_ROW ,
// -902 => // Covers too many errors, need to use regex on msg
-904 => MDB2_ERROR_CONNECT_FAILED ,
-922 => MDB2_ERROR_NOSUCHDB ,
-923 => MDB2_ERROR_CONNECT_FAILED ,
-924 => MDB2_ERROR_CONNECT_FAILED
if (isset ($ecode_map[$native_code])) {
$error = $ecode_map[$native_code];
if (!isset ($error_regexps)) {
'/generator .* is not defined/'
=> MDB2_ERROR_SYNTAX , // for compat. w ibase_errcode()
'/table.*(not exist|not found|unknown)/i'
=> MDB2_ERROR_NOSUCHTABLE ,
'/table .* already exists/i'
=> MDB2_ERROR_ALREADY_EXISTS ,
'/unsuccessful metadata update .* failed attempt to store duplicate value/i'
=> MDB2_ERROR_ALREADY_EXISTS ,
'/unsuccessful metadata update .* not found/i'
'/validation error for column .* value "\*\*\* null/i'
=> MDB2_ERROR_CONSTRAINT_NOT_NULL ,
'/violation of [\w ]+ constraint/i'
=> MDB2_ERROR_CONSTRAINT ,
'/conversion error from string/i'
=> MDB2_ERROR_INVALID_NUMBER ,
=> MDB2_ERROR_ACCESS_VIOLATION ,
'/arithmetic exception, numeric overflow, or string truncation/i'
'/feature is not supported/i'
=> MDB2_ERROR_NOT_CAPABLE ,
foreach ($error_regexps as $regexp => $code) {
return array ($error, $native_code, $native_msg);
* Delimited identifiers are a nightmare with InterBase, so they're disabled
* @param string $str identifier name to be quoted
* @param bool $check_option check the 'quote_identifier' option
* @return string quoted identifier string
if ($check_option && !$this->options['quote_identifier']) {
* Returns a native connection
* @return mixed a valid MDB2 connection object,
* or a MDB2 error object on error
if (PEAR ::isError ($result)) {
if ($this->in_transaction) {
return $this->connection;
// {{{ beginTransaction()
* Start a transaction or set a savepoint.
* @param string name of a savepoint to set
* @return mixed MDB2_OK on success, a MDB2 error on failure
$this->debug ('Starting transaction/savepoint', __FUNCTION__ , array ('is_manip' => true , 'savepoint' => $savepoint));
if (!$this->in_transaction) {
return $this->raiseError (MDB2_ERROR_INVALID , null , null ,
'savepoint cannot be released when changes are auto committed', __FUNCTION__ );
$query = 'SAVEPOINT '. $savepoint;
} elseif ($this->in_transaction) {
return MDB2_OK; //nothing to do
if (PEAR ::isError ($connection)) {
$result = @ibase_trans (IBASE_DEFAULT , $connection);
return $this->raiseError (null , null , null ,
'could not start a transaction', __FUNCTION__ );
$this->in_transaction = true;
* Commit the database changes done during a transaction that is in
* progress or release a savepoint. This function may only be called when
* auto-committing is disabled, otherwise it will fail. Therefore, a new
* transaction is implicitly started after committing the pending changes.
* @param string name of a savepoint to release
* @return mixed MDB2_OK on success, a MDB2 error on failure
function commit($savepoint = null )
$this->debug ('Committing transaction/savepoint', __FUNCTION__ , array ('is_manip' => true , 'savepoint' => $savepoint));
if (!$this->in_transaction) {
return $this->raiseError (MDB2_ERROR_INVALID , null , null ,
'commit/release savepoint cannot be done changes are auto committed', __FUNCTION__ );
$query = 'RELEASE SAVEPOINT '. $savepoint;
return $this->raiseError (null , null , null ,
'could not commit a transaction', __FUNCTION__ );
$this->in_transaction = false;
* Cancel any database changes done during a transaction or since a specific
* savepoint that is in progress. This function may only be called when
* auto-committing is disabled, otherwise it will fail. Therefore, a new
* transaction is implicitly started after canceling the pending changes.
* @param string name of a savepoint to rollback to
* @return mixed MDB2_OK on success, a MDB2 error on failure
$this->debug ('Rolling back transaction/savepoint', __FUNCTION__ , array ('is_manip' => true , 'savepoint' => $savepoint));
if (!$this->in_transaction) {
return $this->raiseError (MDB2_ERROR_INVALID , null , null ,
'rollback cannot be done changes are auto committed', __FUNCTION__ );
$query = 'ROLLBACK TO SAVEPOINT '. $savepoint;
return $this->raiseError (null , null , null ,
'Could not rollback a pending transaction: '. @ibase_errmsg (), __FUNCTION__ );
$this->in_transaction = false;
// {{{ setTransactionIsolation()
* Set the transacton isolation level.
* @param string standard isolation level (SQL-92)
* READ UNCOMMITTED (allows dirty reads)
* READ COMMITTED (prevents dirty reads)
* REPEATABLE READ (prevents nonrepeatable reads)
* SERIALIZABLE (prevents phantom reads)
* @param array some transaction options:
* 'wait' => 'WAIT' | 'NO WAIT'
* 'rw' => 'READ WRITE' | 'READ ONLY'
* @return mixed MDB2_OK on success, a MDB2 error on failure
$this->debug ('Setting transaction isolation level', __FUNCTION__ , array ('is_manip' => true ));
$ibase_isolation = 'READ COMMITTED RECORD_VERSION';
$ibase_isolation = 'READ COMMITTED NO RECORD_VERSION';
$ibase_isolation = 'SNAPSHOT';
$ibase_isolation = 'SNAPSHOT TABLE STABILITY';
return $this->raiseError (MDB2_ERROR_UNSUPPORTED , null , null ,
'isolation level is not supported: '. $isolation, __FUNCTION__ );
if (!empty ($options['wait'])) {
switch ($options['wait']) {
$wait = $options['wait'];
return $this->raiseError (MDB2_ERROR_UNSUPPORTED , null , null ,
'wait option is not supported: '. $options['wait'], __FUNCTION__ );
if (!empty ($options['rw'])) {
switch ($options['rw']) {
return $this->raiseError (MDB2_ERROR_UNSUPPORTED , null , null ,
'wait option is not supported: '. $options['rw'], __FUNCTION__ );
$query = " SET TRANSACTION $rw $wait ISOLATION LEVEL $ibase_isolation";
// {{{ getDatabaseFile($database_name)
* Builds the string with path+dbname+extension
* @return string full database path+file
if ($database_name == '') {
$ret = $this->options['database_path'] . $database_name;
if (!preg_match('/\.[fg]db$/i', $database_name)) {
$ret .= $this->options['database_extension'];
* Does the grunt work of connecting to the database
* @return mixed connection resource on success, MDB2 Error Object on failure
function _doConnect($database_name, $persistent = false )
$user = $this->dsn['username'];
$pw = $this->dsn['password'];
$dbhost = $this->dsn['hostspec'] ?
($this->dsn['hostspec']. ':'. $database_name) : $database_name;
$params[] = !empty ($user) ? $user : null;
$params[] = !empty ($pw) ? $pw : null;
$params[] = isset ($this->dsn['charset']) ? $this->dsn['charset'] : null;
$params[] = isset ($this->dsn['buffers']) ? $this->dsn['buffers'] : null;
$params[] = isset ($this->dsn['dialect']) ? $this->dsn['dialect'] : null;
$params[] = isset ($this->dsn['role']) ? $this->dsn['role'] : null;
$connect_function = $persistent ? 'ibase_pconnect' : 'ibase_connect';
return $this->raiseError (MDB2_ERROR_CONNECT_FAILED , null , null ,
'unable to establish a connection', __FUNCTION__ );
if (empty ($this->dsn['disable_iso_date'])) {
@ibase_timefmt ("%Y-%m-%d %H:%M:%S", IBASE_TIMESTAMP );
@ibase_timefmt ("%Y-%m-%d", IBASE_DATE );
@ini_set("ibase.timestampformat", "%Y-%m-%d %H:%M:%S");
//@ini_set("ibase.timeformat", "%H:%M:%S");
@ini_set("ibase.dateformat", "%Y-%m-%d");
* Connect to the database
* @return true on success, MDB2 Error Object on failure
&& $this->connected_database_name == $database_file
&& $this->opened_persistent == $this->options['persistent']
if (!PEAR ::loadExtension ('interbase')) {
return $this->raiseError (MDB2_ERROR_NOT_FOUND , null , null ,
'extension '. $this->phptype. ' is not compiled into PHP', __FUNCTION__ );
if (!empty ($this->database_name)) {
$connection = $this->_doConnect($database_file, $this->options['persistent']);
if (PEAR ::isError ($connection)) {
$this->connection = & $connection;
$this->connected_dsn = $this->dsn;
$this->connected_database_name = $database_file;
$this->opened_persistent = $this->options['persistent'];
$this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype;
$this->supported['limit_queries'] = ($this->dbsyntax == 'firebird') ? true : 'emulated';
* Set the charset on the current connection
* @param resource connection handle
* @return true on success, MDB2 Error Object on failure
if (PEAR ::isError ($connection)) {
$query = 'SET NAMES '. $this->quote ($charset, 'text');
$result = @ibase_query ($connection, $query);
return $this->raiseError (null , null , null ,
'Unable to set client charset: '. $charset, __FUNCTION__ );
* Log out and disconnect from the database.
* @param boolean $force if the disconnect should be forced even if the
* connection is opened persistently
* @return mixed true on success, false if not connected and error
if ($this->in_transaction) {
$database_name = $this->database_name;
$persistent = $this->options['persistent'];
$this->dsn = $this->connected_dsn;
$this->database_name = $this->connected_database_name;
$this->options['persistent'] = $this->opened_persistent;
$this->database_name = $database_name;
$this->options['persistent'] = $persistent;
if (!$this->opened_persistent || $force) {
@ibase_close ($this->connection);
return parent ::disconnect ($force);
* @param string $query query
* @param boolean $is_manip if the query is a manipulation query
* @param resource $connection
* @param string $database_name
* @return result or error object
function &_doQuery($query, $is_manip = false , $connection = null , $database_name = null )
$this->last_query = $query;
$result = $this->debug ($query, 'query', array ('is_manip' => $is_manip, 'when' => 'pre'));
if (PEAR ::isError ($result)) {
if ($this->getOption ('disable_query')) {
if (PEAR ::isError ($connection)) {
$result = @ibase_query ($connection, $query);
$err = & $this->raiseError (null , null , null ,
'Could not execute statement', __FUNCTION__ );
$this->debug ($query, 'query', array ('is_manip' => $is_manip, 'when' => 'post', 'result' => $result));
* Returns the number of rows affected
* @param resource $result
* @param resource $connection
* @return mixed MDB2 Error Object or the number of rows affected
function _affectedRows ($connection, $result = null )
if (PEAR ::isError ($connection)) {
return (function_exists('ibase_affected_rows') ? @ibase_affected_rows ($connection) : 0 );
* Changes a query string for various DBMS specific reasons
* @param string $query query to modify
* @param boolean $is_manip if it is a DML query
* @param integer $limit limit the number of rows
* @param integer $offset start reading from given offset
* @return string modified query
if ($limit > 0 && $this->supports ('limit_queries') === true ) {
$query = preg_replace('/^([\s(])*SELECT(?!\s*FIRST\s*\d+)/i',
" SELECT FIRST $limit SKIP $offset" , $query);
// {{{ getServerVersion()
* return version information about the server
* @param bool $native determines if the raw version string should be returned
* @return mixed array/string with version information or MDB2 error object
if ($this->connected_server_info) {
$server_info = $this->connected_server_info;
} elseif ($this->options['server_version']) {
$server_info = $this->options['server_version'];
} elseif ($this->options['DBA_username']) {
$ibserv = @ibase_service_attach ($this->dsn['hostspec'], $this->options['DBA_username'], $this->options['DBA_password']);
$server_info = @ibase_server_info ($ibserv, IBASE_SVC_SERVER_VERSION );
@ibase_service_detach ($ibserv);
return $this->raiseError (MDB2_ERROR_UNSUPPORTED , null , null ,
'Requires either "server_version" or "DBA_username"/"DBA_password" option', __FUNCTION__ );
$this->connected_server_info = $server_info;
//WI-V1.5.3.4854 Firebird 1.5
if (!preg_match('/-V([\d\.]*)/', $server_info, $matches)) {
return $this->raiseError (MDB2_ERROR_INVALID , null , null ,
'Could not parse version information:'. $server_info, __FUNCTION__ );
$tmp = explode('.', $matches[1 ], 4 );
'major' => isset ($tmp[0 ]) ? $tmp[0 ] : null ,
'minor' => isset ($tmp[1 ]) ? $tmp[1 ] : null ,
'patch' => isset ($tmp[2 ]) ? $tmp[2 ] : null ,
'extra' => isset ($tmp[3 ]) ? $tmp[3 ] : null ,
'native' => $server_info,
* Prepares a query for multiple execution with execute().
* With some database backends, this is emulated.
* prepare() requires a generic query as string like
* 'INSERT INTO numbers VALUES(?,?)' or
* 'INSERT INTO numbers VALUES(:foo,:bar)'.
* The ? and :[a-zA-Z] and are placeholders which can be set using
* bindParam() and the query can be send off using the execute() method.
* @param string $query the query to prepare
* @param mixed $types array that contains the types of the placeholders
* @param mixed $result_types array that contains the types of the columns in
* the result set or MDB2_PREPARE_RESULT, if set to
* MDB2_PREPARE_MANIP the query is handled as a manipulation query
* @param mixed $lobs key (field) value (parameter) pair for all lob placeholders
* @return mixed resource handle for the prepared query on success, a MDB2
* @see bindParam, execute
function &prepare($query, $types = null , $result_types = null , $lobs = array ())
if ($this->options['emulate_prepared']) {
$obj = & parent ::prepare ($query, $types, $result_types, $lobs);
$is_manip = ($result_types === MDB2_PREPARE_MANIP );
$this->offset = $this->limit = 0;
$result = $this->debug ($query, __FUNCTION__ , array ('is_manip' => $is_manip, 'when' => 'pre'));
if (PEAR ::isError ($result)) {
$placeholder_type_guess = $placeholder_type = null;
while ($position < strlen($query)) {
$q_position = strpos($query, $question, $position);
$c_position = strpos($query, $colon, $position);
if ($q_position && $c_position) {
$p_position = min($q_position, $c_position);
$p_position = $q_position;
$p_position = $c_position;
$placeholder_type_guess = $query[$p_position];
$new_pos = $this->_skipDelimitedStrings ($query, $position, $p_position);
if (PEAR ::isError ($new_pos)) {
if ($new_pos != $position) {
continue; //evaluate again starting from the new position
if ($query[$position] == $placeholder_type_guess) {
$placeholder_type = $query[$p_position];
$question = $colon = $placeholder_type;
if ($placeholder_type == ':') {
$parameter = preg_replace('/^.{'. ($position+1 ). '}([a-z0-9_]+).*$/si', '\\1', $query);
$err = & $this->raiseError (MDB2_ERROR_SYNTAX , null , null ,
'named parameter with an empty name', __FUNCTION__ );
$positions[] = $parameter;
$positions[] = count($positions);
$position = $p_position + 1;
if (PEAR ::isError ($connection)) {
$statement = @ibase_prepare ($connection, $query);
$err = & $this->raiseError (null , null , null ,
'Could not create statement', __FUNCTION__ );
$class_name = 'MDB2_Statement_'. $this->phptype;
$obj = & new $class_name($this, $statement, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);
$this->debug ($query, __FUNCTION__ , array ('is_manip' => $is_manip, 'when' => 'post', 'result' => $obj));
* adds sequence name formatting to a sequence name
* @param string $sqn name of the sequence
* @return string formatted sequence name
* Returns the next free id of a sequence
* @param string $seq_name name of the sequence
* @param boolean $ondemand when true the sequence is
* automatic created, if it
* @return mixed MDB2 Error Object or id
function nextID($seq_name, $ondemand = true )
$query = 'SELECT GEN_ID('. $sequence_name. ', 1) as the_value FROM RDB$DATABASE';
$result = $this->queryOne ($query, 'integer');
if (PEAR ::isError ($result)) {
$this->loadModule ('Manager', null , true );
$result = $this->manager->createSequence ($seq_name);
if (PEAR ::isError ($result)) {
return $this->raiseError ($result, null , null ,
'on demand sequence could not be created', __FUNCTION__ );
return $this->nextID($seq_name, false );
* Returns the autoincrement ID if supported or $id or fetches the current
* ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
* @param string $table name of the table into which a new row was inserted
* @param string $field name of the field into which a new row was inserted
* @return mixed MDB2 Error Object or id
$seq = $table. (empty ($field) ? '' : '_'. $field);
* Returns the current id of a sequence
* @param string $seq_name name of the sequence
* @return mixed MDB2 Error Object or id
$query = 'SELECT GEN_ID('. $sequence_name. ', 0) as the_value FROM RDB$DATABASE';
$value = $this->queryOne ($query);
if (PEAR ::isError ($value)) {
return $this->raiseError ($value, null , null ,
'Unable to select from ' . $seq_name, __FUNCTION__ );
return $this->raiseError (MDB2_ERROR , null , null ,
'could not find value in sequence table', __FUNCTION__ );
* MDB2 FireBird/InterBase result driver
* @author Lorenzo Alberton <l.alberton@quipo.it>
// {{{ _skipLimitOffset()
* Skip the first row of a result set.
* @param resource $result
* @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
if ($this->db->supports ('limit_queries') === true ) {
if ($this->rownum > $this->limit) {
while ($this->offset_count < $this->offset) {
if (!is_array(@ibase_fetch_row ($this->result))) {
$this->offset_count = $this->offset;
* Fetch a row and insert the data into an existing array.
* @param int $fetchmode how the array data should be indexed
* @param int $rownum number of the row where the data can be found
* @return int data array on success, a MDB2 error on failure
function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT , $rownum = null )
if ($this->result === true ) {
//query successfully executed, but without results...
$seek = $this->seek ($rownum);
if (PEAR ::isError ($seek)) {
if ($fetchmode == MDB2_FETCHMODE_DEFAULT ) {
$fetchmode = $this->db->fetchmode;
if ($fetchmode & MDB2_FETCHMODE_ASSOC ) {
$row = @ibase_fetch_assoc ($this->result);
&& $this->db->options ['portability'] & MDB2_PORTABILITY_FIX_CASE
$row = @ibase_fetch_row ($this->result);
if ($this->result === false ) {
$err = & $this->db->raiseError (MDB2_ERROR_NEED_MORE_DATA , null , null ,
'resultset has already been freed', __FUNCTION__ );
$mode = $this->db->options ['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL;
if ($this->db->options ['portability'] & MDB2_PORTABILITY_RTRIM ) {
if (empty ($this->types)) {
$mode += MDB2_PORTABILITY_RTRIM;
$this->db->_fixResultArrayValues ($row, $mode);
if (!empty ($this->types)) {
$row = $this->db->datatype ->convertResultRow ($this->types, $row, $rtrim);
if (!empty ($this->values)) {
$this->_assignBindColumns ($row);
if ($fetchmode === MDB2_FETCHMODE_OBJECT ) {
$object_class = $this->db->options ['fetch_class'];
if ($object_class == 'stdClass') {
$row = &new $object_class($row);
* Retrieve the names of columns returned by the DBMS in a query result.
* @return mixed Array variable that holds the names of columns as keys
* or an MDB2 error on failure.
* Some DBMS may not return any columns when the result set
* does not contain any rows.
function _getColumnNames ()
if (PEAR ::isError ($numcols)) {
for ($column = 0; $column < $numcols; $column++ ) {
$column_info = @ibase_field_info ($this->result, $column);
$columns[$column_info['alias']] = $column;
if ($this->db->options ['portability'] & MDB2_PORTABILITY_FIX_CASE ) {
* Count the number of columns returned by the DBMS in a query result.
* @return mixed integer value with the number of columns, a MDB2 error
if ($this->result === true ) {
//query successfully executed, but without results...
return $this->db->raiseError (MDB2_ERROR_NOT_FOUND , null , null ,
'numCols(): not a valid ibase resource', __FUNCTION__ );
$cols = @ibase_num_fields ($this->result);
if ($this->result === false ) {
return $this->db->raiseError (MDB2_ERROR_NEED_MORE_DATA , null , null ,
'resultset has already been freed', __FUNCTION__ );
} elseif (is_null($this->result)) {
return count($this->types);
return $this->db->raiseError (null , null , null ,
'Could not get column count', __FUNCTION__ );
* Free the internal resources associated with $result.
* @return boolean true on success, false if $result is invalid
if (is_resource($this->result) && $this->db->connection ) {
$free = @ibase_free_result ($this->result);
return $this->db->raiseError (null , null , null ,
'Could not free result', __FUNCTION__ );
* MDB2 FireBird/InterBase buffered result driver
* @author Lorenzo Alberton <l.alberton@quipo.it>
* @param int $rownum row number upto which the buffer should be filled
* if the row number is null all rows are ready into the buffer
* @return boolean true on success, false on failure
} elseif (isset ($this->buffer[$rownum])) {
return (bool) $this->buffer[$rownum];
&& ($buffer = @ibase_fetch_row ($this->result))
} elseif ($this->limit && $this->buffer_rownum >= $this->limit) {
* Fetch a row and insert the data into an existing array.
* @param int $fetchmode how the array data should be indexed
* @param int $rownum number of the row where the data can be found
* @return int data array on success, a MDB2 error on failure
function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT , $rownum = null )
if ($this->result === true || is_null($this->result)) {
//query successfully executed, but without results...
if ($this->result === false ) {
$err = & $this->db->raiseError (MDB2_ERROR_NEED_MORE_DATA , null , null ,
'resultset has already been freed', __FUNCTION__ );
$seek = $this->seek($rownum);
if (PEAR ::isError ($seek)) {
$target_rownum = $this->rownum + 1;
if ($fetchmode == MDB2_FETCHMODE_DEFAULT ) {
$fetchmode = $this->db->fetchmode;
$row = $this->buffer[$target_rownum];
if ($fetchmode & MDB2_FETCHMODE_ASSOC ) {
$column_names = $this->getColumnNames ();
foreach ($column_names as $name => $i) {
$column_names[$name] = $row[$i];
$mode = $this->db->options ['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL;
if ($this->db->options ['portability'] & MDB2_PORTABILITY_RTRIM ) {
if (empty ($this->types)) {
$mode += MDB2_PORTABILITY_RTRIM;
$this->db->_fixResultArrayValues ($row, $mode);
if (!empty ($this->types)) {
$row = $this->db->datatype ->convertResultRow ($this->types, $row, $rtrim);
if (!empty ($this->values)) {
$this->_assignBindColumns ($row);
if ($fetchmode === MDB2_FETCHMODE_OBJECT ) {
$object_class = $this->db->options ['fetch_class'];
if ($object_class == 'stdClass') {
$row = &new $object_class($row);
* Seek to a specific row in a result set
* @param int $rownum number of the row where the data can be found
* @return mixed MDB2_OK on success, a MDB2 error on failure
function seek($rownum = 0 )
if ($this->result === false ) {
return $this->db->raiseError (MDB2_ERROR_NEED_MORE_DATA , null , null ,
'resultset has already been freed', __FUNCTION__ );
$this->rownum = $rownum - 1;
* Check if the end of the result set has been reached
* @return mixed true or false on sucess, a MDB2 error on failure
if ($this->result === false ) {
return $this->db->raiseError (MDB2_ERROR_NEED_MORE_DATA , null , null ,
'resultset has already been freed', __FUNCTION__ );
} elseif (is_null($this->result)) {
* Returns the number of rows in a result object
* @return mixed MDB2 Error Object or the number of rows
if ($this->result === false ) {
return $this->db->raiseError (MDB2_ERROR_NEED_MORE_DATA , null , null ,
'resultset has already been freed', __FUNCTION__ );
} elseif (is_null($this->result)) {
* Free the internal resources associated with $result.
* @return boolean true on success, false if $result is invalid
* MDB2 FireBird/InterBase statement driver
* @author Lorenzo Alberton <l.alberton@quipo.it>
* Execute a prepared query statement helper method.
* @param mixed $result_class string which specifies which result class to use
* @param mixed $result_wrap_class string which specifies which class to wrap results in
* @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
function &_execute ($result_class = true , $result_wrap_class = false )
$result = & parent ::_execute ($result_class, $result_wrap_class);
$this->db->last_query = $this->query;
$this->db->debug ($this->query, 'execute', array ('is_manip' => $this->is_manip, 'when' => 'pre', 'parameters' => $this->values));
if ($this->db->getOption ('disable_query')) {
$result = $this->is_manip ? 0 : null;
$connection = $this->db->getConnection ();
if (PEAR ::isError ($connection)) {
$parameters = array (0 => $this->statement);
foreach ($this->positions as $parameter) {
return $this->db->raiseError (MDB2_ERROR_NOT_FOUND , null , null ,
'Unable to bind to missing placeholder: '. $parameter, __FUNCTION__ );
$value = $this->values[$parameter];
$type = !empty ($this->types[$parameter]) ? $this->types[$parameter] : null;
$parameters[] = $this->db->quote ($value, $type, false );
$err = & $this->db->raiseError (null , null , null ,
'Could not execute statement', __FUNCTION__ );
$affected_rows = $this->db->_affectedRows ($connection);
$result = & $this->db->_wrapResult ($result, $this->result_types,
$result_class, $result_wrap_class, $this->limit, $this->offset);
$this->db->debug ($this->query, 'execute', array ('is_manip' => $this->is_manip, 'when' => 'post', 'result' => $result));
* Release resources allocated for the specified prepared query.
* @return mixed MDB2_OK on success, a MDB2 error on failure
return $this->db->raiseError (MDB2_ERROR , null , null ,
'Prepared statement has already been freed', __FUNCTION__ );
if (!is_null($this->statement) && !@ibase_free_query ($this->statement)) {
$result = $this->db->raiseError (null , null , null ,
'Could not free statement', __FUNCTION__ );
Documentation generated on Mon, 11 Mar 2019 15:01:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|