nur-sery/nur_src/m/sqlite/SqliteConn.php

67 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2023-12-03 22:10:18 +04:00
<?php
namespace nur\m\sqlite;
use Exception;
use nur\A;
use nur\m\base\AbstractConn;
use nur\m\base\QueryException;
use SQLite3;
class SqliteConn extends AbstractConn {
protected $filename, $flags, $encryptionKey;
protected function beforeInit(?int &$flags, ?string &$encryptionKey): void {
if ($flags === null) $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE;
if ($encryptionKey === null) $encryptionKey = "";
}
protected function afterInit(SQLite3 $conn): void {
$conn->enableExceptions(true);
}
protected $conn;
function __construct($filename="", ?int $flags=null, ?string $encryptionKey=null) {
if (is_array($filename)) {
if ($flags === null) $flags = A::get($filename, "flags");
if ($encryptionKey === null) $encryptionKey = A::get($filename, "encryption_key");
$filename = $filename["name"];
}
$this->beforeInit($flags, $encryptionKey);
$this->filename = $filename;
$this->flags = $flags;
$this->encryptionKey = $encryptionKey;
try {
$conn = new SQLite3($filename, $flags, $encryptionKey);
} catch (Exception $e) {
throw new QueryException("unable to open database '$filename'", $e);
}
$this->afterInit($conn);
$this->conn = $conn;
}
function __destruct() {
$this->close();
}
function getInfos(): array {
return [$this->filename, $this->flags, $this->encryptionKey];
}
function beginTransaction(): void {
}
function commit(): void {
}
function rollback(): void {
}
function close(): void {
if ($this->conn !== null) {
$this->conn->close();
$this->conn = null;
}
}
}