fix: only set WHERE filters on delete() if conditions are provided (#51)

This PR fixes a bug where if no conditions are passed to `MySQL->from("table")->delete()`, the generated query will look like this:
```sql
DELETE FROM `table` WHERE
```

This is obviously invalid SQL syntax. This PR only adds the `WHERE` keyword and rules if conditions have been supplied to either `where()` or with `delete([])`

Reviewed-on: https://codeberg.org/vlw/php-mysql/pulls/51
This commit is contained in:
Victor Westerlund 2025-07-29 09:46:46 +02:00
parent ddcd8a2961
commit 0e367f797f

View file

@ -268,9 +268,14 @@
$this->throw_if_no_table();
// Set DELETE WHERE conditions from arguments
$this->where(...$conditions);
if ($conditions) {
$this->where(...$conditions);
}
$sql = "DELETE FROM `{$this->table}` WHERE {$this->filter_sql}";
// Get array of SQL WHERE string and filter values
$filter_sql = !is_null($this->filter_sql) ? " WHERE {$this->filter_sql}" : "";
$sql = "DELETE FROM `{$this->table}`{$filter_sql}";
return $this->execute_query($sql, self::to_list_array($this->filter_values));
}