Adding phishy's Oracle patch, fixing Sybase queries and column defs

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4620 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-03-16 14:23:54 +00:00
parent 00c3711dd4
commit de7423e4b6
4 changed files with 135 additions and 102 deletions

View file

@ -419,7 +419,10 @@ class DboMysql extends DboSource {
function length($real) { function length($real) {
$col = r(array(')', 'unsigned'), '', $real); $col = r(array(')', 'unsigned'), '', $real);
$limit = null; $limit = null;
@list($col, $limit) = explode('(', $col);
if (strpos($col, '(') !== false) {
list($col, $limit) = explode('(', $col);
}
if ($limit != null) { if ($limit != null) {
return intval($limit); return intval($limit);

View file

@ -7,8 +7,8 @@
* *
* PHP versions 4 and 5 * PHP versions 4 and 5
* *
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> * CakePHP : Rapid Development Framework <http://www.cakephp.org/>
* Copyright 2005-2007, Cake Software Foundation, Inc. * Copyright (c) 2006, 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,20 +16,17 @@
* Redistributions of files must retain the above copyright notice. * Redistributions of files must retain the above copyright notice.
* *
* @filesource * @filesource
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc. * @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake * @package cake
* @subpackage cake.cake.libs.model.dbo * @subpackage cake.cake.libs.model.dbo
* @since CakePHP(tm) v 1.1.11.4041 * @since CakePHP v 1.1.11.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
*/ */
/**
* Include DBO.
*/
uses('model'.DS.'datasources'.DS.'dbo_source');
/** /**
* Short description for class. * Short description for class.
* *
@ -39,90 +36,97 @@ uses('model'.DS.'datasources'.DS.'dbo_source');
* @subpackage cake.cake.libs.model.dbo * @subpackage cake.cake.libs.model.dbo
*/ */
class DboOracle extends DboSource { class DboOracle extends DboSource {
/**
/**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
* @access public * @access public
*/ */
var $config; var $config;
/** /**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
* @access public * @access public
*/ */
var $alias = ''; var $alias = '';
/**
/** * The name of the model's sequence
* The name of the model's sequence *
* * @var unknown_type
* @var unknown_type */
*/
var $sequence = ''; var $sequence = '';
/**
* Transaction in progress flag
*
* @var boolean
*/
var $_transactionStarted = false;
/** /**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
* @access public * @access public
*/ */
var $columns = array('primary_key' => array('name' => 'number NOT NULL'), var $columns = array(
'string' => array('name' => 'varchar2', 'limit' => '255'), 'primary_key' => array('name' => 'number NOT NULL'),
'text' => array('name' => 'varchar2'), 'string' => array('name' => 'varchar2', 'limit' => '255'),
'integer' => array('name' => 'numeric'), 'text' => array('name' => 'varchar2'),
'float' => array('name' => 'float'), 'integer' => array('name' => 'numeric'),
'datetime' => array('name' => 'date'), 'float' => array('name' => 'float'),
'timestamp' => array('name' => 'date'), 'datetime' => array('name' => 'date'),
'time' => array('name' => 'date'), 'timestamp' => array('name' => 'date'),
'date' => array('name' => 'date'), 'time' => array('name' => 'date'),
'binary' => array('name' => 'bytea'), 'date' => array('name' => 'date'),
'boolean' => array('name' => 'boolean'), 'binary' => array('name' => 'bytea'),
'number' => array('name' => 'numeric'), 'boolean' => array('name' => 'boolean'),
'inet' => array('name' => 'inet')); 'number' => array('name' => 'numeric'),
/** 'inet' => array('name' => 'inet')
);
/**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
* @access protected * @access protected
*/ */
var $connection; var $connection;
/** /**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
* @access protected * @access protected
*/ */
var $_limit = -1; var $_limit = -1;
/** /**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
* @access protected * @access protected
*/ */
var $_offset = 0; var $_offset = 0;
/** /**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
* @access protected * @access protected
*/ */
var $_map; var $_map;
/** /**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
* @access protected * @access protected
*/ */
var $_currentRow; var $_currentRow;
/** /**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
* @access protected * @access protected
*/ */
var $_numRows; var $_numRows;
/** /**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
@ -151,6 +155,35 @@ class DboOracle extends DboSource {
return $this->connected; 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)) {
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)) {
return false;
}
if (!$row = $this->fetchRow()) {
return false;
}
return $row[0]['VALUE'];
}
/** /**
* Disconnects from database. * Disconnects from database.
* *
@ -202,6 +235,8 @@ 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,6 +281,7 @@ class DboOracle extends DboSource {
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 // 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':
@ -273,6 +309,10 @@ class DboOracle extends DboSource {
function fetchRow() { function fetchRow() {
if ($this->_currentRow >= $this->_numRows) { if ($this->_currentRow >= $this->_numRows) {
ocifreestatement($this->_statementId); ocifreestatement($this->_statementId);
$this->_map = null;
$this->_results = null;
$this->_currentRow = null;
$this->_numRows = null;
return false; return false;
} }
$resultRow = array(); $resultRow = array();
@ -353,7 +393,7 @@ class DboOracle extends DboSource {
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($model->table) . '\''; $sql .= strtoupper($this->fullTableName($model)) . '\'';
if (!$this->execute($sql)) { if (!$this->execute($sql)) {
return false; return false;
} }
@ -362,8 +402,7 @@ class DboOracle extends DboSource {
$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']);
} }
$this->__cacheDescription($model->tablePrefix.$model->table, $fields); $this->__cacheDescription($this->fullTableName($model, false), $fields);
//$this->__cacheDescription($this->fullTableName($model, false), $fields);
return $fields; return $fields;
} }
/** /**
@ -394,7 +433,7 @@ class DboOracle extends DboSource {
* @return boolean True on success, false on fail * @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions). * (i.e. if the database/model does not support transactions).
*/ */
function begin(&$model) { function begin() {
//if (parent::begin($model)) { //if (parent::begin($model)) {
//if ($this->execute('BEGIN')) { //if ($this->execute('BEGIN')) {
$this->_transactionStarted = true; $this->_transactionStarted = true;
@ -425,7 +464,7 @@ class DboOracle extends DboSource {
* (i.e. if the database/model does not support transactions, * (i.e. if the database/model does not support transactions,
* or a transaction has not started). * or a transaction has not started).
*/ */
function commit(&$model) { function commit() {
//if (parent::commit($model)) { //if (parent::commit($model)) {
$this->_transactionStarted = false; $this->_transactionStarted = false;
return ocicommit($this->connection); return ocicommit($this->connection);

View file

@ -47,7 +47,8 @@ class DboPostgres extends DboSource {
'login' => 'root', 'login' => 'root',
'password' => '', 'password' => '',
'database' => 'cake', 'database' => 'cake',
'port' => 5432 'port' => 5432,
'encoding' => ''
); );
var $columns = array( var $columns = array(
@ -86,8 +87,7 @@ class DboPostgres extends DboSource {
} else { } else {
$this->connected = false; $this->connected = false;
} }
if (!empty($config['encoding'])) {
if (isset($config['encoding']) && !empty($config['encoding'])) {
$this->setEncoding($config['encoding']); $this->setEncoding($config['encoding']);
} }

View file

@ -36,51 +36,54 @@
*/ */
class DboSybase extends DboSource { class DboSybase extends DboSource {
/** /**
* Enter description here... * Driver description
* *
* @var unknown_type * @var string
*/ */
var $description = "Sybase DBO Driver"; var $description = "Sybase DBO Driver";
/** /**
* Enter description here... * Start quote for quoted identifiers
* *
* @var unknown_type * @var string
*/ */
var $startQuote = "`"; var $startQuote = "`";
/** /**
* Enter description here... * End quote for quoted identifiers
* *
* @var unknown_type * @var string
*/ */
var $endQuote = "`"; var $endQuote = "`";
/** /**
* Base configuration settings for MySQL driver * Base configuration settings for Sybase driver
* *
* @var array * @var array
*/ */
var $_baseConfig = array('persistent' => true, var $_baseConfig = array(
'host' => 'localhost', 'persistent' => true,
'login' => 'root', 'host' => 'localhost',
'password' => '', 'login' => 'sa',
'database' => 'cake', 'password' => '',
'port' => '3306', 'database' => 'cake',
'connect' => 'sybase_pconnect'); 'port' => '4100'
);
/** /**
* MySQL column definition * Sybase column definition
* *
* @var array * @var array
*/ */
var $columns = array('primary_key' => array('name' => 'int(11) DEFAULT NULL auto_increment'), var $columns = array(
'string' => array('name' => 'varchar', 'limit' => '255'), 'primary_key' => array('name' => 'numeric(9,0) IDENTITY PRIMARY KEY'),
'text' => array('name' => 'text'), 'string' => array('name' => 'varchar', 'limit' => '255'),
'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'), 'text' => array('name' => 'text'),
'float' => array('name' => 'float', 'formatter' => 'floatval'), 'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'), 'float' => array('name' => 'float', 'formatter' => 'floatval'),
'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'), 'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
'time' => array('name' => 'time', 'format' => 'H:i:s', 'formatter' => 'date'), 'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'), 'time' => array('name' => 'datetime', 'format' => 'H:i:s', 'formatter' => 'date'),
'binary' => array('name' => 'blob'), 'date' => array('name' => 'datetime', 'format' => 'Y-m-d', 'formatter' => 'date'),
'boolean' => array('name' => 'tinyint', 'limit' => '1')); 'binary' => array('name' => 'image'),
'boolean' => array('name' => 'bit')
);
/** /**
* Connects to the database using options in the given configuration array. * Connects to the database using options in the given configuration array.
* *
@ -88,13 +91,12 @@ class DboSybase extends DboSource {
*/ */
function connect() { function connect() {
$config = $this->config; $config = $this->config;
$connect = $config['connect'];
$this->connected = false; $this->connected = false;
if (!$config['persistent']) { if (!$config['persistent']) {
$this->connection = sybase_connect($config['host'], $config['login'], $config['password'], true); $this->connection = sybase_connect($config['host'], $config['login'], $config['password'], true);
} else { } else {
$this->connection = $connect($config['host'], $config['login'], $config['password']); $this->connection = sybase_pconnect($config['host'], $config['login'], $config['password']);
} }
if (sybase_select_db($config['database'], $this->connection)) { if (sybase_select_db($config['database'], $this->connection)) {
@ -132,7 +134,7 @@ class DboSybase extends DboSource {
return $cache; return $cache;
} }
$result = $this->_execute("SHOW TABLES FROM " . $this->config['database']); $result = $this->_execute("select name from sysobjects where type='U'");
if (!$result) { if (!$result) {
return array(); return array();
} else { } else {
@ -234,7 +236,7 @@ class DboSybase extends DboSource {
*/ */
function begin(&$model) { function begin(&$model) {
if (parent::begin($model)) { if (parent::begin($model)) {
if ($this->execute('START TRANSACTION')) { if ($this->execute('BEGIN TRAN')) {
$this->_transactionStarted = true; $this->_transactionStarted = true;
return true; return true;
} }
@ -252,7 +254,7 @@ class DboSybase extends DboSource {
function commit(&$model) { function commit(&$model) {
if (parent::commit($model)) { if (parent::commit($model)) {
$this->_transactionStarted = false; $this->_transactionStarted = false;
return $this->execute('COMMIT'); return $this->execute('COMMIT TRAN');
} }
return false; return false;
} }
@ -266,7 +268,7 @@ class DboSybase extends DboSource {
*/ */
function rollback(&$model) { function rollback(&$model) {
if (parent::rollback($model)) { if (parent::rollback($model)) {
return $this->execute('ROLLBACK'); return $this->execute('ROLLBACK TRAN');
} }
return false; return false;
} }
@ -342,29 +344,18 @@ class DboSybase extends DboSource {
$limit = null; $limit = null;
@list($col, $limit) = explode('(', $col); @list($col, $limit) = explode('(', $col);
if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) { if (in_array($col, array('datetime', 'smalldatetime'))) {
return $col; return 'datetime';
} } elseif (in_array($col, array('int', 'bigint', 'smallint', 'tinyint'))) {
if ($col == 'tinyint' && $limit == '1') {
return 'boolean';
}
if (strpos($col, 'int') !== false) {
return 'integer'; return 'integer';
} } elseif (in_array($col, array('float', 'double', 'real', 'decimal', 'money', 'numeric', 'smallmoney'))) {
if (strpos($col, 'char') !== false || $col == 'tinytext') {
return 'string';
}
if (strpos($col, 'text') !== false) {
return 'text';
}
if (strpos($col, 'blob') !== false) {
return 'binary';
}
if (in_array($col, array('float', 'double', 'decimal'))) {
return 'float'; return 'float';
} } elseif (strpos($col, 'text') !== false) {
if (strpos($col, 'enum') !== false) { return 'text';
return "enum($limit)"; } elseif (in_array($col, array('char', 'nchar', 'nvarchar', 'string', 'varchar'))) {
return 'binary';
} elseif (in_array($col, array('binary', 'image', 'varbinary'))) {
return 'binary';
} }
return 'text'; return 'text';