This commit is contained in:
mcndave committed 2023-11-03 08:26:16 -04:00
commit 531908b04c
12 files changed
+93 -51

No files matched your search

+31 -17
View File
@@ -19,22 +19,11 @@ class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface
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
{
@@ -44,9 +33,14 @@ class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface
$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){
catch(\PDOException $ex){
throw $ex;
}
@@ -63,13 +57,15 @@ class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface
public function setup(array $configuration) : void
{
$this->path = $configuration['path'] ?? "";
$this->pragma = $configuration['pragma'] ?? [];
$this->pragmaBegin = array_filter($configuration['pragma_begin'] ?? []);
$this->pragmaClose = array_filter($configuration['pragma_close'] ?? []);
}
# https://sqlite.org/lang_keywords.html
public function escapeIdentifier(string $segment, int $type) : string
{
switch($type) {
default:
case static::IDENTIFIER_DATABASE:
case static::IDENTIFIER_TABLE:
case static::IDENTIFIER_FIELD:
@@ -194,6 +190,24 @@ class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface
$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) + [ null, null ];
$sql = $builder->pragma($key, $value)->render();
$query = $pdo->query($sql);
if ( ! $query->execute() ) {
throw new \InvalidArgumentException(sprintf("Pragma query could not be executed : %s", $sql));
}
$builder->reset();
}
}
public function generateAlterColumn(FieldDefinition $definition, array $field) : string|\Stringable
{
return implode(" ", [