conversion automatique des dates et booléens

This commit is contained in:
Jephté Clain 2024-11-14 18:02:48 +04:00
parent b5b1b83d46
commit 6a44ab7727
6 changed files with 87 additions and 60 deletions

View File

@ -0,0 +1,36 @@
<?php
namespace nur\sery\db\_private;
use DateTimeInterface;
use nur\sery\php\time\Date;
use nur\sery\php\time\DateTime;
use nur\sery\str;
trait Tbindings {
static function is_sqldate(string $date): bool {
return preg_match('/^\d{4}-\d{2}-\d{2}(?: \d{2}:\d{2}:\d{2})?$/', $date);
}
protected function verifixBindings(&$value): void {
if ($value instanceof Date) {
$value = $value->format('Y-m-d');
} elseif ($value instanceof DateTime) {
$value = $value->format('Y-m-d H:i:s');
} elseif ($value instanceof DateTimeInterface) {
$value = $value->format('Y-m-d H:i:s');
str::del_suffix($value, " 00:00:00");
} elseif (is_string($value)) {
if (self::is_sqldate($value)) {
# déjà dans le bon format
} elseif (Date::isa_date($value, true)) {
$value = new Date($value);
$value = $value->format('Y-m-d');
} elseif (DateTime::isa_datetime($value, true)) {
$value = new DateTime($value);
$value = $value->format('Y-m-d H:i:s');
}
} elseif (is_bool($value)) {
$value = $value? 1: 0;
}
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace nur\sery\db\_private;
use nur\sery\php\time\Date;
use nur\sery\php\time\DateTime;
trait Tvalues {
/**
* Tester si $date est une date/heure valide de la forme "YYYY-mm-dd HH:MM:SS"
*
* Si oui, $ms obtient les 6 éléments de la chaine
*/
static function is_datetime($date, ?array &$ms=null): bool {
return is_string($date) && preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', $date, $ms);
}
/**
* Tester si $date est une date valide de la forme "YYYY-mm-dd [00:00:00]"
*
* Si oui, $ms obtient les 3 éléments de la chaine
*/
static function is_date($date, ?array &$ms=null): bool {
return is_string($date) && preg_match('/^(\d{4})-(\d{2})-(\d{2})(?: 00:00:00)?$/', $date, $ms);
}
function verifixRow(array &$row) {
foreach ($row as &$value) {
if (self::is_date($value)) {
$value = new Date($value);
} elseif (self::is_datetime($value)) {
$value = new DateTime($value);
}
}; unset($value);
}
}

View File

@ -3,6 +3,7 @@ namespace nur\sery\db\pdo;
use Generator;
use nur\sery\cl;
use nur\sery\db\_private\Tvalues;
use nur\sery\db\IDatabase;
use nur\sery\db\ITransactor;
use nur\sery\php\nur_func;
@ -11,6 +12,8 @@ use nur\sery\php\time\DateTime;
use nur\sery\ValueException;
class Pdo implements IDatabase {
use Tvalues;
static function with($pdo, ?array $params=null): self {
if ($pdo instanceof static) {
return $pdo;
@ -219,34 +222,6 @@ class Pdo implements IDatabase {
}
}
/**
* Tester si $date est une date/heure valide de la forme "YYYY-mm-dd HH:MM:SS"
*
* Si oui, $ms obtient les 6 éléments de la chaine
*/
static function is_datetime($date, ?array &$ms=null): bool {
return is_string($date) && preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', $date, $ms);
}
/**
* Tester si $date est une date valide de la forme "YYYY-mm-dd [00:00:00]"
*
* Si oui, $ms obtient les 3 éléments de la chaine
*/
static function is_date($date, ?array &$ms=null): bool {
return is_string($date) && preg_match('/^(\d{4})-(\d{2})-(\d{2})(?: 00:00:00)?$/', $date, $ms);
}
function verifixRow(array &$row) {
foreach ($row as &$value) {
if (self::is_date($value)) {
$value = new Date($value);
} elseif (self::is_datetime($value)) {
$value = new DateTime($value);
}
}; unset($value);
}
function get($query, ?array $params=null, bool $entireRow=false) {
$db = $this->db();
$query = new _query_base($query, $params);

View File

@ -1,14 +1,13 @@
<?php
namespace nur\sery\db\pdo;
use DateTimeInterface;
use nur\sery\db\_private\_base;
use nur\sery\php\time\Date;
use nur\sery\php\time\DateTime;
use nur\sery\str;
use nur\sery\db\_private\Tbindings;
use nur\sery\ValueException;
class _query_base extends _base {
use Tbindings;
protected static function verifix(&$sql, ?array &$bindinds=null, ?array &$meta=null): void {
if (is_array($sql)) {
$prefix = $sql[0] ?? null;
@ -55,33 +54,6 @@ class _query_base extends _base {
}
}
static function is_sqldate(string $date): bool {
return preg_match('/^\d{4}-\d{2}-\d{2}(?: \d{2}:\d{2}:\d{2})?$/', $date);
}
protected function verifixBindings(&$value): void {
if ($value instanceof Date) {
$value = $value->format('Y-m-d');
} elseif ($value instanceof DateTime) {
$value = $value->format('Y-m-d H:i:s');
} elseif ($value instanceof DateTimeInterface) {
$value = $value->format('Y-m-d H:i:s');
str::del_suffix($value, " 00:00:00");
} elseif (is_string($value)) {
if (self::is_sqldate($value)) {
# déjà dans le bon format
} elseif (Date::isa_date($value, true)) {
$value = new Date($value);
$value = $value->format('Y-m-d');
} elseif (DateTime::isa_datetime($value, true)) {
$value = new DateTime($value);
$value = $value->format('Y-m-d H:i:s');
}
} elseif (is_bool($value)) {
$value = $value? 1: 0;
}
}
const DEBUG_QUERIES = false;
function useStmt(\PDO $db, ?\PDOStatement &$stmt=null, ?string &$sql=null): bool {

View File

@ -3,6 +3,7 @@ namespace nur\sery\db\sqlite;
use Generator;
use nur\sery\cl;
use nur\sery\db\_private\Tvalues;
use nur\sery\db\IDatabase;
use nur\sery\db\ITransactor;
use nur\sery\php\nur_func;
@ -15,6 +16,8 @@ use SQLite3Stmt;
* Class Sqlite: frontend vers une base de données sqlite3
*/
class Sqlite implements IDatabase {
use Tvalues;
static function with($sqlite, ?array $params=null): self {
if ($sqlite instanceof static) {
return $sqlite;
@ -278,7 +281,8 @@ class Sqlite implements IDatabase {
try {
$row = $result->fetchArray(SQLITE3_ASSOC);
if ($row === false) return null;
elseif ($entireRow) return $row;
$this->verifixRow($row);
if ($entireRow) return $row;
else return cl::first($row);
} finally {
$result->finalize();
@ -299,6 +303,7 @@ class Sqlite implements IDatabase {
if ($primaryKeys !== null) $primaryKeys = cl::with($primaryKeys);
try {
while (($row = $result->fetchArray(SQLITE3_ASSOC)) !== false) {
$this->verifixRow($row);
if ($primaryKeys !== null) {
$key = implode("-", cl::select($row, $primaryKeys));
yield $key => $row;

View File

@ -2,11 +2,14 @@
namespace nur\sery\db\sqlite;
use nur\sery\db\_private\_base;
use nur\sery\db\_private\Tbindings;
use nur\sery\ValueException;
use SQLite3;
use SQLite3Stmt;
class _query_base extends _base {
use Tbindings;
protected static function verifix(&$sql, ?array &$bindinds=null, ?array &$meta=null): void {
if (is_array($sql)) {
$prefix = $sql[0] ?? null;
@ -42,6 +45,7 @@ class _query_base extends _base {
$close = true;
try {
foreach ($this->bindings as $param => $value) {
$this->verifixBindings($value);
SqliteException::check($db, $stmt->bindValue($param, $value));
}
$close = false;