- SQLite pragma handling was not applied ; fixed
This commit is contained in:
parent
cc741566fb
commit
e641bc321d
|
@ -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(" ", [
|
||||
|
|
|
@ -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 ]);
|
||||
|
|
Loading…
Reference in New Issue