odbc.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 586 行 · 第 1/2 页
PHP
586 行
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | 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: Stig Bakken <ssb@php.net> |
// | Maintainer: Daniel Convissor <danielc@php.net> |
// +----------------------------------------------------------------------+
//
// $Id: odbc.php 3355 2005-06-11 22:14:40Z nbm $
// XXX legend:
// More info on ODBC errors could be found here:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/trblsql/tr_err_odbc_5stz.asp
//
// XXX ERRORMSG: The error message from the odbc function should
// be registered here.
require_once 'DB/common.php';
/**
* Database independent query interface definition for PHP's ODBC
* extension.
*
* @package DB
* @version $Id: odbc.php 3355 2005-06-11 22:14:40Z nbm $
* @category Database
* @author Stig Bakken <ssb@php.net>
*/
class DB_odbc extends DB_common
{
// {{{ properties
var $connection;
var $phptype, $dbsyntax;
var $row = array();
// }}}
// {{{ constructor
function DB_odbc()
{
$this->DB_common();
$this->phptype = 'odbc';
$this->dbsyntax = 'sql92';
$this->features = array(
'prepare' => true,
'pconnect' => true,
'transactions' => false,
'limit' => 'emulate'
);
$this->errorcode_map = array(
'01004' => DB_ERROR_TRUNCATED,
'07001' => DB_ERROR_MISMATCH,
'21S01' => DB_ERROR_MISMATCH,
'21S02' => DB_ERROR_MISMATCH,
'22003' => DB_ERROR_INVALID_NUMBER,
'22005' => DB_ERROR_INVALID_NUMBER,
'22008' => DB_ERROR_INVALID_DATE,
'22012' => DB_ERROR_DIVZERO,
'23000' => DB_ERROR_CONSTRAINT,
'23502' => DB_ERROR_CONSTRAINT_NOT_NULL,
'23503' => DB_ERROR_CONSTRAINT,
'23505' => DB_ERROR_CONSTRAINT,
'24000' => DB_ERROR_INVALID,
'34000' => DB_ERROR_INVALID,
'37000' => DB_ERROR_SYNTAX,
'42000' => DB_ERROR_SYNTAX,
'42601' => DB_ERROR_SYNTAX,
'IM001' => DB_ERROR_UNSUPPORTED,
'S0000' => DB_ERROR_NOSUCHTABLE,
'S0001' => DB_ERROR_ALREADY_EXISTS,
'S0002' => DB_ERROR_NOSUCHTABLE,
'S0011' => DB_ERROR_ALREADY_EXISTS,
'S0012' => DB_ERROR_NOT_FOUND,
'S0021' => DB_ERROR_ALREADY_EXISTS,
'S0022' => DB_ERROR_NOSUCHFIELD,
'S1000' => DB_ERROR_CONSTRAINT_NOT_NULL,
'S1009' => DB_ERROR_INVALID,
'S1090' => DB_ERROR_INVALID,
'S1C00' => DB_ERROR_NOT_CAPABLE
);
}
// }}}
// {{{ connect()
/**
* Connect to a database and log in as the specified user.
*
* @param $dsn the data source name (see DB::parseDSN for syntax)
* @param $persistent (optional) whether the connection should
* be persistent
*
* @return int DB_OK on success, a DB error code on failure
*/
function connect($dsninfo, $persistent = false)
{
if (!DB::assertExtension('odbc')) {
return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
}
$this->dsn = $dsninfo;
if ($dsninfo['dbsyntax']) {
$this->dbsyntax = $dsninfo['dbsyntax'];
}
switch ($this->dbsyntax) {
case 'solid':
$this->features = array(
'prepare' => true,
'pconnect' => true,
'transactions' => true
);
break;
case 'navision':
// the Navision driver doesn't support fetch row by number
$this->features['limit'] = false;
}
/*
* This is hear for backwards compatibility.
* Should have been using 'database' all along, but used hostspec.
*/
if ($dsninfo['database']) {
$odbcdsn = $dsninfo['database'];
} elseif ($dsninfo['hostspec']) {
$odbcdsn = $dsninfo['hostspec'];
} else {
$odbcdsn = 'localhost';
}
if ($this->provides('pconnect')) {
$connect_function = $persistent ? 'odbc_pconnect' : 'odbc_connect';
} else {
$connect_function = 'odbc_connect';
}
if (empty($dsninfo['cursor'])) {
$conn = @$connect_function($odbcdsn, $dsninfo['username'],
$dsninfo['password']);
} else {
$conn = @$connect_function($odbcdsn, $dsninfo['username'],
$dsninfo['password'],
$dsninfo['cursor']);
}
if (!is_resource($conn)) {
return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null,
null, $this->errorNative());
}
$this->connection = $conn;
return DB_OK;
}
// }}}
// {{{ disconnect()
function disconnect()
{
$err = @odbc_close($this->connection);
$this->connection = null;
return $err;
}
// }}}
// {{{ simpleQuery()
/**
* Send a query to ODBC and return the results as a ODBC resource
* identifier.
*
* @param $query the SQL query
*
* @return int returns a valid ODBC result for successful SELECT
* queries, DB_OK for other successful queries. A DB error code
* is returned on failure.
*/
function simpleQuery($query)
{
$this->last_query = $query;
$query = $this->modifyQuery($query);
$result = @odbc_exec($this->connection, $query);
if (!$result) {
return $this->odbcRaiseError(); // XXX ERRORMSG
}
// Determine which queries that should return data, and which
// should return an error code only.
if (DB::isManip($query)) {
$this->manip_result = $result; // For affectedRows()
return DB_OK;
}
$this->row[(int)$result] = 0;
$this->manip_result = 0;
return $result;
}
// }}}
// {{{ nextResult()
/**
* Move the internal odbc result pointer to the next available result
*
* @param a valid fbsql result resource
*
* @access public
*
* @return true if a result is available otherwise return false
*/
function nextResult($result)
{
return @odbc_next_result($result);
}
// }}}
// {{{ fetchInto()
/**
* 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
* should be placed
* @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
* reached or on failure
*
* @see DB_result::fetchInto()
* @access private
*/
function fetchInto($result, &$arr, $fetchmode, $rownum=null)
{
$arr = array();
if ($rownum !== null) {
$rownum++; // ODBC first row is 1
if (version_compare(phpversion(), '4.2.0', 'ge')) {
$cols = @odbc_fetch_into($result, $arr, $rownum);
} else {
$cols = @odbc_fetch_into($result, $rownum, $arr);
}
} else {
$cols = @odbc_fetch_into($result, $arr);
}
if (!$cols) {
/* XXX FIXME: doesn't work with unixODBC and easysoft
(get corrupted $errno values)
if ($errno = @odbc_error($this->connection)) {
return $this->RaiseError($errno);
}*/
return null;
}
if ($fetchmode !== DB_FETCHMODE_ORDERED) {
for ($i = 0; $i < count($arr); $i++) {
$colName = @odbc_field_name($result, $i+1);
$a[$colName] = $arr[$i];
}
if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
$a = array_change_key_case($a, CASE_LOWER);
}
$arr = $a;
}
if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
$this->_rtrimArrayValues($arr);
}
if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
$this->_convertNullArrayValuesToEmpty($arr);
}
return DB_OK;
}
// }}}
// {{{ freeResult()
function freeResult($result)
{
unset($this->row[(int)$result]);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?