156 lines
5.7 KiB
PHP
156 lines
5.7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace WMarkDown\PDO;
|
||
|
|
||
|
class SQLite{
|
||
|
|
||
|
private $wmarkdown = null;
|
||
|
private $input = null;
|
||
|
private $connection = null;
|
||
|
private $error = 0;
|
||
|
public $type = "mysql";
|
||
|
|
||
|
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 << 0;
|
||
|
};
|
||
|
}else
|
||
|
$this->error |= 1 << 6;
|
||
|
|
||
|
$this->connection = null;
|
||
|
|
||
|
}
|
||
|
|
||
|
private function connect(){
|
||
|
if($this->connection)
|
||
|
return;
|
||
|
|
||
|
try{
|
||
|
$this->connection = new \PDO(\WMarkDown::string_variables($this->wmarkdown->settings(["db_connection_string", "connection_string"], $this->input), [
|
||
|
"engine" => $this->wmarkdown->settings(["db_engine", "engine"], $this->input),
|
||
|
"file" => $this->wmarkdown->settings(["db_file", "file"], $this->input),
|
||
|
"memory" => $this->wmarkdown->settings(["db_memory", "memory"], $this->input) ? ":memory:" : ""
|
||
|
]), $this->wmarkdown->settings(["db_user", "user"], $this->input), $this->wmarkdown->settings(["db_password", "password"], $this->input), $this->wmarkdown->settings(["db_parameters", "parameters"], $this->input));
|
||
|
}catch(\Exception $exception){
|
||
|
$this->error |= 1 << 2;
|
||
|
$this->close();
|
||
|
return;
|
||
|
};
|
||
|
|
||
|
$this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||
|
|
||
|
$this->connection->beginTransaction();
|
||
|
|
||
|
}
|
||
|
|
||
|
public function __construct($wmarkdown, $input = null){
|
||
|
|
||
|
$this->wmarkdown = $wmarkdown;
|
||
|
$this->input = $input;
|
||
|
|
||
|
$secrets_class = $this->wmarkdown->settings(["db_secrets_class", "secrets_class"], $this->input);
|
||
|
$secrets_settings = $this->wmarkdown->settings(["db_secrets_settings", "secrets_settings"], $this->input);
|
||
|
|
||
|
foreach(class_exists($secrets_class) && property_exists($secrets_class, $secrets_settings) ? $secrets_class::$$secrets_settings : [] as $key => $value)
|
||
|
if(!isset($this->input[$key]))
|
||
|
$this->input[$key] = $value;
|
||
|
|
||
|
if($this->wmarkdown->settings(["db_autoconnect", "autoconnect"], $this->input))
|
||
|
$this->connect();
|
||
|
|
||
|
}
|
||
|
|
||
|
public function __destruct(){
|
||
|
|
||
|
if($this->connection)
|
||
|
try{
|
||
|
$this->close();
|
||
|
}catch(\Exception $exception){};
|
||
|
|
||
|
}
|
||
|
|
||
|
public static function query_variables($query, $variables){
|
||
|
return preg_replace_callback('/{([^\{\}]+)}/', function($values) use($variables){
|
||
|
if($variables && isset($variables[$values[1]])){
|
||
|
if($variables[$values[1]] === null)
|
||
|
return "null";
|
||
|
if(is_string($variables[$values[1]]))
|
||
|
return "'" . preg_replace('/\'/', '\\\'', $variables[$values[1]]) . "'";
|
||
|
if(is_bool($variables[$values[1]]))
|
||
|
return $variables[$values[1]] ? "true" : "false";
|
||
|
return $variables[$values[1]];
|
||
|
};
|
||
|
return "null";
|
||
|
}, $query);
|
||
|
}
|
||
|
|
||
|
public function query($query, $input = null){
|
||
|
|
||
|
$results = [
|
||
|
"tables" => [],
|
||
|
"variables" => [],
|
||
|
"error" => $this->error | 0
|
||
|
];
|
||
|
|
||
|
if($results["error"])
|
||
|
return $results;
|
||
|
|
||
|
if(!$query){
|
||
|
$results["error"] |= 1 << 4;
|
||
|
return $results;
|
||
|
};
|
||
|
|
||
|
if(!$this->connection){
|
||
|
$this->connect();
|
||
|
$results["error"] = $this->error;
|
||
|
if(!$this->connection)
|
||
|
$results["error"] |= 1 << 5;
|
||
|
if($results["error"])
|
||
|
return $results;
|
||
|
};
|
||
|
|
||
|
$query_completed = self::query_variables($query, $input);
|
||
|
|
||
|
print_r(["query_completed" => $query_completed]);
|
||
|
|
||
|
$statement = $this->connection->prepare($query_completed);
|
||
|
$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] = 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){};
|
||
|
|
||
|
return $results;
|
||
|
}
|
||
|
|
||
|
};
|