3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Author: Stig Bakken <ssb@fast.no> |
17 // +----------------------------------------------------------------------+
19 // $Id: odbc.php,v 1.1 2002-09-12 11:47:07 rurban Exp $
21 // Database independent query interface definition for PHP's ODBC
27 // More info on ODBC errors could be found here:
28 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/trblsql/tr_err_odbc_5stz.asp
30 // XXX ERRORMSG: The error message from the odbc function should
31 // be registered here.
33 // Based on DB 1.3 from the pear.php.net repository.
34 // The only modifications made have been modification of the include paths.
36 rcs_id('$Id: odbc.php,v 1.1 2002-09-12 11:47:07 rurban Exp $');
37 rcs_id('From Pear CVS: Id: odbc.php,v 1.3 2002/05/09 12:29:53 ssb Exp');
39 require_once 'lib/pear/DB/common.php';
41 class DB_odbc extends DB_common
46 var $phptype, $dbsyntax;
55 $this->phptype = 'odbc';
56 $this->dbsyntax = 'sql92';
57 $this->features = array(
60 'transactions' => false,
63 $this->errorcode_map = array(
64 '01004' => DB_ERROR_TRUNCATED,
65 '07001' => DB_ERROR_MISMATCH,
66 '21S01' => DB_ERROR_MISMATCH,
67 '21S02' => DB_ERROR_MISMATCH,
68 '22003' => DB_ERROR_INVALID_NUMBER,
69 '22008' => DB_ERROR_INVALID_DATE,
70 '22012' => DB_ERROR_DIVZERO,
71 '23000' => DB_ERROR_CONSTRAINT,
72 '24000' => DB_ERROR_INVALID,
73 '34000' => DB_ERROR_INVALID,
74 '37000' => DB_ERROR_SYNTAX,
75 '42000' => DB_ERROR_SYNTAX,
76 'IM001' => DB_ERROR_UNSUPPORTED,
77 'S0000' => DB_ERROR_NOSUCHTABLE,
78 'S0001' => DB_ERROR_NOT_FOUND,
79 'S0002' => DB_ERROR_NOT_FOUND,
80 'S0011' => DB_ERROR_ALREADY_EXISTS,
81 'S0012' => DB_ERROR_NOT_FOUND,
82 'S0021' => DB_ERROR_ALREADY_EXISTS,
83 'S0022' => DB_ERROR_NOT_FOUND,
84 'S1009' => DB_ERROR_INVALID,
85 'S1090' => DB_ERROR_INVALID,
86 'S1C00' => DB_ERROR_NOT_CAPABLE
94 * Connect to a database and log in as the specified user.
96 * @param $dsn the data source name (see DB::parseDSN for syntax)
97 * @param $persistent (optional) whether the connection should
100 * @return int DB_OK on success, a DB error code on failure
102 function connect($dsninfo, $persistent = false)
104 if (!DB::assertExtension('odbc'))
105 return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
107 $this->dsn = $dsninfo;
108 if (!empty($dsninfo['dbsyntax'])) {
109 $this->dbsyntax = $dsninfo['dbsyntax'];
111 switch ($this->dbsyntax) {
113 $this->features = array(
116 'transactions' => true
118 $default_dsn = 'localhost';
121 // the Navision driver doesn't support fetch row by number
122 $this->features['limit'] = false;
127 $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
128 $user = $dsninfo['username'];
129 $pw = $dsninfo['password'];
130 if ($this->provides('pconnect')) {
131 $connect_function = $persistent ? 'odbc_pconnect' : 'odbc_connect';
133 $connect_function = 'odbc_connect';
135 $conn = @$connect_function($dbhost, $user, $pw);
136 if (!is_resource($conn)) {
137 return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null,
138 null, $this->errorNative());
140 $this->connection = $conn;
147 function disconnect()
149 $err = @odbc_close($this->connection);
150 $this->connection = null;
158 * Send a query to ODBC and return the results as a ODBC resource
161 * @param $query the SQL query
163 * @return int returns a valid ODBC result for successful SELECT
164 * queries, DB_OK for other successful queries. A DB error code
165 * is returned on failure.
167 function simpleQuery($query)
169 $this->last_query = $query;
170 $query = $this->modifyQuery($query);
171 $result = @odbc_exec($this->connection, $query);
173 return $this->odbcRaiseError(); // XXX ERRORMSG
175 // Determine which queries that should return data, and which
176 // should return an error code only.
177 if (DB::isManip($query)) {
178 $this->manip_result = $result; // For affectedRows()
181 $this->row[$result] = 0;
182 $this->manip_result = 0;
190 * Move the internal odbc result pointer to the next available result
192 * @param a valid fbsql result resource
196 * @return true if a result is available otherwise return false
198 function nextResult($result)
200 return odbc_next_result($result);
206 function fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
208 if ($fetchmode == DB_FETCHMODE_DEFAULT) {
209 $fetchmode = $this->fetchmode;
211 $res = $this->fetchInto ($result, $arr, $fetchmode, $rownum);
212 if ($res !== DB_OK) {
221 function fetchInto($result, &$row, $fetchmode, $rownum=null)
224 if ($rownum !== null) {
225 $rownum++; // ODBC first row is 1
226 if (!function_exists('version_compare') || version_compare(phpversion(), "4.0.5", "lt")) {
227 $cols = odbc_fetch_into($result, $rownum, &$row);
228 } elseif (version_compare(phpversion(), '4.2.0', 'ge')) {
229 $cols = odbc_fetch_into($result, $row, $rownum);
231 $cols = odbc_fetch_into($result, $rownum, $row);
234 if (!function_exists('version_compare') || version_compare(phpversion(), "4.0.5", "lt")) {
235 $cols = odbc_fetch_into($result, &$row);
237 $cols = odbc_fetch_into($result, $row);
242 /* XXX FIXME: doesn't work with unixODBC and easysoft
243 (get corrupted $errno values)
244 if ($errno = odbc_error($this->connection)) {
245 return $this->RaiseError($errno);
249 if ($fetchmode !== DB_FETCHMODE_ORDERED) {
250 for ($i = 0; $i < count($row); $i++) {
251 $colName = odbc_field_name($result, $i+1);
252 $a[$colName] = $row[$i];
262 function freeResult($result)
264 if (is_resource($result)) {
265 // Always return true
266 return odbc_free_result($result);
268 if (!isset($this->prepare_tokens[(int)$result])) {
271 unset($this->prepare_tokens[(int)$result]);
272 unset($this->prepare_types[(int)$result]);
279 function numCols($result)
281 $cols = @odbc_num_fields($result);
283 return $this->odbcRaiseError();
289 // {{{ affectedRows()
292 * Returns the number of rows affected by a manipulative query
293 * (INSERT, DELETE, UPDATE)
294 * @return mixed int affected rows, 0 when non manip queries or
297 function affectedRows()
299 if (empty($this->manip_result)) { // In case of SELECT stms
302 $nrows = odbc_num_rows($this->manip_result);
304 return $this->odbcRaiseError();
313 * ODBC may or may not support counting rows in the result set of
316 * @param $result the odbc result resource
317 * @return the number of rows, or 0
319 function numRows($result)
321 $nrows = odbc_num_rows($result);
323 return $this->odbcRaiseError(DB_ERROR_UNSUPPORTED);
332 * Get the native error code of the last error (if any) that
333 * occured on the current connection.
337 * @return int ODBC error code
340 function errorNative()
342 if (!isset($this->connection) || !is_resource($this->connection)) {
343 return odbc_error() . ' ' . odbc_errormsg();
345 return odbc_error($this->connection) . ' ' . odbc_errormsg($this->connection);
352 * Get the next value in a sequence. We emulate sequences
353 * for odbc. Will create the sequence if it does not exist.
357 * @param $seq_name the name of the sequence
359 * @param $ondemand whether to create the sequence table on demand
362 * @return a sequence integer, or a DB error
364 function nextId($seq_name, $ondemand = true)
366 $seqname = $this->getSequenceName($seq_name);
369 $result = $this->query("update ${seqname} set id = id + 1");
370 if ($ondemand && DB::isError($result) &&
371 $result->getCode() == DB_ERROR_NOT_FOUND) {
373 $result = $this->createSequence($seq_name);
374 if (DB::isError($result)) {
377 $result = $this->query("insert into ${seqname} (id) values(0)");
383 if (DB::isError($result)) {
387 $result = $this->query("select id from ${seqname}");
388 if (DB::isError($result)) {
392 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
393 if (DB::isError($row || !$row)) {
401 // {{{ createSequence()
403 function createSequence($seq_name)
405 $seqname = $this->getSequenceName($seq_name);
406 return $this->query("CREATE TABLE ${seqname} ".
407 '(id bigint NOT NULL,'.
408 ' PRIMARY KEY(id))');
412 // {{{ dropSequence()
414 function dropSequence($seq_name)
416 $seqname = $this->getSequenceName($seq_name);
417 return $this->query("DROP TABLE ${seqname}");
423 function autoCommit($onoff = false)
425 if (!@odbc_autocommit($this->connection, $onoff)) {
426 return $this->odbcRaiseError();
436 if (!@odbc_commit($this->connection)) {
437 return $this->odbcRaiseError();
447 if (!@odbc_rollback($this->connection)) {
448 return $this->odbcRaiseError();
454 // {{{ odbcRaiseError()
456 function odbcRaiseError($errno = null)
458 if ($errno === null) {
459 $errno = $this->errorCode(odbc_error($this->connection));
461 return $this->raiseError($errno, null, null, null,
462 $this->errorNative());
466 // {{{ getSpecialQuery()
469 * Returns the query needed to get some backend info
470 * @param string $type What kind of info you want to retrieve
471 * @return string The SQL query string
473 function getSpecialQuery($type)