Example #1
0
 function connectDatabase()
 {
     $this->enum_map = array();
     $parameters = $this->parameters;
     $dbName = !isset($this->database) ? $parameters['database'] : ($dbName = $this->database);
     //assumes that the path to dbname will always be provided:
     $file = $parameters['path'] . '/' . $dbName;
     // use the very lightspeed SQLite In-Memory feature for testing
     if (SapphireTest::using_temp_db() && $parameters['memory']) {
         $file = ':memory:';
         $this->lives_in_memory = true;
     } else {
         $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 PDO("sqlite:{$file}");
     //By virtue of getting here, the connection is active:
     $this->active = true;
     $this->database = $dbName;
     if (!$this->dbConn) {
         $this->databaseError("Couldn't connect to SQLite3 database");
         return false;
     }
     foreach (self::$default_pragma as $pragma => $value) {
         $this->pragma($pragma, $value);
     }
     if (empty(self::$default_pragma['locking_mode'])) {
         self::$default_pragma['locking_mode'] = $this->pragma('locking_mode');
     }
     return true;
 }
Example #2
0
 /**
  * Connect to a SQLite3 database.
  * @param array $parameters An map of parameters, which should include:
  *  - database: The database to connect to, with the correct file extension (.sqlite)
  *  - path: the path to the SQLite3 database file
  *  - key: the encryption key (needs testing)
  *  - memory: use the faster In-Memory database for unit tests
  */
 public function connect($parameters)
 {
     if (!empty($parameters['memory'])) {
         Deprecation::notice('1.4.0', "\$databaseConfig['memory'] is deprecated. Use \$databaseConfig['path'] = ':memory:' instead.", Deprecation::SCOPE_GLOBAL);
         unset($parameters['memory']);
         $parameters['path'] = ':memory:';
     }
     //We will store these connection parameters for use elsewhere (ie, unit tests)
     $this->parameters = $parameters;
     $this->schemaManager->flushCache();
     // Ensure database name is set
     if (empty($parameters['database'])) {
         $parameters['database'] = 'database' . self::database_extension();
     }
     $dbName = $parameters['database'];
     if (!self::is_valid_database_name($dbName)) {
         // If not using the correct file extension for database files then the
         // results of SQLite3SchemaManager::databaseList will be unpredictable
         $extension = self::database_extension();
         Deprecation::notice('3.2', "SQLite3Database now expects a database file with extension \"{$extension}\". Behaviour may be unpredictable otherwise.");
     }
     // use the very lightspeed SQLite In-Memory feature for testing
     if ($this->getLivesInMemory()) {
         $file = ':memory:';
     } else {
         // Ensure path is given
         if (empty($parameters['path'])) {
             $parameters['path'] = ASSETS_PATH . '/.sqlitedb';
         }
         //assumes that the path to dbname will always be provided:
         $file = $parameters['path'] . '/' . $dbName;
         if (!file_exists($parameters['path'])) {
             SQLiteDatabaseConfigurationHelper::create_db_dir($parameters['path']);
             SQLiteDatabaseConfigurationHelper::secure_db_dir($parameters['path']);
         }
     }
     // 'path' and 'database' are merged into the full file path, which
     // is the format that connectors such as PDOConnector expect
     $parameters['filepath'] = $file;
     // Ensure that driver is available (required by PDO)
     if (empty($parameters['driver'])) {
         $parameters['driver'] = $this->getDatabaseServer();
     }
     $this->connector->connect($parameters, true);
     foreach (self::$default_pragma as $pragma => $value) {
         $this->setPragma($pragma, $value);
     }
     if (empty(self::$default_pragma['locking_mode'])) {
         self::$default_pragma['locking_mode'] = $this->getPragma('locking_mode');
     }
 }