summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiogo Galvão <diogo.galvao@fiocruz.br>2025-11-21 17:54:54 -0300
committerDamien Regad <dregad@mantisbt.org>2025-11-22 00:46:22 +0100
commit99390853e507056351f1b4dd38f35cd700179c30 (patch)
treefdf54682213dfae49a3abccaddb7c5426aefc08d
parent481bb5292033af2918bea9a9af4b551781d17f5f (diff)
downloadadodb-99390853e507056351f1b4dd38f35cd700179c30.tar.gz
adodb-99390853e507056351f1b4dd38f35cd700179c30.tar.bz2
adodb-99390853e507056351f1b4dd38f35cd700179c30.zip
Fix autoExecute() skipping updates
AutoExecute fetches a row from the table prior to performing an insert or update. When $forceUpdate is false, only modified columns should be updated. However, the WHERE clause was not being used when fetching the row, so the comparison was always made against an arbitrary row, causing differences to be detected or missed by chance. This change makes use of the $where parameter when fetching the row from the table, making the comparison work as intended. Fixes #1146
-rw-r--r--adodb.inc.php7
-rw-r--r--tests/test-autoexecute-update.php28
2 files changed, 32 insertions, 3 deletions
diff --git a/adodb.inc.php b/adodb.inc.php
index f995eeb0..6726d042 100644
--- a/adodb.inc.php
+++ b/adodb.inc.php
@@ -2665,15 +2665,16 @@ if (!defined('_ADODB_LAYER')) {
}
$sql = "SELECT * FROM $table";
+ if (!empty($where)) {
+ $sql .= " WHERE $where";
+ }
+
$rs = $this->SelectLimit($sql, 1);
if (!$rs) {
return false; // table does not exist
}
$rs->tableName = $table;
- if (!empty($where)) {
- $sql .= " WHERE $where";
- }
$rs->sql = $sql;
switch($mode) {
diff --git a/tests/test-autoexecute-update.php b/tests/test-autoexecute-update.php
new file mode 100644
index 00000000..4ac8b619
--- /dev/null
+++ b/tests/test-autoexecute-update.php
@@ -0,0 +1,28 @@
+<?php
+
+require __DIR__ . "/../adodb.inc.php";
+
+$db = NewADOConnection("sqlite3");
+$db->Connect(":memory:");
+
+$db->Execute("CREATE TABLE users(id primary key, name, year_of_birth)");
+$db->Execute("INSERT INTO users VALUES (1, 'John', 2000)");
+$db->Execute("INSERT INTO users VALUES (2, 'Jane', 1981)");
+
+$new_value = 2000;
+
+$db->autoExecute(
+ table: "users",
+ fields_values: ["year_of_birth" => $new_value],
+ mode: "UPDATE",
+ where: "id = 2",
+ forceUpdate: false,
+);
+
+$updated_value = $db->GetOne("SELECT year_of_birth FROM users WHERE id = 2");
+
+if ($updated_value != $new_value) {
+ die("ERROR: updated_value $updated_value != new_value $new_value");
+}
+
+echo "Finished.\n";