updated msqli driver

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3276 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2006-07-21 22:01:26 +00:00
parent 37e7d798d1
commit 6b62ff231a

View file

@ -103,7 +103,8 @@ class DboMysqli extends DboSource {
if (mysqli_select_db($this->connection, $config['database'])) { if (mysqli_select_db($this->connection, $config['database'])) {
$this->connected = true; $this->connected = true;
} }
return $this->connected; return $this->connected;
} }
/** /**
@ -122,21 +123,20 @@ class DboMysqli extends DboSource {
* @return resource Result resource identifier * @return resource Result resource identifier
* @access protected * @access protected
*/ */
function _execute($sql) { function _execute($sql) {
return mysqli_query($this->connection, $sql); return mysqli_query($this->connection, $sql);
} }
/** /**
* Returns an array of sources (tables) in the database. * Returns an array of sources (tables) in the database.
* *
* @return array Array of tablenames in the database * @return array Array of tablenames in the database
*/ */
function listSources() { function listSources() {
$cache = parent::listSources(); $cache = parent::listSources();
if ($cache != null) { if ($cache != null) {
return $cache; return $cache;
} }
$result = $this->_execute('SHOW TABLES FROM ' . $this->config['database'] . ';'); $result = $this->_execute('SHOW TABLES FROM ' . $this->config['database'] . ';');
if (!$result) { if (!$result) {
return array(); return array();
} else { } else {
@ -145,7 +145,7 @@ class DboMysqli extends DboSource {
while ($line = mysqli_fetch_array($result)) { while ($line = mysqli_fetch_array($result)) {
$tables[] = $line[0]; $tables[] = $line[0];
} }
parent::listSources($tables); parent::listSources($tables);
return $tables; return $tables;
} }
@ -156,26 +156,31 @@ class DboMysqli extends DboSource {
* @param string $tableName Name of database table to inspect * @param string $tableName Name of database table to inspect
* @return array Fields in table. Keys are name and type * @return array Fields in table. Keys are name and type
*/ */
function describe(&$model) { function describe(&$model) {
$cache = parent::describe($model); $cache = parent::describe($model);
if ($cache != null) { if ($cache != null) {
return $cache; return $cache;
} }
$fields = false; $fields = false;
$cols = $this->_execute('DESCRIBE ' . $this->fullTableName($model)); $cols = $this->query('DESCRIBE ' . $this->fullTableName($model));
foreach ($cols as $column) { foreach ($cols as $column) {
$colKey = array_keys($column); $colKey = array_keys($column);
if (isset($column[$colKey[0]]) && !isset($column[0])) { if (isset($column[$colKey[0]]) && !isset($column[0])) {
$column[0] = $column[$colKey[0]]; $column[0] = $column[$colKey[0]];
} }
if (isset($column[0])) { if (isset($column[0])) {
$fields[] = array('name' => $column[0]['Field'], 'type' => $this->column($column[0]['Type']), 'null' => $column[0]['Null']); $fields[] = array(
'name' => $column[0]['Field'],
'type' => $this->column($column[0]['Type']),
'null' => $column[0]['Null'],
'default' => $column[0]['Default']
);
} }
} }
$this->__cacheDescription($model->tablePrefix.$model->table, $fields); $this->__cacheDescription($model->tablePrefix.$model->table, $fields);
return $fields; return $fields;
} }
@ -307,7 +312,7 @@ class DboMysqli extends DboSource {
* @return int Number of rows in resultset * @return int Number of rows in resultset
*/ */
function lastNumRows() { function lastNumRows() {
if ($this->_result and is_resource($this->_result)) { if ($this->_result and is_object($this->_result)) {
return @mysqli_num_rows($this->_result); return @mysqli_num_rows($this->_result);
} }
return null; return null;
@ -383,43 +388,56 @@ class DboMysqli extends DboSource {
* *
* @param unknown_type $results * @param unknown_type $results
*/ */
function resultSet(&$results) { function resultSet(&$results) {
$this->results =& $results; $this->results =& $results;
$this->map = array(); $this->map = array();
$num_fields = mysqli_num_fields($results); $num_fields = mysqli_num_fields($results);
$index = 0; $index = 0;
$j = 0; $j = 0;
while ($j < $num_fields) { while ($j < $num_fields) {
$column = mysqli_fetch_field_direct($results, $j);
$column = mysqli_fetch_field($results,$j);
if (!empty($column->table)) { if (!empty($column->table)) {
$this->map[$index++] = array($column->table, $column->name); $this->map[$index++] = array($column->table, $column->name);
} else { } else {
$this->map[$index++] = array(0, $column->name); $this->map[$index++] = array(0, $column->name);
} }
$j++; $j++;
} }
} }
/** /**
* 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 = mysqli_fetch_row($this->results)) { if ($row = mysqli_fetch_row($this->results)) {
$resultRow = array(); $resultRow = array();
$i = 0; $i = 0;
foreach ($row as $index => $field) { foreach ($row as $index => $field) {
list($table, $column) = $this->map[$index]; @list($table, $column) = $this->map[$index];
$resultRow[$table][$column] = $row[$index]; $resultRow[$table][$column] = $row[$index];
$i++; $i++;
} }
return $resultRow; return $resultRow;
} else { } else {
return false; return false;
} }
} }
/**
* Returns a row from given resultset as an array .
*
* @param bool $assoc Associative array only, or both?
* @return array The fetched row as an array
*/
function fetchRow($assoc = false) {
if (is_object($this->_result)) {
$this->resultSet($this->_result);
$resultRow = $this->fetchResult();
return $resultRow;
} else {
return null;
}
}
/** /**
* Enter description here... * Enter description here...
* *