-
Notifications
You must be signed in to change notification settings - Fork 0
/
KvLite.php
254 lines (224 loc) · 6.51 KB
/
KvLite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
* An open source application development framework for PHP
*
* @category RoboTaMeR
* @author Dennis T Kaplan
* @copyright Copyright (c) 2008 - 2011, Dennis T Kaplan
* @license http://RoboTamer.com/license.html
* @link http://RoboTamer.com
*/
/**
* Sqlite key value database
*
* @category RoboTaMeR
* @package KvLite
* @author Dennis T Kaplan
* @copyright Copyright (c) 20010 - 2011, Dennis T Kaplan
* @license http://RoboTamer.com/license.html
* @link http://RoboTamer.com
*/
class KvLite {
private static $instance = NULL;
protected $db;
protected $file;
public $msg = array();
public static function get($cat, $key = FALSE) {
$rows = $dbdata = FALSE;
$dbdata = self::dbGet($cat, $key);
if(is_array($dbdata)){
foreach($dbdata as $k=>$v){
if(is_array($v['value'])){
$rows[$k]['Name']=$v['key'];
foreach($v['value'] as $kk=>$kv)
$rows[$k][$kk]=$kv;
}elseif($key === FALSE){
$rows[$v['key']] = $v['value'];
}else{
$rows = $v['value'];
}
}
$dbdata = $rows;
}
return $dbdata;
}
public static function dbGet($cat, $key = FALSE)
{
$kv = self::getInstance();
if(!is_object($kv->db)) $kv->connect();
$result = $row = NULL;
$data = array('cat'=>$cat);
if($key !== FALSE){
$data['key'] = $key;
$key = ' AND key = :key';
}else{
$key = '';
}
$sql = "SELECT * FROM kv WHERE cat = :cat $key";
$stmt = $kv->db->prepare($sql);
foreach($data as $k => $v){
$stmt->bindValue(':'.$k, $v);
}unset($k,$v);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC) ;
unset($stmt);
foreach($result as $k=>$v){
if(strpos($v['value'], '~StrSafe~>') > 0){
$strsafe = new Strsafe();
$result[$k]['value'] = $strsafe->run($v['value']);
}
}
return $result;
}
public function delete($cat,$key){
$sql="DELETE FROM [kv] WHERE cat=? AND key= ?";
$sth = $this->db->prepare($sql);
return $sth->execute(array($cat, $key));
}
public function deleteSet($cat){
$sql="DELETE FROM [kv] WHERE cat=?";
$sth = $this->db->prepare($sql);
return $sth->execute(array($cat));
}
public static function save($cat,$key,$value){
$kv = self::getInstance();
if(!is_object($kv->db)) $kv->connect();
$cleansedType = self::getType($value);
$type = $cleansedType['type'];
$value = $cleansedType['value'];
if($type == 'array' || $type == 'object'){
$strsafe = new StrSafe();
$value = $strsafe->run($value);
}
$sql="SELECT count(*) FROM [kv] WHERE cat == ? AND key == ? ";
$sth = $kv->db->prepare($sql);
$sth->execute(array($cat, $key));;
$rows = $sth->fetch(PDO::FETCH_NUM);
if ($rows[0]>0){
return $kv->update($cat,$key,$value);
}else{
return $kv->insert($cat,$key,$value);
}
}
protected function insert($cat,$key,$value){
$sql = "INSERT INTO [kv] (cat,key,value) VALUES (?,?,?)";
$sth = $this->db->prepare($sql);
return $sth->execute(array($cat, $key, $value));
}
protected function update($cat,$key,$value){
$sql = "UPDATE [kv] SET value = :value WHERE cat = :cat AND key = :key";
$sth = $this->db->prepare($sql);
return $sth->execute(array(':cat'=>$cat,':key'=>$key,':value'=>$value));
}
/**
* Insert a one dimentional array as key value set in to the database
*
* @param string $cat
* @param array $array
* @return bool TRUE FALSE
*/
public static function insertArray($cat,$array){
if(!self::isMultiArray($array)){
$kv = self::getInstance();
if(!is_object($kv->db)) $kv->connect();
try {
$kv->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$kv->db->beginTransaction();
foreach ($array as $key=>$value)
$kv->save($cat,$key,$value);
$kv->db->commit();
} catch (Exception $e){
$kv->db->rollBack();
$kv->msg[] = "Failed: " . $e->getMessage();
return FALSE;
}
return TRUE;
}else{
$kv->msg[] = 'You can only insert a one dimentional array with insertArray!';
return FALSE;
}
}
protected function numRows($cat){
$sql="SELECT count(*) FROM [kv] WHERE cat == ? ";
$sth = $this->db->prepare($sql);
$sth->execute(array($cat));
$rows = $sth->fetch(PDO::FETCH_NUM);
return $rows[0];
} // PDO SQLITE3
public static function getType($value, $max_length = 50){
$type = gettype($value);
if($type == 'NULL'
|| $type == 'boolean'
|| $type == 'integer'
|| $type == 'double'
|| $type == 'object'
|| $type == 'resource'
|| $type == 'array'
)
return array('type'=>$type,'value'=>$value);
if($type == 'string' && empty($value))
return array('type'=>'NULL','value'=>$value);
if($type == 'string' && strlen($value) > $max_length)
return array('type'=>'blob','value'=>$value);
if($type == 'string' && substr($value, 0,1) === '0')
return array('type'=>'string','value'=>$value);
if($type == 'string' && is_numeric($value)){
$int = (int) $value;
$float = (float) $value;
if($int == $value){
$value = $int;
$type = 'integer';
}elseif($float == $value){
$value = $float;
$type = 'double';
}
}elseif($type == 'string'){
$type = 'string';
}else{
$type = 'blob';
}
return array('type'=>$type,'value'=>$value);
}
public static function isMultiArray($a){
foreach($a as $v) if(is_array($v)) return TRUE;
return FALSE;
}
private function __construct(){}
protected static function getInstance(){
if(self::$instance === NULL){
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
public function __clone(){
trigger_error('Cloning is not allowed. ', E_USER_ERROR);
}
public function __destruct(){
$kvlite = self::getInstance();
if(isset($kvlite->db) && is_object($kvlite->db))
$kvlite->db = NULL;
}
protected function connect(){
if(is_object($this->db)) return $this->db;
if($this->file === NULL) $this->file = self::setFile();
$tables = array();
try {
$this->db = new PDO('sqlite:'.$this->file);
$sql = "SELECT name FROM sqlite_master WHERE type='table' UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table' ORDER BY name";
$result = $this->db->query($sql);
foreach ($result as $row) $tables[] = current($row);
if( ! in_array('kv', $tables)) {
if($this->db->exec('CREATE TABLE [kv] (cat, key, value)') === FALSE) die(print_r($this->db->errorInfo(), true));;
chmod($this->file,00666);
}
} catch (Exception $e) {
trigger_error("Database Connection failed: $this->file could not be created. Write Error!".PHP_EOL.$e->getMessage());
}
}
public static function setFile($file = 'kv.db3'){
$file = ROOT.'data/'.$file;
return $file;
}
}
?>