modifs.mineures sans commentaires
This commit is contained in:
parent
e49bcb9852
commit
55ea8cdc81
|
@ -50,7 +50,7 @@ class Sqlite {
|
|||
|
||||
const MIGRATE = null;
|
||||
|
||||
const SCHEMA = [
|
||||
const params_SCHEMA = [
|
||||
"file" => ["string", ""],
|
||||
"flags" => ["int", SQLITE3_OPEN_READWRITE + SQLITE3_OPEN_CREATE],
|
||||
"encryption_key" => ["string", ""],
|
||||
|
@ -63,14 +63,14 @@ class Sqlite {
|
|||
function __construct(?string $file=null, ?array $params=null) {
|
||||
if ($file !== null) $params["file"] = $file;
|
||||
##schéma
|
||||
$defaultFile = self::SCHEMA["file"][1];
|
||||
$defaultFile = self::params_SCHEMA["file"][1];
|
||||
$this->file = $file = strval($params["file"] ?? $defaultFile);
|
||||
$inMemory = $file === ":memory:";
|
||||
#
|
||||
$defaultFlags = self::SCHEMA["flags"][1];
|
||||
$defaultFlags = self::params_SCHEMA["flags"][1];
|
||||
$this->flags = intval($params["flags"] ?? $defaultFlags);
|
||||
#
|
||||
$defaultEncryptionKey = self::SCHEMA["encryption_key"][1];
|
||||
$defaultEncryptionKey = self::params_SCHEMA["encryption_key"][1];
|
||||
$this->encryptionKey = strval($params["encryption_key"] ?? $defaultEncryptionKey);
|
||||
#
|
||||
$defaultAllowWal = static::ALLOW_WAL ?? !$inMemory;
|
||||
|
@ -80,7 +80,7 @@ class Sqlite {
|
|||
# migrations
|
||||
$this->migration = $params["migrate"] ?? static::MIGRATE;
|
||||
#
|
||||
$defaultAutoOpen = self::SCHEMA["auto_open"][1];
|
||||
$defaultAutoOpen = self::params_SCHEMA["auto_open"][1];
|
||||
if ($params["auto_open"] ?? $defaultAutoOpen) {
|
||||
$this->open();
|
||||
}
|
||||
|
@ -201,10 +201,16 @@ class Sqlite {
|
|||
return $this->get($query, $params, true);
|
||||
}
|
||||
|
||||
protected function _fetchResult(SQLite3Result $result, ?SQLite3Stmt $stmt=null): Generator {
|
||||
protected function _fetchResult(SQLite3Result $result, ?SQLite3Stmt $stmt=null, $primaryKeys=null): Generator {
|
||||
if ($primaryKeys !== null) $primaryKeys = cl::with($primaryKeys);
|
||||
try {
|
||||
while (($row = $result->fetchArray(SQLITE3_ASSOC)) !== false) {
|
||||
yield $row;
|
||||
if ($primaryKeys !== null) {
|
||||
$key = implode("-", cl::select($row, $primaryKeys));
|
||||
yield $key => $row;
|
||||
} else {
|
||||
yield $row;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
$result->finalize();
|
||||
|
@ -212,15 +218,19 @@ class Sqlite {
|
|||
}
|
||||
}
|
||||
|
||||
function all($query, ?array $params=null): iterable {
|
||||
/**
|
||||
* si $primaryKeys est fourni, le résultat est indexé sur la(es) colonne(s)
|
||||
* spécifiée(s)
|
||||
*/
|
||||
function all($query, ?array $params=null, $primaryKeys=null): iterable {
|
||||
$db = $this->db();
|
||||
$query = new _query($query, $params);
|
||||
if ($query->useStmt($db, $stmt, $sql)) {
|
||||
$result = $this->checkResult($stmt->execute());
|
||||
return $this->_fetchResult($result, $stmt);
|
||||
return $this->_fetchResult($result, $stmt, $primaryKeys);
|
||||
} else {
|
||||
$result = $this->checkResult($db->query($sql));
|
||||
return $this->_fetchResult($result);
|
||||
return $this->_fetchResult($result, null, $primaryKeys);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -250,9 +250,9 @@ class SqliteStorage extends CapacitorStorage {
|
|||
"select",
|
||||
"from" => $channel->getTableName(),
|
||||
"where" => $filter,
|
||||
]);
|
||||
foreach ($rows as $row) {
|
||||
yield $this->unserialize($channel, $row);
|
||||
], null, "id_");
|
||||
foreach ($rows as $key => $row) {
|
||||
yield $key => $this->unserialize($channel, $row);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
namespace nur\sery\wip\db\pdo;
|
||||
|
||||
use nur\sery\cl;
|
||||
|
||||
class Pdo {
|
||||
static function config_errmodeException_lowerCase(self $pdo) {
|
||||
$pdo->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||
$pdo->db->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_LOWER);
|
||||
}
|
||||
|
||||
const OPTIONS = [
|
||||
\PDO::ATTR_PERSISTENT => true,
|
||||
];
|
||||
|
||||
const CONFIG = [
|
||||
[self::class, "config_errmodeException_lowerCase"],
|
||||
];
|
||||
|
||||
const MIGRATE = null;
|
||||
|
||||
const dbconn_SCHEMA = [
|
||||
"name" => "string",
|
||||
"user" => "?string",
|
||||
"pass" => "?string",
|
||||
];
|
||||
|
||||
const params_SCHEMA = [
|
||||
"options" => ["?array"],
|
||||
"config" => ["?array|callable"],
|
||||
"migrate" => ["?array|string|callable"],
|
||||
"auto_open" => ["bool", true],
|
||||
];
|
||||
|
||||
function __construct($dbconn, ?array $params=null) {
|
||||
if (!is_array($dbconn)) $dbconn = ["name" => $dbconn];
|
||||
$this->dbname = $dbconn["name"];
|
||||
$this->dbuser = $dbconn["user"] ?? null;
|
||||
$this->dbpass = $dbconn["pass"] ?? null;
|
||||
# options
|
||||
$this->options = cl::with($params["options"] ?? static::OPTIONS);
|
||||
# configuration
|
||||
$this->config = $params["config"] ?? static::CONFIG;
|
||||
# migrations
|
||||
$this->migration = $params["migrate"] ?? static::MIGRATE;
|
||||
#
|
||||
$defaultAutoOpen = self::params_SCHEMA["auto_open"][1];
|
||||
if ($params["auto_open"] ?? $defaultAutoOpen) {
|
||||
$this->open();
|
||||
}
|
||||
}
|
||||
|
||||
protected ?string $dbconn;
|
||||
|
||||
protected ?string $dbuser;
|
||||
|
||||
protected ?string $dbpass;
|
||||
|
||||
protected array $options;
|
||||
|
||||
/** @var array|string|callable */
|
||||
protected $config;
|
||||
|
||||
/** @var array|string|callable */
|
||||
protected $migration;
|
||||
|
||||
protected ?\PDO $db;
|
||||
|
||||
function open(): self {
|
||||
if ($this->db === null) {
|
||||
$this->db = new \PDO($this->dbname, $this->dbuser, $this->dbpass, $this->options);
|
||||
_config::with($this->config)->configure($this);
|
||||
_migration::with($this->migration)->migrate($this);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
function close(): void {
|
||||
$this->db = null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
namespace nur\sery\wip\db\pdo;
|
||||
|
||||
use nur\sery\php\func;
|
||||
|
||||
class _config {
|
||||
static function with($configs): self {
|
||||
if ($configs instanceof static) return $configs;
|
||||
return new static($configs);
|
||||
}
|
||||
|
||||
const CONFIG = null;
|
||||
|
||||
function __construct($configs) {
|
||||
if ($configs === null) $configs = static::CONFIG;
|
||||
if ($configs === null) $configs = [];
|
||||
elseif (is_string($configs)) $configs = [$configs];
|
||||
elseif (is_callable($configs)) $configs = [$configs];
|
||||
elseif (!is_array($configs)) $configs = [strval($configs)];
|
||||
$this->configs = $configs;
|
||||
}
|
||||
|
||||
/** @var array */
|
||||
protected $configs;
|
||||
|
||||
function configure(Pdo $pdo): void {
|
||||
foreach ($this->configs as $key => $config) {
|
||||
if (is_string($config) && !func::is_method($config)) {
|
||||
$pdo->exec($config);
|
||||
} else {
|
||||
func::ensure_func($config, $this, $args);
|
||||
func::call($config, $pdo, $key, ...$args);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue