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