1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183 |
- <?php
- require_once 'common.php';
- class DB_oci8 extends DB_common
- {
-
-
- public $phptype = 'oci8';
-
- public $dbsyntax = 'oci8';
-
- public $features = array(
- 'limit' => 'alter',
- 'new_link' => '5.0.0',
- 'numrows' => 'subquery',
- 'pconnect' => true,
- 'prepare' => true,
- 'ssl' => false,
- 'transactions' => true,
- );
-
- public $errorcode_map = array(
- 1 => DB_ERROR_CONSTRAINT,
- 900 => DB_ERROR_SYNTAX,
- 904 => DB_ERROR_NOSUCHFIELD,
- 913 => DB_ERROR_VALUE_COUNT_ON_ROW,
- 921 => DB_ERROR_SYNTAX,
- 923 => DB_ERROR_SYNTAX,
- 942 => DB_ERROR_NOSUCHTABLE,
- 955 => DB_ERROR_ALREADY_EXISTS,
- 1400 => DB_ERROR_CONSTRAINT_NOT_NULL,
- 1401 => DB_ERROR_INVALID,
- 1407 => DB_ERROR_CONSTRAINT_NOT_NULL,
- 1418 => DB_ERROR_NOT_FOUND,
- 1476 => DB_ERROR_DIVZERO,
- 1722 => DB_ERROR_INVALID_NUMBER,
- 2289 => DB_ERROR_NOSUCHTABLE,
- 2291 => DB_ERROR_CONSTRAINT,
- 2292 => DB_ERROR_CONSTRAINT,
- 2449 => DB_ERROR_CONSTRAINT,
- 12899 => DB_ERROR_INVALID,
- );
-
- public $connection;
-
- public $dsn = array();
-
- public $autocommit = true;
-
- public $_data = array();
-
- public $last_stmt;
-
- public $manip_query = array();
-
- public $_prepared_queries = array();
-
-
-
- public function __construct()
- {
- parent::__construct();
- }
-
-
-
- public function connect($dsn, $persistent = false)
- {
- if (!PEAR::loadExtension('oci8')) {
- return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
- }
- $this->dsn = $dsn;
- if ($dsn['dbsyntax']) {
- $this->dbsyntax = $dsn['dbsyntax'];
- }
-
- if (empty($dsn['database']) && !empty($dsn['hostspec'])) {
- $db = $dsn['hostspec'];
- } else {
- $db = $dsn['database'];
- }
- if (function_exists('oci_connect')) {
- if (isset($dsn['new_link'])
- && ($dsn['new_link'] == 'true' || $dsn['new_link'] === true)) {
- $connect_function = 'oci_new_connect';
- } else {
- $connect_function = $persistent ? 'oci_pconnect'
- : 'oci_connect';
- }
- if (isset($this->dsn['port']) && $this->dsn['port']) {
- $db = '//' . $db . ':' . $this->dsn['port'];
- }
- $char = empty($dsn['charset']) ? null : $dsn['charset'];
- $this->connection = @$connect_function(
- $dsn['username'],
- $dsn['password'],
- $db,
- $char
- );
- $error = OCIError();
- if (!empty($error) && $error['code'] == 12541) {
-
- $this->connection = @$connect_function(
- $dsn['username'],
- $dsn['password'],
- null,
- $char
- );
- }
- } else {
- $connect_function = $persistent ? 'OCIPLogon' : 'OCILogon';
- if ($db) {
- $this->connection = @$connect_function(
- $dsn['username'],
- $dsn['password'],
- $db
- );
- } elseif ($dsn['username'] || $dsn['password']) {
- $this->connection = @$connect_function(
- $dsn['username'],
- $dsn['password']
- );
- }
- }
- if (!$this->connection) {
- $error = OCIError();
- $error = (is_array($error)) ? $error['message'] : null;
- return $this->raiseError(
- DB_ERROR_CONNECT_FAILED,
- null,
- null,
- null,
- $error
- );
- }
- return DB_OK;
- }
-
-
-
- public function disconnect()
- {
- if (function_exists('oci_close')) {
- $ret = @oci_close($this->connection);
- } else {
- $ret = @OCILogOff($this->connection);
- }
- $this->connection = null;
- return $ret;
- }
-
-
-
- public function simpleQuery($query)
- {
- $this->_data = array();
- $this->last_parameters = array();
- $this->last_query = $query;
- $query = $this->modifyQuery($query);
- $result = @OCIParse($this->connection, $query);
- if (!$result) {
- return $this->oci8RaiseError();
- }
- if ($this->autocommit) {
- $success = @OCIExecute($result, OCI_COMMIT_ON_SUCCESS);
- } else {
- $success = @OCIExecute($result, OCI_DEFAULT);
- }
- if (!$success) {
- return $this->oci8RaiseError($result);
- }
- $this->last_stmt = $result;
- if ($this->_checkManip($query)) {
- return DB_OK;
- } else {
- @ocisetprefetch($result, $this->options['result_buffering']);
- return $result;
- }
- }
-
-
-
- public function modifyQuery($query)
- {
- if (preg_match('/^\s*SELECT/i', $query) &&
- !preg_match('/\sFROM\s/i', $query)) {
- $query .= ' FROM dual';
- }
- return $query;
- }
-
-
-
- public function oci8RaiseError($errno = null)
- {
- if ($errno === null) {
- $error = @OCIError($this->connection);
- return $this->raiseError(
- $this->errorCode($error['code']),
- null,
- null,
- null,
- $error['message']
- );
- } elseif (is_resource($errno)) {
- $error = @OCIError($errno);
- return $this->raiseError(
- $this->errorCode($error['code']),
- null,
- null,
- null,
- $error['message']
- );
- }
- return $this->raiseError($this->errorCode($errno));
- }
-
-
-
- public function nextResult($result)
- {
- return false;
- }
-
- public function fetchInto($result, &$arr, $fetchmode, $rownum = null)
- {
- if ($rownum !== null) {
- return $this->raiseError(DB_ERROR_NOT_CAPABLE);
- }
- if ($fetchmode & DB_FETCHMODE_ASSOC) {
- $moredata = @OCIFetchInto($result, $arr, OCI_ASSOC + OCI_RETURN_NULLS + OCI_RETURN_LOBS);
- if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE &&
- $moredata) {
- $arr = array_change_key_case($arr, CASE_LOWER);
- }
- } else {
- $moredata = OCIFetchInto($result, $arr, OCI_RETURN_NULLS + OCI_RETURN_LOBS);
- }
- if (!$moredata) {
- return null;
- }
- 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) ? OCIFreeStatement($result) : false;
- }
-
-
-
- public function freePrepared($stmt, $free_resource = true)
- {
- if (!is_resource($stmt)) {
- return false;
- }
- if ($free_resource) {
- @ocifreestatement($stmt);
- }
- if (isset($this->prepare_types[(int)$stmt])) {
- unset($this->prepare_types[(int)$stmt]);
- unset($this->manip_query[(int)$stmt]);
- unset($this->_prepared_queries[(int)$stmt]);
- } else {
- return false;
- }
- return true;
- }
-
-
-
- public function numRows($result)
- {
-
- if ($this->options['portability'] & DB_PORTABILITY_NUMROWS &&
- $result === $this->last_stmt) {
- $countquery = 'SELECT COUNT(*) FROM (' . $this->last_query . ')';
- $save_query = $this->last_query;
- $save_stmt = $this->last_stmt;
- $count = $this->query($countquery);
-
- $this->last_query = $save_query;
- $this->last_stmt = $save_stmt;
- if (DB::isError($count) ||
- DB::isError($row = $count->fetchRow(DB_FETCHMODE_ORDERED))) {
- return $this->raiseError(DB_ERROR_NOT_CAPABLE);
- }
- return $row[0];
- }
- return $this->raiseError(DB_ERROR_NOT_CAPABLE);
- }
-
-
-
- public function numCols($result)
- {
- $cols = @OCINumCols($result);
- if (!$cols) {
- return $this->oci8RaiseError($result);
- }
- return $cols;
- }
-
-
-
- public function autoCommit($onoff = false)
- {
- $this->autocommit = (bool)$onoff;;
- return DB_OK;
- }
-
-
-
- public function commit()
- {
- $result = @OCICommit($this->connection);
- if (!$result) {
- return $this->oci8RaiseError();
- }
- return DB_OK;
- }
-
-
-
- public function rollback()
- {
- $result = @OCIRollback($this->connection);
- if (!$result) {
- return $this->oci8RaiseError();
- }
- return DB_OK;
- }
-
-
-
- public function affectedRows()
- {
- if ($this->last_stmt === false) {
- return $this->oci8RaiseError();
- }
- $result = @OCIRowCount($this->last_stmt);
- if ($result === false) {
- return $this->oci8RaiseError($this->last_stmt);
- }
- return $result;
- }
-
-
-
- public function modifyLimitQuery($query, $from, $count, $params = array())
- {
-
-
- if (count($params)) {
- $result = $this->prepare("SELECT * FROM ($query) "
- . 'WHERE NULL = NULL');
- $tmp = $this->execute($result, $params);
- } else {
- $q_fields = "SELECT * FROM ($query) WHERE NULL = NULL";
- if (!$result = @OCIParse($this->connection, $q_fields)) {
- $this->last_query = $q_fields;
- return $this->oci8RaiseError();
- }
- if (!@OCIExecute($result, OCI_DEFAULT)) {
- $this->last_query = $q_fields;
- return $this->oci8RaiseError($result);
- }
- }
- $ncols = OCINumCols($result);
- $cols = array();
- for ($i = 1; $i <= $ncols; $i++) {
- $cols[] = '"' . OCIColumnName($result, $i) . '"';
- }
- $fields = implode(', ', $cols);
-
-
-
-
-
-
-
-
-
- $query = "SELECT $fields FROM" .
- " (SELECT rownum as linenum, $fields FROM" .
- " ($query)" .
- ' WHERE rownum <= ' . ($from + $count) .
- ') WHERE linenum >= ' . ++$from;
- return $query;
- }
-
-
-
- public function prepare($query)
- {
- $tokens = preg_split(
- '/((?<!\\\)[&?!])/',
- $query,
- -1,
- PREG_SPLIT_DELIM_CAPTURE
- );
- $binds = count($tokens) - 1;
- $token = 0;
- $types = array();
- $newquery = '';
- foreach ($tokens as $key => $val) {
- switch ($val) {
- case '?':
- $types[$token++] = DB_PARAM_SCALAR;
- unset($tokens[$key]);
- break;
- case '&':
- $types[$token++] = DB_PARAM_OPAQUE;
- unset($tokens[$key]);
- break;
- case '!':
- $types[$token++] = DB_PARAM_MISC;
- unset($tokens[$key]);
- break;
- default:
- $tokens[$key] = preg_replace('/\\\([&?!])/', "\\1", $val);
- if ($key != $binds) {
- $newquery .= $tokens[$key] . ':bind' . $token;
- } else {
- $newquery .= $tokens[$key];
- }
- }
- }
- $this->last_query = $query;
- $newquery = $this->modifyQuery($newquery);
- if (!$stmt = @OCIParse($this->connection, $newquery)) {
- return $this->oci8RaiseError();
- }
- $this->prepare_types[(int)$stmt] = $types;
- $this->manip_query[(int)$stmt] = DB::isManip($query);
- $this->_prepared_queries[(int)$stmt] = $newquery;
- return $stmt;
- }
-
-
-
- public function &execute($stmt, $data = array())
- {
- $data = (array)$data;
- $this->last_parameters = $data;
- $this->last_query = $this->_prepared_queries[(int)$stmt];
- $this->_data = $data;
- $types = $this->prepare_types[(int)$stmt];
- if (count($types) != count($data)) {
- $tmp = $this->raiseError(DB_ERROR_MISMATCH);
- return $tmp;
- }
- $i = 0;
- foreach ($data as $key => $value) {
- if ($types[$i] == DB_PARAM_MISC) {
-
- $data[$key] = preg_replace("/^'(.*)'$/", "\\1", $data[$key]);
- $data[$key] = str_replace("''", "'", $data[$key]);
- } elseif ($types[$i] == DB_PARAM_OPAQUE) {
- $fp = @fopen($data[$key], 'rb');
- if (!$fp) {
- $tmp = $this->raiseError(DB_ERROR_ACCESS_VIOLATION);
- return $tmp;
- }
- $data[$key] = fread($fp, filesize($data[$key]));
- fclose($fp);
- } elseif ($types[$i] == DB_PARAM_SCALAR) {
-
-
- if (is_float($data[$key])) {
- $data[$key] = $this->quoteFloat($data[$key]);
- }
- }
- if (!@OCIBindByName($stmt, ':bind' . $i, $data[$key], -1)) {
- $tmp = $this->oci8RaiseError($stmt);
- return $tmp;
- }
- $this->last_query = preg_replace(
- "/:bind$i(?!\d)/",
- $this->quoteSmart($data[$key]),
- $this->last_query,
- 1
- );
- $i++;
- }
- if ($this->autocommit) {
- $success = @OCIExecute($stmt, OCI_COMMIT_ON_SUCCESS);
- } else {
- $success = @OCIExecute($stmt, OCI_DEFAULT);
- }
- if (!$success) {
- $tmp = $this->oci8RaiseError($stmt);
- return $tmp;
- }
- $this->last_stmt = $stmt;
- if ($this->manip_query[(int)$stmt] || $this->_next_query_manip) {
- $this->_last_query_manip = true;
- $this->_next_query_manip = false;
- $tmp = DB_OK;
- } else {
- $this->_last_query_manip = false;
- @ocisetprefetch($stmt, $this->options['result_buffering']);
- $tmp = new DB_result($this, $stmt);
- }
- return $tmp;
- }
-
- public function quoteFloat($float)
- {
- return $this->escapeSimple(str_replace(',', '.', strval(floatval($float))));
- }
-
-
-
- public function nextId($seq_name, $ondemand = true)
- {
- $seqname = $this->getSequenceName($seq_name);
- $repeat = 0;
- do {
- $this->expectError(DB_ERROR_NOSUCHTABLE);
- $result = $this->query("SELECT ${seqname}.nextval FROM dual");
- $this->popExpect();
- if ($ondemand && DB::isError($result) &&
- $result->getCode() == DB_ERROR_NOSUCHTABLE) {
- $repeat = 1;
- $result = $this->createSequence($seq_name);
- if (DB::isError($result)) {
- return $this->raiseError($result);
- }
- } else {
- $repeat = 0;
- }
- } while ($repeat);
- if (DB::isError($result)) {
- return $this->raiseError($result);
- }
- $arr = $result->fetchRow(DB_FETCHMODE_ORDERED);
- return $arr[0];
- }
-
-
-
- public function createSequence($seq_name)
- {
- return $this->query('CREATE SEQUENCE '
- . $this->getSequenceName($seq_name));
- }
-
-
-
- public function dropSequence($seq_name)
- {
- return $this->query('DROP SEQUENCE '
- . $this->getSequenceName($seq_name));
- }
-
-
-
- public function errorNative()
- {
- if (is_resource($this->last_stmt)) {
- $error = @OCIError($this->last_stmt);
- } else {
- $error = @OCIError($this->connection);
- }
- if (is_array($error)) {
- return $error['code'];
- }
- return false;
- }
-
-
-
- public function tableInfo($result, $mode = null)
- {
- if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
- $case_func = 'strtolower';
- } else {
- $case_func = 'strval';
- }
- $res = array();
- if (is_string($result)) {
-
- $result = strtoupper($result);
- $q_fields = 'SELECT column_name, data_type, data_length, '
- . 'nullable '
- . 'FROM user_tab_columns '
- . "WHERE table_name='$result' ORDER BY column_id";
- $this->last_query = $q_fields;
- if (!$stmt = @OCIParse($this->connection, $q_fields)) {
- return $this->oci8RaiseError(DB_ERROR_NEED_MORE_DATA);
- }
- if (!@OCIExecute($stmt, OCI_DEFAULT)) {
- return $this->oci8RaiseError($stmt);
- }
- $i = 0;
- while (@OCIFetch($stmt)) {
- $res[$i] = array(
- 'table' => $case_func($result),
- 'name' => $case_func(@OCIResult($stmt, 1)),
- 'type' => @OCIResult($stmt, 2),
- 'len' => @OCIResult($stmt, 3),
- 'flags' => (@OCIResult($stmt, 4) == 'N') ? 'not_null' : '',
- );
- 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;
- }
- $i++;
- }
- if ($mode) {
- $res['num_fields'] = $i;
- }
- @OCIFreeStatement($stmt);
- } else {
- if (isset($result->result)) {
-
- $result = $result->result;
- }
- $res = array();
- if ($result === $this->last_stmt) {
- $count = @OCINumCols($result);
- if ($mode) {
- $res['num_fields'] = $count;
- }
- for ($i = 0; $i < $count; $i++) {
- $res[$i] = array(
- 'table' => '',
- 'name' => $case_func(@OCIColumnName($result, $i + 1)),
- 'type' => @OCIColumnType($result, $i + 1),
- 'len' => @OCIColumnSize($result, $i + 1),
- '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;
- }
- }
- } else {
- return $this->raiseError(DB_ERROR_NOT_CAPABLE);
- }
- }
- return $res;
- }
-
-
-
- public function getSpecialQuery($type)
- {
- switch ($type) {
- case 'tables':
- return 'SELECT table_name FROM user_tables';
- case 'synonyms':
- return 'SELECT synonym_name FROM user_synonyms';
- case 'views':
- return 'SELECT view_name FROM user_views';
- default:
- return null;
- }
- }
-
- }
|