Brings odbc driver mostly up-to-date. Fixes #5730

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7921 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
TommyO 2008-12-14 18:36:40 +00:00
parent 211bb2bb81
commit a601129d46

View file

@ -91,10 +91,14 @@ class DboOdbc extends DboSource {
function connect() {
$config = $this->config;
$connect = $config['connect'];
if (!$config['persistent']) {
$connect = 'odbc_connect';
}
if (!function_exists($connect)) {
die('no odbc?');
}
$this->connected = false;
$this->connection = $connect($config['database'], $config['login'], $config['password']);
$this->connection = $connect($config['database'], $config['login'], $config['password'], SQL_CUR_USE_ODBC);
if ($this->connection) {
$this->connected = true;
}
@ -117,6 +121,15 @@ class DboOdbc extends DboSource {
* @access protected
*/
function _execute($sql) {
switch ($sql) {
case 'BEGIN':
return odbc_autocommit($this->connection, false);
case 'COMMIT':
return odbc_commit($this->connection);
case 'ROLLBACK':
return odbc_rollback($this->connection);
}
// TODO: should flags be set? possible requirement: SQL_CURSOR_STATIC
return odbc_exec($this->connection, $sql);
}
/**
@ -134,7 +147,7 @@ class DboOdbc extends DboSource {
$tables = array();
while (odbc_fetch_row($result)) {
array_push($tables, odbc_result($result, "TABLE_NAME"));
array_push($tables, odbc_result($result, 'TABLE_NAME'));
}
parent::listSources($tables);
@ -164,32 +177,13 @@ class DboOdbc extends DboSource {
}
foreach ($cols as $column) {
$type = odbc_field_type(odbc_exec($this->connection, "SELECT " . $column . " FROM " . $this->fullTableName($model)), 1);
$type = odbc_field_type(odbc_exec($this->connection, 'SELECT ' . $column . ' FROM ' . $this->fullTableName($model)), 1);
$fields[$column] = array('type' => $type);
}
$this->__cacheDescription($model->tablePrefix . $model->table, $fields);
return $fields;
}
/**
* Name
*
* @param string $data
* @access public
* @return void
*/
function name($data) {
if ($data == '*') {
return '*';
}
if (strpos($data, '`') === false) {
$data = '' . str_replace('.', '.', $data) . '';
//$data = '`'. str_replace('.', '`.`', $data) .'`';
}
return $data;
}
/**
* Returns a quoted and escaped string of $data for use in an SQL statement.
*
@ -210,80 +204,10 @@ class DboOdbc extends DboSource {
}
if (!is_numeric($data)) {
$return = "'" . $data . "'";
} else {
$return = $data;
return "'" . $data . "'";
}
return $return;
}
/**
* Not sure about this one, MySQL needs it but does ODBC? Safer just to leave it
* Translates between PHP boolean values and MySQL (faked) boolean values
*
* @param mixed $data Value to be translated
* @return mixed Converted boolean value
*/
function boolean($data) {
if ($data === true || $data === false) {
if ($data === true) {
return 1;
}
return 0;
} else {
if (intval($data !== 0)) {
return true;
}
return false;
}
}
/**
* Begin a transaction
*
* @param unknown_type $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions).
*/
function begin(&$model) {
if (parent::begin($model)) {
if (odbc_autocommit($this->connection, false)) {
$this->_transactionStarted = true;
return true;
}
}
return false;
}
/**
* Commit a transaction
*
* @param unknown_type $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions,
* or a transaction has not started).
*/
function commit(&$model) {
if (parent::commit($model)) {
if (odbc_commit($this->connection)) {
$this->_transactionStarted = false;
return true;
}
}
return false;
}
/**
* Rollback a transaction
*
* @param unknown_type $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions,
* or a transaction has not started).
*/
function rollback(&$model) {
if (parent::rollback($model)) {
$this->_transactionStarted=false;
return odbc_rollback($this->connection);
}
return false;
return $data;
}
/**
* Returns a formatted error message from previous database operation.
@ -291,8 +215,8 @@ class DboOdbc extends DboSource {
* @return string Error message with error number
*/
function lastError() {
if (odbc_errormsg($this->connection)) {
return odbc_error($this->connection) . ': ' . odbc_errormsg($this->connection);
if ($error = odbc_errormsg($this->connection)) {
return odbc_error($this->connection) . ': ' . $error;
}
return null;
}
@ -304,9 +228,9 @@ class DboOdbc extends DboSource {
*/
function lastAffected() {
if ($this->hasResult()) {
return false;
return odbc_num_rows($this->_result);
}
return false;
return null;
}
/**
* Returns number of rows in previous resultset. If no previous resultset exists,
@ -316,12 +240,7 @@ class DboOdbc extends DboSource {
*/
function lastNumRows() {
if ($this->hasResult()) {
$counter = 0;
if (@odbc_fetch_into($this->_result, $results)) {
return count($results);
} else {
return null;
}
return odbc_num_rows($this->_result);
}
return null;
}
@ -361,14 +280,14 @@ class DboOdbc extends DboSource {
$this->map = array();
$index = 0;
$j = 0;
while($j < $num_fields) {
$columnName = odbc_field_name($results, $j+1);
while ($j < $num_fields) {
$column = odbc_field_name($results, $j+1);
if (strpos($columnName, '_dot_') !== false) {
$parts = explode('_dot_', $columnName);
$this->map[$index++] = array($parts[0], $parts[1]);
if (strpos($column, '_dot_') !== false) {
list($table, $column) = explode('_dot_', $column);
$this->map[$index++] = array($table, $column);
} else {
$this->map[$index++] = array(0, $columnName);
$this->map[$index++] = array(0, $column);
}
$j++;
}
@ -387,12 +306,7 @@ class DboOdbc extends DboSource {
}
if (!is_array($fields)) {
if ($fields != null) {
if (strpos($fields, ',')) {
$fields = explode(',', $fields);
} else {
$fields = array($fields);
}
$fields = array_map('trim', $fields);
$fields = array_map('trim', explode(',', $fields));
} else {
foreach($model->tableToModel as $tableName => $modelName) {
foreach($this->__descriptions[$model->tablePrefix .$tableName] as $field => $type) {
@ -410,7 +324,7 @@ class DboOdbc extends DboSource {
$prepend = '';
if (strpos($fields[$i], 'DISTINCT') !== false) {
$prepend = 'DISTINCT ';
$fields[$i] = trim(r('DISTINCT', '', $fields[$i]));
$fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));
}
if (strrpos($fields[$i], '.') === false) {
@ -436,12 +350,11 @@ class DboOdbc extends DboSource {
$i = 0;
for($i = 0; $i < $numFields; $i++) {
list($table, $column) = $this->map[$i];
$resultRow[$table][$column] = odbc_result($this->results, $i +1);
$resultRow[$table][$column] = odbc_result($this->results, $i + 1);
}
return $resultRow;
} else {
return false;
}
return false;
}
}
?>