Skip to content

Commit 06876ef

Browse files
author
Wazabii
committed
Patch
1 parent cdd7e3e commit 06876ef

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

DB.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ public function whereRaw(string $sql, ...$arr): self
335335
if (is_array($arr[0] ?? null)) {
336336
$arr = $arr[0];
337337
}
338-
$this->resetWhere();
339338
$this->where[$this->whereIndex][$this->whereAnd][] = $this->sprint($sql, $arr);
339+
$this->resetWhere();
340340
return $this;
341341
}
342342

@@ -402,8 +402,8 @@ public function havingRaw(string $sql, ...$arr): self
402402
if (is_array($arr[0] ?? null)) {
403403
$arr = $arr[0];
404404
}
405-
$this->resetWhere();
406405
$this->having[$this->whereIndex][$this->whereAnd][] = $this->sprint($sql, $arr);
406+
$this->resetWhere();
407407
return $this;
408408
}
409409

@@ -495,7 +495,6 @@ public function join(
495495
array $sprint = array(),
496496
string $type = "INNER"
497497
): self {
498-
499498
if ($table instanceof MigrateInterface) {
500499
$this->join = array_merge($this->join, $this->buildJoinFromMig($table, $type));
501500
} else {
@@ -512,10 +511,16 @@ public function join(
512511
$data = array();
513512
foreach ($where as $key => $val) {
514513
if (is_array($val)) {
515-
foreach ($val as $k => $v) {
516-
$this->setWhereData($k, $v, $data);
514+
foreach ($val as $grpKey => $grpVal) {
515+
if(!($grpVal instanceof AttrInterface)) {
516+
$grpVal = $this::withAttr($grpVal)->enclose(false);
517+
}
518+
$this->setWhereData($grpKey, $grpVal, $data);
517519
}
518520
} else {
521+
if(!($val instanceof AttrInterface)) {
522+
$val = $this::withAttr($val)->enclose(false);
523+
}
519524
$this->setWhereData($key, $val, $data);
520525
}
521526
}
@@ -527,7 +532,6 @@ public function join(
527532
$this->join[] = "{$type} JOIN {$prefix}{$table}{$alias} ON " . $out;
528533
$this->joinedTables[$table] = "{$prefix}{$table}";
529534
}
530-
531535
return $this;
532536
}
533537

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ $select->not()->whereId(1)->whereEmail("john.doe@gmail.com");
5757
```
5858
### Where 3
5959
```php
60-
$select->whereBind(function($inst) {
61-
$select->where("start_date", "2023-01-01", ">=")
60+
$select->whereBind(function($select) {
61+
$select
62+
->where("start_date", "2023-01-01", ">=")
6263
->where("end_date", "2023-01-14", "<=");
6364
})->or()->whereStatus(1);
6465
// (start_date >= '2023-01-01' AND end_date <= '2023-01-14') OR (status = '1')
@@ -97,8 +98,20 @@ $select->orderRaw("id ASC, parent DESC");
9798
// ORDER BY id ASC, parent DESC
9899
```
99100
### Join
101+
**Note** that no value in the join is, by default, enclosed. This means it will not add quotes to strings. This means it will attempt to add a database column by default if it is a string, and will return an error if the string column does not exist. If you want to enclose the value with quotes, use Attributes (see the section below).
100102
```php
101103
$select->join(["login", "aliasB"], ["aliasB.user_id" => "aliasA.id"]); // PROTECTED INPUT
104+
105+
$select->join("login", ["user_id" => "id"]);
106+
// user_id = id AND org_id = oid
107+
108+
// This will enclose and reset all protections
109+
$slug = DB::withAttr("my-slug-value");
110+
$select->join("login", [
111+
["slug" => $slug],
112+
["visible" => 1]
113+
]);
114+
102115
$select->join("tableName", "b.user_id = '%d'", [872], "LEFT"); // PROTECTED INPUT
103116
$select->join("tableName", "b.user_id = a.id"); // "UNPROTECTED" INPUT
104117

@@ -191,5 +204,5 @@ public function jsonEncode(bool $jsonEncode): self;
191204
- Input value: array("firstname" => "John", "lastname" => "Doe");
192205
- Output value: {"firstname":"John","lastname":"Doe"}
193206

194-
The default values vary based on whether it is a table column, a condition in a WHERE clause, or a value to be set. For instance, columns default to enclose set to false, whereas for WHERE or SET inputs, it defaults to true. Regardless, every value defaults to **prep**, **encode** and **jsonEncode** being set to **true**.
207+
The default values vary based on whether it is a table column, a condition in a WHERE clause, or a value to be set. For instance, if expecting a table columns, the default is not to enclose value with quotes, whereas for WHERE or SET inputs, it defaults is to enclose the values. Regardless, every value defaults to **prep**, **encode** and **jsonEncode** being set to **true**.
195208

0 commit comments

Comments
 (0)