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:');
This commit is contained in:
Sean Harvey 2014-02-18 16:44:00 +13:00
parent 7a11b353d7
commit 69fee85469
2 changed files with 16 additions and 35 deletions

View File

@ -37,11 +37,6 @@ class SQLite3Database extends SS_Database {
*/ */
protected $parameters; protected $parameters;
/*
* if we're on a In-Memory db
*/
protected $lives_in_memory = false;
public static $default_pragma = array(); public static $default_pragma = array();
public static $vacuum = true; public static $vacuum = true;
@ -65,23 +60,16 @@ class SQLite3Database extends SS_Database {
/* /*
* Uses whatever connection details are in the $parameters array to connect to a database of a given name * 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(); $this->enum_map = array();
$parameters = $this->parameters;
$dbName = !isset($this->database) ? $parameters['database'] : $this->database;
$file = $parameters['path'];
$parameters=$this->parameters; // 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
$dbName = !isset($this->database) ? $parameters['database'] : $dbName=$this->database; if($file != ':memory:') {
//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 {
$file = $parameters['path'] . '/' . $dbName; $file = $parameters['path'] . '/' . $dbName;
$this->lives_in_memory = false;
if(!file_exists($parameters['path'])) { if(!file_exists($parameters['path'])) {
SQLiteDatabaseConfigurationHelper::create_db_dir($parameters['path']); SQLiteDatabaseConfigurationHelper::create_db_dir($parameters['path']);
SQLiteDatabaseConfigurationHelper::secure_db_dir($parameters['path']); SQLiteDatabaseConfigurationHelper::secure_db_dir($parameters['path']);
@ -91,8 +79,7 @@ class SQLite3Database extends SS_Database {
$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']);
if(method_exists('SQLite3', 'busyTimeout')) $this->dbConn->busyTimeout(60000); 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; $this->database = $dbName;
if(!$this->dbConn) { if(!$this->dbConn) {

View File

@ -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 * 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(); $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;
$file = $parameters['path'];
// use the very lightspeed SQLite In-Memory feature for testing // assumes that the path to dbname will always be provided
if((isset($parameters['memory']) && $parameters['memory']) || !isset($parameters['path'])) { // this is only necessary if we're using a filesystem path, and not an in-memory database
$file = ':memory:'; if($file != ':memory:') {
$this->lives_in_memory = true;
} else {
$file = $parameters['path'] . '/' . $dbName; $file = $parameters['path'] . '/' . $dbName;
$this->lives_in_memory = false;
if(!file_exists($parameters['path'])) { if(!file_exists($parameters['path'])) {
SQLiteDatabaseConfigurationHelper::create_db_dir($parameters['path']); SQLiteDatabaseConfigurationHelper::create_db_dir($parameters['path']);
SQLiteDatabaseConfigurationHelper::secure_db_dir($parameters['path']); SQLiteDatabaseConfigurationHelper::secure_db_dir($parameters['path']);
@ -33,8 +28,7 @@ class SQLitePDODatabase extends SQLite3Database {
$this->dbConn = new PDO("sqlite:$file"); $this->dbConn = new PDO("sqlite:$file");
//By virtue of getting here, the connection is active: $this->active = true;
$this->active=true;
$this->database = $dbName; $this->database = $dbName;
if(!$this->dbConn) { if(!$this->dbConn) {