Skip to content

Commit ae29712

Browse files
committed
Fix bug with IN statement
1 parent 1846495 commit ae29712

3 files changed

Lines changed: 14 additions & 21 deletions

File tree

src/QueryBuilder/Expr.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public static function in(string $column, array $value): array {
135135
return [
136136
'value' => '(' . implode(',', $value) . ')',
137137
'column' => $column,
138-
'expr' => "`{$column}` IN :{$column}"
138+
'expr' => "`{$column}` IN (:{$column}"
139139
];
140140
}
141141
}

src/QueryBuilder/QueryBuilder.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function __toString(): string
4848
if (!is_null($this->as)) $request .= " AS {$this->as}";
4949
if (!is_null($this->join)) $request .= " {$this->join}";
5050
if (!is_null($this->condition)) $request .= $this->condition;
51-
if (!is_null($this->in)) $request .= (is_null($this->condition) ? 'WHERE ' : ' AND ') . implode(' AND ', $this->in);
5251
if (!is_null($this->orderBy)) $request .= $this->orderBy;
5352
if (!is_null($this->limit)) $request .= $this->limit;
5453

src/QueryBuilder/QueryMethods.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Bubu\Database\QueryBuilder;
34

45
trait QueryMethods
@@ -11,7 +12,6 @@ trait QueryMethods
1112
protected array $values = [];
1213
protected ?string $orderBy = null;
1314
protected ?string $limit = null;
14-
protected ?array $in = null;
1515
protected ?string $join = null;
1616

1717
public function table(string $table): self
@@ -31,9 +31,7 @@ public function insert(array $values): self
3131
foreach ($values as $key => $value) {
3232
$columns .= "`{$key}`, ";
3333
$markers .= ":{$key}, ";
34-
$this->values[
35-
$key
36-
] = $value;
34+
$this->values[$key] = $value;
3735
}
3836
$columns = trim($columns, ', ');
3937
$markers = trim($markers, ', ');
@@ -83,9 +81,7 @@ public function update(array $values): self
8381

8482
$request .= "`{$col}` = :{$marker}, ";
8583

86-
$this->values[
87-
$marker
88-
] = $value;
84+
$this->values[$marker] = $value;
8985
}
9086
$request = trim($request, ', ');
9187
$this->action = 'UPDATE `[TABLE_NAME]` SET ' . $request;
@@ -107,20 +103,18 @@ public function where(array ...$where): self
107103
$condition = $this->condition . ' OR (';
108104
}
109105
foreach ($where as $value) {
110-
if (str_contains($value['expr'], '` IN :' . $value['column'])) {
111-
$this->in[] = str_replace(':' . $value['column'], $value['value'], $value['expr']);
112-
} else {
113-
$marker = ':' . str_replace('.', '_', $value['column']) . QueryBuilder::SECURE . count($this->values);
114-
$tmpExpr = explode(' ', $value['expr']);
115-
$tmpExpr[count($tmpExpr)-1] = str_replace('.', '_', $tmpExpr[count($tmpExpr)-1]);
116-
$value['expr'] = implode(' ', $tmpExpr);
117-
$condition .= $value['expr'] . QueryBuilder::SECURE . count($this->values) . " AND ";
118106

119-
$this->values[
120-
$marker
121-
] = $value['value'];
107+
$marker = ':' . str_replace('.', '_', $value['column']) . QueryBuilder::SECURE . count($this->values);
108+
$tmpExpr = explode(' ', $value['expr']);
109+
$tmpExpr[count($tmpExpr) - 1] = str_replace('.', '_', $tmpExpr[count($tmpExpr) - 1]);
110+
$value['expr'] = implode(' ', $tmpExpr);
111+
$condition .= $value['expr']
112+
. QueryBuilder::SECURE
113+
. count($this->values)
114+
. (str_contains($value['expr'], '` IN (:' . $value['column']) ? ')' : '')
115+
. " AND ";
122116

123-
}
117+
$this->values[$marker] = $value['value'];
124118
}
125119
$condition = rtrim($condition, ' AND ') . ')';
126120
$this->condition = $condition;

0 commit comments

Comments
 (0)