Skip to content

Commit

Permalink
Adding PUDL options 'key' (encryption key) and 'readonly'
Browse files Browse the repository at this point in the history
Sqlite now properly throws PUDL exception
Sqlite busy timeout is now configurable through PUDL 'timeout' option
  • Loading branch information
darkain committed Nov 9, 2018
1 parent 29024c2 commit c11d66f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions pudl.php
Expand Up @@ -52,8 +52,10 @@ public function __construct($data, $autoconnect=true) {
if (empty($data['server'])) $data['server'] = 'localhost';
if (empty($data['prefix'])) $data['prefix'] = [];
if (empty($data['persistent'])) $data['persistent'] = false;
if (empty($data['key'])) $data['key'] = NULL;
if (empty($data['salt'])) $data['salt'] = '';
if (empty($data['timeout'])) $data['timeout'] = 10;
if (empty($data['readonly'])) $data['readonly'] = false;

//SET INITIAL DATA
$this->microtime = microtime(true);
Expand Down
27 changes: 21 additions & 6 deletions sqlite/pudlSqlite.php
Expand Up @@ -56,21 +56,36 @@ public static function instance($data=[], $autoconnect=true) {
public function connect() {
$auth = $this->auth();


// Verify we have the Sqlite3 PHP extension installed
pudl_require_extension('sqlite3');

//Create Sqlite3 object instance
$this->connection = new SQLite3($auth['database']);

//Cannot connect - Error out
if (empty($this->connection)) {
// Set READ-ONLY / READ-WRITE access
$flags = $auth['readonly']
? SQLITE3_OPEN_READONLY
: SQLITE3_OPEN_READWRITE;


// Create Sqlite3 object instance
try {
$this->connection = new SQLite3(
$auth['database'],
SQLITE3_OPEN_CREATE | $flags,
$auth['key']
);

// Convert PHP exception to PUDL exception
} catch (Exception $e) {
throw new pudlConnectionException(
$this,
'Unable to open Sqlite database file: ' . $auth['database']
);
}

//Set a busy timeout for Sqlite to 5 seconds
$this->connection->busyTimeout(5000);

// Set a busy timeout for Sqlite to 'timeout' seconds
$this->connection->busyTimeout($auth['timeout'] * 1000);
}


Expand Down

0 comments on commit c11d66f

Please sign in to comment.