diff --git a/nur_src/m/base/Query.php b/nur_src/m/base/Query.php index 0fcad82..596cd38 100644 --- a/nur_src/m/base/Query.php +++ b/nur_src/m/base/Query.php @@ -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; diff --git a/nur_src/m/pdo/PdoQuery.php b/nur_src/m/pdo/PdoQuery.php index 5f2a4eb..64a5fc1 100644 --- a/nur_src/m/pdo/PdoQuery.php +++ b/nur_src/m/pdo/PdoQuery.php @@ -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); diff --git a/nur_src/m/pdo/mysql/MysqlConn.php b/nur_src/m/pdo/mysql/MysqlConn.php index fe63228..7c24c87 100644 --- a/nur_src/m/pdo/mysql/MysqlConn.php +++ b/nur_src/m/pdo/mysql/MysqlConn.php @@ -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); } } diff --git a/nur_src/m/pdo/mysql/MysqlQuery.php b/nur_src/m/pdo/mysql/MysqlQuery.php index ee8b611..407660c 100644 --- a/nur_src/m/pdo/mysql/MysqlQuery.php +++ b/nur_src/m/pdo/mysql/MysqlQuery.php @@ -1,11 +1,28 @@ $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];