diff --git a/src/Table.php b/src/Table.php index b2a97ee38e40fc6de7c543deab19a53fd7e4e008..b79900c6415c247707d2c49c44c54523c85b97ec 100644 --- a/src/Table.php +++ b/src/Table.php @@ -39,7 +39,7 @@ class Table public function getDdl(): array { if (empty($this->ddl)) { - $sTable = $this->getBdd()->table(); + $sTable = $this->getBdd()->table(); $this->ddl = $sTable->get($this->name)[$this->name]; } if (!$this->ddl) { @@ -62,7 +62,7 @@ class Table public function hasHistorique(): bool { - $ddl = $this->getDdl(); + $ddl = $this->getDdl(); $hasHisto = isset($ddl['columns']['HISTO_CREATION']) && isset($ddl['columns']['HISTO_MODIFICATION']) && isset($ddl['columns']['HISTO_DESTRUCTION']) @@ -77,7 +77,7 @@ class Table public function hasImport(): bool { - $ddl = $this->getDdl(); + $ddl = $this->getDdl(); $hasImport = isset($ddl['columns']['SOURCE_ID']) && isset($ddl['columns']['SOURCE_CODE']); @@ -110,7 +110,7 @@ class Table 'key' => null, 'orderBy' => '', ]; - $options = array_merge($defaultOptions, $options); + $options = array_merge($defaultOptions, $options); $ddl = $this->getDdl(); @@ -127,7 +127,7 @@ class Table $sql .= $this->name; } $params = []; - $sql .= $this->makeWhere($where, $options, $params); + $sql .= $this->makeWhere($where, $options, $params); if ($options['orderBy']) { $sql .= ' ORDER BY ' . $options['orderBy']; @@ -138,7 +138,7 @@ class Table /* Mise en forme des résultats */ $data = []; foreach ($select as $d) { - $keyValue = $this->makeKey($d, $options['key']); + $keyValue = $this->makeKey($d, $options['key']); $data[$keyValue] = $d; } @@ -155,7 +155,7 @@ class Table $options = ['types' => $this->makeTypesOptions(), 'fetch' => Bdd::FETCH_EACH]; $count = (int)$source->select('SELECT count(*) C FROM ' . $this->getName(), [], ['fetch' => Bdd::FETCH_ONE])['C']; - $r = $source->select('SELECT * FROM ' . $this->getName(), [], $options); + $r = $source->select('SELECT * FROM ' . $this->getName(), [], $options); if (!$this->getBdd()->isInCopy()) { $this->getBdd()->logBegin("Copie de la table " . $this->getName()); @@ -194,7 +194,7 @@ class Table $options = ['types' => $this->makeTypesOptions(), 'fetch' => Bdd::FETCH_EACH]; $count = (int)$this->getBdd()->select('SELECT count(*) C FROM ' . $this->getName(), [], ['fetch' => Bdd::FETCH_ONE])['C']; - $r = $this->getBdd()->select('SELECT * FROM ' . $this->getName(), [], $options); + $r = $this->getBdd()->select('SELECT * FROM ' . $this->getName(), [], $options); if (!$this->getBdd()->isInCopy()) { $this->getBdd()->logBegin("Sauvegarde de la table " . $this->getName()); @@ -242,10 +242,10 @@ class Table $this->getBdd()->logBegin("Restauration de la table " . $this->getName()); } - $buff = fopen($filename, 'r'); - $count = fgets($buff); - $count = (int)trim($count); - $data = ''; + $buff = fopen($filename, 'r'); + $count = fgets($buff); + $count = (int)trim($count); + $data = ''; $current = 0; $this->getBdd()->beginTransaction(); while (($d = fgets($buff)) !== false) { @@ -294,7 +294,7 @@ class Table if (!isset($data['ID']) && $this->hasId() && $this->hasSequence()) { $this->lastInsertId = $this->getBdd()->sequence()->nextVal($this->ddl['sequence']); - $data['ID'] = $this->lastInsertId; + $data['ID'] = $this->lastInsertId; } $histoUserId = (int)$bdd->getOption('histo-user-id'); @@ -310,8 +310,8 @@ class Table if (!isset($data['SOURCE_ID'])) $data['SOURCE_ID'] = $sourceId; } - $cols = []; - $vals = []; + $cols = []; + $vals = []; $params = []; foreach ($data as $col => $val) { $transformer = isset($options['columns'][$col]['transformer']) ? $options['columns'][$col]['transformer'] : null; @@ -327,7 +327,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()); } @@ -354,7 +354,7 @@ class Table if (isset($options['columns'][$col]['transformer'])) { $transVal = '(' . sprintf($options['columns'][$col]['transformer'], $transVal) . ')'; } - $dataSql .= $col . '=' . $transVal; + $dataSql .= $col . '=' . $transVal; $params['new_' . $col] = $val; } @@ -368,7 +368,7 @@ class Table 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); } @@ -402,7 +402,7 @@ class Table 'update-ignore-cols' => [], 'update-only-null' => [], ]; - $options = array_merge($defaultOptions, $options); + $options = array_merge($defaultOptions, $options); if ($options['return-insert-data']) { $result['insert-data'] = []; @@ -411,7 +411,7 @@ class Table $ddl = $this->getDdl(); $bdd = $this->getBdd(); - $histoUserId = (int)$bdd->getOption('histo-user-id'); + $histoUserId = (int)$bdd->getOption('histo-user-id'); $hasHistorique = $this->hasHistorique(); if (empty($options['where']) && $hasHistorique) { @@ -433,7 +433,7 @@ class Table unset($d[$c]); } } - $k = $this->makeKey($d, $key); + $k = $this->makeKey($d, $key); $new[$k] = $d; } @@ -441,9 +441,9 @@ class Table /* Chargement des données actuelles et traitement */ $bdd->beginTransaction(); - $selOptions = $options; + $selOptions = $options; $selOptions['fetch'] = Bdd::FETCH_EACH; - $stmt = $this->select($options['where'], $selOptions); + $stmt = $this->select($options['where'], $selOptions); while ($o = $stmt->next()) { // récupération de k et n $k = $this->makeKey($o, $key); @@ -456,9 +456,9 @@ class Table if (empty($n) && $options['soft-delete'] && $hasHistorique && $histoUserId) { // SOFT DELETE //On ne delete pas mais on historise - $n = $o; + $n = $o; $n['HISTO_DESTRUCTEUR_ID'] = $histoUserId; - $n['HISTO_DESTRUCTION'] = new \DateTime(); + $n['HISTO_DESTRUCTION'] = new \DateTime(); $this->update($n, $this->makeKeyArray($o, $k)); $result['soft-delete']++; } elseif (empty($n) && !$options['soft-delete']) { // DELETE @@ -481,13 +481,14 @@ class Table if (in_array($c, $options['update-only-null']) && $oldc !== null) $ok = false; if ($ok) { - if ((is_float($newc) || is_double($newc)) - && (is_float($oldc) || is_double($oldc)) - && abs($newc - $oldc) > 0.0000000001 - ) { - // nécessaire à cause des pb de précision des float/doubles en PHP : - // dans ce cas, on considère quand même que les 2 chiffres sont identiques - $toUpdate[$c] = $n[$c]; + if ((is_float($newc) || is_double($newc)) && (is_float($oldc) || is_double($oldc))) { + if (abs($newc - $oldc) > 0.0000000001) { + // nécessaire à cause des pb de précision des float/doubles en PHP : + // dans ce cas, on considère quand même que les 2 chiffres sont identiques + $toUpdate[$c] = $n[$c]; + } + }else{ + $toUpdate[$c] = $n[$c]; } } } @@ -591,16 +592,16 @@ class Table if (isset($options['columns'][$c]['transformer'])) { - $transVal = ':' . $c; - $transVal = '(' . sprintf($options['columns'][$c]['transformer'], $transVal) . ')'; - $whereSql .= $c . ' = ' . $transVal; + $transVal = ':' . $c; + $transVal = '(' . sprintf($options['columns'][$c]['transformer'], $transVal) . ')'; + $whereSql .= $c . ' = ' . $transVal; $params[$c] = $v; } else { if ($v === null) { $whereSql .= $c . ' IS NULL'; } else { - $transVal = ':' . $c; - $whereSql .= $c . ' = ' . $transVal; + $transVal = ':' . $c; + $whereSql .= $c . ' = ' . $transVal; $params[$c] = $v; } }