mirror of
https://github.com/silverstripe/silverstripe-sqlite3
synced 2024-10-22 15:05:37 +00:00
MINOR: caching enum map
MINOR: fixed indentation
This commit is contained in:
parent
787820dd35
commit
29ca870f76
@ -65,6 +65,8 @@ class SQLite3Database extends SS_Database {
|
|||||||
*/
|
*/
|
||||||
function connectDatabase(){
|
function connectDatabase(){
|
||||||
|
|
||||||
|
$this->enum_map = array();
|
||||||
|
|
||||||
$parameters=$this->parameters;
|
$parameters=$this->parameters;
|
||||||
|
|
||||||
$dbName = !isset($this->database) ? $parameters['database'] : $dbName=$this->database;
|
$dbName = !isset($this->database) ? $parameters['database'] : $dbName=$this->database;
|
||||||
@ -73,7 +75,7 @@ class SQLite3Database extends SS_Database {
|
|||||||
$file = $parameters['path'] . '/' . $dbName;
|
$file = $parameters['path'] . '/' . $dbName;
|
||||||
|
|
||||||
// use the very lightspeed SQLite In-Memory feature for testing
|
// 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']);
|
$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");
|
$this->databaseError("Couldn't connect to SQLite3 database");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Not implemented, needed for PDO
|
* Not implemented, needed for PDO
|
||||||
*/
|
*/
|
||||||
@ -190,6 +194,7 @@ class SQLite3Database extends SS_Database {
|
|||||||
*/
|
*/
|
||||||
public function createDatabase() {
|
public function createDatabase() {
|
||||||
|
|
||||||
|
$this->dbConn = null;
|
||||||
$fullpath = $this->parameters['path'] . '/' . $this->database;
|
$fullpath = $this->parameters['path'] . '/' . $this->database;
|
||||||
if(is_writable($fullpath)) unlink($fullpath);
|
if(is_writable($fullpath)) unlink($fullpath);
|
||||||
|
|
||||||
@ -203,6 +208,7 @@ class SQLite3Database extends SS_Database {
|
|||||||
*/
|
*/
|
||||||
public function dropDatabase() {
|
public function dropDatabase() {
|
||||||
//First, we need to switch back to the original database so we can drop the current one
|
//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;
|
$db_to_drop=$this->database;
|
||||||
$this->selectDatabase($this->database_original);
|
$this->selectDatabase($this->database_original);
|
||||||
$this->connectDatabase();
|
$this->connectDatabase();
|
||||||
@ -284,7 +290,9 @@ class SQLite3Database extends SS_Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function renameTable($oldTableName, $newTableName) {
|
public function renameTable($oldTableName, $newTableName) {
|
||||||
|
|
||||||
$this->query("ALTER TABLE \"$oldTableName\" RENAME \"$newTableName\"");
|
$this->query("ALTER TABLE \"$oldTableName\" RENAME \"$newTableName\"");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -345,7 +353,6 @@ class SQLite3Database extends SS_Database {
|
|||||||
|
|
||||||
foreach($queries as $query) $this->query($query.';');
|
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
|
* @params array $values Contains a tokenised list of info about this data type
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
|
protected $enum_map = array();
|
||||||
|
|
||||||
public function enum($values){
|
public function enum($values){
|
||||||
|
|
||||||
$bt=debug_backtrace();
|
$bt=debug_backtrace();
|
||||||
if(basename($bt[0]['file']) == 'Database.php') {
|
if(basename($bt[0]['file']) == 'Database.php') {
|
||||||
$column = $bt[0]['args'][0]['table'].'.'.$bt[0]['args'][0]['name'];
|
$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->query("REPLACE INTO SQLiteEnums (TableColumn,EnumList) VALUES (\"$column\",\"".implode(',', $values['enums'])."\")");
|
||||||
|
$this->enum_map[$column] = implode(',', $values['enums']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'TEXT DEFAULT \'' . $values['default'] . '\'';
|
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
|
* Returns the SQL command to get all the tables in this database
|
||||||
*/
|
*/
|
||||||
function allTablesSQL(){
|
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"';
|
return 'SELECT name FROM sqlite_master WHERE type = "table"';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -953,7 +964,6 @@ class SQLite3Database extends SS_Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return implode(',', $terms);
|
return implode(',', $terms);
|
||||||
@ -965,6 +975,7 @@ class SQLite3Database extends SS_Database {
|
|||||||
* @package SQLite3Database
|
* @package SQLite3Database
|
||||||
*/
|
*/
|
||||||
class SQLite3Query extends SS_Query {
|
class SQLite3Query extends SS_Query {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The SQLite3Database object that created this result set.
|
* The SQLite3Database object that created this result set.
|
||||||
* @var SQLite3Database
|
* @var SQLite3Database
|
||||||
@ -1026,6 +1037,4 @@ class SQLite3Query extends SS_Query {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user