Adding misc. db driver updates, and fixing code formatting of SQLite driver

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3218 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-07-07 19:49:13 +00:00
parent 0d2ebb1cb3
commit 5d7d605723
3 changed files with 106 additions and 173 deletions

View file

@ -219,8 +219,12 @@ class DboMssql extends DboSource {
$cols = $this->fetchAll("SELECT COLUMN_NAME as Field, DATA_TYPE as Type, COL_LENGTH('" . $model->tablePrefix . $model->table . "', COLUMN_NAME) as Length, IS_NULLABLE As [Null], COLUMN_DEFAULT as [Default], COLUMNPROPERTY(OBJECT_ID('" . $model->table . "'), COLUMN_NAME, 'IsIdentity') as [Key], NUMERIC_SCALE as Size FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" . $model->table . "'", false); $cols = $this->fetchAll("SELECT COLUMN_NAME as Field, DATA_TYPE as Type, COL_LENGTH('" . $model->tablePrefix . $model->table . "', COLUMN_NAME) as Length, IS_NULLABLE As [Null], COLUMN_DEFAULT as [Default], COLUMNPROPERTY(OBJECT_ID('" . $model->table . "'), COLUMN_NAME, 'IsIdentity') as [Key], NUMERIC_SCALE as Size FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" . $model->table . "'", false);
foreach($cols as $column) { foreach($cols as $column) {
$field = array('name' => $column[0]['Field'], 'type' => $this->column($column[0]['Type'] . $column[0]['Length']), 'null' => $column[0]['Null']); $fields[] = array(
$fields[] = $field; 'name' => $column[0]['Field'],
'type' => $this->column($column[0]['Type'] . $column[0]['Length']),
'null' => up($column[0]['Null']),
'default' => $column[0]['Default']
);
} }
$this->__cacheDescription($model->tablePrefix . $model->table, $fields); $this->__cacheDescription($model->tablePrefix . $model->table, $fields);
@ -243,8 +247,7 @@ class DboMssql extends DboSource {
$data = '[' . r('.', '].[', $data) . ']'; $data = '[' . r('.', '].[', $data) . ']';
} }
$data=r(']]', ']', r('[[', '[', $data)); return r(']]', ']', r('[[', '[', $data));
return $data;
} }
/** /**

View file

@ -186,10 +186,14 @@ class DboPostgres extends DboSource {
} }
if (isset($column[0])) { if (isset($column[0])) {
$fields[] = array('name' => $column[0]['name'], if (strpos($column[0]['default'], 'nextval(') === 0) {
'type' => $this->column($column[0]['type']), $column[0]['default'] = null;
'null' => $column[0]['null'], }
'default' => $column[0]['default'] $fields[] = array(
'name' => $column[0]['name'],
'type' => $this->column($column[0]['type']),
'null' => $column[0]['null'],
'default' => $column[0]['default']
); );
} }
} }
@ -542,7 +546,6 @@ class DboPostgres extends DboSource {
return false; return false;
} }
} }
}
/** /**
* Sets the database encoding * Sets the database encoding
* *
@ -560,5 +563,6 @@ class DboPostgres extends DboSource {
function getEncoding() { function getEncoding() {
return pg_client_encoding($this->connection); return pg_client_encoding($this->connection);
} }
}
?> ?>

View file

@ -41,138 +41,119 @@ uses('model'.DS.'datasources'.DS.'dbo_source');
* @package cake * @package cake
* @subpackage cake.cake.libs.model.dbo * @subpackage cake.cake.libs.model.dbo
*/ */
class DboSqlite extends DboSource class DboSqlite extends DboSource {
{
/** /**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
*/ */
var $description = "SQLite DBO Driver"; var $description = "SQLite DBO Driver";
/** /**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
*/ */
var $startQuote = '"'; var $startQuote = '"';
/** /**
* Enter description here... * Enter description here...
* *
* @var unknown_type * @var unknown_type
*/ */
var $endQuote = '"'; var $endQuote = '"';
/** /**
* Base configuration settings for SQLite driver * Base configuration settings for SQLite driver
* *
* @var array * @var array
*/ */
var $_baseConfig = array('persistent' => true, var $_baseConfig = array(
'database' => 'cake', 'persistent' => true,
'connect' => 'sqlite_popen'); 'database' => null,
'connect' => 'sqlite_popen'
);
/** /**
* SQLite column definition * SQLite column definition
* *
* @var array * @var array
*/ */
var $columns = array('primary_key' => array('name' => 'integer primary key'), var $columns = array(
'string' => array('name' => 'varchar', 'limit' => '255'), 'primary_key' => array('name' => 'integer primary key'),
'text' => array('name' => 'text'), 'string' => array('name' => 'varchar', 'limit' => '255'),
'integer' => array('name' => 'integer', 'limit' => '11', 'formatter' => 'intval'), 'text' => array('name' => 'text'),
'float' => array('name' => 'float', 'formatter' => 'floatval'), 'integer' => array('name' => 'integer', 'limit' => '11', 'formatter' => 'intval'),
'datetime' => array('name' => 'timestamp', 'format' => 'YmdHis', 'formatter' => 'date'), 'float' => array('name' => 'float', 'formatter' => 'floatval'),
'timestamp' => array('name' => 'timestamp', 'format' => 'YmdHis', 'formatter' => 'date'), 'datetime' => array('name' => 'timestamp', 'format' => 'YmdHis', 'formatter' => 'date'),
'time' => array('name' => 'timestamp', 'format' => 'His', 'formatter' => 'date'), 'timestamp' => array('name' => 'timestamp', 'format' => 'YmdHis', 'formatter' => 'date'),
'date' => array('name' => 'date', 'format' => 'Ymd', 'formatter' => 'date'), 'time' => array('name' => 'timestamp', 'format' => 'His', 'formatter' => 'date'),
'binary' => array('name' => 'blob'), 'date' => array('name' => 'date', 'format' => 'Ymd', 'formatter' => 'date'),
'boolean' => array('name' => 'integer', 'limit' => '1')); 'binary' => array('name' => 'blob'),
'boolean' => array('name' => 'integer', 'limit' => '1')
);
/** /**
* Connects to the database using config['file'] as a filename. * Connects to the database using config['database'] as a filename.
* *
* @param array $config Configuration array for connecting * @param array $config Configuration array for connecting
* @return mixed * @return mixed
*/ */
function connect() function connect() {
{
$config = $this->config; $config = $this->config;
$this->connection = $config['connect']($config['database']); $this->connection = $config['connect']($config['database']);
$this->connected = is_resource($this->connection); $this->connected = is_resource($this->connection);
return $this->connected; return $this->connected;
} }
/** /**
* Disconnects from database. * Disconnects from database.
* *
* @return boolean True if the database could be disconnected, else false * @return boolean True if the database could be disconnected, else false
*/ */
function disconnect() function disconnect() {
{
$this->connected = !@sqlite_close($this->connection); $this->connected = !@sqlite_close($this->connection);
return !$this->connected; return !$this->connected;
} }
/** /**
* Executes given SQL statement. * Executes given SQL statement.
* *
* @param string $sql SQL statement * @param string $sql SQL statement
* @return resource Result resource identifier * @return resource Result resource identifier
*/ */
function _execute($sql) function _execute($sql) {
{
return sqlite_query($this->connection, $sql); return sqlite_query($this->connection, $sql);
} }
/** /**
* Returns a row from given resultset as an array . * Returns a row from given resultset as an array .
* *
* @return array The fetched row as an array * @return array The fetched row as an array
*/ */
function fetchRow ($assoc = false) function fetchRow ($assoc = false) {
{ if(is_resource($this->_result)) {
if(is_resource($this->_result))
{
$this->resultSet($this->_result); $this->resultSet($this->_result);
$resultRow = $this->fetchResult(); $resultRow = $this->fetchResult();
return $resultRow; return $resultRow;
} } else {
else
{
return null; return null;
} }
} }
/** /**
* Returns an array of tables in the database. If there are no tables, an error is raised and the application exits. * Returns an array of tables in the database. If there are no tables, an error is raised and the application exits.
* *
* @return array Array of tablenames in the database * @return array Array of tablenames in the database
*/ */
function listSources() function listSources() {
{
$db = $this->config['database']; $db = $this->config['database'];
$this->config['database'] = basename($this->config['database']); $this->config['database'] = basename($this->config['database']);
$cache = parent::listSources(); $cache = parent::listSources();
if ($cache != null) if ($cache != null) {
{
return $cache; return $cache;
} }
$result = $this->fetchAll("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"); $result = $this->fetchAll("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;");
if (!$result || empty($result)) if (!$result || empty($result)) {
{
return array(); return array();
} } else {
else
{
$tables = array(); $tables = array();
foreach ($result as $table) foreach ($result as $table) {
{
$tables[] = $table[0]['name']; $tables[] = $table[0]['name'];
} }
parent::listSources($tables); parent::listSources($tables);
@ -183,79 +164,71 @@ class DboSqlite extends DboSource
$this->config['database'] = $db; $this->config['database'] = $db;
return array(); return array();
} }
/** /**
* Returns an array of the fields in given table name. * Returns an array of the fields in given table name.
* *
* @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 = array();
$cols = sqlite_fetch_column_types($model->tablePrefix.$model->table, $this->connection); $cols = sqlite_fetch_column_types($model->tablePrefix . $model->table, $this->connection);
foreach ($cols as $column => $type) pr($cols);
{ die();
$fields[] = array('name' => $column, 'type' => $this->column($type), 'null' => true); foreach ($cols as $column => $type) {
$fields[] = array(
'name' => $column,
'type' => $this->column($type),
'null' => true
);
} }
$this->__cacheDescription($model->tablePrefix.$model->table, $fields); $this->__cacheDescription($model->tablePrefix . $model->table, $fields);
return $fields; return $fields;
} }
/** /**
* Returns a quoted name of $data for use in an SQL statement. * Returns a quoted name of $data for use in an SQL statement.
* *
* @param string $data Name (table.field) to be prepared for use in an SQL statement * @param string $data Name (table.field) to be prepared for use in an SQL statement
* @return string Quoted for SQLite * @return string Quoted for SQLite
*/ */
function name ($data) function name ($data) {
{ if ($data == '*') {
if ($data == '*')
{
return '*'; return '*';
} }
$pos = strpos($data, '"'); $pos = strpos($data, '"');
if ($pos === false) if ($pos === false) {
{
$data = '"'. str_replace('.', '"."', $data) .'"'; $data = '"'. str_replace('.', '"."', $data) .'"';
} }
return $data; return $data;
} }
/** /**
* Returns a quoted and escaped string of $data for use in an SQL statement. * Returns a quoted and escaped string of $data for use in an SQL statement.
* *
* @param string $data String to be prepared for use in an SQL statement * @param string $data String to be prepared for use in an SQL statement
* @return string Quoted and escaped * @return string Quoted and escaped
*/ */
function value ($data, $column = null, $safe = false) function value ($data, $column = null, $safe = false) {
{
$parent = parent::value($data, $column, $safe); $parent = parent::value($data, $column, $safe);
if ($parent != null) if ($parent != null) {
{
return $parent; return $parent;
} }
if ($data === null) if ($data === null) {
{
return 'NULL'; return 'NULL';
} }
if($data == '') if($data == '') {
{
return "''"; return "''";
} }
switch ($column) switch ($column) {
{
case 'boolean': case 'boolean':
$data = $this->boolean((bool)$data); $data = $this->boolean((bool)$data);
break; break;
@ -269,7 +242,6 @@ class DboSqlite extends DboSource
} }
return "'" . $data . "'"; return "'" . $data . "'";
} }
/** /**
* Begin a transaction * Begin a transaction
* *
@ -277,19 +249,15 @@ class DboSqlite 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 (&$model) {
{ if (parent::begin($model)) {
if (parent::begin($model)) if ($this->execute('BEGIN')) {
{
if ($this->execute('BEGIN'))
{
$this->__transactionStarted = true; $this->__transactionStarted = true;
return true; return true;
} }
} }
return false; return false;
} }
/** /**
* Commit a transaction * Commit a transaction
* *
@ -298,16 +266,13 @@ class DboSqlite 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 (&$model) {
{ if (parent::commit($model)) {
if (parent::commit($model))
{
$this->__transactionStarted; $this->__transactionStarted;
return $this->execute('COMMIT'); return $this->execute('COMMIT');
} }
return false; return false;
} }
/** /**
* Rollback a transaction * Rollback a transaction
* *
@ -316,82 +281,65 @@ class DboSqlite 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 rollback (&$model) function rollback (&$model) {
{ if (parent::rollback($model)) {
if (parent::rollback($model))
{
return $this->execute('ROLLBACK'); return $this->execute('ROLLBACK');
} }
return false; return false;
} }
/** /**
* Returns a formatted error message from previous database operation. * Returns a formatted error message from previous database operation.
* *
* @return string Error message * @return string Error message
*/ */
function lastError() function lastError() {
{
$error = sqlite_last_error($this->connection); $error = sqlite_last_error($this->connection);
if ($error) if ($error) {
{
return $error.': '.sqlite_error_string($error); return $error.': '.sqlite_error_string($error);
} }
return null; return null;
} }
/** /**
* Returns number of affected rows in previous database operation. If no previous operation exists, this returns false. * Returns number of affected rows in previous database operation. If no previous operation exists, this returns false.
* *
* @return int Number of affected rows * @return int Number of affected rows
*/ */
function lastAffected() function lastAffected() {
{ if ($this->_result) {
if ($this->_result)
{
return sqlite_changes($this->connection); return sqlite_changes($this->connection);
} }
return false; return false;
} }
/** /**
* 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 int Number of rows in resultset * @return int Number of rows in resultset
*/ */
function lastNumRows() function lastNumRows() {
{ if ($this->_result) {
if ($this->_result)
{
sqlite_num_rows($this->_result); sqlite_num_rows($this->_result);
} }
return false; return false;
} }
/** /**
* Returns the ID generated from the previous INSERT operation. * Returns the ID generated from the previous INSERT operation.
* *
* @return int * @return int
*/ */
function lastInsertId() function lastInsertId() {
{
return sqlite_last_insert_rowid($this->connection); return sqlite_last_insert_rowid($this->connection);
} }
/** /**
* Converts database-layer column types to basic types * Converts database-layer column types to basic types
* *
* @param string $real Real database-layer column type (i.e. "varchar(255)") * @param string $real Real database-layer column type (i.e. "varchar(255)")
* @return string Abstract column type (i.e. "string") * @return string Abstract column type (i.e. "string")
*/ */
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;
@ -401,88 +349,69 @@ class DboSqlite extends DboSource
$limit = null; $limit = null;
@list($col, $limit) = explode('(', $col); @list($col, $limit) = explode('(', $col);
if (in_array($col, array('text', 'integer', 'float', 'boolean', 'timestamp'))) if (in_array($col, array('text', 'integer', 'float', 'boolean', 'timestamp'))) {
{
return $col; return $col;
} }
if (strpos($col, 'varchar') !== false) if (strpos($col, 'varchar') !== false) {
{
return 'string'; return 'string';
} }
if (in_array($col, array('blob', 'clob'))) if (in_array($col, array('blob', 'clob'))) {
{
return 'binary'; return 'binary';
} }
if (strpos($col, 'numeric') !== false) if (strpos($col, 'numeric') !== false) {
{
return 'float'; return 'float';
} }
return 'text'; return 'text';
} }
/** /**
* 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(); $this->map = array();
$num_fields = sqlite_num_fields($results); $num_fields = sqlite_num_fields($results);
$index = 0; $index = 0;
$j = 0; $j = 0;
while ($j < $num_fields) while ($j < $num_fields) {
{
$columnName = str_replace('"', '', sqlite_field_name($results, $j)); $columnName = str_replace('"', '', sqlite_field_name($results, $j));
if (strpos($columnName, '.')) if (strpos($columnName, '.')) {
{
$parts = explode('.', $columnName); $parts = explode('.', $columnName);
$this->map[$index++] = array($parts[0], $parts[1]); $this->map[$index++] = array($parts[0], $parts[1]);
} } else {
else
{
$this->map[$index++] = array(0, $columnName); $this->map[$index++] = array(0, $columnName);
} }
$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 = sqlite_fetch_array($this->results, SQLITE_ASSOC)) {
if ($row = sqlite_fetch_array($this->results, SQLITE_ASSOC))
{
$resultRow = array(); $resultRow = array();
$i = 0; $i = 0;
foreach ($row as $index => $field)
{ foreach ($row as $index => $field) {
if (strpos($index, '.')) if (strpos($index, '.')) {
{
list($table, $column) = explode('.', str_replace('"', '', $index)); list($table, $column) = explode('.', str_replace('"', '', $index));
$resultRow[$table][$column] = $row[$index]; $resultRow[$table][$column] = $row[$index];
} } else {
else
{
$resultRow[0][str_replace('"', '', $index)] = $row[$index]; $resultRow[0][str_replace('"', '', $index)] = $row[$index];
} }
$i++; $i++;
} }
return $resultRow; return $resultRow;
} } else {
else
{
return false; return false;
} }
} }
/** /**
* Returns a limit statement in the correct format for the particular database. * Returns a limit statement in the correct format for the particular database.
* *
@ -490,18 +419,14 @@ class DboSqlite extends DboSource
* @param int $offset Offset from which to start results * @param int $offset Offset from which to start results
* @return string SQL limit/offset statement * @return string SQL limit/offset statement
*/ */
function limit ($limit, $offset = null) function limit ($limit, $offset = null) {
{ if ($limit) {
if ($limit)
{
$rt = ''; $rt = '';
if (!strpos(strtolower($limit), 'limit') || strpos(strtolower($limit), 'limit') === 0) if (!strpos(strtolower($limit), 'limit') || strpos(strtolower($limit), 'limit') === 0) {
{
$rt = ' LIMIT'; $rt = ' LIMIT';
} }
$rt .= ' ' . $limit; $rt .= ' ' . $limit;
if ($offset) if ($offset) {
{
$rt .= ', ' . $offset; $rt .= ', ' . $offset;
} }
return $rt; return $rt;
@ -509,4 +434,5 @@ class DboSqlite extends DboSource
return null; return null;
} }
} }
?> ?>