BUGFIX: protected default db location MINOR: polished adapter setup

This commit is contained in:
Andreas Piening 2010-02-08 04:20:09 +00:00
parent 8564804d77
commit b85db20c88
4 changed files with 21 additions and 8 deletions

4
README
View File

@ -15,9 +15,9 @@ SilverStripe 2.4 or newer
Installation Installation
------------ ------------
copy the sqlite3 folder to your project root so that it becomes a sibling of cms, sapphire and co 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! you are done!

View File

@ -1,11 +1,14 @@
<?php <?php
if(defined('SS_DATABASE_CLASS') && (SS_DATABASE_CLASS == 'SQLiteDatabase' || SS_DATABASE_CLASS == 'SQLite3Database' || SS_DATABASE_CLASS == 'SQLitePDODatabase')) {
global $databaseConfig; global $databaseConfig;
if(defined('SS_DATABASE_CLASS')) $databaseConfig['type'] = SS_DATABASE_CLASS;
if(array_search($databaseConfig['type'], array('SQLiteDatabase', 'SQLite3Database', 'SQLitePDODatabase')) !== false) {
$databaseConfig = array( $databaseConfig = array(
'type' => $databaseConfig['type'],
'database' => (defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : '') . $database . (defined('SS_DATABASE_SUFFIX') ? SS_DATABASE_SUFFIX : ''), '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 'memory' => true, // run tests in memory
); );
@ -18,9 +21,8 @@ if(defined('SS_DATABASE_CLASS') && (SS_DATABASE_CLASS == 'SQLiteDatabase' || SS_
'locking_mode' => 'NORMAL', 'locking_mode' => 'NORMAL',
); );
// The SQLite3 class is available in PHP 5.3 and newer // 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'; $databaseConfig['type'] = 'SQLitePDODatabase';
} else { } else {
$databaseConfig['type'] = 'SQLite3Database'; $databaseConfig['type'] = 'SQLite3Database';

View File

@ -88,6 +88,8 @@ class SQLite3Database extends SS_Database {
$this->lives_in_memory = false; $this->lives_in_memory = false;
} }
self::safe_dir($parameters['path']);
$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']);
//By virtue of getting here, the connection is active: //By virtue of getting here, the connection is active:
@ -115,6 +117,13 @@ class SQLite3Database extends SS_Database {
return null; 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 * Returns true if this database supports collations
* TODO: get rid of this? * TODO: get rid of this?
@ -338,7 +347,7 @@ 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 TO \"$newTableName\"");
} }

View File

@ -29,6 +29,8 @@ class SQLitePDODatabase extends SQLite3Database {
$this->lives_in_memory = false; $this->lives_in_memory = false;
} }
self::safe_dir($parameters['path']);
$this->dbConn = new PDO("sqlite:$file"); $this->dbConn = new PDO("sqlite:$file");
//By virtue of getting here, the connection is active: //By virtue of getting here, the connection is active: