Compare commits

...

5 commits

Author SHA1 Message Date
73297feb82 fix: use isset() when checking table property value (#52)
We can't access the `$this->table` property before initialization, so let's use `isset()` to check if the property has a value.

Reviewed-on: https://codeberg.org/vlw/php-mysql/pulls/52
2025-08-30 09:49:14 +02:00
0e367f797f 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
2025-07-29 09:46:46 +02:00
ddcd8a2961 fix: $offset not set when passing integer to $limit in limit() (#50)
This PR just sets the `OFFSET` value to `0` if no offset is provided for all calls to `limit()`. This fixes a bug where no `OFFSET` was set if the `$limit` parameter was given an integer.. which was the only allowed type except null anyways.

Reviewed-on: https://codeberg.org/vlw/php-mysql/pulls/50
2025-07-17 11:09:02 +02:00
e062930c41 fix: add missing return statement from deprecated 'for()' method (#49)
Follow-up PR from #46. Forgot to return from the deprecated method.

Reviewed-on: https://codeberg.org/vlw/php-mysql/pulls/49
2025-06-12 12:44:26 +02:00
vlw
814070a52e doc(fix): missed reference of "for()" to "from()" in README (#48)
Of course I missed to change one reference of `for()` to `from()`.

Reviewed-on: https://codeberg.org/vlw/php-mysql/pulls/48
Co-authored-by: vlw <victor@vlw.se>
Co-committed-by: vlw <victor@vlw.se>
2025-06-12 12:44:10 +02:00
2 changed files with 12 additions and 16 deletions

View file

@ -15,7 +15,7 @@ which would be equivalent to the following in MySQL:
SELECT `columns` FROM `table` WHERE `filter` ORDER BY `order_by` LIMIT `limit`; SELECT `columns` FROM `table` WHERE `filter` ORDER BY `order_by` LIMIT `limit`;
``` ```
- All methods can be chained in any order (even multiple times) after a [`for()`](#for) as long as a [`select()`](#select), [`insert()`](#insert), [`update()`](#update), or [`delete()`](#delete) is the last method. - All methods can be chained in any order (even multiple times) after a [`from()`](#from) as long as a [`select()`](#select), [`insert()`](#insert), [`update()`](#update), or [`delete()`](#delete) is the last method.
- Chaining the same method more than once will override its previous value. Passing `null` to any method that accepts it will unset its value completely. - Chaining the same method more than once will override its previous value. Passing `null` to any method that accepts it will unset its value completely.
## Install from composer ## Install from composer

View file

@ -35,7 +35,7 @@
*/ */
private function throw_if_no_table() { private function throw_if_no_table() {
if (!$this->table) { if (!isset($this->table)) {
throw new Exception("No table name defined"); throw new Exception("No table name defined");
} }
} }
@ -81,7 +81,7 @@
since: "3.5.7", since: "3.5.7",
)] )]
public function for(string $table): self { public function for(string $table): self {
$this->from($table); return $this->from($table);
} }
// Create a WHERE statement from filters // Create a WHERE statement from filters
@ -163,17 +163,8 @@
return $this; return $this;
} }
// Set LIMIT without range directly as integer // Coerce offset to zero if no offset is defined
if (is_int($limit)) { $offset = $offset ?? 0;
$this->limit = $limit;
return $this;
}
// No offset defined, set limit property directly as string
if (is_null($offset)) {
$this->limit = (string) $limit;
return $this;
}
// Set limit and offset as SQL CSV // Set limit and offset as SQL CSV
$this->limit = "{$offset},{$limit}"; $this->limit = "{$offset},{$limit}";
@ -277,9 +268,14 @@
$this->throw_if_no_table(); $this->throw_if_no_table();
// Set DELETE WHERE conditions from arguments // 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)); return $this->execute_query($sql, self::to_list_array($this->filter_values));
} }