modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-05-31 14:20:20 +04:00
parent 3af456e493
commit abb3a45c8d
5 changed files with 65 additions and 20 deletions

View File

@ -111,7 +111,7 @@ abstract class Query implements IQuery {
/** @var string requête SQL de sélection par défaut */ /** @var string requête SQL de sélection par défaut */
const SQL_SELECT = null; const SQL_SELECT = null;
function select(?string $sql=null, ?array $filter=null): IQuery { function select($sql=null, ?array $filter=null): IQuery {
if ($sql === null) $sql = static::SQL_SELECT; if ($sql === null) $sql = static::SQL_SELECT;
$this->type = self::TYPE_SELECT; $this->type = self::TYPE_SELECT;
$this->sql = $sql; $this->sql = $sql;
@ -125,7 +125,7 @@ abstract class Query implements IQuery {
/** @var string requête SQL de mise à jour par défaut */ /** @var string requête SQL de mise à jour par défaut */
const SQL_UPDATE = null; const SQL_UPDATE = null;
function update(?string $sql=null, ?array $filter=null, $row=null, ?array &$results=null): IQuery { function update($sql=null, ?array $filter=null, $row=null, ?array &$results=null): IQuery {
if ($sql === null) $sql = static::SQL_UPDATE; if ($sql === null) $sql = static::SQL_UPDATE;
$this->type = self::TYPE_UPDATE; $this->type = self::TYPE_UPDATE;
$this->sql = $sql; $this->sql = $sql;
@ -138,7 +138,7 @@ abstract class Query implements IQuery {
/** @var string requête SQL d'insertion par défaut */ /** @var string requête SQL d'insertion par défaut */
const SQL_INSERT = null; const SQL_INSERT = null;
function insert(?string $sql=null, $row=null, ?array &$results=null): IQuery { function insert($sql=null, $row=null, ?array &$results=null): IQuery {
if ($sql === null) $sql = static::SQL_INSERT; if ($sql === null) $sql = static::SQL_INSERT;
$this->type = self::TYPE_INSERT; $this->type = self::TYPE_INSERT;
$this->sql = $sql; $this->sql = $sql;

View File

@ -20,7 +20,7 @@ class PdoQuery extends Query {
return new PdoRowIterator($this->conn, $sql, $bindings, $incarnation); return new PdoRowIterator($this->conn, $sql, $bindings, $incarnation);
} }
function __construct(PdoConn $conn, ?string $sql=null, ?array $filter=null, ?IRowIncarnation $incarnation=null) { function __construct(PdoConn $conn, $sql=null, ?array $filter=null, ?IRowIncarnation $incarnation=null) {
$this->conn = $conn; $this->conn = $conn;
if ($incarnation === null) $incarnation = $this->newRowIncarnation(); if ($incarnation === null) $incarnation = $this->newRowIncarnation();
$this->setIncarnation($incarnation); $this->setIncarnation($incarnation);

View File

@ -8,7 +8,7 @@ use nur\m\pdo\PdoConn;
class MysqlConn extends PdoConn { class MysqlConn extends PdoConn {
const HAVE_LAST_INSERT_ID = true; const HAVE_LAST_INSERT_ID = true;
function query(?string $sql=null, ?array $filter=null, ?IRowIncarnation $incarnation=null): IQuery { function query($sql=null, ?array $filter=null, ?IRowIncarnation $incarnation=null): IQuery {
return new MysqlQuery($this, $sql, $filter, $incarnation); return new MysqlQuery($this, $sql, $filter, $incarnation);
} }
} }

View File

@ -1,11 +1,28 @@
<?php <?php
namespace nur\m\pdo\mysql; namespace nur\m\pdo\mysql;
use nur\m\IQuery;
use nur\m\IRowIncarnation; use nur\m\IRowIncarnation;
use nur\m\pdo\PdoQuery; use nur\m\pdo\PdoQuery;
use nur\sery\db\mysql\query;
class MysqlQuery extends PdoQuery { class MysqlQuery extends PdoQuery {
protected function newRowIncarnation(): IRowIncarnation { protected function newRowIncarnation(): IRowIncarnation {
return new MysqlRowIncarnation(); return new MysqlRowIncarnation();
} }
function select($sql=null, ?array $filter=null): IQuery {
if (is_array($sql)) [$sql, $filter] = query::with($sql, $filter);
return parent::select($sql, $filter);
}
function update($sql=null, ?array $filter=null, $row=null, ?array &$results=null): IQuery {
if (is_array($sql)) [$sql, $filter] = query::with($sql, $filter);
return parent::update($sql, $filter, $row, $results);
}
function insert($sql=null, $row=null, ?array &$results=null): IQuery {
if (is_array($sql)) [$sql, $row] = query::with($sql, $row);
return parent::insert($sql, $row, $results);
}
} }

View File

@ -134,25 +134,50 @@ class cl {
/** /**
* retourner un tableau construit à partir des clés de $keys * retourner un tableau construit à partir des clés de $keys
* - [$key => $skey] --> $dest[$key] = self::get($array, $skey) * - [$to => $from] --> $dest[$to] = self::get($array, $from)
* - [$key => null] --> $dest[$key] = null * - [$to => null] --> $dest[$to] = null
* - [$key] --> $dest[$key] = self::get($array, $key) * - [$to] --> $dest[$to] = self::get($array, $to)
* - [null] --> NOP * - [null] --> NOP
*
* Si $inverse===true, le mapping est inversé:
* - [$to => $from] --> $dest[$from] = self::get($array, $to)
* - [$to => null] --> $dest[$to] = self::get($array, $to)
* - [$to] --> $dest[$to] = self::get($array, $to)
* - [null] --> NOP
*
* notez que l'ordre est inversé par rapport à {@link self::rekey()} qui
* attend des mappings [$from => $to], alors que cette méthode attend des
* mappings [$to => $from]
*/ */
static final function select($array, ?array $keys): array { static final function select($array, ?array $mappings, bool $inverse=false): array {
$selected = []; $selected = [];
$index = 0; $index = 0;
foreach ($keys as $key => $skey) { if ($inverse) {
if ($key === $index) { foreach ($mappings as $to => $from) {
$index++; if ($to === $index) {
if ($skey === null) continue; $index++;
$value = self::get($array, $skey); $to = $from;
$key = $skey; if ($to === null) continue;
} else { $selected[$to] = self::get($array, $to);
if ($skey === null) $value = null; } elseif ($from === null) {
else $value = self::pget($array, $skey); $selected[$to] = self::pget($array, $to);
} else {
$selected[$from] = self::pget($array, $to);
}
}
} else {
foreach ($mappings as $to => $from) {
if ($to === $index) {
$index++;
if ($from === null) continue;
$value = self::get($array, $from);
$to = $from;
} else {
if ($from === null) $value = null;
else $value = self::pget($array, $from);
}
$selected[$to] = $value;
} }
$selected[$key] = $value;
} }
return $selected; return $selected;
} }
@ -451,9 +476,12 @@ class cl {
/** /**
* retourner le tableau $array en "renommant" les clés selon le tableau * retourner le tableau $array en "renommant" les clés selon le tableau
* $mappings qui contient des associations de la forme [$from => $to] * $mappings qui contient des associations de la forme [$from => $to]
*
* Si $inverse===true, renommer dans le sens $to => $from
*/ */
static function rekey(?array $array, ?array $mappings): ?array { static function rekey(?array $array, ?array $mappings, bool $inverse=false): ?array {
if ($array === null || $mappings === null) return $array; if ($array === null || $mappings === null) return $array;
if ($inverse) $mappings = array_flip($mappings);
$mapped = []; $mapped = [];
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
if (array_key_exists($key, $mappings)) $key = $mappings[$key]; if (array_key_exists($key, $mappings)) $key = $mappings[$key];