From 69fee85469f984c25d2f71db8c5a0904d394ba41 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Tue, 18 Feb 2014 16:44:00 +1300 Subject: [PATCH] Change database path to allow use of ":memory:" Instead of using a custom "memory" key in the $databaseConfig which never really got used this now allows someone to simply enable in-memory databases by setting the path to ":memory:" or to use the following environment variable: define('SS_SQLITE_DATABASE_PATH', ':memory:'); --- code/SQLite3Database.php | 31 +++++++++---------------------- code/SQLitePDODatabase.php | 20 +++++++------------- 2 files changed, 16 insertions(+), 35 deletions(-) diff --git a/code/SQLite3Database.php b/code/SQLite3Database.php index 623ea69..19eab92 100644 --- a/code/SQLite3Database.php +++ b/code/SQLite3Database.php @@ -37,11 +37,6 @@ class SQLite3Database extends SS_Database { */ protected $parameters; - /* - * if we're on a In-Memory db - */ - protected $lives_in_memory = false; - public static $default_pragma = array(); public static $vacuum = true; @@ -65,34 +60,26 @@ class SQLite3Database extends SS_Database { /* * Uses whatever connection details are in the $parameters array to connect to a database of a given name */ - function connectDatabase(){ + function connectDatabase() { $this->enum_map = array(); + $parameters = $this->parameters; + $dbName = !isset($this->database) ? $parameters['database'] : $this->database; + $file = $parameters['path']; - $parameters=$this->parameters; - - $dbName = !isset($this->database) ? $parameters['database'] : $dbName=$this->database; - - //assumes that the path to dbname will always be provided: - - - // use the very lightspeed SQLite In-Memory feature for testing - if((isset($parameters['memory']) && $parameters['memory']) || !isset($parameters['path'])) { - $file = ':memory:'; - $this->lives_in_memory = true; - } else { + // assumes that the path to dbname will always be provided + // this is only necessary if we're using a filesystem path, and not an in-memory database + if($file != ':memory:') { $file = $parameters['path'] . '/' . $dbName; - $this->lives_in_memory = false; if(!file_exists($parameters['path'])) { SQLiteDatabaseConfigurationHelper::create_db_dir($parameters['path']); SQLiteDatabaseConfigurationHelper::secure_db_dir($parameters['path']); } } - + $this->dbConn = new SQLite3($file, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $parameters['key']); if(method_exists('SQLite3', 'busyTimeout')) $this->dbConn->busyTimeout(60000); - //By virtue of getting here, the connection is active: - $this->active=true; + $this->active = true; $this->database = $dbName; if(!$this->dbConn) { diff --git a/code/SQLitePDODatabase.php b/code/SQLitePDODatabase.php index 5bae5c0..2d015bc 100644 --- a/code/SQLitePDODatabase.php +++ b/code/SQLitePDODatabase.php @@ -10,21 +10,16 @@ class SQLitePDODatabase extends SQLite3Database { /* * Uses whatever connection details are in the $parameters array to connect to a database of a given name */ - function connectDatabase(){ - + function connectDatabase() { $this->enum_map = array(); - - $parameters=$this->parameters; - + $parameters = $this->parameters; $dbName = !isset($this->database) ? $parameters['database'] : $dbName=$this->database; + $file = $parameters['path']; - // use the very lightspeed SQLite In-Memory feature for testing - if((isset($parameters['memory']) && $parameters['memory']) || !isset($parameters['path'])) { - $file = ':memory:'; - $this->lives_in_memory = true; - } else { + // assumes that the path to dbname will always be provided + // this is only necessary if we're using a filesystem path, and not an in-memory database + if($file != ':memory:') { $file = $parameters['path'] . '/' . $dbName; - $this->lives_in_memory = false; if(!file_exists($parameters['path'])) { SQLiteDatabaseConfigurationHelper::create_db_dir($parameters['path']); SQLiteDatabaseConfigurationHelper::secure_db_dir($parameters['path']); @@ -33,8 +28,7 @@ class SQLitePDODatabase extends SQLite3Database { $this->dbConn = new PDO("sqlite:$file"); - //By virtue of getting here, the connection is active: - $this->active=true; + $this->active = true; $this->database = $dbName; if(!$this->dbConn) {