123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890 |
- <?php
- require_once 'common.php';
- class DB_odbc extends DB_common
- {
-
-
- public $phptype = 'odbc';
-
- public $dbsyntax = 'sql92';
-
- public $features = array(
- 'limit' => 'emulate',
- 'new_link' => false,
- 'numrows' => true,
- 'pconnect' => true,
- 'prepare' => false,
- 'ssl' => false,
- 'transactions' => false,
- );
-
- public $errorcode_map = array(
- '01004' => DB_ERROR_TRUNCATED,
- '07001' => DB_ERROR_MISMATCH,
- '21S01' => DB_ERROR_VALUE_COUNT_ON_ROW,
- '21S02' => DB_ERROR_MISMATCH,
- '22001' => DB_ERROR_INVALID,
- '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,
- '23504' => 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,
- 'S1009' => DB_ERROR_INVALID,
- 'S1090' => DB_ERROR_INVALID,
- 'S1C00' => DB_ERROR_NOT_CAPABLE,
- );
-
- public $connection;
-
- public $dsn = array();
-
- public $affected = 0;
-
-
-
- public function __construct()
- {
- parent::__construct();
- }
-
-
-
- public function connect($dsn, $persistent = false)
- {
- if (!PEAR::loadExtension('odbc')) {
- return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
- }
- $this->dsn = $dsn;
- if ($dsn['dbsyntax']) {
- $this->dbsyntax = $dsn['dbsyntax'];
- }
- switch ($this->dbsyntax) {
- case 'access':
- case 'db2':
- case 'solid':
- $this->features['transactions'] = true;
- break;
- case 'navision':
- $this->features['limit'] = false;
- }
-
- if ($dsn['database']) {
- $odbcdsn = $dsn['database'];
- } elseif ($dsn['hostspec']) {
- $odbcdsn = $dsn['hostspec'];
- } else {
- $odbcdsn = 'localhost';
- }
- $connect_function = $persistent ? 'odbc_pconnect' : 'odbc_connect';
- if (empty($dsn['cursor'])) {
- $this->connection = @$connect_function(
- $odbcdsn,
- $dsn['username'],
- $dsn['password']
- );
- } else {
- $this->connection = @$connect_function(
- $odbcdsn,
- $dsn['username'],
- $dsn['password'],
- $dsn['cursor']
- );
- }
- if (!is_resource($this->connection)) {
- return $this->raiseError(
- DB_ERROR_CONNECT_FAILED,
- null,
- null,
- null,
- $this->errorNative()
- );
- }
- return DB_OK;
- }
-
-
-
- public function errorNative()
- {
- if (!is_resource($this->connection)) {
- return @odbc_error() . ' ' . @odbc_errormsg();
- }
- return @odbc_error($this->connection) . ' ' . @odbc_errormsg($this->connection);
- }
-
-
-
- public function disconnect()
- {
- $err = @odbc_close($this->connection);
- $this->connection = null;
- return $err;
- }
-
-
-
- public function simpleQuery($query)
- {
- $this->last_query = $query;
- $query = $this->modifyQuery($query);
- $result = @odbc_exec($this->connection, $query);
- if (!$result) {
- return $this->odbcRaiseError();
- }
-
-
- if ($this->_checkManip($query)) {
- $this->affected = $result;
- return DB_OK;
- }
- $this->affected = 0;
- return $result;
- }
-
-
-
- public function odbcRaiseError($errno = null)
- {
- if ($errno === null) {
- switch ($this->dbsyntax) {
- case 'access':
- if ($this->options['portability'] & DB_PORTABILITY_ERRORS) {
- $this->errorcode_map['07001'] = DB_ERROR_NOSUCHFIELD;
- } else {
-
- $this->errorcode_map['07001'] = DB_ERROR_MISMATCH;
- }
- $native_code = odbc_error($this->connection);
-
- if ($native_code == 'S1000') {
- $errormsg = odbc_errormsg($this->connection);
- static $error_regexps;
- if (!isset($error_regexps)) {
- $error_regexps = array(
- '/includes related records.$/i' => DB_ERROR_CONSTRAINT,
- '/cannot contain a Null value/i' => DB_ERROR_CONSTRAINT_NOT_NULL,
- );
- }
- foreach ($error_regexps as $regexp => $code) {
- if (preg_match($regexp, $errormsg)) {
- return $this->raiseError(
- $code,
- null,
- null,
- null,
- $native_code . ' ' . $errormsg
- );
- }
- }
- $errno = DB_ERROR;
- } else {
- $errno = $this->errorCode($native_code);
- }
- break;
- default:
- $errno = $this->errorCode(odbc_error($this->connection));
- }
- }
- return $this->raiseError(
- $errno,
- null,
- null,
- null,
- $this->errorNative()
- );
- }
-
-
-
- public function nextResult($result)
- {
- return @odbc_next_result($result);
- }
-
-
-
- public function fetchInto($result, &$arr, $fetchmode, $rownum = null)
- {
- $arr = array();
- if ($rownum !== null) {
- $rownum++;
- 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) {
- 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;
- }
-
-
-
- public function freeResult($result)
- {
- return is_resource($result) ? odbc_free_result($result) : false;
- }
-
-
-
- public function numCols($result)
- {
- $cols = @odbc_num_fields($result);
- if (!$cols) {
- return $this->odbcRaiseError();
- }
- return $cols;
- }
-
-
-
- public function affectedRows()
- {
- if (empty($this->affected)) {
- return 0;
- }
- $nrows = @odbc_num_rows($this->affected);
- if ($nrows == -1) {
- return $this->odbcRaiseError();
- }
- return $nrows;
- }
-
-
-
- public function numRows($result)
- {
- $nrows = @odbc_num_rows($result);
- if ($nrows == -1) {
- return $this->odbcRaiseError(DB_ERROR_UNSUPPORTED);
- }
- if ($nrows === false) {
- return $this->odbcRaiseError();
- }
- return $nrows;
- }
-
- public function quoteIdentifier($str)
- {
- switch ($this->dsn['dbsyntax']) {
- case 'access':
- return '[' . $str . ']';
- case 'mssql':
- case 'sybase':
- return '[' . str_replace(']', ']]', $str) . ']';
- case 'mysql':
- case 'mysqli':
- return '`' . $str . '`';
- default:
- return '"' . str_replace('"', '""', $str) . '"';
- }
- }
-
-
-
- public function nextId($seq_name, $ondemand = true)
- {
- $seqname = $this->getSequenceName($seq_name);
- $repeat = 0;
- do {
- $this->pushErrorHandling(PEAR_ERROR_RETURN);
- $result = $this->query("update ${seqname} set id = id + 1");
- $this->popErrorHandling();
- if ($ondemand && DB::isError($result) &&
- $result->getCode() == DB_ERROR_NOSUCHTABLE) {
- $repeat = 1;
- $this->pushErrorHandling(PEAR_ERROR_RETURN);
- $result = $this->createSequence($seq_name);
- $this->popErrorHandling();
- if (DB::isError($result)) {
- return $this->raiseError($result);
- }
- $result = $this->query("insert into ${seqname} (id) values(0)");
- } else {
- $repeat = 0;
- }
- } while ($repeat);
- if (DB::isError($result)) {
- return $this->raiseError($result);
- }
- $result = $this->query("select id from ${seqname}");
- if (DB::isError($result)) {
- return $result;
- }
- $row = $result->fetchRow(DB_FETCHMODE_ORDERED);
- if (DB::isError($row || !$row)) {
- return $row;
- }
- return $row[0];
- }
-
-
-
- public function createSequence($seq_name)
- {
- return $this->query('CREATE TABLE '
- . $this->getSequenceName($seq_name)
- . ' (id integer NOT NULL,'
- . ' PRIMARY KEY(id))');
- }
-
-
-
- public function dropSequence($seq_name)
- {
- return $this->query('DROP TABLE ' . $this->getSequenceName($seq_name));
- }
-
-
-
- public function autoCommit($onoff = false)
- {
- if (!@odbc_autocommit($this->connection, $onoff)) {
- return $this->odbcRaiseError();
- }
- return DB_OK;
- }
-
-
-
- public function commit()
- {
- if (!@odbc_commit($this->connection)) {
- return $this->odbcRaiseError();
- }
- return DB_OK;
- }
-
-
-
- public function rollback()
- {
- if (!@odbc_rollback($this->connection)) {
- return $this->odbcRaiseError();
- }
- return DB_OK;
- }
-
-
-
- public function tableInfo($result, $mode = null)
- {
- if (is_string($result)) {
-
- $id = @odbc_exec($this->connection, "SELECT * FROM $result");
- if (!$id) {
- return $this->odbcRaiseError();
- }
- $got_string = true;
- } elseif (isset($result->result)) {
-
- $id = $result->result;
- $got_string = false;
- } else {
-
- $id = $result;
- $got_string = false;
- }
- if (!is_resource($id)) {
- return $this->odbcRaiseError(DB_ERROR_NEED_MORE_DATA);
- }
- if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
- $case_func = 'strtolower';
- } else {
- $case_func = 'strval';
- }
- $count = @odbc_num_fields($id);
- $res = array();
- if ($mode) {
- $res['num_fields'] = $count;
- }
- for ($i = 0; $i < $count; $i++) {
- $col = $i + 1;
- $res[$i] = array(
- 'table' => $got_string ? $case_func($result) : '',
- 'name' => $case_func(@odbc_field_name($id, $col)),
- 'type' => @odbc_field_type($id, $col),
- 'len' => @odbc_field_len($id, $col),
- 'flags' => '',
- );
- if ($mode & DB_TABLEINFO_ORDER) {
- $res['order'][$res[$i]['name']] = $i;
- }
- if ($mode & DB_TABLEINFO_ORDERTABLE) {
- $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
- }
- }
-
- if ($got_string) {
- @odbc_free_result($id);
- }
- return $res;
- }
-
-
-
- public function getSpecialQuery($type)
- {
- switch ($type) {
- case 'databases':
- if (!function_exists('odbc_data_source')) {
- return null;
- }
- $res = @odbc_data_source($this->connection, SQL_FETCH_FIRST);
- if (is_array($res)) {
- $out = array($res['server']);
- while ($res = @odbc_data_source(
- $this->connection,
- SQL_FETCH_NEXT
- )) {
- $out[] = $res['server'];
- }
- return $out;
- } else {
- return $this->odbcRaiseError();
- }
- break;
- case 'tables':
- case 'schema.tables':
- $keep = 'TABLE';
- break;
- case 'views':
- $keep = 'VIEW';
- break;
- default:
- return null;
- }
-
- $res = @odbc_tables($this->connection);
- if (!$res) {
- return $this->odbcRaiseError();
- }
- $out = array();
- while ($row = odbc_fetch_array($res)) {
- if ($row['TABLE_TYPE'] != $keep) {
- continue;
- }
- if ($type == 'schema.tables') {
- $out[] = $row['TABLE_SCHEM'] . '.' . $row['TABLE_NAME'];
- } else {
- $out[] = $row['TABLE_NAME'];
- }
- }
- return $out;
- }
-
- }
|