diff --git a/src/db/CapacitorChannel.php b/src/db/CapacitorChannel.php index ee1e97f..8e4f5c9 100644 --- a/src/db/CapacitorChannel.php +++ b/src/db/CapacitorChannel.php @@ -1,6 +1,9 @@ name = self::verifix_name($name ?? static::NAME); $this->eachCommitThreshold = $eachCommitThreshold ?? static::EACH_COMMIT_THRESHOLD; $this->created = false; + $keyDefinitions = cl::withn(static::KEY_DEFINITIONS); + $primaryKeys = cl::withn(static::PRIMARY_KEYS); + if ($primaryKeys === null && $keyDefinitions !== null) { + $index = 0; + foreach ($keyDefinitions as $col => $def) { + if ($col == $index) { + $index++; + if (preg_match('/\bprimary\s+key\s+\((.+)\)/i', $def, $ms)) { + $primaryKeys = preg_split('/\s*,\s*/', trim($ms[1])); + } + } else { + if (preg_match('/\bprimary\s+key\b/i', $def)) { + $primaryKeys[] = $col; + } + } + } + } + $this->keyDefinitions = $keyDefinitions; + $this->primaryKeys = $primaryKeys; } /** @var string */ @@ -54,6 +80,8 @@ class CapacitorChannel { $this->created = $created; } + protected ?array $keyDefinitions; + /** * retourner un ensemble de définitions pour des colonnes supplémentaires à * insérer lors du chargement d'une valeur @@ -71,7 +99,13 @@ class CapacitorChannel { * avant d'être retournées à l'utilisateur (sans le suffixe "__") */ function getKeyDefinitions(): ?array { - return null; + return $this->keyDefinitions; + } + + protected ?array $primaryKeys; + + function getPrimaryKeys(): ?array { + return $this->primaryKeys; } /** diff --git a/src/db/CapacitorStorage.php b/src/db/CapacitorStorage.php index b392270..929b320 100644 --- a/src/db/CapacitorStorage.php +++ b/src/db/CapacitorStorage.php @@ -75,6 +75,12 @@ abstract class CapacitorStorage { return $this->_one($this->getChannel($channel), $filter); } + protected function getPrimaryKeys(CapacitorChannel $channel): array { + $primaryKeys = $channel->getPrimaryKeys(); + if ($primaryKeys === null) $primaryKeys = ["id_"]; + return $primaryKeys; + } + abstract function _all(CapacitorChannel $channel, $filter): iterable; /** @@ -83,7 +89,7 @@ abstract class CapacitorStorage { * si $filter n'est pas un tableau, il est transformé en ["id_" => $filter] */ function all(?string $channel, $filter): iterable { - return $this->_one($this->getChannel($channel), $filter); + return $this->_all($this->getChannel($channel), $filter); } abstract function _each(CapacitorChannel $channel, $filter, ?callable $func, ?array $args): int; diff --git a/src/db/mysql/MysqlStorage.php b/src/db/mysql/MysqlStorage.php index ab227cb..c8763a2 100644 --- a/src/db/mysql/MysqlStorage.php +++ b/src/db/mysql/MysqlStorage.php @@ -251,7 +251,7 @@ class MysqlStorage extends CapacitorStorage { "select", "from" => $channel->getTableName(), "where" => $filter, - ], null, "id_"); + ], null, $this->getPrimaryKeys($channel)); foreach ($rows as $key => $row) { yield $key => $this->unserialize($channel, $row); } diff --git a/src/db/sqlite/SqliteStorage.php b/src/db/sqlite/SqliteStorage.php index 1049001..c04f931 100644 --- a/src/db/sqlite/SqliteStorage.php +++ b/src/db/sqlite/SqliteStorage.php @@ -250,7 +250,7 @@ class SqliteStorage extends CapacitorStorage { "select", "from" => $channel->getTableName(), "where" => $filter, - ], null, "id_"); + ], null, $this->getPrimaryKeys($channel)); foreach ($rows as $key => $row) { yield $key => $this->unserialize($channel, $row); }