Adding patches from andy__ to dbo_odbc

Schema related functions will not work due to the way odbc works.
Closes #5398

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7646 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mark_story 2008-09-23 02:59:05 +00:00
parent caa4e321f2
commit 676459224c

View file

@ -36,7 +36,7 @@
* @package cake * @package cake
* @subpackage cake.cake.libs.model.datasources.dbo * @subpackage cake.cake.libs.model.datasources.dbo
*/ */
class DboOdbc extends DboSource{ class DboOdbc extends DboSource {
/** /**
* Driver description * Driver description
@ -76,7 +76,6 @@ class DboOdbc extends DboSource{
* *
* @var unknown_type * @var unknown_type
*/ */
var $columns = array(); var $columns = array();
// var $columns = array('primary_key' => array('name' => 'int(11) DEFAULT NULL auto_increment'), // var $columns = array('primary_key' => array('name' => 'int(11) DEFAULT NULL auto_increment'),
@ -189,6 +188,13 @@ class DboOdbc extends DboSource{
return $fields; return $fields;
} }
/**
* Name
*
* @param string $data
* @access public
* @return void
*/
function name($data) { function name($data) {
if ($data == '*') { if ($data == '*') {
return '*'; return '*';
@ -244,13 +250,11 @@ class DboOdbc extends DboSource{
if ($data === true) { if ($data === true) {
return 1; return 1;
} }
return 0; return 0;
} else { } else {
if (intval($data !== 0)) { if (intval($data !== 0)) {
return true; return true;
} }
return false; return false;
} }
} }
@ -269,7 +273,6 @@ class DboOdbc extends DboSource{
return true; return true;
} }
} }
return false; return false;
} }
@ -288,7 +291,6 @@ class DboOdbc extends DboSource{
return true; return true;
} }
} }
return false; return false;
} }
@ -305,7 +307,6 @@ class DboOdbc extends DboSource{
$this->_transactionStarted=false; $this->_transactionStarted=false;
return odbc_rollback($this->connection); return odbc_rollback($this->connection);
} }
return false; return false;
} }
@ -315,10 +316,9 @@ class DboOdbc extends DboSource{
* @return string Error message with error number * @return string Error message with error number
*/ */
function lastError() { function lastError() {
if (odbc_error($this->connection)) { if (odbc_errormsg($this->connection)) {
return odbc_error($this->connection) . ': ' . odbc_errormsg($this->connection); return odbc_error($this->connection) . ': ' . odbc_errormsg($this->connection);
} }
return null; return null;
} }
@ -329,26 +329,30 @@ class DboOdbc extends DboSource{
* @return integer Number of affected rows * @return integer Number of affected rows
*/ */
function lastAffected() { function lastAffected() {
if ($this->_result) { if ($this->hasResult()) {
return null; return false;
} }
return false;
return null;
} }
/** /**
* Returns number of rows in previous resultset. If no previous resultset exists, * Returns number of rows in previous resultset. If no previous resultset exists,
* this returns false. * this returns false.
* *
* @return integer Number of rows in resultset * @return int Number of rows in resultset
*/ */
function lastNumRows() { function lastNumRows() {
if ($this->_result) { if($this->hasResult()) {
return@odbc_num_rows($this->_result); $counter = 0;
} if(@odbc_fetch_into($this->_result, $results)) {
return count($results);
} else {
return null; return null;
} }
}
return null;
}
/** /**
* Returns the ID generated from the previous INSERT operation. * Returns the ID generated from the previous INSERT operation.
@ -369,67 +373,112 @@ class DboOdbc extends DboSource{
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;
} }
return $real; return $real;
} }
/** /**
* Enter description here... * Enter description here...
* *
* @param unknown_type $results * @param unknown_type $results
*/ */
function resultSet(&$results) { function resultSet(&$results) {
$this->results=&$results; $this->results =& $results;
$this->map=array(); $num_fields = odbc_num_fields($results);
$num_fields =odbc_num_fields($results); $this->map = array();
$index =0; $index = 0;
$j =0; $j = 0;
while($j < $num_fields) {
$columnName = odbc_field_name($results, $j+1);
while ($j < $num_fields) { if(strpos($columnName, '_dot_') !== false) {
$column = odbc_fetch_array($results, $j); $parts = explode('_dot_', $columnName);
$this->map[$index++] = array($parts[0], $parts[1]);
if (!empty($column->table)) {
$this->map[$index++] = array($column->table,
$column->name);
} else { } else {
echo array(0, $this->map[$index++] = array(0, $columnName);
$column->name);
$this->map[$index++]=array(0,
$column->name);
} }
$j++; $j++;
} }
} }
/**
* Generates the fields list of an SQL query.
*
* @param Model $model
* @param string $alias Alias tablename
* @param mixed $fields
* @return array
*/
function fields(&$model, $alias = null, $fields = null, $quote = true) {
if (empty($alias)) {
$alias = $model->name;
}
if (!is_array($fields)) {
if ($fields != null) {
if (strpos($fields, ',')) {
$fields = explode(',', $fields);
} else {
$fields = array($fields);
}
$fields = array_map('trim', $fields);
} else {
foreach($model->tableToModel as $tableName => $modelName) {
foreach($this->__descriptions[$model->tablePrefix .$tableName] as $field => $type) {
$fields[] = $modelName .'.' .$field;
}
}
}
}
$count = count($fields);
if ($count >= 1 && $fields[0] != '*' && strpos($fields[0], 'COUNT(*)') === false) {
for ($i = 0; $i < $count; $i++) {
if (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
$prepend = '';
if (strpos($fields[$i], 'DISTINCT') !== false) {
$prepend = 'DISTINCT ';
$fields[$i] = trim(r('DISTINCT', '', $fields[$i]));
}
if(strrpos($fields[$i], '.') === false) {
$fields[$i] = $prepend . $this->name($alias) . '.' . $this->name($fields[$i]) . ' AS ' . $this->name($alias . '_dot_' . $fields[$i]);
} else {
$build = explode('.', $fields[$i]);
$fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]) . ' AS ' . $this->name($build[0] . '_dot_' . $build[1]);
}
}
}
}
return $fields;
}
/** /**
* Fetches the next row from the current result set * Fetches the next row from the current result set
* *
* @return unknown * @return unknown
*/ */
function fetchResult() { function fetchResult() {
if ($row = odbc_fetch_row($this->results)) { if($row = odbc_fetch_row($this->results)) {
$resultRow=array(); $resultRow = array();
$i=0; $numFields = odbc_num_fields($this->results);
$i = 0;
foreach ($row as $index => $field) { for($i = 0; $i < $numFields; $i++) {
list($table, $column) = $this->map[$index]; list($table, $column) = $this->map[$i];
$resultRow[$table][$column]=$row[$index]; $resultRow[$table][$column] = odbc_result($this->results, $i +1);
$i++;
} }
return $resultRow; return $resultRow;
} else { } else {
return false; return false;
} }
} }
} }
?> ?>