updatetable with logs

This commit is contained in:
Mikhail Chusavitin
2026-01-21 17:52:19 +03:00
parent 5a2bc4cd88
commit f93bfebd5a

View File

@@ -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)) {