Now deletions will also work with complicated queries

This commit is contained in:
Michael 2017-08-12 18:07:47 +00:00
parent 89017d4e6c
commit 41a81624a2
2 changed files with 18 additions and 10 deletions

View File

@ -1080,16 +1080,16 @@ class dba {
$callstack[$qkey] = true; $callstack[$qkey] = true;
// Fetch all rows that are to be deleted // Fetch all rows that are to be deleted
$sql = "SELECT ".self::$dbo->escape($field)." FROM `".$table."` WHERE `". $data = self::select($table, array($field), $param);
implode("` = ? AND `", array_keys($param))."` = ?";
$data = self::p($sql, $param);
while ($row = self::fetch($data)) { while ($row = self::fetch($data)) {
// Now we accumulate the delete commands // Now we accumulate the delete commands
$retval = self::delete($table, array($field => $row[$field]), true, $callstack); $retval = self::delete($table, array($field => $row[$field]), true, $callstack);
$commands = array_merge($commands, $retval); $commands = array_merge($commands, $retval);
} }
self::close($data);
// Since we had split the delete command we don't need the original command anymore // Since we had split the delete command we don't need the original command anymore
unset($commands[$key]); unset($commands[$key]);
} }
@ -1105,14 +1105,22 @@ class dba {
$compacted = array(); $compacted = array();
$counter = array(); $counter = array();
foreach ($commands AS $command) { foreach ($commands AS $command) {
if (count($command['param']) > 1) { $condition = $command['param'];
$sql = "DELETE FROM `".$command['table']."` WHERE `". $array_element = each($condition);
implode("` = ? AND `", array_keys($command['param']))."` = ?"; $array_key = $array_element['key'];
if (is_int($array_key)) {
$condition_string = " WHERE ".array_shift($condition);
} else {
$condition_string = " WHERE `".implode("` = ? AND `", array_keys($condition))."` = ?";
}
logger(self::replace_parameters($sql, $command['param']), LOGGER_DATA); if ((count($command['param']) > 1) || is_int($array_key)) {
$sql = "DELETE FROM `".$command['table']."`".$condition_string;
logger(self::replace_parameters($sql, $condition), LOGGER_DATA);
if (!self::e($sql, $command['param'])) { if (!self::e($sql, $condition)) {
if ($do_transaction) { if ($do_transaction) {
self::rollback(); self::rollback();
} }
@ -1159,7 +1167,7 @@ class dba {
return $commands; return $commands;
} }
// $param
/** /**
* @brief Updates rows * @brief Updates rows
* *

View File

@ -81,7 +81,7 @@ function display_init(App $a) {
$contactid = get_contact($r['owner-link'], local_user()); $contactid = get_contact($r['owner-link'], local_user());
if ($contactid) { if ($contactid) {
$items = dba::p("SELECT * FROM `item` WHERE `parent` = ? ORDER BY `id`", $r["id"]); $items = dba::p("SELECT * FROM `item` WHERE `parent` = ? ORDER BY `id`", $r["id"]);
while ($item = self::fetch($items)) { while ($item = dba::fetch($items)) {
$itemcontactid = get_contact($item['owner-link'], local_user()); $itemcontactid = get_contact($item['owner-link'], local_user());
if (!$itemcontactid) { if (!$itemcontactid) {
$itemcontactid = $contactid; $itemcontactid = $contactid;