From e641bc321da4bb8c74c4aefaec82ea6b0ccb0f9b Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 31 Oct 2023 14:37:46 +0000 Subject: [PATCH] - SQLite pragma handling was not applied ; fixed --- src/Adapter/SQLite.php | 38 ++++++++++++++++++++++---------------- src/Common/PdoObject.php | 9 +++++++++ 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Adapter/SQLite.php b/src/Adapter/SQLite.php index 5a90d55..8eaf659 100644 --- a/src/Adapter/SQLite.php +++ b/src/Adapter/SQLite.php @@ -12,28 +12,18 @@ use Ulmus\{Repository, QueryBuilder, Ulmus}; class SQLite implements AdapterInterface { use DefaultAdapterTrait; + const ALLOWED_ATTRIBUTES = [ 'default', 'primary_key', 'auto_increment' ]; const DSN_PREFIX = "sqlite"; - public string $path; - - public array $pragma; - public function __construct( - ? string $path = null, - ? array $pragma = null - ) { - if ($path !== null) { - $this->path = $path; - } - - if ($pragma !== null) { - $this->pragma = $pragma; - } - } + public null|string $path = null, + public null|array $pragmaBegin = null, + public null|array $pragmaClose = null, + ) { } public function connect() : PdoObject { @@ -43,7 +33,12 @@ class SQLite implements AdapterInterface { $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); + $pdo->onClose = function(PdoObject $obj) { + static::registerPragma($obj, $this->pragmaClose); + }; + $this->exportFunctions($pdo); + $this->registerPragma($pdo, $this->pragmaBegin); } catch(PDOException $ex){ throw $ex; @@ -62,7 +57,8 @@ class SQLite implements AdapterInterface { public function setup(array $configuration) : void { $this->path = $configuration['path'] ?? ""; - $this->pragma = $configuration['pragma'] ?? []; + $this->pragmaBegin = $configuration['pragma_begin'] ?? []; + $this->pragmaClose = $configuration['pragma_close'] ?? []; } # https://sqlite.org/lang_keywords.html @@ -185,6 +181,16 @@ class SQLite implements AdapterInterface { $pdo->sqliteCreateFunction('year', fn($date) => ( new \DateTime($date) )->format('Y'), 1); } + public static function registerPragma(PdoObject $pdo, array $pragmaList) : void + { + $builder = new QueryBuilder\SqliteQueryBuilder(); + + foreach($pragmaList as $pragma) { + list($key, $value) = explode('=', $pragma); + $pdo->query($builder->pragma($key, $value)->render()); + } + } + public function generateAlterColumn(FieldDefinition $definition, array $field) : string|\Stringable { return implode(" ", [ diff --git a/src/Common/PdoObject.php b/src/Common/PdoObject.php index 94e5d76..15f86a2 100644 --- a/src/Common/PdoObject.php +++ b/src/Common/PdoObject.php @@ -15,6 +15,8 @@ class PdoObject extends PDO { public mixed $lastInsertId = null; + public \Closure $onClose; + public function select(string $sql, array $parameters = []): PDOStatement { static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]); @@ -32,6 +34,13 @@ class PdoObject extends PDO { } } + public function __destruct() + { + if ($this->onClose ?? null) { + call_user_func($this->onClose, $this); + } + } + public function runQuery(string $sql, array $parameters = []): ? static { static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]);