22 $datadir =
$config->getPathValue(
'datadir',
'data/');
24 if (!is_dir($datadir)) {
25 throw new Exception(
'Data directory ['.$datadir.
'] does not exist');
26 }
else if (!is_writable($datadir)) {
27 throw new Exception(
'Data directory ['.$datadir.
'] is not writable');
30 $sqllitedir = $datadir.
'sqllite/';
31 if (!is_dir($sqllitedir)) {
35 $dbfile =
'sqlite:'.$sqllitedir.$name.
'.sqlite';
36 if ($this->db =
new \
PDO($dbfile)) {
37 $q = @$this->db->query(
'SELECT key1 FROM data LIMIT 1');
48 PRIMARY KEY (key1,key2,type) 53 throw new Exception(
'Error creating SQL lite database ['.$dbfile.
'].');
57 public function set(
$type, $key1, $key2, $value, $duration = null)
60 $this->
update(
$type, $key1, $key2, $value, $duration);
62 $this->
insert(
$type, $key1, $key2, $value, $duration);
66 private function insert(
$type, $key1, $key2, $value, $duration = null)
68 $expire = is_null($duration) ? null : (time() + $duration);
70 $query =
"INSERT INTO data (key1, key2, type, created, updated, expire, value)".
71 " VALUES(:key1, :key2, :type, :created, :updated, :expire, :value)";
72 $prepared = $this->db->prepare(
$query);
73 $data = array(
':key1' => $key1,
':key2' => $key2,
74 ':type' =>
$type,
':created' => time(),
75 ':updated' => time(),
':expire' =>
$expire,
76 ':value' => serialize($value));
77 $prepared->execute(
$data);
78 $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
82 private function update(
$type, $key1, $key2, $value, $duration = null)
84 $expire = is_null($duration) ? null : (time() + $duration);
86 $query =
"UPDATE data SET updated = :updated, value = :value, expire = :expire WHERE key1 = :key1 AND key2 = :key2 AND type = :type";
87 $prepared = $this->db->prepare(
$query);
88 $data = array(
':key1' => $key1,
':key2' => $key2,
89 ':type' =>
$type,
':updated' => time(),
90 ':expire' =>
$expire,
':value' => serialize($value));
91 $prepared->execute(
$data);
92 $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
96 public function get(
$type = null, $key1 = null, $key2 = null)
98 $conditions = self::getCondition(
$type, $key1, $key2);
99 $query =
'SELECT * FROM data WHERE '.$conditions;
101 $prepared = $this->db->prepare(
$query);
102 $prepared->execute();
103 $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
109 $res[
'value'] = unserialize(
$res[
'value']);
122 return $res[
'value'];
127 $query =
'SELECT * FROM data WHERE type = :type AND key1 = :key1 AND key2 = :key2 LIMIT 1';
128 $prepared = $this->db->prepare(
$query);
129 $data = array(
':type' =>
$type,
':key1' => $key1,
':key2' => $key2);
130 $prepared->execute(
$data);
131 $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
137 $conditions = self::getCondition(
$type, $key1, $key2);
138 $query =
'SELECT * FROM data WHERE '.$conditions;
139 $prepared = $this->db->prepare(
$query);
140 $prepared->execute();
142 $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
153 public function getKeys(
$type = null, $key1 = null, $key2 = null, $whichKey =
'type')
155 if (!in_array($whichKey, array(
'key1',
'key2',
'type'),
true)) {
159 $conditions = self::getCondition(
$type, $key1, $key2);
160 $query =
'SELECT DISTINCT :whichKey FROM data WHERE '.$conditions;
161 $prepared = $this->db->prepare(
$query);
162 $data = array(
'whichKey' => $whichKey);
163 $prepared->execute(
$data);
164 $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
172 $resarray[] = $value[$whichKey];
177 public function remove(
$type, $key1, $key2)
179 $query =
'DELETE FROM data WHERE type = :type AND key1 = :key1 AND key2 = :key2';
180 $prepared = $this->db->prepare(
$query);
181 $data = array(
':type' =>
$type,
':key1' => $key1,
':key2' => $key2);
182 $prepared->execute(
$data);
183 $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
189 $query =
"DELETE FROM data WHERE expire IS NOT NULL AND expire < :expire";
190 $prepared = $this->db->prepare(
$query);
191 $data = array(
':expire' => time());
192 $prepared->execute(
$data);
193 return $prepared->rowCount();
201 $conditions = array();
202 if (!is_null(
$type)) {
203 $conditions[] =
"type = ".$this->db->quote(
$type);
205 if (!is_null($key1)) {
206 $conditions[] =
"key1 = ".$this->db->quote($key1);
208 if (!is_null($key2)) {
209 $conditions[] =
"key2 = ".$this->db->quote($key2);
212 $conditions[] =
"(expire IS NULL OR expire >= ".time().
")";
213 return join(
' AND ', $conditions);
update($type, $key1, $key2, $value, $duration=null)
insert($type, $key1, $key2, $value, $duration=null)
getValue($type=null, $key1=null, $key2=null)
foreach($_POST as $key=> $value) $res
exists($type, $key1, $key2)
__construct($name, $config=null)
getCondition($type=null, $key1=null, $key2=null)
Create a SQL condition statement based on parameters.
getList($type=null, $key1=null, $key2=null)
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
getKeys($type=null, $key1=null, $key2=null, $whichKey='type')