MINOR: caching enum map

MINOR: fixed indentation
This commit is contained in:
Andreas Piening 2010-01-17 10:48:22 +00:00
parent 787820dd35
commit 29ca870f76
1 changed files with 884 additions and 875 deletions

View File

@ -65,6 +65,8 @@ class SQLite3Database extends SS_Database {
*/
function connectDatabase(){
$this->enum_map = array();
$parameters=$this->parameters;
$dbName = !isset($this->database) ? $parameters['database'] : $dbName=$this->database;
@ -73,7 +75,7 @@ class SQLite3Database extends SS_Database {
$file = $parameters['path'] . '/' . $dbName;
// use the very lightspeed SQLite In-Memory feature for testing
if($parameters['memory'] && preg_match('/^tmpdb[0-9]+$/', $dbName)) $file = ':memory:';
if(SapphireTest::using_temp_db()) $file = ':memory:';
$this->dbConn = new SQLite3($file, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $parameters['key']);
@ -85,8 +87,10 @@ class SQLite3Database extends SS_Database {
$this->databaseError("Couldn't connect to SQLite3 database");
return false;
}
return true;
}
/**
* Not implemented, needed for PDO
*/
@ -190,6 +194,7 @@ class SQLite3Database extends SS_Database {
*/
public function createDatabase() {
$this->dbConn = null;
$fullpath = $this->parameters['path'] . '/' . $this->database;
if(is_writable($fullpath)) unlink($fullpath);
@ -203,6 +208,7 @@ class SQLite3Database extends SS_Database {
*/
public function dropDatabase() {
//First, we need to switch back to the original database so we can drop the current one
$this->dbConn = null;
$db_to_drop=$this->database;
$this->selectDatabase($this->database_original);
$this->connectDatabase();
@ -284,7 +290,9 @@ class SQLite3Database extends SS_Database {
}
public function renameTable($oldTableName, $newTableName) {
$this->query("ALTER TABLE \"$oldTableName\" RENAME \"$newTableName\"");
}
/**
@ -345,7 +353,6 @@ class SQLite3Database extends SS_Database {
foreach($queries as $query) $this->query($query.';');
}
}
@ -549,13 +556,18 @@ class SQLite3Database extends SS_Database {
* @params array $values Contains a tokenised list of info about this data type
* @return string
*/
protected $enum_map = array();
public function enum($values){
$bt=debug_backtrace();
if(basename($bt[0]['file']) == 'Database.php') {
$column = $bt[0]['args'][0]['table'].'.'.$bt[0]['args'][0]['name'];
$this->query("CREATE TABLE IF NOT EXISTS SQLiteEnums (TableColumn TEXT PRIMARY KEY, EnumList TEXT)");
if(empty($this->enum_map)) $this->query("CREATE TABLE IF NOT EXISTS SQLiteEnums (TableColumn TEXT PRIMARY KEY, EnumList TEXT)");
if(empty($this->enum_map[$column]) || $this->enum_map[$column] != implode(',', $values['enums'])) {
$this->query("REPLACE INTO SQLiteEnums (TableColumn,EnumList) VALUES (\"$column\",\"".implode(',', $values['enums'])."\")");
$this->enum_map[$column] = implode(',', $values['enums']);
}
}
return 'TEXT DEFAULT \'' . $values['default'] . '\'';
@ -674,7 +686,6 @@ class SQLite3Database extends SS_Database {
* Returns the SQL command to get all the tables in this database
*/
function allTablesSQL(){
//ANDY return "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE';";
return 'SELECT name FROM sqlite_master WHERE type = "table"';
}
@ -953,7 +964,6 @@ class SQLite3Database extends SS_Database {
}
}
}
}
return implode(',', $terms);
@ -965,6 +975,7 @@ class SQLite3Database extends SS_Database {
* @package SQLite3Database
*/
class SQLite3Query extends SS_Query {
/**
* The SQLite3Database object that created this result set.
* @var SQLite3Database
@ -1026,6 +1037,4 @@ class SQLite3Query extends SS_Query {
return false;
}
}
}