ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SQLPermanentStorage.php
Go to the documentation of this file.
1<?php
2
13
14 private $db;
15
16 function __construct($name, $config = NULL) {
17 if (is_null($config))
19
20 $datadir = $config->getPathValue('datadir', 'data/');
21
22 if (!is_dir($datadir))
23 throw new Exception('Data directory [' . $datadir. '] does not exist');
24 if (!is_writable($datadir))
25 throw new Exception('Data directory [' . $datadir. '] is not writable');
26
27 $sqllitedir = $datadir . 'sqllite/';
28 if (!is_dir($sqllitedir)) {
29 mkdir($sqllitedir);
30 }
31
32 $dbfile = $sqllitedir . $name . '.sqllite';
33
34 if ($this->db = new SQLiteDatabase($dbfile)) {
35 $q = @$this->db->query('SELECT key1 FROM data LIMIT 1');
36 if ($q === false) {
37 $this->db->queryExec('
38 CREATE TABLE data (
39 key1 text,
40 key2 text,
41 type text,
42 value text,
43 created timestamp,
44 updated timestamp,
45 expire timestamp,
46 PRIMARY KEY (key1,key2,type)
47 );
48 ');
49 }
50 } else {
51 throw new Exception('Error creating SQL lite database [' . $dbfile . '].');
52 }
53 }
54
55 public function set($type, $key1, $key2, $value, $duration = NULL) {
56 if ($this->exists($type, $key1, $key2)) {
57 $this->update($type, $key1, $key2, $value, $duration);
58 } else {
59 $this->insert($type, $key1, $key2, $value, $duration);
60 }
61 }
62
63 private function insert($type, $key1, $key2, $value, $duration = NULL) {
64
65 $setDuration = '';
66 if (is_null($duration)) {
67 $setDuration = 'NULL';
68 } else {
69 $setDuration = "'" . sqlite_escape_string(time() + $duration) . "'";
70 }
71
72 $query = "INSERT INTO data (key1,key2,type,created,updated,expire,value) VALUES (" .
73 "'" . sqlite_escape_string($key1) . "'," .
74 "'" . sqlite_escape_string($key2) . "'," .
75 "'" . sqlite_escape_string($type) . "'," .
76 "'" . sqlite_escape_string(time()) . "'," .
77 "'" . sqlite_escape_string(time()) . "'," .
78 $setDuration . "," .
79 "'" . sqlite_escape_string(serialize($value)) . "')";
80 $results = $this->db->queryExec($query);
81 return $results;
82 }
83
84 private function update($type, $key1, $key2, $value, $duration = NULL) {
85
86 $setDuration = '';
87 if (is_null($duration)) {
88 $setDuration = ", expire = NULL ";
89 } else {
90 $setDuration = ", expire = '" . sqlite_escape_string(time() + $duration) . "' ";
91 }
92
93 $query = "UPDATE data SET " .
94 "updated = '" . sqlite_escape_string(time()) . "'," .
95 "value = '" . sqlite_escape_string(serialize($value)) . "'" .
96 $setDuration .
97 "WHERE " .
98 "key1 = '" . sqlite_escape_string($key1) . "' AND " .
99 "key2 = '" . sqlite_escape_string($key2) . "' AND " .
100 "type = '" . sqlite_escape_string($type) . "'";
101 $results = $this->db->queryExec($query);
102 return $results;
103 }
104
105 public function get($type = NULL, $key1 = NULL, $key2 = NULL) {
106
107 $condition = self::getCondition($type, $key1, $key2);
108 $query = "SELECT * FROM data WHERE " . $condition;
109 $results = $this->db->arrayQuery($query, SQLITE_ASSOC);
110
111 if (count($results) !== 1) return NULL;
112
113 $res = $results[0];
114 $res['value'] = unserialize($res['value']);
115 return $res;
116 }
117
118 /*
119 * Return the value directly (not in a container)
120 */
121 public function getValue($type = NULL, $key1 = NULL, $key2 = NULL) {
122 $res = $this->get($type, $key1, $key2);
123 if ($res === NULL) return NULL;
124 return $res['value'];
125 }
126
127 public function exists($type, $key1, $key2) {
128 $query = "SELECT * FROM data WHERE " .
129 "key1 = '" . sqlite_escape_string($key1) . "' AND " .
130 "key2 = '" . sqlite_escape_string($key2) . "' AND " .
131 "type = '" . sqlite_escape_string($type) . "' LIMIT 1";
132 $results = $this->db->arrayQuery($query, SQLITE_ASSOC);
133 return (count($results) == 1);
134 }
135
136 public function getList($type = NULL, $key1 = NULL, $key2 = NULL) {
137
138 $condition = self::getCondition($type, $key1, $key2);
139 $query = "SELECT * FROM data WHERE " . $condition;
140 $results = $this->db->arrayQuery($query, SQLITE_ASSOC);
141 if (count($results) == 0) return NULL;
142
143 foreach($results AS $key => $value) {
144 $results[$key]['value'] = unserialize($results[$key]['value']);
145 }
146 return $results;
147 }
148
149 public function getKeys($type = NULL, $key1 = NULL, $key2 = NULL, $whichKey = 'type') {
150
151 if (!in_array($whichKey, array('key1', 'key2', 'type'), true))
152 throw new Exception('Invalid key type');
153
154 $condition = self::getCondition($type, $key1, $key2);
155
156 $query = "SELECT DISTINCT " . $whichKey . " FROM data WHERE " . $condition;
157 $results = $this->db->arrayQuery($query, SQLITE_ASSOC);
158
159 if (count($results) == 0) return NULL;
160
161 $resarray = array();
162 foreach($results AS $key => $value) {
163 $resarray[] = $value[$whichKey];
164 }
165
166 return $resarray;
167 }
168
169
170 public function remove($type, $key1, $key2) {
171 $query = "DELETE FROM data WHERE " .
172 "key1 = '" . sqlite_escape_string($key1) . "' AND " .
173 "key2 = '" . sqlite_escape_string($key2) . "' AND " .
174 "type = '" . sqlite_escape_string($type) . "'";
175 $results = $this->db->arrayQuery($query, SQLITE_ASSOC);
176 return (count($results) == 1);
177 }
178
179 public function removeExpired() {
180 $query = "DELETE FROM data WHERE expire NOT NULL AND expire < " . time();
181 $this->db->arrayQuery($query, SQLITE_ASSOC);
182 $changes = $this->db->changes();
183 return $changes;
184 }
185
186
190 private static function getCondition($type = NULL, $key1 = NULL, $key2 = NULL) {
191 $conditions = array();
192
193 if (!is_null($type)) $conditions[] = "type = '" . sqlite_escape_string($type) . "'";
194 if (!is_null($key1)) $conditions[] = "key1 = '" . sqlite_escape_string($key1) . "'";
195 if (!is_null($key2)) $conditions[] = "key2 = '" . sqlite_escape_string($key2) . "'";
196
197 if (count($conditions) === 0) return '1';
198
199 $condition = join(' AND ', $conditions);
200
201 return $condition;
202 }
203
204
205}
206
An exception for terminatinating execution or to throw for unit testing.
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
getKeys($type=NULL, $key1=NULL, $key2=NULL, $whichKey='type')
getValue($type=NULL, $key1=NULL, $key2=NULL)
update($type, $key1, $key2, $value, $duration=NULL)
getList($type=NULL, $key1=NULL, $key2=NULL)
static getCondition($type=NULL, $key1=NULL, $key2=NULL)
Create a SQL condition statement based on parameters.
insert($type, $key1, $key2, $value, $duration=NULL)
$key
Definition: croninfo.php:18
if($format !==null) $name
Definition: metadata.php:146
$query
$type
foreach($_POST as $key=> $value) $res
$results
Definition: svg-scanner.php:47