123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915 |
- <?php
- class MDB2_Driver_fbsql extends MDB2_Driver_Common
- {
-
- var $string_quoting = array('start' => "'", 'end' => "'", 'escape' => "'", 'escape_pattern' => false);
- var $identifier_quoting = array('start' => '"', 'end' => '"', 'escape' => '"');
-
-
-
- function __construct()
- {
- parent::__construct();
- $this->phptype = 'fbsql';
- $this->dbsyntax = 'fbsql';
- $this->supported['sequences'] = 'emulated';
- $this->supported['indexes'] = true;
- $this->supported['affected_rows'] = true;
- $this->supported['transactions'] = true;
- $this->supported['savepoints'] = false;
- $this->supported['summary_functions'] = true;
- $this->supported['order_by_text'] = true;
- $this->supported['current_id'] = 'emulated';
- $this->supported['limit_queries'] = 'emulated';
- $this->supported['LOBs'] = true;
- $this->supported['replace'] ='emulated';
- $this->supported['sub_selects'] = true;
- $this->supported['auto_increment'] = false;
- $this->supported['primary_key'] = true;
- $this->supported['result_introspection'] = true;
- $this->supported['prepared_statements'] = 'emulated';
- $this->supported['identifier_quoting'] = true;
- $this->supported['pattern_escaping'] = false;
- $this->supported['new_link'] = false;
- $this->options['DBA_username'] = false;
- $this->options['DBA_password'] = false;
- }
-
-
-
- function errorInfo($error = null)
- {
- if ($this->connection) {
- $native_code = @fbsql_errno($this->connection);
- $native_msg = @fbsql_error($this->connection);
- } else {
- $native_code = @fbsql_errno();
- $native_msg = @fbsql_error();
- }
- if (null === $error) {
- static $ecode_map;
- if (empty($ecode_map)) {
- $ecode_map = array(
- 22 => MDB2_ERROR_SYNTAX,
- 85 => MDB2_ERROR_ALREADY_EXISTS,
- 108 => MDB2_ERROR_SYNTAX,
- 116 => MDB2_ERROR_NOSUCHTABLE,
- 124 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
- 215 => MDB2_ERROR_NOSUCHFIELD,
- 217 => MDB2_ERROR_INVALID_NUMBER,
- 226 => MDB2_ERROR_NOSUCHFIELD,
- 231 => MDB2_ERROR_INVALID,
- 239 => MDB2_ERROR_TRUNCATED,
- 251 => MDB2_ERROR_SYNTAX,
- 266 => MDB2_ERROR_NOT_FOUND,
- 357 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
- 358 => MDB2_ERROR_CONSTRAINT,
- 360 => MDB2_ERROR_CONSTRAINT,
- 361 => MDB2_ERROR_CONSTRAINT,
- );
- }
- if (isset($ecode_map[$native_code])) {
- $error = $ecode_map[$native_code];
- }
- }
- return array($error, $native_code, $native_msg);
- }
-
-
-
- function beginTransaction($savepoint = null)
- {
- $this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
- if (null !== $savepoint) {
- return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'savepoints are not supported', __FUNCTION__);
- }
- if ($this->in_transaction) {
- return MDB2_OK;
- }
- if (!$this->destructor_registered && $this->opened_persistent) {
- $this->destructor_registered = true;
- register_shutdown_function('MDB2_closeOpenTransactions');
- }
- $result =& $this->_doQuery('SET COMMIT FALSE;', true);
- if (MDB2::isError($result)) {
- return $result;
- }
- $this->in_transaction = true;
- return MDB2_OK;
- }
-
-
-
- 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__);
- }
- if (null !== $savepoint) {
- return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'savepoints are not supported', __FUNCTION__);
- }
- $result =& $this->_doQuery('COMMIT;', true);
- if (MDB2::isError($result)) {
- return $result;
- }
- $result =& $this->_doQuery('SET COMMIT TRUE;', true);
- if (MDB2::isError($result)) {
- return $result;
- }
- $this->in_transaction = false;
- return MDB2_OK;
- }
-
-
-
- function rollback($savepoint = null)
- {
- $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__);
- }
- if (null !== $savepoint) {
- return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'savepoints are not supported', __FUNCTION__);
- }
- $result =& $this->_doQuery('ROLLBACK;', true);
- if (MDB2::isError($result)) {
- return $result;
- }
- $result =& $this->_doQuery('SET COMMIT TRUE;', true);
- if (MDB2::isError($result)) {
- return $result;
- }
- $this->in_transaction = false;
- return MDB2_OK;
- }
-
-
-
- function _doConnect($username, $password, $persistent = false)
- {
- if (!extension_loaded($this->phptype)) {
- return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
- 'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);
- }
- $params = array(
- $this->dsn['hostspec'] ? $this->dsn['hostspec'] : 'localhost',
- $username ? $username : null,
- $password ? $password : null,
- );
- $connect_function = $persistent ? 'fbsql_pconnect' : 'fbsql_connect';
- $connection = @call_user_func_array($connect_function, $params);
- if ($connection <= 0) {
- return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,
- 'unable to establish a connection', __FUNCTION__);
- }
- if (!empty($this->dsn['charset'])) {
- $result = $this->setCharset($this->dsn['charset'], $connection);
- if (MDB2::isError($result)) {
- return $result;
- }
- }
- return $connection;
- }
-
-
-
- function connect()
- {
- if (is_resource($this->connection)) {
-
- if (MDB2::areEquals($this->connected_dsn, $this->dsn)
- && $this->opened_persistent == $this->options['persistent']
- ) {
- return MDB2_OK;
- }
- $this->disconnect(false);
- }
- $connection = $this->_doConnect($this->dsn['username'],
- $this->dsn['password'],
- $this->options['persistent']);
- if (MDB2::isError($connection)) {
- return $connection;
- }
- $this->connection = $connection;
- $this->connected_dsn = $this->dsn;
- $this->connected_database_name = '';
- $this->opened_persistent = $this->options['persistent'];
- $this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype;
- if ($this->database_name) {
- if ($this->database_name != $this->connected_database_name) {
- if (!@fbsql_select_db($this->database_name, $connection)) {
- $err = $this->raiseError(null, null, null,
- 'Could not select the database: '.$this->database_name, __FUNCTION__);
- return $err;
- }
- $this->connected_database_name = $this->database_name;
- }
- }
- return MDB2_OK;
- }
-
-
-
- function databaseExists($name)
- {
- $connection = $this->_doConnect($this->dsn['username'],
- $this->dsn['password'],
- $this->options['persistent']);
- if (MDB2::isError($connection)) {
- return $connection;
- }
- $result = @fbsql_select_db($name, $connection);
- @fbsql_close($connection);
- return $result;
- }
-
-
-
- function disconnect($force = true)
- {
- if (is_resource($this->connection)) {
- if ($this->in_transaction) {
- $dsn = $this->dsn;
- $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->rollback();
- $this->dsn = $dsn;
- $this->database_name = $database_name;
- $this->options['persistent'] = $persistent;
- }
- if (!$this->opened_persistent || $force) {
- @fbsql_close($this->connection);
- }
- }
- return parent::disconnect($force);
- }
-
-
-
- function &standaloneQuery($query, $types = null, $is_manip = false)
- {
- $user = $this->options['DBA_username']? $this->options['DBA_username'] : $this->dsn['username'];
- $pass = $this->options['DBA_password']? $this->options['DBA_password'] : $this->dsn['password'];
- $connection = $this->_doConnect($user, $pass, $this->options['persistent']);
- if (MDB2::isError($connection)) {
- return $connection;
- }
- $offset = $this->offset;
- $limit = $this->limit;
- $this->offset = $this->limit = 0;
- $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
- $result =& $this->_doQuery($query, $is_manip, $connection, $this->database_name);
- if (!MDB2::isError($result)) {
- $result = $this->_affectedRows($connection, $result);
- }
- @fbsql_close($connection);
- return $result;
- }
-
-
-
- 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 ($result) {
- if (MDB2::isError($result)) {
- return $result;
- }
- $query = $result;
- }
- if ($this->options['disable_query']) {
- $result = $is_manip ? 0 : null;
- return $result;
- }
- if (null === $connection) {
- $connection = $this->getConnection();
- if (MDB2::isError($connection)) {
- return $connection;
- }
- }
- if (null === $database_name) {
- $database_name = $this->database_name;
- }
- if ($database_name) {
- if ($database_name != $this->connected_database_name) {
- if (!@fbsql_select_db($database_name, $connection)) {
- $err = $this->raiseError(null, null, null,
- 'Could not select the database: '.$database_name, __FUNCTION__);
- return $err;
- }
- $this->connected_database_name = $database_name;
- }
- }
- $result = @fbsql_query($query, $connection);
- if (!$result) {
- $err =& $this->raiseError(null, null, null,
- 'Could not execute statement', __FUNCTION__);
- return $err;
- }
- $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'post', 'result' => $result));
- return $result;
- }
-
-
-
- function _affectedRows($connection, $result = null)
- {
- if (null === $connection) {
- $connection = $this->getConnection();
- if (MDB2::isError($connection)) {
- return $connection;
- }
- }
- return @fbsql_affected_rows($connection);
- }
-
-
-
- function _modifyQuery($query, $is_manip, $limit, $offset)
- {
- if ($limit > 0) {
- if ($is_manip) {
- return preg_replace('/^([\s(])*SELECT(?!\s*TOP\s*\()/i',
- "\\1SELECT TOP($limit)", $query);
- } else {
- return preg_replace('/([\s(])*SELECT(?!\s*TOP\s*\()/i',
- "\\1SELECT TOP($offset,$limit)", $query);
- }
- }
-
- return $query.';';
- }
-
-
-
- function nextID($seq_name, $ondemand = true)
- {
- $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
- $seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true);
- $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL);";
- $this->pushErrorHandling(PEAR_ERROR_RETURN);
- $this->expectError(MDB2_ERROR_NOSUCHTABLE);
- $result =& $this->_doQuery($query, true);
- $this->popExpect();
- $this->popErrorHandling();
- if (MDB2::isError($result)) {
- if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) {
- $this->loadModule('Manager', null, true);
- $result = $this->manager->createSequence($seq_name);
- if (MDB2::isError($result)) {
- return $this->raiseError($result, null, null,
- 'on demand sequence '.$seq_name.' could not be created', __FUNCTION__);
- } else {
- return $this->nextID($seq_name, false);
- }
- }
- return $result;
- }
- $value = $this->lastInsertID();
- if (is_numeric($value)) {
- $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
- $result =& $this->_doQuery($query, true);
- if (MDB2::isError($result)) {
- $this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;
- }
- }
- return $value;
- }
-
-
-
- function lastInsertID($table = null, $field = null)
- {
- $connection = $this->getConnection();
- if (MDB2::isError($connection)) {
- return $connection;
- }
- $value = @fbsql_insert_id($connection);
- if (!$value) {
- return $this->raiseError(null, null, null,
- 'Could not get last insert ID', __FUNCTION__);
- }
- return $value;
- }
-
-
-
- function currID($seq_name)
- {
- $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
- $seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true);
- $query = "SELECT MAX($seqcol_name) FROM $sequence_name";
- return $this->queryOne($query, 'integer');
- }
- }
- class MDB2_Result_fbsql extends MDB2_Result_Common
- {
-
-
-
- function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
- {
- if (null !== $rownum) {
- $seek = $this->seek($rownum);
- if (MDB2::isError($seek)) {
- return $seek;
- }
- }
- if ($fetchmode == MDB2_FETCHMODE_DEFAULT) {
- $fetchmode = $this->db->fetchmode;
- }
- if ( $fetchmode == MDB2_FETCHMODE_ASSOC
- || $fetchmode == MDB2_FETCHMODE_OBJECT
- ) {
- $row = @fbsql_fetch_assoc($this->result);
- if (is_array($row)
- && $this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE
- ) {
- $row = array_change_key_case($row, $this->db->options['field_case']);
- }
- } else {
- $row = @fbsql_fetch_row($this->result);
- }
- if (!$row) {
- if (false === $this->result) {
- $err =& $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
- 'resultset has already been freed', __FUNCTION__);
- return $err;
- }
- $null = null;
- return $null;
- }
- $mode = $this->db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL;
- if ($mode) {
- $this->db->_fixResultArrayValues($row, $mode);
- }
- if (($fetchmode != MDB2_FETCHMODE_ASSOC) && !empty($this->types)) {
- $row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim);
- } elseif (($fetchmode == MDB2_FETCHMODE_ASSOC) && !empty($this->types_assoc)) {
- $row = $this->db->datatype->convertResultRow($this->types_assoc, $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 = (object) $row;
- } else {
- $row = new $object_class($row);
- }
- }
- ++$this->rownum;
- return $row;
- }
-
-
-
- function _getColumnNames()
- {
- $columns = array();
- $numcols = $this->numCols();
- if (MDB2::isError($numcols)) {
- return $numcols;
- }
- for ($column = 0; $column < $numcols; $column++) {
- $column_name = @fbsql_field_name($this->result, $column);
- $columns[$column_name] = $column;
- }
- if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
- $columns = array_change_key_case($columns, $this->db->options['field_case']);
- }
- return $columns;
- }
-
-
-
- function numCols()
- {
- $cols = @fbsql_num_fields($this->result);
- if (null === $cols) {
- if (false === $this->result) {
- return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
- 'resultset has already been freed', __FUNCTION__);
- }
- if (null === $this->result) {
- return count($this->types);
- }
- return $this->db->raiseError(null, null, null,
- 'Could not get column count', __FUNCTION__);
- }
- return $cols;
- }
-
-
-
- function nextResult()
- {
- if (false === $this->result) {
- return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
- 'resultset has already been freed', __FUNCTION__);
- }
- if (null === $this->result) {
- return false;
- }
- return @fbsql_next_result($this->result);
- }
-
-
-
- function free()
- {
- if (is_resource($this->result) && $this->db->connection) {
- $free = @fbsql_free_result($this->result);
- if (false === $free) {
- return $this->db->raiseError(null, null, null,
- 'Could not free result', __FUNCTION__);
- }
- }
- $this->result = false;
- return MDB2_OK;
- }
- }
- class MDB2_BufferedResult_fbsql extends MDB2_Result_fbsql
- {
-
-
-
- function seek($rownum = 0)
- {
- if ($this->rownum != ($rownum - 1) && !@fbsql_data_seek($this->result, $rownum)) {
- if (false === $this->result) {
- return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
- 'resultset has already been freed', __FUNCTION__);
- }
- if (null === $this->result) {
- return MDB2_OK;
- }
- return $this->db->raiseError(MDB2_ERROR_INVALID, null, null,
- 'tried to seek to an invalid row number ('.$rownum.')', __FUNCTION__);
- }
- $this->rownum = $rownum - 1;
- return MDB2_OK;
- }
-
-
-
- function valid()
- {
- $numrows = $this->numRows();
- if (MDB2::isError($numrows)) {
- return $numrows;
- }
- return $this->rownum < ($numrows - 1);
- }
-
-
-
- function numRows()
- {
- $rows = @fbsql_num_rows($this->result);
- if (null === $rows) {
- if (false === $this->result) {
- return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
- 'resultset has already been freed', __FUNCTION__);
- }
- if (null === $this->result) {
- return 0;
- }
- return $this->db->raiseError(null, null, null,
- 'Could not get row count', __FUNCTION__);
- }
- return $rows;
- }
- }
- class MDB2_Statement_fbsql extends MDB2_Statement_Common
- {
- }
- ?>
|