This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
JsonDB.class.php
207 lines (176 loc) · 5.7 KB
/
JsonDB.class.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
<?php
// Straussn's JSON-Databaseclass
// Handle JSON-Files like a very, very simple DB. Useful for little ajax applications.
// Last change: 05-06-2012
// Version: 1.0b
// by Manuel Strauss, Web: http://straussn.eu, E-Mail: [email protected], Skype: StrZlee
/*
Example:
$db = new JsonDB( "./path_to_my_jsonfiles/" );
$result = $db -> select( "json_file_name_without_extension", "search-key", "search-value" );
Example JSON-File:
[
{"ID": "0", "Name": "Hans Wurst", "Age": "12"},
{"ID": "1", "Name": "Karl Stoascheissa", "Age": "15"},
{"ID": "2", "Name": "Poidl Peidlbecka", "Age": "14"}
]
Method Overview:
new JsonDB(".(path_to_my_jsonfiles/");
JsonDB -> createTable("hello_world_table");
JsonDB -> select ( "table", "key", "value" ) - Selects multible lines which contains the key/value and returns it as array
JsonDB -> selectAll ( "table" ) - Returns the entire file as array
JsonDB -> update ( "table", "key", "value", ARRAY ) - Replaces the line which corresponds to the key/value with the array-data
JsonDB -> updateAll ( "table", ARRAY ) - Replaces the entire file with the array-data
JsonDB -> insert ( "table", ARRAY , $create = FALSE) - Appends a row, returns true on success. if $create is TRUE, we will create the table if it doesn't already exist.
JsonDB -> delete ( "table", "key", "value" ) - Deletes all lines which corresponds to the key/value, returns number of deleted lines
JsonDB -> deleteAll ( "table" ) - Deletes the whole data, returns "true" on success
new JsonTable("./data/test.json", $create = FALSE) - If $create is TRUE, creates table if it doesn't exist.
*/
class JsonTable {
protected $jsonFile;
protected $fileHandle;
protected $fileData = array();
public function __construct($_jsonFile, $create = false) {
if (!file_exists($_jsonFile)) {
if($create === true)
{
$this->createTable($_jsonFile, true);
}
else
{
throw new Exception("JsonTable Error: Table not found: ".$_jsonFile);
}
}
$this->jsonFile = $_jsonFile;
$this->fileData = json_decode(file_get_contents($this->jsonFile), true);
$this->lockFile();
}
public function __destruct() {
$this->save();
fclose($this->fileHandle);
}
protected function lockFile() {
$handle = fopen($this->jsonFile, "c");
if (flock($handle, LOCK_EX)) $this->fileHandle = $handle;
else throw new Exception("JsonTable Error: Can't set file-lock");
}
protected function save() {
if (ftruncate($this->fileHandle, 0) && fwrite($this->fileHandle, json_encode($this->fileData))) return true;
else throw new Exception("JsonTable Error: Can't write data to: ".$this->jsonFile);
}
public function selectAll() {
return $this->fileData;
}
public function select($key, $val = 0) {
$result = array();
if (is_array($key)) $result = $this->select($key[1], $key[2]);
else {
$data = $this->fileData;
foreach($data as $_key => $_val) {
if (isset($data[$_key][$key])) {
if ($data[$_key][$key] == $val) {
$result[] = $data[$_key];
}
}
}
}
return $result;
}
public function updateAll($data = array()) {
if (isset($data[0]) && substr_compare($data[0],$this->jsonFile,0)) $data = $data[1];
return $this->fileData = empty($data) ? array() : $data;
}
public function update($key, $val = 0, $newData = array()) {
$result = false;
if (is_array($key)) $result = $this->update($key[1], $key[2], $key[3]);
else {
$data = $this->fileData;
foreach($data as $_key => $_val) {
if (isset($data[$_key][$key])) {
if ($data[$_key][$key] == $val) {
$data[$_key] = $newData;
$result = true;
break;
}
}
}
if ($result) $this->fileData = $data;
}
return $result;
}
public function insert($data = array(), $create = false) {
if (isset($data[0]) && substr_compare($data[0],$this->jsonFile,0)) $data = $data[1];
$this->fileData[] = $data;
return true;
}
public function deleteAll() {
$this->fileData = array();
return true;
}
public function delete($key, $val = 0) {
$result = 0;
if (is_array($key)) $result = $this->delete($key[1], $key[2]);
else {
$data = $this->fileData;
foreach($data as $_key => $_val) {
if (isset($data[$_key][$key])) {
if ($data[$_key][$key] == $val) {
unset($data[$_key]);
$result++;
}
}
}
if ($result) {
sort($data);
$this->fileData = $data;
}
}
return $result;
}
public function createTable($tablePath) {
if(is_array($tablePath)) $tablePath = $tablePath[0];
if(file_exists($tablePath))
throw new Exception("Table already exists: ".$tablePath);
if(fclose(fopen($tablePath, 'a')))
{
return true;
}
else
{
throw new Exception("New table couldn't be created: ".$tablePath);
}
}
}
class JsonDB {
protected $path = "./";
protected $fileExt = ".json";
protected $tables = array();
public function __construct($path) {
if (is_dir($path)) $this->path = $path;
else throw new Exception("JsonDB Error: Database not found");
}
protected function getTableInstance($table, $create) {
if (isset($tables[$table])) return $tables[$table];
else return $tables[$table] = new JsonTable($this->path.$table, $create);
}
public function __call($op, $args) {
if ($args && method_exists("JsonTable", $op)) {
$table = $args[0].$this->fileExt;
$create = false;
if($op == "createTable")
{
return $this->getTableInstance($table, true);
}
elseif($op == "insert" && isset($args[2]) && $args[2] === true)
{
$create = true;
}
return $this->getTableInstance($table, $create)->$op($args);
} else throw new Exception("JsonDB Error: Unknown method or wrong arguments ");
}
public function setExtension($_fileExt) {
$this->fileExt = $_fileExt;
return $this;
}
}
?>