From f93bfebd5a504eb5b69e44eaa3949843201a46a4 Mon Sep 17 00:00:00 2001 From: Mikhail Chusavitin Date: Wed, 21 Jan 2026 17:52:19 +0300 Subject: [PATCH] updatetable with logs --- src/DataService.php | 103 ++++++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 43 deletions(-) diff --git a/src/DataService.php b/src/DataService.php index 7ee3576..1f25b7f 100644 --- a/src/DataService.php +++ b/src/DataService.php @@ -194,51 +194,68 @@ private function formatPDOError(\PDOException $e, string $schema, string $table, } - public function updateRow(string $schema, string $table, array $row, array $columns, array $pk): array - { - if (empty($pk)) { - throw new \RuntimeException('No primary key — update disabled'); - } - - $sets = []; - $params = []; - - foreach ($columns as $c) { - $name = $c['COLUMN_NAME']; - if (in_array($name, $pk, true)) { - continue; - } - if (array_key_exists($name, $row)) { - $sets[] = "`$name` = :v_$name"; - $params[":v_$name"] = $row[$name]; - } - } - - if (empty($sets)) { - return ['updated' => 0, 'message' => 'No changes']; - } - - $whereParts = []; - foreach ($pk as $name) { - if (!array_key_exists($name, $row)) { - throw new \RuntimeException("Missing PK value: $name"); - } - $whereParts[] = "`$name` = :pk_$name"; - $params[":pk_$name"] = $row[$name]; - } - - $sql = sprintf( - "UPDATE `%s`.`%s` SET %s WHERE %s", - $schema, $table, - implode(', ', $sets), - implode(' AND ', $whereParts) - ); - $stmt = $this->pdo->prepare($sql); - $stmt->execute($params); - - return ['updated' => $stmt->rowCount()]; +public function updateRow(string $schema, string $table, array $row, array $columns, array $pk): array +{ + error_log("=== UPDATE ROW ==="); + error_log("Schema: $schema"); + error_log("Table: $table"); + error_log("Row data: " . json_encode($row, JSON_UNESCAPED_UNICODE)); + error_log("PK: " . json_encode($pk)); + + if (empty($pk)) { + throw new \RuntimeException('No primary key — update disabled'); } + $sets = []; + $params = []; + + foreach ($columns as $c) { + $name = $c['COLUMN_NAME']; + if (in_array($name, $pk, true)) { + continue; + } + if (array_key_exists($name, $row)) { + $sets[] = "`$name` = :v_$name"; + $params[":v_$name"] = $row[$name]; + } + } + + if (empty($sets)) { + error_log("No changes to update"); + return ['updated' => 0, 'message' => 'No changes']; + } + + $whereParts = []; + foreach ($pk as $name) { + if (!array_key_exists($name, $row)) { + error_log("ERROR: Missing PK value: $name"); + throw new \RuntimeException("Missing PK value: $name"); + } + $whereParts[] = "`$name` = :pk_$name"; + $params[":pk_$name"] = $row[$name]; + } + + $sql = sprintf( + "UPDATE `%s`.`%s` SET %s WHERE %s", + $schema, $table, + implode(', ', $sets), + implode(' AND ', $whereParts) + ); + + error_log("SQL: $sql"); + error_log("Params: " . json_encode($params, JSON_UNESCAPED_UNICODE)); + + $stmt = $this->pdo->prepare($sql); + $stmt->execute($params); + + $rowCount = $stmt->rowCount(); + error_log("Rows updated: $rowCount"); + error_log("=== UPDATE COMPLETE ==="); + + return ['updated' => $rowCount]; +} + + public function deleteRow(string $schema, string $table, array $row, array $pk): array { if (empty($pk)) {