├── JSON2SQL.php ├── README.md ├── test.db └── test.php /JSON2SQL.php: -------------------------------------------------------------------------------- 1 | debug = false; 23 | 24 | // save table name 25 | $this->tableName = $tableName; 26 | 27 | // save database file 28 | $this->SQLiteFile = $sqlitefile; 29 | $this->dbHandler = new SQLite3($this->SQLiteFile); 30 | 31 | // Clear result 32 | $this->result = array(); 33 | 34 | } 35 | 36 | // 37 | // @function open or close debug mode 38 | // 39 | public function debugMode($mode) 40 | { 41 | $this->debug = $mode; 42 | } 43 | 44 | // 45 | // @function output debug infomation 46 | // 47 | private function _D($msg) 48 | { 49 | if($this->debug) 50 | { 51 | echo "DEBUG >>> "; 52 | echo $msg . "\n"; 53 | } 54 | } 55 | 56 | // 57 | // @function output as JSON 58 | // 59 | public function toJSON() 60 | { 61 | return json_encode($this->result); 62 | } 63 | 64 | // 65 | // @function output as JSON array 66 | // 67 | public function toJSONArray() 68 | { 69 | return json_encode($this->result); 70 | } 71 | 72 | // 73 | // @function output as JSON Object 74 | // 75 | public function toJSONObject() 76 | { 77 | return json_encode($this->result, JSON_FORCE_OBJECT); 78 | } 79 | 80 | // 81 | // @function Select table by manual 82 | // @param $tableName(String) The new tabel name 83 | // 84 | public function selectTable($tableName) 85 | { 86 | $this->tableName = $tableName; 87 | } 88 | 89 | // 90 | // @function Drop table 91 | // @param $tableName(String) Drop table name 92 | // 93 | public function dropTable($tableName = null) 94 | { 95 | $_tableName = empty($tableName) ? $this->tableName : $tableName; 96 | $sql = "DROP TABLE IF EXISTS $_tableName"; 97 | 98 | $this->dbHandler->exec($sql); 99 | 100 | $this->_D($sql); 101 | 102 | return $this; 103 | } 104 | 105 | // 106 | // @function Create new table 107 | // @param $tableName(String) Table Name 108 | // @param #schema(JSON) Table schema 109 | // JSON Struct 110 | // {"Column Name":"Column Type"} 111 | // 112 | public function createTable($schema,$tableName=null) 113 | { 114 | $_tableName = empty($tableName) ? $this->tableName : $tableName; 115 | $sql = "CREATE TABLE IF NOT EXISTS %s(ID INTEGER PRIMARY KEY AUTOINCREMENT,%s);"; 116 | 117 | $columns = json_decode($schema); // convert JSON to PHP Array or PHP Object 118 | 119 | // for Object 120 | if(is_object($columns)) 121 | { 122 | $tableColumns = array(); 123 | 124 | foreach ((array)$columns as $key => $val) { 125 | array_push($tableColumns, $key . " " . $val ); 126 | } 127 | 128 | } 129 | // for Array 130 | else if(is_array($columns)) 131 | { 132 | $tableColumns = array(); 133 | foreach ($columns as $col) { 134 | $key = key((array)$col); 135 | $val = current((array)$col); 136 | array_push($tableColumns, $key . " " . $val); 137 | } 138 | 139 | 140 | } 141 | 142 | $cmd = sprintf($sql, $_tableName, implode(",", $tableColumns)); 143 | // Execute sql command 144 | $this->dbHandler->exec($cmd); 145 | 146 | // Debug 147 | $this->_D($cmd); 148 | 149 | // return self 150 | return $this; 151 | } 152 | 153 | // 154 | // @function get all record from table 155 | // 156 | public function findAll() 157 | { 158 | $sql = "SELECT * FROM %s"; 159 | $cmd = sprintf($sql, $this->tableName); 160 | 161 | $result = $this->dbHandler->query($cmd); 162 | 163 | $this->_D($cmd); 164 | 165 | // Store result 166 | while($res = $result->fetchArray(SQLITE3_ASSOC)) 167 | { 168 | array_push($this->result, $res); 169 | } 170 | 171 | return $this; 172 | } 173 | 174 | public function getAll() 175 | { 176 | return $this->findAll(); 177 | } 178 | 179 | // 180 | // @function select data from database 181 | // @param $where 182 | // 183 | public function find($where) 184 | { 185 | $sql = "SELECT * FROM %s WHERE %s"; 186 | 187 | $cmd = sprintf($sql, $this->tableName, $where); 188 | 189 | // query 190 | $result = $this->dbHandler->query($cmd); 191 | 192 | // debug 193 | $this->_D($cmd); 194 | 195 | // if can not find anything 196 | // return self dircetly 197 | if($result == false) 198 | return $this; 199 | 200 | // Store result 201 | while($res = $result->fetchArray(SQLITE3_ASSOC)) 202 | { 203 | array_push($this->result, $res); 204 | } 205 | 206 | return $this; 207 | } 208 | 209 | public function select($where) 210 | { 211 | return $this->find($where); 212 | } 213 | 214 | public function get($where) 215 | { 216 | return $this->find($where); 217 | } 218 | 219 | // 220 | // @function Clear result set 221 | // 222 | public function clearResult() 223 | { 224 | $this->result = null; 225 | $this->result = array(); 226 | return $this; 227 | } 228 | 229 | // 230 | // @function add new data 231 | // 232 | public function add($JSONData) 233 | { 234 | $sql = "INSERT INTO %s(%s) VALUES(%s)"; 235 | 236 | $data = json_decode($JSONData); 237 | 238 | // for object 239 | if(is_object($data)) 240 | { 241 | $items = (array)$data; 242 | 243 | $keys = array(); 244 | $values = array(); 245 | foreach ($items as $key => $value) { 246 | array_push($keys, "`".$key."`"); 247 | array_push($values, "'".$value."'"); 248 | } 249 | 250 | // format command 251 | $cmd = sprintf($sql, $this->tableName, implode(',', $keys), implode(',', $values)); 252 | 253 | // execute 254 | $this->dbHandler->exec($cmd); 255 | 256 | // debug 257 | $this->_D($cmd); 258 | 259 | } 260 | // for array 261 | else if(is_array($data)) 262 | { 263 | $items = (array)$data; 264 | 265 | foreach ($items as $key => $value) { 266 | 267 | // $this->_D($value); 268 | // execute self add function 269 | $this->add(json_encode($value)); 270 | } 271 | } 272 | 273 | return $this; 274 | } 275 | 276 | // 277 | // @function update data 278 | // 279 | public function update($data) 280 | { 281 | $sql = "UPDATE %s SET %s WHERE ID = %s"; 282 | 283 | // No result return false 284 | if(count($this->result)) 285 | return false; 286 | 287 | // fetch update data ID from self result set 288 | foreach ($this->result as $item) { 289 | $ID = $item['ID']; 290 | 291 | // translate JSON data to PHP array 292 | $_data = (array)json_decode($data); 293 | 294 | $changes = array(); 295 | 296 | foreach ($_data as $key => $value) { 297 | array_push($changes, $key."='".$value."'"); 298 | } 299 | 300 | // format query 301 | $cmd = sprintf($sql, $this->tableName, implode(",", $changes) ,$ID); 302 | 303 | // debug 304 | $this->_D($cmd); 305 | 306 | // execute 307 | $this->dbHandler->exec($cmd); 308 | } 309 | 310 | return $this; 311 | } 312 | 313 | // 314 | // @function delete data 315 | // 316 | public function delete() 317 | { 318 | $sql = "DELETE FROM %s WHERE ID = %s"; 319 | 320 | // return false if no result 321 | if(count($this->result)) 322 | return false; 323 | 324 | // fetch delete data ID from self result set 325 | foreach ($this->result as $item) { 326 | $ID = $item['ID']; 327 | 328 | // format query 329 | $cmd = sprintf($sql, $this->tableName, $ID); 330 | 331 | // debug 332 | $this->_D($cmd); 333 | 334 | // execute 335 | $this->dbHandler->exec($cmd); 336 | } 337 | 338 | return $this; 339 | } 340 | 341 | 342 | // 343 | // @function open SQLite database file 344 | // 345 | private function openDatabase() 346 | { 347 | $this->dbHandler = new SQLite3($this->SQLiteFile); 348 | } 349 | 350 | // 351 | // @function close Database 352 | // 353 | private function closeDatabase() 354 | { 355 | $this->dbHandler->close(); 356 | } 357 | 358 | public function __destruct() 359 | { 360 | $this->closeDatabase(); 361 | } 362 | 363 | } 364 | 365 | ?> -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | JSON2SQL 2 | ======== 3 | 4 | A PHP class convert JSON to SQLite OR SQLite to JSON 5 | 6 | 7 | The aim of this class is to use easy way convert from JSON to SQLite OR SQLite to JSON 8 | 9 | [Usage] 10 | 11 | debugMode(true); 28 | 29 | // 4. OK, Let's create a new table in this database 30 | // the JSON key is the column name 31 | // the JSON value is the type of column 32 | // you can use the JSON Object or JSON array 33 | 34 | 35 | $test->createTable('{"name":"text"}'); // for JSON Object 36 | 37 | 38 | $test->createTable('[{"name":"text"},{"age":"integer"}]'); 39 | 40 | // for JSON array, I use this way to show you how to using, so this table columns contain 2 cloumn (name:text, age:integer) 41 | 42 | // 5. Next, I will add a new record to the table 43 | // As you see, you can add only one record with JSON Object, and you can add more record using JSON Array 44 | 45 | 46 | 47 | $test->add('{"name":"aokihu","age":28}'); 48 | $test->add('[{"name":"bake","age":27},{"name":"cake","age":26}]'); 49 | 50 | 51 | // 6. Next, let me modify the record which the age is 27 52 | // 53 | // First we use find() function to find the records we need 54 | // the find() function param is SQL "WHERE ...", so you can using any SQL to find the record 55 | // and will return all columns content 56 | // 57 | // Second we use update function() to modify the record content, just pass it JSON Object 58 | // 59 | // Third print the result, because the result was save in the instance varible $this->result, so we must clear the result set first, then find the result again. Otherwise you should never see the corect result 60 | 61 | 62 | 63 | $test->find("age = 27")->update('{"name":"dog"}'); 64 | 65 | 66 | 67 | echo $test->clearResult()->find("age = 27")->toJSON(); 68 | 69 | // 7. Last, delete a record 70 | 71 | 72 | $test->clearResult()->find("age = 26")->delete(); 73 | 74 | -------------------------------------------------------------------------------- /test.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aokihu/JSON2SQL/edb9a0a0d4e57629c2b86cc97f38ef8941b025aa/test.db -------------------------------------------------------------------------------- /test.php: -------------------------------------------------------------------------------- 1 | debugMode(true); 6 | 7 | // Unit 1 8 | $test->dropTable()->createTable('[{"name":"text"},{"age":"integer"}]'); 9 | 10 | // Unit2 11 | $test->add('{"name":"aokihu","age":28}'); 12 | $test->add('{"name":"aokihu","age":29}'); 13 | 14 | // Unit 3 15 | $data = json_encode(array( 16 | array("name"=>"aokihu","age"=>"28"), 17 | array("name"=>"bit", "age"=>"19"), 18 | array("name"=>"cake", "age"=>23) 19 | ) 20 | ); 21 | 22 | $test->add($data)->add('{"name":"rainbow","age":28}'); 23 | 24 | // Unit 4 25 | echo $test->find("age = 28")->toJSON(); 26 | echo "\n\n"; 27 | 28 | // Unit 5 29 | echo "Update >>>\n"; 30 | echo $test->clearResult() 31 | ->find("age = 28") 32 | ->update('{"name":"yunyun"}') 33 | ->clearResult() 34 | ->find("age = 28") 35 | ->toJSON(); 36 | echo "\n\n"; 37 | 38 | // Unit 6 39 | // Delete 40 | echo "Delete >>> \n"; 41 | $test->clearResult() 42 | ->find("ID = 1") 43 | ->delete(); 44 | 45 | ?> --------------------------------------------------------------------------------