connection) return; try{ $this->connection = new \PDO("sqlite:" . $this->wmarkdown->settings("database_file")); $this->connected = true; }catch(\Exception $exception){ $this->error |= 1 << 0; $this->close(); return; }; $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $this->connection->beginTransaction(); if(!$this->query("select * from sqlite_master")["tables"][0]){ $this->query(file_get_contents($this->wmarkdown->settings("sql_generator"))); $this->query(file_get_contents($this->wmarkdown->settings("sql_views"))); }; } public function __construct($wmarkdown){ $this->wmarkdown = $wmarkdown; $this->connect(); } private function close(){ if($this->connection){ try{ $this->connection->commit(); }catch(\Exception $exception){ try{ $this->connection->rollback(); }catch(\Exception $exception){ $this->error |= 1 << 3; }; $this->error |= 1 << 2; }; }else $this->error |= 1 << 1; $this->connection = null; } public function __destruct(){ $this->close(); } public function is_connected(){ return !!$this->connected; } public function reconnect(){ $this->close(); $this->connect(); } public function query($query){ $results = [ "connection_error" => $this->error, "tables" => [], "query_error" => 0 ]; if(!$this->is_connected()){ $results["query_error"] |= 1 << 1; return $results; }; preg_replace_callback('/(([^\;\'"]+|\'(([^\'\\\\]+|\\\\(.|[\r\n]))*)\'|"(([^"\\\\]+|\\\\(.|[\r\n]))*)")+);?/', function($values) use(&$results){ $statement = $this->connection->prepare($values[1]); $statement->execute(); try{ $table = []; foreach($statement->fetchAll(\PDO::FETCH_ASSOC) as $new_row){ $row = []; foreach($new_row as $key => $value){ if(is_string($value)){ if($value[0] == "[" || $value[0] == "{"){ try{ $row[$key] = $this->ur3->json_decode(utf8_encode($value), true); if(!$row[$key]) $row[$key] = utf8_encode($value); }catch(\Exception $exception){ $row[$key] = utf8_encode($value); }; }else $row[$key] = utf8_encode($value); }else $row[$key] = $value; }; $table[] = $row; }; $results["tables"][] = $table; }catch(\Exception $exception){}; }, $query); return $results; } };