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);
foreach($cols as $column) {
$field = array('name' => $column[0]['Field'], 'type' => $this->column($column[0]['Type'] . $column[0]['Length']), 'null' => $column[0]['Null']);
$fields[] = $field;
$fields[] = array(
'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);
@ -243,8 +247,7 @@ class DboMssql extends DboSource {
$data = '[' . r('.', '].[', $data) . ']';
}
$data=r(']]', ']', r('[[', '[', $data));
return $data;
return r(']]', ']', r('[[', '[', $data));
}
/**

View file

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

View file

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