diff --git a/CHANGELOG.md b/CHANGELOG.md index c9c4db114c57f8a4ea0faea7dc046af77a01c9ed..d1ef1088eb368515b3a87ea49e5c2a15863a60f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - [Fix] Pour Oracle, la recherche des séquences des tables fonctionne pour les tables & séquences de plus de 30 caractères - Ajout de la méthode Table->getLastInsertId permettant de retourner la dernière valeur de séquence utilisée pour peupler un ID lors d'un INSERT - Nouvelle option "return-insert-data" pour Table->merge, permettant de retourner dans "insert-data" les données insérées pour pouvoir récupérer les ID peuplés à partir des séquences - +- Modernisation & renforcement du typage de Table 0.9.7 (19/07/2024) ------------------ diff --git a/src/Table.php b/src/Table.php index 9b339a563da11ca8e80a2cd381a8972b1bb13e73..6e86dcf6292489d8700cef963e561b6168243342 100644 --- a/src/Table.php +++ b/src/Table.php @@ -3,27 +3,17 @@ namespace Unicaen\BddAdmin; -use BddAdmin\Manager\DdlTable; class Table { use BddAwareTrait; - /** - * @var string - */ - private $name; + private string $name; - /** - * @var array - */ - private $ddl; + private array $ddl = []; - /** - * @var array - */ - private $transformCache = []; + private array $transformCache = []; private ?int $lastInsertId = null; @@ -40,9 +30,6 @@ class Table - /** - * @return string - */ public function getName(): string { return $this->name; @@ -50,9 +37,6 @@ class Table - /** - * @return array - */ public function getDdl(): array { if (empty($this->ddl)) { @@ -68,9 +52,11 @@ class Table - public function setDdl(array $ddl) + public function setDdl(array $ddl): self { $this->ddl = $ddl; + + return $this; } @@ -115,16 +101,7 @@ class Table - /** - * @param array|integer|null $where - * @param array|null $options - * - * @return array|null|SelectParser - * @throws Exception\BddCompileException - * @throws Exception\BddException - * @throws Exception\BddIndexExistsException - */ - public function select($where = null, array $options = []) + public function select( array|int|null $where = null, array $options = []): array|null|SelectParser { /* Initialisation des entrées */ $defaultOptions = [ @@ -143,7 +120,7 @@ class Table if ($cols != '') $cols .= ', '; $cols .= $colDdl['name'] ?? $cname; } - $sql = "SELECT $cols FROM \"$this->name\""; + $sql = "SELECT $cols FROM ".$this->name; $params = []; $sql .= $this->makeWhere($where, $options, $params); @@ -168,7 +145,7 @@ class Table - public function copy(Bdd $source, ?callable $fnc = null) + public function copy(Bdd $source, ?callable $fnc = null): self { $options = ['types' => $this->makeTypesOptions(), 'fetch' => Bdd::FETCH_EACH]; @@ -207,7 +184,7 @@ class Table - public function save(string $filename, ?callable $fnc = null) + public function save(string $filename, ?callable $fnc = null): self { $options = ['types' => $this->makeTypesOptions(), 'fetch' => Bdd::FETCH_EACH]; @@ -254,7 +231,7 @@ class Table - public function load(string $filename, ?callable $fnc = null) + public function load(string $filename, ?callable $fnc = null): self { if (!$this->getBdd()->isInCopy()) { $this->getBdd()->logBegin("Restauration de la table " . $this->getName()); @@ -293,6 +270,8 @@ class Table } else { $this->getBdd()->logMsg("\n", true); } + + return $this; } @@ -304,15 +283,6 @@ class Table - /** - * @param array $data - * @param array $options - * - * @return bool - * @throws Exception\BddCompileException - * @throws Exception\BddException - * @throws Exception\BddIndexExistsException - */ public function insert(array &$data, array $options = []): bool { $bdd = $this->getBdd(); @@ -352,7 +322,7 @@ class Table $cols = implode(", ", $cols); $vals = implode(", ", $vals); - $sql = "INSERT INTO \"$this->name\" ($cols) VALUES ($vals)"; + $sql = "INSERT INTO ".$this->name." ($cols) VALUES ($vals)"; return $bdd->exec($sql, $params, $this->makeTypesOptions()); } @@ -383,39 +353,26 @@ class Table $params['new_' . $col] = $val; } - $sql = "UPDATE \"$this->name\" SET $dataSql" . $this->makeWhere($where, $options, $params); + $sql = "UPDATE ".$this->name." SET $dataSql" . $this->makeWhere($where, $options, $params); return $bdd->exec($sql, $params, $this->makeTypesOptions()); } - /** - * @param int|string|array|null $where - * @param array $options - * - * @return bool - */ - public function delete($where = null, array $options = []): bool + public function delete(int|string|array|null $where = null, array $options = []): bool { $params = []; - $sql = "DELETE FROM \"$this->name\"" . $this->makeWhere($where, $options, $params); + $sql = "DELETE FROM ".$this->name . $this->makeWhere($where, $options, $params); return $this->getBdd()->exec($sql, $params); } - /** - * Vide une table - * - * @param string $table - * - * @return bool - */ public function truncate(): bool { - $sql = "TRUNCATE TABLE \"$this->name\""; + $sql = "TRUNCATE TABLE ".$this->name; return $this->getBdd()->exec($sql); } @@ -583,13 +540,7 @@ class Table - /** - * @param int|string|array|null $where - * @param array $options - * - * @return string - */ - private function makeWhere($where, array $options, array &$params): string + private function makeWhere(int|string|array|null $where, array $options, array &$params): string { if (is_string($where) && ( str_contains($where, '=') @@ -642,9 +593,6 @@ class Table - /** - * @return bool - */ protected function hasId(): bool { $ddl = $this->getDdl(); @@ -654,9 +602,6 @@ class Table - /** - * @return bool - */ protected function hasSequence(): bool { $ddl = $this->getDdl(); @@ -666,7 +611,7 @@ class Table - protected function transform($value, string $transformer) + protected function transform($value, string $transformer): mixed { if (!isset($this->transformCache[$transformer][$value])) { $val = $this->getBdd()->select(sprintf($transformer, ':val'), ['val' => $value]);