diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-05-26 03:14:50 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-05-26 21:51:43 +0300 |
commit | c3ed2483ea508141431be74f29f7c209271897cd (patch) | |
tree | 9f7a416ab433ffc9bd245d1dd382f350a4e61efc /localwebsite/engine | |
parent | 997c6da6c40c11ec0bc6cc749f78deca8dd95db1 (diff) |
cron: vk sms checker
Diffstat (limited to 'localwebsite/engine')
-rw-r--r-- | localwebsite/engine/database.php | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/localwebsite/engine/database.php b/localwebsite/engine/database.php new file mode 100644 index 0000000..186d2ef --- /dev/null +++ b/localwebsite/engine/database.php @@ -0,0 +1,80 @@ +<?php + +class database { + + const SCHEMA_VERSION = 2; + + protected SQLite3 $link; + + public function __construct(string $db_path) { + $this->link = new SQLite3($db_path); + $this->link->enableExceptions(true); + $this->upgradeSchema(); + } + + protected function upgradeSchema() { + $cur = $this->getSchemaVersion(); + if ($cur < 1) { + $this->link->exec("CREATE TABLE users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT, + password TEXT + )"); + } + if ($cur < 2) { + $this->link->exec("CREATE TABLE vk_processed ( + last_message_time INTEGER + )"); + $this->link->exec("INSERT INTO vk_processed (last_message_time) VALUES (0)"); + } + $this->syncSchemaVersion(); + } + + protected function getSchemaVersion() { + return $this->link->query("PRAGMA user_version")->fetchArray()[0]; + } + + protected function syncSchemaVersion() { + $this->link->exec("PRAGMA user_version=".self::SCHEMA_VERSION); + } + + protected function prepareQuery(string $sql): string { + if (func_num_args() > 1) { + $mark_count = substr_count($sql, '?'); + $positions = array(); + $last_pos = -1; + for ($i = 0; $i < $mark_count; $i++) { + $last_pos = strpos($sql, '?', $last_pos + 1); + $positions[] = $last_pos; + } + for ($i = $mark_count - 1; $i >= 0; $i--) { + $arg_val = func_get_arg($i + 1); + if (is_null($arg_val)) { + $v = 'NULL'; + } else { + $v = '\''.$this->link->escapeString($arg_val) . '\''; + } + $sql = substr_replace($sql, $v, $positions[$i], 1); + } + } + + return $sql; + } + + public function query(string $sql, ...$params): SQLite3Result { + return $this->link->query($this->prepareQuery($sql, ...$params)); + } + + public function exec(string $sql, ...$params) { + return $this->link->exec($this->prepareQuery($sql, ...$params)); + } + + public function querySingle(string $sql, ...$params) { + return $this->link->querySingle($this->prepareQuery($sql, ...$params)); + } + + public function querySingleRow(string $sql, ...$params) { + return $this->link->querySingle($this->prepareQuery($sql, ...$params), true); + } + +}
\ No newline at end of file |