diff --git a/README b/README index 1a3d717..e320ba4 100644 --- a/README +++ b/README @@ -15,9 +15,9 @@ SilverStripe 2.4 or newer Installation ------------ copy the sqlite3 folder to your project root so that it becomes a sibling of cms, sapphire and co -add this to your _config.php +add this to your _config.php (right after "require_once("conf/ConfigureFromEnv.php");" if you are using _ss_environment.php) - define('SS_DATABASE_CLASS','SQLiteDatabase'); + $databaseConfig['type'] = 'SQLiteDatabase'; you are done! diff --git a/_config.php b/_config.php index 6cb1368..1d9f4b7 100644 --- a/_config.php +++ b/_config.php @@ -1,11 +1,14 @@ $databaseConfig['type'], 'database' => (defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : '') . $database . (defined('SS_DATABASE_SUFFIX') ? SS_DATABASE_SUFFIX : ''), - 'path' => defined('SS_SQLITE_DATABASE_PATH') && SS_SQLITE_DATABASE_PATH ? SS_SQLITE_DATABASE_PATH : ASSETS_PATH, // where to put the database file + 'path' => defined('SS_SQLITE_DATABASE_PATH') && SS_SQLITE_DATABASE_PATH ? SS_SQLITE_DATABASE_PATH : ASSETS_PATH . '/.sqlitedb/', // where to put the database file 'memory' => true, // run tests in memory ); @@ -17,10 +20,9 @@ if(defined('SS_DATABASE_CLASS') && (SS_DATABASE_CLASS == 'SQLiteDatabase' || SS_ 'encoding' => '"UTF-8"', 'locking_mode' => 'NORMAL', ); - // The SQLite3 class is available in PHP 5.3 and newer - if(SS_DATABASE_CLASS == 'SQLitePDODatabase' || version_compare(phpversion(), '5.3.0', '<')) { + if($databaseConfig['type'] == 'SQLitePDODatabase' || version_compare(phpversion(), '5.3.0', '<')) { $databaseConfig['type'] = 'SQLitePDODatabase'; } else { $databaseConfig['type'] = 'SQLite3Database'; diff --git a/code/SQLite3Database.php b/code/SQLite3Database.php index ab431cb..b9b91ed 100644 --- a/code/SQLite3Database.php +++ b/code/SQLite3Database.php @@ -87,6 +87,8 @@ class SQLite3Database extends SS_Database { } else { $this->lives_in_memory = false; } + + self::safe_dir($parameters['path']); $this->dbConn = new SQLite3($file, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $parameters['key']); @@ -115,6 +117,13 @@ class SQLite3Database extends SS_Database { return null; } + public static function safe_dir($path) { + if($path == ASSETS_PATH . '/.sqlitedb/' && !file_exists($path)) { + mkdir($path); + file_put_contents($path . '.htaccess', 'deny from all'); + } + } + /** * Returns true if this database supports collations * TODO: get rid of this? @@ -338,7 +347,7 @@ class SQLite3Database extends SS_Database { public function renameTable($oldTableName, $newTableName) { - $this->query("ALTER TABLE \"$oldTableName\" RENAME \"$newTableName\""); + $this->query("ALTER TABLE \"$oldTableName\" RENAME TO \"$newTableName\""); } diff --git a/code/SQLitePDODatabase.php b/code/SQLitePDODatabase.php index 9cc2fc6..3de80d3 100644 --- a/code/SQLitePDODatabase.php +++ b/code/SQLitePDODatabase.php @@ -29,6 +29,8 @@ class SQLitePDODatabase extends SQLite3Database { $this->lives_in_memory = false; } + self::safe_dir($parameters['path']); + $this->dbConn = new PDO("sqlite:$file"); //By virtue of getting here, the connection is active: