diff --git a/extdbms/lib/Database/QueryBuilder.php b/extdbms/lib/Database/QueryBuilder.php index ddf3a79..86a78db 100644 --- a/extdbms/lib/Database/QueryBuilder.php +++ b/extdbms/lib/Database/QueryBuilder.php @@ -75,35 +75,33 @@ class QueryBuilder } //Where절부분 - final public function where(array|string $column, mixed $operator = null, mixed $value = null): static + final public function where(array|string $column, mixed $value = null, ?string $operator = null, string $boolean = "AND"): static { if (is_array($column)) { foreach ($column as $col => $val) { - $this->where($col, '=', $val); // 재귀 호출 + $this->where($col, $val); // 재귀 호출 } return $this; } // where("col = NOW()") 형태의 raw 처리 - if ($operator === null && $value === null) { - $this->where[] = ['condition' => $column, 'boolean' => 'AND']; + if ($value === null && $operator === null) { + $this->where[] = ['condition' => $column, 'boolean' => $boolean]; return $this; } // where("col", "val") → operator 생략 시 = 처리 - if ($value === null) { - $value = $operator; - $operator = '='; + if ($operator === null) { + $operator = "="; } $placeholder = ':w_' . count($this->bindings); - $this->where[] = ['condition' => "$column $operator $placeholder", 'boolean' => 'AND']; + $this->where[] = ['condition' => "$column $operator $placeholder", 'boolean' => $boolean]; $this->bindings[$placeholder] = $value; return $this; } - final public function orWhere(string $column, mixed $operator = null, mixed $value = null): static + final public function orWhere(array|string $column, mixed $value = null, ?string $operator = null, string $boolean = "OR"): static { - return $this->where($column, $operator, $value, "OR"); + return $this->where($column, $value, $operator, $boolean); } - - final public function whereIn(string $column, array $values, string $boolean = 'AND', $conditon_boolean = "IN"): static + final public function whereIn(string $column, array $values, string $boolean = 'AND', $conditon = "IN"): static { if (empty($values)) { throw new \InvalidArgumentException(__FUNCTION__ . ": values 배열이 비어있을 수 없습니다."); @@ -114,23 +112,23 @@ class QueryBuilder $placeholders[] = $placeholder; $this->bindings[$placeholder] = $value; } - $condition = "$column $conditon_boolean (" . implode(', ', $placeholders) . ")"; + $condition = "$column $conditon (" . implode(', ', $placeholders) . ")"; $this->where[] = ['condition' => $condition, 'boolean' => $boolean]; return $this; } - final public function whereNotIn(string $column, array $values, $boolean = "AND", $conditon_boolean = "NOT IN"): static + final public function whereNotIn(string $column, array $values, $boolean = "AND", $conditon = "NOT IN"): static { if (empty($values)) { throw new \InvalidArgumentException(__FUNCTION__ . ": values 배열이 비어있을 수 없습니다."); } - return $this->whereIn($column, $values, $boolean, $conditon_boolean); + return $this->whereIn($column, $values, $boolean, $conditon); } - final public function orWhereIn(string $column, array $values, $boolean = "OR", $conditon_boolean = "IN"): static + final public function orWhereIn(string $column, array $values, $boolean = "OR", $conditon = "IN"): static { if (empty($values)) { throw new \InvalidArgumentException(__FUNCTION__ . ": values 배열이 비어있을 수 없습니다."); } - return $this->whereIn($column, $values, $boolean, $conditon_boolean); + return $this->whereIn($column, $values, $boolean, $conditon); } //Join @@ -140,6 +138,59 @@ class QueryBuilder return $this; } + //Like + //사용예: + // $model->like(['name' => '%홍%', 'email' => '%@naver.com%']); + // $model->likeIn(['title', 'description'], '%공지%'); + // $model->orLikeIn(['name', 'nickname'], '%철수%'); + public function like(array|string $column, ?string $value = null, string $operator = 'LIKE', string $boolean = "AND"): static + { + if (is_array($column)) { + $conditions = []; + foreach ($column as $col => $val) { + $placeholder = ':l_' . count($this->bindings); + $conditions[] = "$col $operator $placeholder"; + $this->bindings[$placeholder] = $val; + } + $this->where[] = '(' . implode(" $boolean ", $conditions) . ')'; + } else { + $placeholder = ':l_' . count($this->bindings); + $condition = "$column $operator $placeholder"; + if (empty($this->where)) { + $this->where[] = $condition; + } else { + $last = array_pop($this->where); + $this->where[] = "($last $boolean $condition)"; + } + $this->bindings[$placeholder] = $value; + } + return $this; + } + public function orLike(array|string $column, ?string $value = null, string $operator = 'LIKE', string $boolean = "OR"): static + { + return $this->like($column, $value, $operator, $boolean); + } + + public function likeIn(array $columns, string $value, string $operator = "LIKE", string $boolean = "AND"): static + { + $orConditions = []; + foreach ($columns as $col) { + $placeholder = ':li_' . count($this->bindings); + $orConditions[] = "$col $operator $placeholder"; + $this->bindings[$placeholder] = $value; + } + $this->where[] = '(' . implode(" $boolean ", $orConditions) . ')'; + return $this; + } + public function orLikeIn(array $columns, string $value, string $operator = "LIKE", string $boolean = "OR"): static + { + return $this->likeIn($columns, $value, $operator, $boolean); + } + public function notLikeIn(array $columns, string $value, string $operator = "NOT LIKE", string $boolean = "AND"): static + { + return $this->likeIn($columns, $value, $operator, $boolean); + } + //Order final public function orderBy(mixed $column, string $direction = 'ASC'): static {