diff --git a/extdbms/lib/Core/Model.php b/extdbms/lib/Core/Model.php index 47ef6f0..8d46760 100644 --- a/extdbms/lib/Core/Model.php +++ b/extdbms/lib/Core/Model.php @@ -70,7 +70,7 @@ abstract class Model } return implode(",", $temps); } - final public function makeQuery(mixed $columns, mixed $values = null, $delimeter = ","): string + final public function makeWhere(mixed $columns, mixed $values = null, $delimeter = ","): string { $temps = []; if (is_array($columns)) { @@ -85,9 +85,14 @@ abstract class Model // throw new \Exception("DATA:" . $columns . $value === null ? "NULL" : $value); return implode($delimeter . " ", $temps); } + final public function getWhere(): string + { + return count($this->_wheres) ? "WHERE " . implode(" ", $this->_wheres) : ""; + } + final public function where(mixed $columns, mixed $values = null, string $delimeter = "AND"): void { - $query = $this->makeQuery($columns, $values, $delimeter); + $query = $this->makeWhere($columns, $values, $delimeter); $this->_wheres[] = count($this->_wheres) ? $delimeter . " " . $query : $query; } final public function orWhere(mixed $columns, mixed $values = null, string $delimeter = "OR"): void @@ -96,16 +101,13 @@ abstract class Model } final public function whereIn(string $column, array $values, string $delimeter = "AND", $range = "IN") { - $this->_wheres[] = "{$delimeter} {$column} {$range} (" . $this->getValue($values) . ")"; + $query = " {$column} {$range} (" . $this->getValue($values) . ")"; + $this->_wheres[] = count($this->_wheres) ? $delimeter . " " . $query : $query; } final public function whereNotIn(string $column, array $values, string $delimeter = "AND", $range = "NOT IN") { $this->whereIn($column, $values, $delimeter, $range); } - final public function getWhere(): string - { - return count($this->_wheres) ? "WHERE " . implode(" ", $this->_wheres) : ""; - } final public function execute(string $query): bool|PDOStatement { @@ -125,12 +127,12 @@ abstract class Model //CURD문 final protected function create_process(mixed $columns, mixed $values = null): bool|PDOStatement { - $query = sprintf("INSERT INTO %s VALUES(%s) %s", $this->getTable(), $this->makeQuery($columns, $values), $this->getWhere()); + $query = sprintf("INSERT INTO %s VALUES(%s) %s", $this->getTable(), $this->makeWhere($columns, $values), $this->getWhere()); return $this->execute($query); } // final protected function modify_process(mixed $columns, mixed $values = null): bool|PDOStatement { - $query = sprintf("UPDATE %s SET %s %s", $this->getTable(), $this->makeQuery($columns, $values), $this->getWhere()); + $query = sprintf("UPDATE %s SET %s %s", $this->getTable(), $this->makeWhere($columns, $values), $this->getWhere()); return $this->execute($query); } // final protected function delete_process(): bool|PDOStatement @@ -160,20 +162,20 @@ abstract class Model //Result protected function getResult($mode = PDO::FETCH_ASSOC): mixed { - $query = "{$this->_querys['SELECT']} FROM {$this->getTable()} {$this->getWhere()} {$this->_querys['JOIN']} {$this->_querys['LIMIT']} {$this->_querys['ORDERBY']}"; + $query = "{$this->_querys['SELECT']} FROM {$this->getTable()} {$this->_querys['JOIN']} {$this->getWhere()} {$this->_querys['ORDERBY']} {$this->_querys['LIMIT']}"; $stmt = $this->execute($query); return $stmt->fetch($mode); } protected function getResults($mode = PDO::FETCH_ASSOC): mixed { - $query = "{$this->_querys["SELECT"]} FROM {$this->getTable()} {$this->getWhere()} {$this->_querys['JOIN']} {$this->_querys['ORDERBY']} {$this->_querys['LIMIT']}"; + $query = "{$this->_querys["SELECT"]} FROM {$this->getTable()} {$this->_querys['JOIN']} {$this->getWhere()} {$this->_querys['ORDERBY']} {$this->_querys['LIMIT']}"; $stmt = $this->execute($query); return $stmt->fetchAll($mode); } final public function countAllResults(string $column = "*", $reset = true): int { $this->_reset = $reset; - $query = "SELECT COUNT({$column}) FROM {$this->getTable()} {$this->getWhere()} {$this->_querys['JOIN']}"; + $query = "SELECT COUNT({$column}) FROM {$this->getTable()} {$this->_querys['JOIN']} {$this->getWhere()}"; $stmt = $this->execute($query); $count = $stmt->fetchColumn(0); $this->_reset = true;