Skip to content

Commit 6efa8d2

Browse files
committed
Change IN statement
1 parent 502338b commit 6efa8d2

4 files changed

Lines changed: 38 additions & 24 deletions

File tree

src/QueryBuilder/Expr.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,19 @@ public static function isNotNull(string $column, mixed $value): array {
123123
'expr' => "`{$column}` IS NOT NULL :{$column}"
124124
];
125125
}
126+
127+
/**
128+
* in (IN)
129+
*
130+
* @param string $column
131+
* @param array $value
132+
* @return array
133+
*/
134+
public static function in(string $column, array $value): array {
135+
return [
136+
'value' => '(' . implode(',', $value) . ')',
137+
'column' => $column,
138+
'expr' => "`{$column}` IN :{$column}"
139+
];
140+
}
126141
}

src/QueryBuilder/QueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __toString(): string
3838
$request = str_replace('[TABLE_NAME]', $this->table, $this->action);
3939

4040
if (!is_null($this->condition)) $request .= $this->condition;
41-
if (!is_null($this->in)) $request .= $this->in;
41+
if (!is_null($this->in)) $request .= (is_null($this->condition) ? 'WHERE ' : ' AND ') . implode(' AND ', $this->in);
4242
if (!is_null($this->orderBy)) $request .= $this->orderBy;
4343
if (!is_null($this->limit)) $request .= $this->limit;
4444

src/QueryBuilder/QueryMethods.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ trait QueryMethods
1111
protected array $values = [];
1212
protected ?string $orderBy = null;
1313
protected ?string $limit = null;
14-
protected ?string $in = null;
14+
protected ?array $in = null;
1515

1616
public function table(string $table): self
1717
{
@@ -106,16 +106,20 @@ public function where(array ...$where): self
106106
$condition = $this->condition . ' OR (';
107107
}
108108
foreach ($where as $value) {
109-
$marker = ':' . $value['column'] . QueryBuilder::SECURE . count($this->values);
110-
111-
$condition .= $value['expr'] . QueryBuilder::SECURE . count($this->values) . " AND ";
109+
if (str_contains($value['expr'], '` IN :' . $value['column'])) {
110+
$this->in[] = str_replace(':' . $value['column'], $value['value'], $value['expr']);
111+
} else {
112+
$marker = ':' . $value['column'] . QueryBuilder::SECURE . count($this->values);
113+
$condition .= $value['expr'] . QueryBuilder::SECURE . count($this->values) . " AND ";
112114

113-
$this->values[
114-
$marker
115+
$this->values[
116+
$marker
115117
] = $value['value'];
118+
119+
$condition = rtrim($condition, ' AND ') . ')';
120+
$this->condition = $condition;
121+
}
116122
}
117-
$condition = rtrim($condition, ' AND ') . ')';
118-
$this->condition = $condition;
119123
return $this;
120124
}
121125

@@ -144,17 +148,4 @@ public function limit(int $limit, int $offset = 0): self
144148
$this->limit = " LIMIT $limit OFFSET $offset";
145149
return $this;
146150
}
147-
148-
/**
149-
* in
150-
*
151-
* @param array $in
152-
* @return self
153-
*/
154-
public function in(array $in): self
155-
{
156-
$in = implode(',', $in);
157-
$this->in = " IN($in)";
158-
return $this;
159-
}
160151
}

tests/index.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@
5757
])
5858
->execute($i);
5959

60-
var_dump(Database::queryBuilder('test')
60+
Database::queryBuilder('test')
6161
->select('col1', 'col2')
6262
->orderBy('col1', QueryBuilder::DESC)
6363
->limit(2)
6464
->where(
6565
Database::expr()->neq('col1', 50)
6666
)
67-
->fetchAll($i));
67+
->fetchAll($i);
6868
/*
6969
Database::queryBuilder('test')
7070
->delete()
@@ -81,3 +81,11 @@
8181
Database::expr()::lte('col1', 50)
8282
)
8383
->execute($i);
84+
85+
var_dump(Database::queryBuilder('test')
86+
->select('col2')
87+
->where(Database::expr()::in('id', ['27', 30]),
88+
Database::expr()::in('id', [29, 42, 46]))
89+
->simulate()
90+
->fetchAll()
91+
);

0 commit comments

Comments
 (0)