Adding fixes from phishy.

Added fixed for #2492

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4887 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-04-27 06:43:40 +00:00
parent 45625d65a9
commit 1d64691014

View file

@ -1,14 +1,14 @@
<?php <?php
/* SVN FILE: $Id$ */ /* SVN FILE: $Id$ */
/** /**
* Oracle layer for DBO * AdoDB layer for DBO.
* *
* Long description for file * Long description for file
* *
* PHP versions 4 and 5 * PHP versions 4 and 5
* *
* CakePHP : Rapid Development Framework <http://www.cakephp.org/> * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
* Copyright (c) 2006, Cake Software Foundation, Inc. * Copyright 2005-2007, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204 * 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104 * Las Vegas, Nevada 89104
* *
@ -16,27 +16,25 @@
* Redistributions of files must retain the above copyright notice. * Redistributions of files must retain the above copyright notice.
* *
* @filesource * @filesource
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc. * @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake * @package cake
* @subpackage cake.cake.libs.model.dbo * @subpackage cake.cake.libs.model.datasources.dbo
* @since CakePHP v 1.1.11.4041 * @since CakePHP v 1.2.0.4041
* @version $Revision$ * @version $Revision$
* @modifiedby $LastChangedBy$ * @modifiedby $LastChangedBy$
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License * @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/ */
/** /**
* Short description for class. * Short description for class.
* *
* Long description for class * Long description for class
* *
* @package cake * @package cake
* @subpackage cake.cake.libs.model.dbo * @subpackage cake.cake.libs.model.datasources.dbo
*/ */
class DboOracle extends DboSource { class DboOracle extends DboSource {
/** /**
* Enter description here... * Enter description here...
* *
@ -62,8 +60,8 @@ class DboOracle extends DboSource {
* *
* @var boolean * @var boolean
*/ */
var $_transactionStarted = false; var $__transactionStarted = false;
/** /**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
@ -82,8 +80,7 @@ class DboOracle extends DboSource {
'binary' => array('name' => 'bytea'), 'binary' => array('name' => 'bytea'),
'boolean' => array('name' => 'boolean'), 'boolean' => array('name' => 'boolean'),
'number' => array('name' => 'numeric'), 'number' => array('name' => 'numeric'),
'inet' => array('name' => 'inet') 'inet' => array('name' => 'inet'));
);
/** /**
* Enter description here... * Enter description here...
* *
@ -143,47 +140,52 @@ class DboOracle extends DboSource {
$config = $this->config; $config = $this->config;
$connect = $config['connect']; $connect = $config['connect'];
$this->connected = false; $this->connected = false;
$this->connection = $connect($config['login'], $config['password'], $config['database']); $config['charset'] = !empty($config['charset']) ? $config['charset'] : null;
$this->connection = $connect($config['login'], $config['password'], $config['database'], $config['charset']);
if ($this->connection) { if ($this->connection) {
$this->connected = true; $this->connected = true;
$this->execute('ALTER SESSION SET NLS_SORT=BINARY_CI'); if(!empty($config['nls_sort'])) {
$this->execute('ALTER SESSION SET NLS_COMP=ANSI'); $this->execute('ALTER SESSION SET NLS_SORT='.$config['nls_sort']);
}
if(!empty($config['nls_comp'])) {
$this->execute('ALTER SESSION SET NLS_COMP='.$config['nls_comp']);
}
$this->execute("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'");
} else { } else {
$this->connected = false; $this->connected = false;
} }
return $this->connected; return $this->connected;
} }
/**
/**
* Sets the encoding language of the session * Sets the encoding language of the session
* *
* @param string $lang language constant * @param string $lang language constant
* @return boolean * @return boolean
*/ */
function setEncoding($lang) { function setEncoding($lang) {
if (!$this->execute('ALTER SESSION SET NLS_LANGUAGE='.$lang)) { if(!$this->execute('ALTER SESSION SET NLS_LANGUAGE='.$lang)) {
return false; return false;
} }
return true; return true;
} }
/**
/**
* Gets the current encoding language * Gets the current encoding language
* *
* @return string language constant * @return string language constant
*/ */
function getEncoding() { function getEncoding() {
$sql = 'SELECT VALUE FROM NLS_SESSION_PARAMETERS WHERE PARAMETER=\'NLS_LANGUAGE\''; $sql = 'SELECT VALUE FROM NLS_SESSION_PARAMETERS WHERE PARAMETER=\'NLS_LANGUAGE\'';
if (!$this->execute($sql)) { if(!$this->execute($sql)) {
return false; return false;
} }
if (!$row = $this->fetchRow()) {
if(!$row = $this->fetchRow()) {
return false; return false;
} }
return $row[0]['VALUE']; return $row[0]['VALUE'];
} }
/** /**
* Disconnects from database. * Disconnects from database.
* *
@ -191,8 +193,9 @@ class DboOracle extends DboSource {
* @access public * @access public
*/ */
function disconnect() { function disconnect() {
if ($this->connection) { if($this->connection) {
return ocilogoff($this->connection); $this->connected = !ocilogoff($this->connection);
return !$this->connected;
} }
} }
/** /**
@ -204,29 +207,37 @@ class DboOracle extends DboSource {
* @access protected * @access protected
*/ */
function _scrapeSQL($sql) { function _scrapeSQL($sql) {
$sql = str_replace("\"", '', $sql); $sql = str_replace("\"", '', $sql);
$preFrom = explode('FROM', $sql); $preFrom = preg_split('/\bFROM\b/', $sql);
$preFrom = $preFrom[0]; $preFrom = $preFrom[0];
$find = array('SELECT'); $find = array('SELECT');
$replace = array(''); $replace = array('');
$fieldList = trim(str_replace($find, $replace, $preFrom)); $fieldList = trim(str_replace($find, $replace, $preFrom));
$fields = explode(', ', $fieldList); $fields = preg_split('/,\s+/', $fieldList);//explode(', ', $fieldList);
$lastTableName = '';
// clean fields of functions foreach($fields as $key => $value) {
foreach ($fields as &$value) { if($value != 'COUNT(*) AS count') {
if ($value != 'COUNT(*) AS count') { if(preg_match('/\s+(\w+(\.\w+)*)$/', $value, $matches)) {
preg_match_all('/[[:alnum:]_]+\.[[:alnum:]_]+/', $value, $matches); $fields[$key] = $matches[1];
if ($matches[0]) {
$value = $matches[0][0];
}
}
}
if(preg_match('/^(\w+\.)/', $value, $matches)) {
$fields[$key] = $matches[1] . $fields[$key];
$lastTableName = $matches[1];
}
}
/*
if(preg_match('/(([[:alnum:]_]+)\.[[:alnum:]_]+)(\s+AS\s+(\w+))?$/i', $value, $matches)) {
$fields[$key] = isset($matches[4]) ? $matches[2] . '.' . $matches[4] : $matches[1];
}
*/
}
}
$this->_map = array(); $this->_map = array();
foreach ($fields as $f) {
foreach($fields as $f) {
$e = explode('.', $f); $e = explode('.', $f);
if (count($e) > 1) { if(count($e) > 1) {
$table = $e[0]; $table = $e[0];
$field = strtolower($e[1]); $field = strtolower($e[1]);
} else { } else {
@ -235,8 +246,6 @@ class DboOracle extends DboSource {
} }
$this->_map[] = array($table, $field); $this->_map[] = array($table, $field);
} }
} }
/** /**
* Modify a SQL query to limit (and offset) the result set * Modify a SQL query to limit (and offset) the result set
@ -246,9 +255,9 @@ class DboOracle extends DboSource {
* @return modified SQL Query * @return modified SQL Query
* @access public * @access public
*/ */
function limit($limit, $offset = 0) { function limit($limit = -1, $offset = 0) {
$this->_limit = (float) $limit; $this->_limit = (int) $limit;
$this->_offset = (float) $offset; $this->_offset = (int) $offset;
} }
/** /**
* Returns number of rows in previous resultset. If no previous resultset exists, * Returns number of rows in previous resultset. If no previous resultset exists,
@ -260,7 +269,6 @@ class DboOracle extends DboSource {
function lastNumRows() { function lastNumRows() {
return $this->_numRows; return $this->_numRows;
} }
/** /**
* Executes given SQL statement. This is an overloaded method. * Executes given SQL statement. This is an overloaded method.
* *
@ -270,34 +278,38 @@ class DboOracle extends DboSource {
*/ */
function _execute($sql) { function _execute($sql) {
$this->_statementId = ociparse($this->connection, $sql); $this->_statementId = ociparse($this->connection, $sql);
if (!$this->_statementId) { if(!$this->_statementId) {
return null; return null;
} }
if ($this->_transactionStarted) {
if($this->__transactionStarted) {
$mode = OCI_DEFAULT; $mode = OCI_DEFAULT;
} else { } else {
$mode = OCI_COMMIT_ON_SUCCESS; $mode = OCI_COMMIT_ON_SUCCESS;
} }
if (!ociexecute($this->_statementId, $mode)) {
if(!ociexecute($this->_statementId, $mode)) {
return false; return false;
} }
// fetch occurs here instead of fetchResult in order to get the number of rows switch(ocistatementtype($this->_statementId)) {
switch (ocistatementtype($this->_statementId)) {
case 'DESCRIBE': case 'DESCRIBE':
case 'SELECT': case 'SELECT':
$this->_scrapeSQL($sql); $this->_scrapeSQL($sql);
break; break;
default: default:
return $this->_statementId; return $this->_statementId;
break;
} }
if ($this->_limit >= 1) {
if($this->_limit >= 1) {
ocisetprefetch($this->_statementId, $this->_limit); ocisetprefetch($this->_statementId, $this->_limit);
} else { } else {
ocisetprefetch($this->_statementId, 3000); ocisetprefetch($this->_statementId, 3000);
} }
$this->_numRows = ocifetchstatement($this->_statementId, $this->_results, $this->_offset, $this->_limit, OCI_NUM | OCI_FETCHSTATEMENT_BY_ROW); $this->_numRows = ocifetchstatement($this->_statementId, $this->_results, $this->_offset, $this->_limit, OCI_NUM | OCI_FETCHSTATEMENT_BY_ROW);
$this->_currentRow = 0; $this->_currentRow = 0;
$this->limit();
return $this->_statementId; return $this->_statementId;
} }
/** /**
@ -307,7 +319,7 @@ class DboOracle extends DboSource {
* @access public * @access public
*/ */
function fetchRow() { function fetchRow() {
if ($this->_currentRow >= $this->_numRows) { if($this->_currentRow >= $this->_numRows) {
ocifreestatement($this->_statementId); ocifreestatement($this->_statementId);
$this->_map = null; $this->_map = null;
$this->_results = null; $this->_results = null;
@ -316,9 +328,11 @@ class DboOracle extends DboSource {
return false; return false;
} }
$resultRow = array(); $resultRow = array();
foreach ($this->_results[$this->_currentRow] as $index => $field) {
foreach($this->_results[$this->_currentRow] as $index => $field) {
list($table, $column) = $this->_map[$index]; list($table, $column) = $this->_map[$index];
if (strpos($column, ' count')) {
if(strpos($column, ' count')) {
$resultRow[0]['count'] = $field; $resultRow[0]['count'] = $field;
} else { } else {
$resultRow[$table][$column] = $this->_results[$this->_currentRow][$index]; $resultRow[$table][$column] = $this->_results[$this->_currentRow][$index];
@ -336,10 +350,11 @@ class DboOracle extends DboSource {
*/ */
function sequenceExists($sequence) { function sequenceExists($sequence) {
$sql = "SELECT SEQUENCE_NAME FROM USER_SEQUENCES WHERE SEQUENCE_NAME = '$sequence'"; $sql = "SELECT SEQUENCE_NAME FROM USER_SEQUENCES WHERE SEQUENCE_NAME = '$sequence'";
if (!$this->execute($sql)) return false; if(!$this->execute($sql)) {
return false;
}
return $this->fetchRow(); return $this->fetchRow();
} }
/** /**
* Creates a database sequence * Creates a database sequence
* *
@ -351,12 +366,17 @@ class DboOracle extends DboSource {
$sql = "CREATE SEQUENCE $sequence"; $sql = "CREATE SEQUENCE $sequence";
return $this->execute($sql); return $this->execute($sql);
} }
/**
* Enter description here...
*
* @param unknown_type $table
* @return unknown
* @access public
*/
function createTrigger($table) { function createTrigger($table) {
$sql = "CREATE OR REPLACE TRIGGER pk_$table" . "_trigger BEFORE INSERT ON $table FOR EACH ROW BEGIN SELECT pk_$table.NEXTVAL INTO :NEW.ID FROM DUAL; END;"; $sql = "CREATE OR REPLACE TRIGGER pk_$table" . "_trigger BEFORE INSERT ON $table FOR EACH ROW BEGIN SELECT pk_$table.NEXTVAL INTO :NEW.ID FROM DUAL; END;";
return $this->execute($sql); return $this->execute($sql);
} }
/** /**
* Returns an array of tables in the database. If there are no tables, an error is * Returns an array of tables in the database. If there are no tables, an error is
* raised and the application exits. * raised and the application exits.
@ -366,16 +386,18 @@ class DboOracle extends DboSource {
*/ */
function listSources() { function listSources() {
$cache = parent::listSources(); $cache = parent::listSources();
if ($cache != null) { if($cache != null) {
return $cache; return $cache;
} }
$sql = 'SELECT view_name AS name FROM user_views UNION SELECT table_name AS name FROM user_tables'; $sql = 'SELECT view_name AS name FROM user_views UNION SELECT table_name AS name FROM user_tables';
if (!$this->execute($sql)) {
if(!$this->execute($sql)) {
return false; return false;
} }
$sources = array(); $sources = array();
while ($r = $this->fetchRow()) {
$sources[] = $r[0]['view_name AS name']; while($r = $this->fetchRow()) {
$sources[] = $r[0]['name'];
} }
parent::listSources($sources); parent::listSources($sources);
return $sources; return $sources;
@ -389,16 +411,19 @@ class DboOracle extends DboSource {
*/ */
function describe(&$model) { function describe(&$model) {
$cache = parent::describe($model); $cache = parent::describe($model);
if ($cache != null) {
if($cache != null) {
return $cache; return $cache;
} }
$sql = 'SELECT COLUMN_NAME, DATA_TYPE FROM user_tab_columns WHERE table_name = \''; $sql = 'SELECT COLUMN_NAME, DATA_TYPE FROM user_tab_columns WHERE table_name = \'';
$sql .= strtoupper($this->fullTableName($model)) . '\''; $sql .= strtoupper($this->fullTableName($model)) . '\'';
if (!$this->execute($sql)) {
if(!$this->execute($sql)) {
return false; return false;
} }
$fields = array(); $fields = array();
for ($i=0; $row = $this->fetchRow(); $i++) {
for($i=0; $row = $this->fetchRow(); $i++) {
$fields[$i]['name'] = strtolower($row[0]['COLUMN_NAME']); $fields[$i]['name'] = strtolower($row[0]['COLUMN_NAME']);
$fields[$i]['type'] = $this->column($row[0]['DATA_TYPE']); $fields[$i]['type'] = $this->column($row[0]['DATA_TYPE']);
} }
@ -414,16 +439,16 @@ class DboOracle extends DboSource {
* @access public * @access public
*/ */
function name($var) { function name($var) {
switch ($var) { switch($var) {
/* the acl creation script uses illegal identifiers w/o quoting. the following
quotes only the illegal identifiers */
case '_create': case '_create':
case '_read': case '_read':
case '_update': case '_update':
case '_delete': case '_delete':
return "\"$var\""; return "\"$var\"";
break;
default: default:
return $var; return $var;
break;
} }
} }
/** /**
@ -434,13 +459,8 @@ class DboOracle extends DboSource {
* (i.e. if the database/model does not support transactions). * (i.e. if the database/model does not support transactions).
*/ */
function begin() { function begin() {
//if (parent::begin($model)) { $this->__transactionStarted = true;
//if ($this->execute('BEGIN')) {
$this->_transactionStarted = true;
return true; return true;
//}
//}
return false;
} }
/** /**
* Rollback a transaction * Rollback a transaction
@ -451,10 +471,7 @@ class DboOracle extends DboSource {
* or a transaction has not started). * or a transaction has not started).
*/ */
function rollback() { function rollback() {
//if (parent::rollback($model)) {
return ocirollback($this->connection); return ocirollback($this->connection);
//}
//return false;
} }
/** /**
* Commit a transaction * Commit a transaction
@ -465,11 +482,8 @@ class DboOracle extends DboSource {
* or a transaction has not started). * or a transaction has not started).
*/ */
function commit() { function commit() {
//if (parent::commit($model)) { $this->__transactionStarted = false;
$this->_transactionStarted = false;
return ocicommit($this->connection); return ocicommit($this->connection);
//}
//return false;
} }
/** /**
* Converts database-layer column types to basic types * Converts database-layer column types to basic types
@ -479,44 +493,42 @@ class DboOracle extends DboSource {
* @access public * @access public
*/ */
function column($real) { function column($real) {
if (is_array($real)) { if(is_array($real)) {
$col = $real['name']; $col = $real['name'];
if (isset($real['limit'])) { if(isset($real['limit'])) {
$col .= '('.$real['limit'].')'; $col .= '('.$real['limit'].')';
} }
return $col; return $col;
} else { } else {
$real = strtolower($real); $real = strtolower($real);
} }
$col = r(')', '', $real); $col = r(')', '', $real);
$limit = null; $limit = null;
@list($col, $limit) = explode('(', $col); @list($col, $limit) = explode('(', $col);
if(in_array($col, array('date', 'timestamp'))) {
if (in_array($col, array('date', 'timestamp'))) {
return $col; return $col;
} }
if (strpos($col, 'number') !== false) { if(strpos($col, 'number') !== false) {
return 'integer'; return 'integer';
} }
if (strpos($col, 'integer') !== false) { if(strpos($col, 'integer') !== false) {
return 'integer'; return 'integer';
} }
if (strpos($col, 'char') !== false) { if(strpos($col, 'char') !== false) {
return 'string'; return 'string';
} }
if (strpos($col, 'text') !== false) { if(strpos($col, 'text') !== false) {
return 'text'; return 'text';
} }
if (strpos($col, 'blob') !== false) { if(strpos($col, 'blob') !== false) {
return 'binary'; return 'binary';
} }
if (in_array($col, array('float', 'double', 'decimal'))) { if(in_array($col, array('float', 'double', 'decimal'))) {
return 'float'; return 'float';
} }
if ($col == 'boolean') { if($col == 'boolean') {
return $col; return $col;
} }
return 'text'; return 'text';
@ -529,17 +541,17 @@ class DboOracle extends DboSource {
* @access public * @access public
*/ */
function value($data, $column_type = null) { function value($data, $column_type = null) {
// this can also be accomplished through an Oracle NLS parameter switch($column_type) {
switch ($column_type) {
case 'date': case 'date':
$date = date('Y-m-d H:i:s', strtotime($data)); $date = date('Y-m-d H:i:s', strtotime($data));
return "TO_DATE('$date', 'YYYY-MM-DD HH24:MI:SS')"; return "TO_DATE('$date', 'YYYY-MM-DD HH24:MI:SS')";
break;
default: default:
$data2 = str_replace("'", "''", $data); $data2 = str_replace("'", "''", $data);
return "'".$data2."'"; return "'".$data2."'";
break;
} }
} }
/** /**
* Returns the ID generated from the previous INSERT operation. * Returns the ID generated from the previous INSERT operation.
* *
@ -548,17 +560,18 @@ class DboOracle extends DboSource {
* @access public * @access public
*/ */
function lastInsertId($source) { function lastInsertId($source) {
$sequence = (!empty($this->sequence)) ? $this->sequence : 'pk_'.$source; $sequence = (!empty($this->sequence)) ? $this->sequence : $source . '_seq';
$sql = "SELECT $sequence.currval FROM dual"; $sql = "SELECT $sequence.currval FROM dual";
if (!$this->execute($sql)) {
if(!$this->execute($sql)) {
return false; return false;
} }
while ($row = $this->fetchRow()) {
while($row = $this->fetchRow()) {
return $row[$sequence]['currval']; return $row[$sequence]['currval'];
} }
return false; return false;
} }
/** /**
* Returns a formatted error message from previous database operation. * Returns a formatted error message from previous database operation.
* *
@ -567,7 +580,8 @@ class DboOracle extends DboSource {
*/ */
function lastError() { function lastError() {
$errors = ocierror(); $errors = ocierror();
if( ($errors != null) && (isset($errors["message"])) ) {
if(($errors != null) && (isset($errors["message"]))) {
return($errors["message"]); return($errors["message"]);
} }
return null; return null;