Skip to content
Snippets Groups Projects
Commit 096cf1ba authored by Tim van Dijen's avatar Tim van Dijen
Browse files

Replace deprecated Sqllite-calls with PDO-calls

parent 82ef51cd
No related branches found
No related tags found
No related merge requests found
...@@ -29,12 +29,11 @@ class sspmod_core_Storage_SQLPermanentStorage { ...@@ -29,12 +29,11 @@ class sspmod_core_Storage_SQLPermanentStorage {
mkdir($sqllitedir); mkdir($sqllitedir);
} }
$dbfile = $sqllitedir . $name . '.sqllite'; $dbfile = 'sqlite:' . $sqllitedir . $name . '.sqlite';
if ($this->db = new \PDO($dbfile)) {
if ($this->db = new SQLiteDatabase($dbfile)) {
$q = @$this->db->query('SELECT key1 FROM data LIMIT 1'); $q = @$this->db->query('SELECT key1 FROM data LIMIT 1');
if ($q === false) { if ($q === false) {
$this->db->queryExec(' $this->db->exec('
CREATE TABLE data ( CREATE TABLE data (
key1 text, key1 text,
key2 text, key2 text,
...@@ -64,50 +63,49 @@ class sspmod_core_Storage_SQLPermanentStorage { ...@@ -64,50 +63,49 @@ class sspmod_core_Storage_SQLPermanentStorage {
$setDuration = ''; $setDuration = '';
if (is_null($duration)) { if (is_null($duration)) {
$setDuration = 'NULL'; $expire = null;
} else { } else {
$setDuration = "'" . sqlite_escape_string(time() + $duration) . "'"; $expire = time() + $duration;
} }
$query = "INSERT INTO data (key1,key2,type,created,updated,expire,value) VALUES (" . $query = "INSERT INTO data (key1, key2, type, created, updated, expire, value)" .
"'" . sqlite_escape_string($key1) . "'," . " VALUES(:key1, :key2, :type, :created, :updated, :expire, :value)";
"'" . sqlite_escape_string($key2) . "'," . $prepared = $this->db->prepare($query);
"'" . sqlite_escape_string($type) . "'," . $data = array(':key1' => $key1, ':key2' => $key2,
"'" . sqlite_escape_string(time()) . "'," . ':type' => $type, ':created' => time(),
"'" . sqlite_escape_string(time()) . "'," . ':updated' => time(), ':expire' => $expire,
$setDuration . "," . ':value' => serialize($value));
"'" . sqlite_escape_string(serialize($value)) . "')"; $prepared->execute($data);
$results = $this->db->queryExec($query); $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
return $results; return $results;
} }
private function update($type, $key1, $key2, $value, $duration = NULL) { private function update($type, $key1, $key2, $value, $duration = NULL) {
$setDuration = ''; $setDuration = '';
if (is_null($duration)) { if (is_null($duration)) {
$setDuration = ", expire = NULL "; $expire = null;
} else { } else {
$setDuration = ", expire = '" . sqlite_escape_string(time() + $duration) . "' "; $expire = time() + $duration;
} }
$query = "UPDATE data SET " . $query = "UPDATE data SET updated = :updated, value = :value, expire = :expire WHERE key1 = :key1 AND key2 = :key2 AND type = :type";
"updated = '" . sqlite_escape_string(time()) . "'," . $prepared = $this->db->prepare($query);
"value = '" . sqlite_escape_string(serialize($value)) . "'" . $data = array(':key1' => $key1, ':key2' => $key2,
$setDuration . ':type' => $type, ':updated' => time(),
"WHERE " . ':expire' => $expire, ':value' => serialize($value));
"key1 = '" . sqlite_escape_string($key1) . "' AND " . $prepared->execute($data);
"key2 = '" . sqlite_escape_string($key2) . "' AND " . $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
"type = '" . sqlite_escape_string($type) . "'"; return $results;
$results = $this->db->queryExec($query);
return $results;
} }
public function get($type = NULL, $key1 = NULL, $key2 = NULL) { public function get($type = NULL, $key1 = NULL, $key2 = NULL) {
$conditions = self::getCondition($type, $key1, $key2);
$condition = self::getCondition($type, $key1, $key2); $query = 'SELECT * FROM data WHERE ' . $conditions;
$query = "SELECT * FROM data WHERE " . $condition;
$results = $this->db->arrayQuery($query, SQLITE_ASSOC);
$prepared = $this->db->prepare($query);
$prepared->execute();
$results = $prepared->fetchAll(PDO::FETCH_ASSOC);
if (count($results) !== 1) return NULL; if (count($results) !== 1) return NULL;
$res = $results[0]; $res = $results[0];
...@@ -125,19 +123,20 @@ class sspmod_core_Storage_SQLPermanentStorage { ...@@ -125,19 +123,20 @@ class sspmod_core_Storage_SQLPermanentStorage {
} }
public function exists($type, $key1, $key2) { public function exists($type, $key1, $key2) {
$query = "SELECT * FROM data WHERE " . $query = 'SELECT * FROM data WHERE type = :type AND key1 = :key1 AND key2 = :key2 LIMIT 1';
"key1 = '" . sqlite_escape_string($key1) . "' AND " . $prepared = $this->db->prepare($query);
"key2 = '" . sqlite_escape_string($key2) . "' AND " . $data = array(':type' => $type, ':key1' => $key1, ':key2' => $key2);
"type = '" . sqlite_escape_string($type) . "' LIMIT 1"; $prepared->execute($data);
$results = $this->db->arrayQuery($query, SQLITE_ASSOC); $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
return (count($results) == 1); return (count($results) == 1);
} }
public function getList($type = NULL, $key1 = NULL, $key2 = NULL) { public function getList($type = NULL, $key1 = NULL, $key2 = NULL) {
$conditions = self::getCondition($type, $key1, $key2);
$condition = self::getCondition($type, $key1, $key2); $query = 'SELECT * FROM data WHERE ' . $conditions;
$query = "SELECT * FROM data WHERE " . $condition; $prepared = $this->db->prepare($query);
$results = $this->db->arrayQuery($query, SQLITE_ASSOC); $prepared->execute();
$results = $prepared->fetchAll(PDO::FETCH_ASSOC);
if (count($results) == 0) return NULL; if (count($results) == 0) return NULL;
foreach($results AS $key => $value) { foreach($results AS $key => $value) {
...@@ -147,14 +146,15 @@ class sspmod_core_Storage_SQLPermanentStorage { ...@@ -147,14 +146,15 @@ class sspmod_core_Storage_SQLPermanentStorage {
} }
public function getKeys($type = NULL, $key1 = NULL, $key2 = NULL, $whichKey = 'type') { public function getKeys($type = NULL, $key1 = NULL, $key2 = NULL, $whichKey = 'type') {
if (!in_array($whichKey, array('key1', 'key2', 'type'), true)) if (!in_array($whichKey, array('key1', 'key2', 'type'), true))
throw new Exception('Invalid key type'); throw new Exception('Invalid key type');
$condition = self::getCondition($type, $key1, $key2); $conditions = self::getCondition($type, $key1, $key2);
$query = 'SELECT DISTINCT :whichKey FROM data WHERE ' . $conditions;
$query = "SELECT DISTINCT " . $whichKey . " FROM data WHERE " . $condition; $prepared = $this->db->prepare($query);
$results = $this->db->arrayQuery($query, SQLITE_ASSOC); $data = array('whichKey' => $whichKey);
$prepared->execute($data);
$results = $prepared->fetchAll(PDO::FETCH_ASSOC);
if (count($results) == 0) return NULL; if (count($results) == 0) return NULL;
...@@ -168,31 +168,30 @@ class sspmod_core_Storage_SQLPermanentStorage { ...@@ -168,31 +168,30 @@ class sspmod_core_Storage_SQLPermanentStorage {
public function remove($type, $key1, $key2) { public function remove($type, $key1, $key2) {
$query = "DELETE FROM data WHERE " . $query = 'DELETE FROM data WHERE type = :type AND key1 = :key1 AND key2 = :key2';
"key1 = '" . sqlite_escape_string($key1) . "' AND " . $prepared = $this->db->prepare($query);
"key2 = '" . sqlite_escape_string($key2) . "' AND " . $data = array(':type' => $type, ':key1' => $key1, ':key2' => $key2);
"type = '" . sqlite_escape_string($type) . "'"; $prepared->execute($data);
$results = $this->db->arrayQuery($query, SQLITE_ASSOC); $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
return (count($results) == 1); return (count($results) == 1);
} }
public function removeExpired() { public function removeExpired() {
$query = "DELETE FROM data WHERE expire NOT NULL AND expire < " . time(); $query = "DELETE FROM data WHERE expire NOT NULL AND expire < :expire";
$this->db->arrayQuery($query, SQLITE_ASSOC); $prepared = $this->db->prepare($query);
$changes = $this->db->changes(); $data = array(':expire' => time());
return $changes; $prepared->execute($data);
return $prepared->rowCount();
} }
/** /**
* Create a SQL condition statement based on parameters * Create a SQL condition statement based on parameters
*/ */
private static function getCondition($type = NULL, $key1 = NULL, $key2 = NULL) { private function getCondition($type = NULL, $key1 = NULL, $key2 = NULL) {
$conditions = array(); $conditions = array();
if (!is_null($type)) $conditions[] = "type = " . $this->db->quote($type);
if (!is_null($type)) $conditions[] = "type = '" . sqlite_escape_string($type) . "'"; if (!is_null($key1)) $conditions[] = "key1 = " . $this->db->quote($key1);
if (!is_null($key1)) $conditions[] = "key1 = '" . sqlite_escape_string($key1) . "'"; if (!is_null($key2)) $conditions[] = "key2 = " . $this->db->quote($key2);
if (!is_null($key2)) $conditions[] = "key2 = '" . sqlite_escape_string($key2) . "'";
if (count($conditions) === 0) return '1'; if (count($conditions) === 0) return '1';
...@@ -200,7 +199,5 @@ class sspmod_core_Storage_SQLPermanentStorage { ...@@ -200,7 +199,5 @@ class sspmod_core_Storage_SQLPermanentStorage {
return $condition; return $condition;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment