ibase.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,757 行 · 第 1/5 页
PHP
1,757 行
function getDatabaseFile($database_name) { if (isset($this->options['DatabasePath'])) { $this->database_path = $this->options['DatabasePath']; } if (isset($this->options['DatabaseExtension'])) { $this->database_extension = $this->options['DatabaseExtension']; } //$this->database_path = (isset($this->options['DatabasePath']) ? $this->options['DatabasePath'] : ''); //$this->database_extension = (isset($this->options['DatabaseExtension']) ? $this->options['DatabaseExtension'] : '.gdb'); //$database_path = (isset($this->options['DatabasePath']) ? $this->options['DatabasePath'] : ''); //$database_extension = (isset($this->options['DatabaseExtension']) ? $this->options['DatabaseExtension'] : '.gdb'); return $this->database_path.$database_name.$this->database_extension; } // }}} // {{{ _doConnect() /** * Does the grunt work of connecting to the database * * @return mixed connection resource on success, MDB_Error on failure * @access private **/ function _doConnect($database_name, $persistent) { $function = ($persistent ? 'ibase_pconnect' : 'ibase_connect'); if (!function_exists($function)) { return($this->raiseError(MDB_ERROR_UNSUPPORTED, NULL, NULL, 'doConnect: FireBird/InterBase support is not available in this PHP configuration')); } $dbhost = $this->host ? ($this->host . ':' . $database_name) : $database_name; $params = array(); $params[] = $dbhost; $params[] = !empty($this->user) ? $this->user : NULL; $params[] = !empty($this->password) ? $this->password : NULL; $connection = @call_user_func_array($function, $params); if ($connection > 0) { @ibase_timefmt("%Y-%m-%d %H:%M:%S", IBASE_TIMESTAMP); @ibase_timefmt("%Y-%m-%d", IBASE_DATE); return $connection; } if (isset($php_errormsg)) { $error_msg = $php_errormsg; } else { $error_msg = 'Could not connect to FireBird/InterBase server'; } return($this->raiseError(MDB_ERROR_CONNECT_FAILED, NULL, NULL, 'doConnect: '.$error_msg)); } // }}} // {{{ connect() /** * Connect to the database * * @return TRUE on success, MDB_Error on failure * @access public **/ function connect() { $port = (isset($this->options['port']) ? $this->options['port'] : ''); $database_file = $this->getDatabaseFile($this->database_name); if ($this->connection != 0) { if (!strcmp($this->connected_host, $this->host) && !strcmp($this->connected_port, $port) && !strcmp($this->selected_database_file, $database_file) && ($this->opened_persistent == $this->options['persistent'])) { return MDB_OK; } @ibase_close($this->connection); $this->affected_rows = -1; $this->connection = 0; } $connection = $this->_doConnect($database_file, $this->options['persistent']); if (MDB::isError($connection)) { return $connection; } $this->connection = $connection; //the if below was added after PEAR::DB. Review me!! if ($this->dbsyntax == 'fbird') { $this->supported['limit'] = 'alter'; } if (!$this->auto_commit && MDB::isError($trans_result = $this->_doQuery('BEGIN'))) { @ibase_close($this->connection); $this->connection = 0; $this->affected_rows = -1; return $trans_result; } $this->connected_host = $this->host; $this->connected_port = $port; $this->selected_database_file = $database_file; $this->opened_persistent = $this->options['persistent']; return MDB_OK; } // }}} // {{{ _close() /** * Close the database connection * * @return boolean * @access private **/ function _close() { if ($this->connection != 0) { if (!$this->auto_commit) { $this->_doQuery('END'); } @ibase_close($this->connection); $this->connection = 0; $this->affected_rows = -1; unset($GLOBALS['_MDB_databases'][$this->database]); return true; } return false; } // }}} // {{{ _doQuery() /** * Execute a query * @param string $query the SQL query * @return mixed result identifier if query executed, else MDB_error * @access private **/ function _doQuery($query, $first=0, $limit=0, $prepared_query=0) // function _doQuery($query) { $connection = ($this->auto_commit ? $this->connection : $this->transaction_id); if ($prepared_query && isset($this->query_parameters[$prepared_query]) && count($this->query_parameters[$prepared_query]) > 2) { $this->query_parameters[$prepared_query][0] = $connection; $this->query_parameters[$prepared_query][1] = $query; $result = @call_user_func_array("ibase_query", $this->query_parameters[$prepared_query]); } else { //Not Prepared Query $result = @ibase_query($connection, $query); while (@ibase_errmsg() == 'Query argument missed') { //ibase_errcode() only available in PHP5 //connection lost, try again... $this->connect(); //rollback the failed transaction to prevent deadlock and execute the query again if ($this->transaction_id) { $this->rollback(); } $result = @ibase_query($this->connection, $query); } } if ($result) { if (!MDB::isManip($query)) { $result_value = intval($result); $this->current_row[$result_value] = -1; if ($limit > 0) { $this->limits[$result_value] = array($first, $limit, 0); } $this->highest_fetched_row[$result_value] = -1; } else { $this->affected_rows = -1; } } else { return ($this->raiseError(MDB_ERROR, NULL, NULL, '_doQuery: Could not execute query ("'.$query.'"): ' . @ibase_errmsg())); } return $result; } // }}} // {{{ query() /** * Send a query to the database and return any results * * @param string $query the SQL query * @param array $types array that contains the types of the columns in * the result set * @return mixed result identifier if query executed, else MDB_error * @access public **/ function query($query, $types = NULL) { $this->debug('Query: '.$query); $this->last_query = $query; $first = $this->first_selected_row; $limit = $this->selected_row_limit; $this->first_selected_row = $this->selected_row_limit = 0; $connected = $this->connect(); if (MDB::isError($connected)) { return $connected; } if (!MDB::isError($result = $this->_doQuery($query, $first, $limit, 0))) { if ($types != NULL) { if (!is_array($types)) { $types = array($types); } if (MDB::isError($err = $this->setResultTypes($result, $types))) { $this->freeResult($result); return $err; } } return $result; } return $this->ibaseRaiseError(); } // }}} // {{{ _executePreparedQuery() /** * Execute a prepared query statement. * * @param int $prepared_query argument is a handle that was returned by * the function prepareQuery() * @param string $query query to be executed * @param array $types array that contains the types of the columns in * the result set * @return mixed a result handle or MDB_OK on success, a MDB error on failure * @access private */ function _executePreparedQuery($prepared_query, $query) { $first = $this->first_selected_row; $limit = $this->selected_row_limit; $this->first_selected_row = $this->selected_row_limit = 0; if (MDB::isError($connect = $this->connect())) { return $connect; } return($this->_doQuery($query, $first, $limit, $prepared_query)); } // }}} // {{{ _skipLimitOffset() /** * Skip the first row of a result set. * * @param resource $result * @return mixed a result handle or MDB_OK on success, a MDB error on failure * @access private */ function _skipLimitOffset($result) { $result_value = intval($result); $first = $this->limits[$result_value][0]; for (; $this->limits[$result_value][2] < $first; $this->limits[$result_value][2]++) { if (!is_array(@ibase_fetch_row($result))) { $this->limits[$result_value][2] = $first; return($this->raiseError(MDB_ERROR, NULL, NULL, 'Skip first rows: could not skip a query result row')); } } return MDB_OK; } // }}} // {{{ getColumnNames() /** * Retrieve the names of columns returned by the DBMS in a query result. * * @param resource $result result identifier * @return mixed an associative array variable * that will hold the names of columns.The * indexes of the array are the column names * mapped to lower case and the values are the * respective numbers of the columns starting * from 0. Some DBMS may not return any * columns when the result set does not * contain any rows. * * a MDB error on failure * @access public */ function getColumnNames($result) { $result_value = intval($result); if (!isset($this->highest_fetched_row[$result_value])) { return($this->raiseError(MDB_ERROR, NULL, NULL, 'Get column names: it was specified an inexisting result set')); } if (!isset($this->columns[$result_value])) { $this->columns[$result_value] = array(); $columns = @ibase_num_fields($result); for ($column=0; $column < $columns; $column++) { $column_info = @ibase_field_info($result, $column); $field_name = $column_info['name']; if ($this->options['optimize'] == 'portability') { $field_name = strtolower($field_name); } $this->columns[$result_value][$field_name] = $column; } } return $this->columns[$result_value]; } // }}} // {{{ numCols() /** * Count the number of columns returned by the DBMS in a query result. * * @param resource $result result identifier * @return mixed integer value with the number of columns, a MDB error * on failure * @access public */ function numCols($result) { if (!isset($this->highest_fetched_row[intval($result)])) { return($this->raiseError(MDB_ERROR, NULL, NULL, 'Number of columns: it was specified an inexisting result set')); } return @ibase_num_fields($result); } // }}} // {{{ endOfResult() /** * check if the end of the result set has been reached * * @param resource $result result identifier * @return mixed TRUE or FALSE on sucess, a MDB error on failure * @access public */ function endOfResult($result) { $result_value = intval($result); if (!isset($this->current_row[$result_value])) { return($this->raiseError(MDB_ERROR, NULL, NULL, 'End of result: attempted to check the end of an unknown result')); } if (isset($this->results[$result_value]) && end($this->results[$result_value]) === false) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?