diff --git a/src/Alias/Database/Query.php b/src/Alias/Database/Query.php deleted file mode 100644 index 0bda90d..0000000 --- a/src/Alias/Database/Query.php +++ /dev/null @@ -1,12 +0,0 @@ -build(); - } -} diff --git a/src/Alias/Database/Query/Delete.php b/src/Alias/Database/Query/Delete.php deleted file mode 100644 index a81d9c2..0000000 --- a/src/Alias/Database/Query/Delete.php +++ /dev/null @@ -1,59 +0,0 @@ -setTable($table); - } - public function where(array $conditions): DeleteInterface - { - return $this->setConditions($conditions); - } - - protected string $table; - public function setTable(string $table): DeleteInterface - { - $this->table = $table; - return $this; - } - public function getTable(): string - { - return $this->table; - } - protected array $conditions; - public function setConditions(array $conditions): DeleteInterface - { - foreach ($conditions as $condition) { - $this->addCondition($condition); - } - return $this; - } - public function addCondition(string $expression): DeleteInterface - { - $this->conditions []= $expression; - return $this; - } - public function getConditions(): array - { - return $this->conditions; - } - public function getConditionString(): string - { - return implode(' ', $this->getConditions()); - } - - public function build(): string - { - return implode(' ', [ - 'DELETE FROM', - $this->getTable(), - 'WHERE', - $this->getConditionString() - ]); - } -} diff --git a/src/Alias/Database/Query/Insert.php b/src/Alias/Database/Query/Insert.php deleted file mode 100644 index 3d9d570..0000000 --- a/src/Alias/Database/Query/Insert.php +++ /dev/null @@ -1,108 +0,0 @@ -setTable($table); - } - public function columns(array $columns): InsertInterface - { - return $this->setColumns($columns); - } - public function values(array $values): InsertInterface - { - return $this->setValues($values); - } - public function select(Select $select): InsertInterface - { - return $this->setSelect($select); - } - - protected string $table; - public function setTable(string $table): InsertInterface - { - $this->table = $table; - return $this; - } - public function getTable(): string - { - return $this->table; - } - protected array $columns; - public function setColumns(array $columns): InsertInterface - { - foreach ($columns as $column) { - $this->addColumn($column); - } - return $this; - } - public function addColumn(string $column): InsertInterface - { - $this->columns []= $column; - return $this; - } - public function getColumns(): array - { - return $this->columns; - } - public function getColumnString(): string - { - return implode(', ', $this->getColumns()); - } - protected array $values; - public function setValues(array $values): InsertInterface - { - foreach ($values as $value) { - $this->addValue($value); - } - return $this; - } - public function addValue(int|string $value): InsertInterface - { - if (!is_numeric($value) and $value !== '?') { - $value = "'{$value}'"; - } - $this->values []= $value; - return $this; - } - public function getValues(): array - { - return $this->values; - } - public function getValueString(): string - { - return implode(', ', $this->getValues()); - } - protected Select $select; - public function setSelect(Select $select): InsertInterface - { - $this->select = $select; - return $this; - } - public function getSelect(): Select - { - return $this->select; - } - - public function build(): string - { - $query = ["INSERT INTO"]; - $query []= $this->getTable(); - if (isset($this->columns)) { - $query []= "({$this->getColumnString()})"; - } - if (isset($this->select)) { - $query []= "{$this->getSelect()}"; - return implode(' ', $query); - } - $query []= 'VALUES'; - $query []= "({$this->getValueString()})"; - return implode(' ', $query); - } -} diff --git a/src/Alias/Database/Query/Select.php b/src/Alias/Database/Query/Select.php deleted file mode 100644 index 70702c4..0000000 --- a/src/Alias/Database/Query/Select.php +++ /dev/null @@ -1,246 +0,0 @@ -setColumns($columns); - } - public function from(string $table): SelectInterface - { - return $this->setTable($table); - } - public function joins(array $joins): SelectInterface - { - return $this->setJoins($joins); - } - public function where(array $conditions): SelectInterface - { - return $this->setConditions($conditions); - } - public function groupBy(array $grouping): SelectInterface - { - return $this->setGroups($grouping); - } - public function having(array $having): SelectInterface - { - return $this->setHaving($having); - } - public function orderBy(array $ordering): SelectInterface - { - return $this->setOrders($ordering); - } - - protected array $columns; - public function setColumns(array $columns): SelectInterface - { - foreach ($columns as $column) { - $col = $column; - $alias = null; - if (is_array($column)) { - $col = $column['column'] ?? $column[0]; - $alias = $column['alias'] ?? $column[1]; - } - $this->addColumn($column, $alias); - } - return $this; - } - public function addColumn(string $column, ?string $alias = null): SelectInterface - { - if ($column === '*') { - $this->columns []= $column; - return $this; - } - $a = ''; - if ($alias !== null) { - $a = " AS '{$alias}'"; - } - $this->columns[] = "`{$column}`{$a}"; - return $this; - } - public function getColumns(): array - { - return $this->columns; - } - public function getColumnString(): string - { - return implode(', ', $this->getColumns()); - } - protected string $table; - public function setTable(string $table, ?string $alias = null): SelectInterface - { - $table = "`{$table}`"; - if ($alias !== null) { - $table = "{$table} '{$alias}'"; - } - $this->table = $table; - return $this; - } - public function getTable(): string - { - return $this->table; - } - protected array $joins; - public function setJoins(array $joins): SelectInterface - { - foreach ($joins as $join) { - $table = $join['table'] ?? $join[0]; - $expression = $join['expression'] ?? $join[1]; - $this->addJoin($table, $expression); - } - return $this; - } - public function addJoin(string $table, string $expression): SelectInterface - { - $this->joins []= "{$table} ON {$expression}"; - return $this; - } - public function getJoins(): array - { - return $this->joins; - } - public function getJoinString(): string - { - $str = []; - foreach ($this->getJoins() as $i => $join) { - if (!str_contains(strtolower($join), 'join')) { - $str []= "JOIN {$join}"; - continue; - } - $str []= $join; - } - return implode(' ', $str); - } - protected array $conditions; - public function setConditions(array $conditions): SelectInterface - { - foreach ($conditions as $condition) { - $this->addCondition($condition); - } - return $this; - } - public function addCondition(string $expression): SelectInterface - { - $this->conditions []= $expression; - return $this; - } - public function getConditions(): array - { - return $this->conditions; - } - public function getConditionString(): string - { - $str = []; - foreach ($this->getConditions() as $i => $condition) { - if ($i === 0) { - $str []= $condition; - continue; - } - if (!str_contains(strtolower($condition), 'and') and !str_contains(strtolower($condition), 'or')) { - $str []= "AND {$condition}"; - continue; - } - $str []= $condition; - } - return implode(' ', $str); - } - protected array $groups; - public function setGroups(array $groups): SelectInterface - { - foreach ($groups as $group) { - $this->addGroup($group); - } - return $this; - } - public function addGroup(string $group): SelectInterface - { - $this->groups []= "`{$group}`"; - return $this; - } - public function getGroups(): array - { - return $this->groups; - } - public function getGroupString(): string - { - return implode(', ', $this->getGroups()); - } - protected array $having; - public function setHaving(array $having): SelectInterface - { - foreach ($having as $item) { - $this->addHaving($item); - } - return $this; - } - public function addHaving(string $having): SelectInterface - { - $this->having []= $having; - return $this; - } - public function getHaving(): array - { - return $this->having; - } - public function getHavingString(): string - { - return implode(', ', $this->getHaving()); - } - protected array $orders; - public function setOrders(array $orders): SelectInterface - { - foreach ($orders as $order) { - $column = $order; - $direction = null; - if (is_array($order)) { - $direction = $order['direction'] ?? $order[1]; - $column = $order['column'] ?? $order[0]; - } - $this->addOrder($column, $direction); - } - return $this; - } - public function addOrder(string $column, ?string $direction = null): SelectInterface - { - if ($direction === null) { - $direction = 'ASC'; - } - $this->orders []= "{$column} {$direction}"; - return $this; - } - public function getOrders(): array - { - return $this->orders; - } - public function getOrderString(): string - { - return implode(', ', $this->getOrders()); - } - - public function build(): string - { - $query = ["SELECT"]; - $query []= $this->getColumnString(); - $query []= "FROM {$this->getTable()}"; - if (isset($this->joins)) { - $query []= $this->getJoinString(); - } - if (isset($this->conditions)) { - $query []= "WHERE {$this->getConditionString()}"; - } - if (isset($this->groups)) { - $query []= "GROUP BY {$this->getGroupString()}"; - } - if (isset($this->having)) { - $query []= "HAVING {$this->getHavingString()}"; - } - if (isset($this->orders)) { - $query []= "ORDER BY {$this->getOrderString()}"; - } - return implode(' ', $query); - } -} diff --git a/src/Alias/Database/Query/Update.php b/src/Alias/Database/Query/Update.php deleted file mode 100644 index e4e0c58..0000000 --- a/src/Alias/Database/Query/Update.php +++ /dev/null @@ -1,91 +0,0 @@ -setTable($table); - } - public function set(array $value_pairs): UpdateInterface - { - return $this->setValues($value_pairs); - } - public function where(array $conditions): UpdateInterface - { - return $this->setConditions($conditions); - } - - protected string $table; - public function setTable(string $table): UpdateInterface - { - $this->table = $table; - return $this; - } - public function getTable(): string - { - return $this->table; - } - protected array $values; - public function setValues(array $values): UpdateInterface - { - foreach ($values as $value) { - $column = $value['column'] ?? $value[0]; - $val = $value['value'] ?? $value[1]; - $this->addValue($column, $val); - } - return $this; - } - public function addValue(string $column, int|string $value): UpdateInterface - { - if (!is_numeric($value)) { - $value = "'{$value}'"; - } - $this->values []= "`{$column}` = {$value}"; - return $this; - } - public function getValues(): array - { - return $this->values; - } - public function getValueString(): string - { - return implode(', ', $this->getValues()); - } - protected array $conditions; - public function setConditions(array $conditions): UpdateInterface - { - foreach ($conditions as $condition) { - $this->addCondition($condition); - } - return $this; - } - public function addCondition(string $expression): UpdateInterface - { - $this->conditions []= $expression; - return $this; - } - public function getConditions(): array - { - return $this->conditions; - } - public function getConditionString(): string - { - return implode(' ', $this->getConditions()); - } - - public function build(): string - { - return implode(' ', [ - 'UPDATE', - $this->getTable(), - 'SET', - $this->getValueString(), - 'WHERE', - $this->getConditionString() - ]); - } -} diff --git a/src/Concept/Database/Query/Builder.php b/src/Concept/Database/Query/Builder.php new file mode 100644 index 0000000..3412c9b --- /dev/null +++ b/src/Concept/Database/Query/Builder.php @@ -0,0 +1,14 @@ +setQueries($query_classes); + } + + protected array $query_classes; + + protected function getQueries(): array + { + return $this->query_classes; + } + + protected function get(string $query_interface): string + { + return $this->query_classes[$query_interface]; + } + + protected function addQuery(string $query_interface, string $query): Builder + { + $this->query_classes[$query_interface] = $query; + return $this; + } + + protected function setQueries(array $queries): Builder + { + foreach ($queries as $interface => $query) { + $this->addQuery($interface, $query); + } + return $this; + } + + protected function build(string $query_interface, ?array $args = null): Query + { + $class = $this->get($query_interface); + if ($args !== null) { + return new $class(...$args); + } + return new $class; + } + + public function select(array|string $columns = '*'): Query\Select + { + return $this->build(Query\Select::class, compact('columns')); + } + public function insert(?string $table = null): Query\Insert + { + return $this->build(Query\Insert::class, compact('table')); + } + public function update(?string $table = null): Query\Update + { + return $this->build(Query\Update::class, compact('table')); + } + public function delete(?string $table = null): Query\Delete + { + return $this->build(Query\Delete::class, compact('table')); + } + + public function create(?string $table = null): Query\Create + { + return $this->build(Query\Create::class, compact('table')); + } + public function truncate(?string $table = null): Query\Truncate + { + return $this->build(Query\Truncate::class, compact('table')); + } + public function drop(?string $table = null): Query\Drop + { + return $this->build(Query\Drop::class, compact('table')); + } +} diff --git a/src/Database/Query/MySQL/Create.php b/src/Database/Query/MySQL/Create.php new file mode 100644 index 0000000..9bd805e --- /dev/null +++ b/src/Database/Query/MySQL/Create.php @@ -0,0 +1,8 @@ +limit = $limit; - return $this; - } + protected int $offset; + public function getLimit(): int { return $this->limit; } - protected int $offset; - public function setOffset(int $offset): Select - { - $this->offset = $offset; - return $this; - } public function getOffset(): int { return $this->offset; } + public function setLimit(int $limit): Select + { + $this->limit = $limit; + return $this; + } + public function setOffset(int $offset): Select + { + $this->offset = $offset; + return $this; + } + + protected function getLimitString(): string + { + return (isset($this->limit)) ? " LIMIT {$this->getLimit()}{$this->getOffsetString()}" : ''; + } + protected function getOffsetString(): string + { + return (isset($this->offset)) ? " OFFSET {$this->getOffset()}" : ''; + } + public function build(): string { - $query = [parent::build()]; - if (isset($this->limit)) { - $query []= 'LIMIT'; - $query []= $this->getLimit(); - if (isset($this->offset)) { - $query []= 'OFFSET'; - $query []= $this->getOffset(); - } - } - return implode(' ', $query); + $query = [ + parent::build(), + $this->getLimitString() + ]; + return implode('', $query); } } diff --git a/src/Database/Query/MySQL/Truncate.php b/src/Database/Query/MySQL/Truncate.php new file mode 100644 index 0000000..455bf19 --- /dev/null +++ b/src/Database/Query/MySQL/Truncate.php @@ -0,0 +1,8 @@ +container = $container; - return $this; - } - public function getContainer(): ContainerInterface - { - return $this->container; - } - - public function select(array $columns = ['*']): Select - { - return $this->getContainer()->make(Select::class)->select($columns); - } - public function insert(string $table): Insert - { - return $this->getContainer()->make(Insert::class)->into($table); - } - public function update(string $table): Update - { - return $this->getContainer()->make(Update::class)->table($table); - } - public function delete(string $table): Delete - { - return $this->getContainer()->make(Delete::class)->from($table); - } -} diff --git a/src/Implement/Database/Query.php b/src/Implement/Database/Query.php new file mode 100644 index 0000000..81edcdc --- /dev/null +++ b/src/Implement/Database/Query.php @@ -0,0 +1,12 @@ +build(); + } +} diff --git a/src/Implement/Database/Query/Create.php b/src/Implement/Database/Query/Create.php new file mode 100644 index 0000000..6ea7aeb --- /dev/null +++ b/src/Implement/Database/Query/Create.php @@ -0,0 +1,62 @@ +table($table); + } + + public function table(string $table): Database\Query\Create + { + return $this->setTable($table); + } + public function columns(array|string $columns): Database\Query\Create + { + return $this->setColumns($columns); + } + + protected array|string $columns; + + public function getColumns(): array + { + return $this->columns; + } + + public function addColumn(string $column): Database\Query\Create + { + $this->columns []= $column; + return $this; + } + + public function setColumns(array|string $columns): Database\Query\Create + { + if (is_string($columns)) { + $this->columns = $columns; + return $this; + } + foreach ($columns as $column) { + $this->addColumn($column); + } + return $this; + } + + protected function getColumnsString(): string + { + if (is_string($this->columns)) { + return $this->columns; + } + return implode(', ', $this->getColumns()); + } + + public function build(): string + { + return "CREATE {$this->getTable()} ({$this->getColumnsString()})"; + } +} diff --git a/src/Implement/Database/Query/Delete.php b/src/Implement/Database/Query/Delete.php new file mode 100644 index 0000000..26b99b9 --- /dev/null +++ b/src/Implement/Database/Query/Delete.php @@ -0,0 +1,27 @@ +setTable($table); + } + public function where(array|string $conditions): Database\Query\Delete + { + return $this->setConditions($conditions); + } + + public function build(): string + { + return implode('', [ + "DELETE FROM {$this->getTable()}", + $this->getConditionsString() + ]); + } +} diff --git a/src/Implement/Database/Query/Drop.php b/src/Implement/Database/Query/Drop.php new file mode 100644 index 0000000..3217561 --- /dev/null +++ b/src/Implement/Database/Query/Drop.php @@ -0,0 +1,27 @@ +table($table); + } + } + + public function table(string $table): Database\Query\Drop + { + return $this->setTable($table); + } + + public function build(): string + { + return "DROP TABLE {$this->getTable()}"; + } +} diff --git a/src/Implement/Database/Query/Insert.php b/src/Implement/Database/Query/Insert.php new file mode 100644 index 0000000..96dbde7 --- /dev/null +++ b/src/Implement/Database/Query/Insert.php @@ -0,0 +1,94 @@ +setTable($table); + } + public function columns(array|string $columns): Database\Query\Insert + { + return $this->setColumns($columns); + } + public function values(array|string $values): Database\Query\Insert + { + return $this->setValues($values); + } + public function select(Database\Query\Select|string $select): Database\Query\Insert + { + return $this->setSelect($select); + } + + protected array|string $columns; + protected array|string $values; + protected Database\Query\Select|string $select; + + public function getColumns(): array|string + { + return $this->columns; + } + public function getValues(): array|string + { + return $this->values; + } + public function getSelect(): Database\Query\Select|string + { + return $this->select; + } + + public function setColumns(array|string $columns): Database\Query\Insert + { + $this->columns = $columns; + return $this; + } + public function setValues(array|string $values): Database\Query\Insert + { + $this->values = $values; + return $this; + } + public function setSelect(Database\Query\Select|string $select): Database\Query\Insert + { + $this->select = $select; + return $this; + } + + protected function getColumnString(): string + { + if (!isset($this->columns)) { + return ''; + } + $columns = (is_array($this->getColumns())) ? implode(', ', $this->getColumns()) : $this->getColumns(); + if (trim($columns) === '') { + return ''; + } + return " ({$columns})"; + } + protected function getValueString(): string + { + if (!isset($this->values)) { + return ''; + } + $values = (is_array($this->getValues())) ? implode(', ', $this->getValues()) : $this->getValues(); + return " VALUES ({$values})"; + } + protected function getSelectString(): string + { + return (isset($this->select)) ? " {$this->getSelect()}" : ''; + } + + public function build(): string + { + return implode('', [ + "INSERT INTO {$this->getTable()}", + $this->getColumnString(), + $this->getValueString(), + $this->getSelectString() + ]); + } +} diff --git a/src/Implement/Database/Query/Select.php b/src/Implement/Database/Query/Select.php new file mode 100644 index 0000000..ea6c2b2 --- /dev/null +++ b/src/Implement/Database/Query/Select.php @@ -0,0 +1,260 @@ +columns($columns); + } + + public function columns(array|string $columns = '*'): Database\Query\Select + { + return $this->setColumns($columns); + } + public function from(string $table): Database\Query\Select + { + return $this->setTable($table); + } + public function joined(array|string $joins): Database\Query\Select + { + return $this->setJoins($joins); + } + public function where(array|string $conditions): Database\Query\Select + { + return $this->setConditions($conditions); + } + public function groupBy(array|string $grouping): Database\Query\Select + { + return $this->setGroups($grouping); + } + public function having(array|string $having): Database\Query\Select + { + return $this->setHaving($having); + } + public function orderBy(array|string $ordering): Database\Query\Select + { + return $this->setOrders($ordering); + } + + protected array|string $columns; + protected array|string $joins; + protected array|string $groups; + protected array|string $having; + protected array|string $orders; + + public function getColumns(): array|string + { + return $this->columns ?? '*'; + } + public function getJoins(): array|string + { + return $this->joins; + } + public function getGroups(): array|string + { + return $this->groups; + } + public function getHaving(): array|string + { + return $this->having; + } + public function getOrders(): array|string + { + return $this->orders; + } + + public function addColumn(string $column, ?string $alias = null): Database\Query\Select + { + if ($column === '*') { + $this->columns []= $column; + return $this; + } + $a = ''; + if ($alias !== null) { + $a = " AS '{$alias}'"; + } + $this->columns[] = "`{$column}`{$a}"; + return $this; + } + public function addJoin(string $table, string $expression): Database\Query\Select + { + $this->joins []= "{$table} ON {$expression}"; + return $this; + } + public function addGroup(string $group): Database\Query\Select + { + $this->groups []= "`{$group}`"; + return $this; + } + public function addHaving(string $having): Database\Query\Select + { + $this->having []= $having; + return $this; + } + public function addOrder(string $column, ?string $direction = null): Database\Query\Select + { + if ($direction === null) { + $direction = 'ASC'; + } + $this->orders []= "{$column} {$direction}"; + return $this; + } + + public function setColumns(array|string $columns): Database\Query\Select + { + if (is_string($columns)) { + $this->columns = $columns; + return $this; + } + foreach ($columns as $column) { + $col = $column; + $alias = null; + if (is_array($column)) { + $col = $column['column'] ?? $column[0]; + $alias = $column['alias'] ?? $column[1]; + } + $this->addColumn($column, $alias); + } + return $this; + } + public function setJoins(array|string $joins): Database\Query\Select + { + if (is_string($joins)) { + $this->joins = $joins; + return $this; + } + foreach ($joins as $join) { + $table = $join['table'] ?? $join[0]; + $expression = $join['expression'] ?? $join[1]; + $this->addJoin($table, $expression); + } + return $this; + } + public function setGroups(array|string $groups): Database\Query\Select + { + if (is_string($groups)) { + $this->groups = $groups; + return $this; + } + foreach ($groups as $group) { + $this->addGroup($group); + } + return $this; + } + public function setHaving(array|string $having): Database\Query\Select + { + if (is_string($having)) { + $this->having = $having; + return $this; + } + foreach ($having as $item) { + $this->addHaving($item); + } + return $this; + } + public function setOrders(array|string $orders): Database\Query\Select + { + if (is_string($orders)) { + $this->orders = $orders; + return $this; + } + foreach ($orders as $order) { + $column = $order; + $direction = null; + if (is_array($order)) { + $direction = $order['direction'] ?? $order[1]; + $column = $order['column'] ?? $order[0]; + } + $this->addOrder($column, $direction); + } + return $this; + } + + protected function getColumnsString(): string + { + $columns = (is_array($this->getColumns())) ? implode(', ', $this->getColumns()) : $this->getColumns(); + return " ({$columns})"; + } + protected function getJoinsString(): string + { + if (!isset($this->joins)) { + return ''; + } + if (is_string($this->getJoins())) { + $str = $this->getJoins(); + return " {$str}"; + } + $str = []; + foreach ($this->getJoins() as $i => $join) { + if (!str_contains(strtolower($join), 'join')) { + $str []= "JOIN {$join}"; + continue; + } + $str []= $join; + } + $str = implode(' ', $str); + return " {$str}"; + } + protected function getGroupsString(): string + { + if (!isset($this->groups)) { + return ''; + } + $groups = (is_array($this->getGroups())) ? implode(', ', $this->getGroups()) : $this->getGroups(); + return " GROUP BY {$groups}"; + } + protected function getHavingString(): string + { + if (!isset($this->having)) { + return ''; + } + $having = $this->getHaving(); + if (is_array($having)) { + $first = array_shift($having); + $matched = preg_match_all('/^and|or\s/i', trim($first)); + if ($matched > 0) { + $temp = str_replace(['and ', 'or '], ['', ''], strtolower($first)); + $dif = strlen($first) - strlen($temp); + $first = substr($first, $dif); + } + $having = "$first " . implode(' ', array_map(function(string $h) { + $matched = preg_match_all('/^and|or\s/i', trim($h)); + if ($matched > 0) { + return $h; + } + return "AND {$h}"; + }, $having)); + } + if (trim($having) === '') { + return ''; + } + return " HAVING {$having}"; + } + protected function getOrderString(): string + { + if (!isset($this->orders)) { + return ''; + } + $ordering = (is_array($this->getOrders())) ? implode(', ', $this->getOrders()) : $this->getOrders(); + return " ORDER BY {$ordering}"; + } + + public function build(): string + { + return implode('', [ + "SELECT {$this->getColumnsString()}", + "FROM {$this->getTable()}", + $this->getJoinsString(), + $this->getConditionsString(), + $this->getGroupsString(), + $this->getHavingString(), + $this->getOrderString() + ]); + } +} diff --git a/src/Implement/Database/Query/Truncate.php b/src/Implement/Database/Query/Truncate.php new file mode 100644 index 0000000..0e35017 --- /dev/null +++ b/src/Implement/Database/Query/Truncate.php @@ -0,0 +1,24 @@ +table($table); + } + public function table(string $table): Database\Query\Truncate + { + return $this->setTable($table); + } + + public function build(): string + { + return "TRUNCATE TABLE {$this->getTable()}"; + } +} diff --git a/src/Implement/Database/Query/Update.php b/src/Implement/Database/Query/Update.php new file mode 100644 index 0000000..a515b9e --- /dev/null +++ b/src/Implement/Database/Query/Update.php @@ -0,0 +1,71 @@ +setTable($table); + } + public function set(array|string $value_pairs): Database\Query\Update + { + return $this->setValues($value_pairs); + } + public function where(array|string $conditions): Database\Query\Update + { + return $this->setConditions($conditions); + } + + protected array|string $values; + + public function getValues(): array + { + return $this->values; + } + + public function addValue(string $column, int|string $value): Database\Query\Update + { + if (!is_numeric($value)) { + $value = "'{$value}'"; + } + $this->values []= "`{$column}` = {$value}"; + return $this; + } + + public function setValues(array|string $values): Database\Query\Update + { + if (is_string($values)) { + $this->values = $values; + return $this; + } + foreach ($values as $value) { + $column = $value['column'] ?? $value[0]; + $val = $value['value'] ?? $value[1]; + $this->addValue($column, $val); + } + return $this; + } + + protected function getValuesString(): string + { + if (!isset($this->values)) { + return ''; + } + $values = (is_array($this->getValues())) ? implode(', ', $this->getValues()) : $this->getValues(); + return " SET {$values}"; + } + + public function build(): string + { + return implode('', [ + "UPDATE {$this->getTable()}", + $this->getValuesString(), + $this->getConditionsString() + ]); + } +} diff --git a/src/Implement/Database/Query/hasConditions.php b/src/Implement/Database/Query/hasConditions.php new file mode 100644 index 0000000..b0e65f7 --- /dev/null +++ b/src/Implement/Database/Query/hasConditions.php @@ -0,0 +1,43 @@ +conditions ?? ''; + } + + protected function setConditions(array|string $conditions) + { + $this->conditions = $conditions; + return $this; + } + + protected function getConditionsString(): string + { + $conditions = $this->getConditions(); + if (is_array($conditions)) { + $first = array_shift($conditions); + $matched = preg_match_all('/^and|or\s/i', trim($first)); + if ($matched > 0) { + $temp = str_replace(['and ', 'or '], ['', ''], strtolower($first)); + $dif = strlen($first) - strlen($temp); + $first = substr($first, $dif); + } + $conditions = "{$first} " . implode(' ', array_map(function(string $condition) { + $matched = preg_match_all('/^and|or\s/i', trim($condition)); + if ($matched > 0) { + return $condition; + } + return "AND {$condition}"; + }, $conditions)); + } + if (trim($conditions) === '') { + return ''; + } + return " WHERE {$conditions}"; + } +} diff --git a/src/Implement/Database/Query/hasTable.php b/src/Implement/Database/Query/hasTable.php new file mode 100644 index 0000000..f1b478d --- /dev/null +++ b/src/Implement/Database/Query/hasTable.php @@ -0,0 +1,22 @@ +table; + } + + public function setTable(string $table, ?string $alias = null) + { + $table = "`{$table}`"; + if ($alias !== null) { + $table = "{$table} '{$alias}'"; + } + $this->table = $table; + return $this; + } +}