Some SQLite cleanups.

git-svn-id: https://svn.cakephp.org/repo/trunk/cake@262 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
brego 2005-06-21 17:06:54 +00:00
parent 34a4490f74
commit 5f0e5a1c59

View file

@ -44,64 +44,68 @@ uses('dbo');
class DBO_SQLite extends DBO class DBO_SQLite extends DBO
{ {
/** /**
* Connects to the database using config['host'] as a filename. * Connects to the database using config['file'] as a filename.
* *
* @param array $config Configuration array for connecting * @param array $config Configuration array for connecting
* @return mixed * @return mixed
*/ */
function connect ($config) function connect($config)
{ {
if ($config) if ($config)
{ {
$this->config = $config; $this->config = $config;
$this->_conn = sqlite_open($config['host']); $this->_conn = sqlite_open($config['file']);
} }
$this->connected = $this->_conn? true: false; $this->connected = $this->_conn? true: false;
if($this->connected) if($this->connected)
{
return $this->_conn; return $this->_conn;
}
else else
{
die('Could not connect to DB.'); die('Could not connect to DB.');
} }
}
/** /**
* 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()
{ {
return sqlite_close($this->_conn); return sqlite_close($this->_conn);
} }
/** /**
* 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->_conn, $sql); return sqlite_query($this->_conn, $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 () function fetchRow()
{ {
return sqlite_fetch_array($this->_result); return sqlite_fetch_array($this->_result);
} }
/** /**
* 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 tablesList () function tablesList()
{ {
$result = sqlite_query($this->_conn, "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"); $result = sqlite_query($this->_conn, "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;");
@ -121,71 +125,73 @@ class DBO_SQLite extends DBO
} }
} }
/** /**
* Returns an array of the fields in given table name. * Returns an array of the fields in given table name.
* *
* @param string $table_name Name of database table to inspect * @param string $table_name 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 fields ($table_name) function fields($table_name)
{ {
$fields = false; $fields = false;
$cols = sqlite_fetch_column_types($table_name, $this->_conn, SQLITE_ASSOC); $cols = sqlite_fetch_column_types($table_name, $this->_conn, SQLITE_ASSOC);
foreach ($cols as $column => $type) foreach ($cols as $column => $type)
{
$fields[] = array('name'=>$column, 'type'=>$type); $fields[] = array('name'=>$column, 'type'=>$type);
}
return $fields; return $fields;
} }
/** /**
* 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 prepareValue ($data) function prepareValue($data)
{ {
return "'".sqlite_escape_string($data)."'"; return "'".sqlite_escape_string($data)."'";
} }
/** /**
* 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()
{ {
return sqlite_last_error($this->_conn)? sqlite_last_error($this->_conn).': '.sqlite_error_string(sqlite_last_error($this->_conn)): null; return sqlite_last_error($this->_conn)? sqlite_last_error($this->_conn).': '.sqlite_error_string(sqlite_last_error($this->_conn)): 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()
{ {
return $this->_result? sqlite_changes($this->_conn): false; return $this->_result? sqlite_changes($this->_conn): 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()
{ {
return $this->_result? sqlite_num_rows($this->_result): false; return $this->_result? sqlite_num_rows($this->_result): 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->_conn); return sqlite_last_insert_rowid($this->_conn);
} }
@ -197,7 +203,7 @@ class DBO_SQLite extends DBO
* @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 selectLimit ($limit, $offset=null) function selectLimit($limit, $offset=null)
{ {
// :TODO: Please verify this with SQLite, untested. // :TODO: Please verify this with SQLite, untested.
return " LIMIT {$limit}".($offset? " OFFSET {$offset}": null); return " LIMIT {$limit}".($offset? " OFFSET {$offset}": null);