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 */
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;
$this->type = self::TYPE_SELECT;
$this->sql = $sql;
@ -125,7 +125,7 @@ abstract class Query implements IQuery {
/** @var string requête SQL de mise à jour par défaut */
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;
$this->type = self::TYPE_UPDATE;
$this->sql = $sql;
@ -138,7 +138,7 @@ abstract class Query implements IQuery {
/** @var string requête SQL d'insertion par défaut */
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;
$this->type = self::TYPE_INSERT;
$this->sql = $sql;

View File

@ -20,7 +20,7 @@ class PdoQuery extends Query {
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;
if ($incarnation === null) $incarnation = $this->newRowIncarnation();
$this->setIncarnation($incarnation);

View File

@ -8,7 +8,7 @@ use nur\m\pdo\PdoConn;
class MysqlConn extends PdoConn {
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);
}
}

View File

@ -1,11 +1,28 @@
<?php
namespace nur\m\pdo\mysql;
use nur\m\IQuery;
use nur\m\IRowIncarnation;
use nur\m\pdo\PdoQuery;
use nur\sery\db\mysql\query;
class MysqlQuery extends PdoQuery {
protected function newRowIncarnation(): IRowIncarnation {
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
* - [$key => $skey] --> $dest[$key] = self::get($array, $skey)
* - [$key => null] --> $dest[$key] = null
* - [$key] --> $dest[$key] = self::get($array, $key)
* - [$to => $from] --> $dest[$to] = self::get($array, $from)
* - [$to => null] --> $dest[$to] = null
* - [$to] --> $dest[$to] = self::get($array, $to)
* - [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 = [];
$index = 0;
foreach ($keys as $key => $skey) {
if ($key === $index) {
$index++;
if ($skey === null) continue;
$value = self::get($array, $skey);
$key = $skey;
} else {
if ($skey === null) $value = null;
else $value = self::pget($array, $skey);
if ($inverse) {
foreach ($mappings as $to => $from) {
if ($to === $index) {
$index++;
$to = $from;
if ($to === null) continue;
$selected[$to] = self::get($array, $to);
} elseif ($from === null) {
$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;
}
@ -451,9 +476,12 @@ class cl {
/**
* retourner le tableau $array en "renommant" les clés selon le tableau
* $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 ($inverse) $mappings = array_flip($mappings);
$mapped = [];
foreach ($array as $key => $value) {
if (array_key_exists($key, $mappings)) $key = $mappings[$key];