diff --git a/DataStorage/Database/Query/Builder.php b/DataStorage/Database/Query/Builder.php index 89239de23..63d9563c5 100644 --- a/DataStorage/Database/Query/Builder.php +++ b/DataStorage/Database/Query/Builder.php @@ -159,6 +159,8 @@ class Builder extends BuilderAbstract protected $unionOrders = []; + public $raw = ''; + /** * Comparison operators. * @@ -204,6 +206,11 @@ class Builder extends BuilderAbstract * @author Dennis Eichhorn */ public function __construct(ConnectionAbstract $connection) + { + $this->setConnection($connection); + } + + public function setConnection(ConnectionAbstract $connection) { $this->connection = $connection; $this->grammar = $connection->getGrammar(); @@ -308,6 +315,14 @@ class Builder extends BuilderAbstract return $this->grammar->compileQuery($this); } + public function raw(string $raw) : Builder + { + $this->type = QueryType::RAW; + $this->raw = $raw; + + return $this; + } + /** * Make raw column selection. * @@ -333,7 +348,7 @@ class Builder extends BuilderAbstract * @since 1.0.0 * @author Dennis Eichhorn */ - public function distinct() : Builder + public function distinct(...$columns) : Builder { $this->distinct = true; @@ -380,23 +395,6 @@ class Builder extends BuilderAbstract return $this; } - /** - * Or Where. - * - * @param string|array|\Closure $columns Columns - * @param string $operator Operator - * @param string|array|\Closure $values Values - * - * @return Builder - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function orWhere($columns, string $operator = null, $values = null) : Builder - { - return $this->where($columns, $operator, $values, 'or'); - } - /** * Where. * @@ -426,9 +424,13 @@ class Builder extends BuilderAbstract throw new \InvalidArgumentException('Unknown operator.'); } - $this->wheres[$key][] = ['column' => $column, 'operator' => $operator[$i], - 'value' => $values[$i], - 'boolean' => $boolean[$i],]; + $this->wheres[$key][] = [ + 'column' => $column, + 'operator' => $operator[$i], + 'value' => $values[$i], + 'boolean' => $boolean[$i], + ]; + $i++; } } elseif (is_string($columns)) { @@ -445,6 +447,26 @@ class Builder extends BuilderAbstract return $this; } + public function andWhere(Where $where) + { + $this->wheres[][] = [ + 'column' => $where, + 'boolean' => 'and', + ]; + + return $this; + } + + public function orWhere(Where $where) + { + $this->wheres[][] = [ + 'column' => $where, + 'boolean' => 'or', + ]; + + return $this; + } + /** * Where in. * diff --git a/DataStorage/Database/Query/Grammar/Grammar.php b/DataStorage/Database/Query/Grammar/Grammar.php index 6ee6b30fa..b198cf0ce 100644 --- a/DataStorage/Database/Query/Grammar/Grammar.php +++ b/DataStorage/Database/Query/Grammar/Grammar.php @@ -21,6 +21,7 @@ use phpOMS\DataStorage\Database\GrammarAbstract; use phpOMS\DataStorage\Database\Query\Builder; use phpOMS\DataStorage\Database\Query\Column; use phpOMS\DataStorage\Database\Query\QueryType; +use phpOMS\DataStorage\Database\Query\Where; /** * Database query grammar. @@ -122,6 +123,9 @@ class Grammar extends GrammarAbstract case QueryType::RANDOM: $components = $this->selectComponents; break; + case QueryType::RAW: + return $query->raw; + break; default: throw new \InvalidArgumentException('Unknown query type.'); } @@ -198,28 +202,7 @@ class Grammar extends GrammarAbstract foreach ($wheres as $key => $where) { foreach ($where as $key2 => $element) { - if (!$first) { - $expression .= ' ' . strtoupper($element['boolean']) . ' '; - } - - if (is_string($element['column'])) { - $expression .= $this->compileSystem($element['column'], $query->getPrefix()) . ' ' . strtoupper($element['operator']) . ' ' . $this->compileValue($element['value'], $query->getPrefix()); - } elseif ($element['column'] instanceof \Closure) { - } elseif ($element['column'] instanceof Builder) { - } - - if (is_string($element['value'])) { - } elseif ($element['value'] instanceof \Closure) { - } elseif ($element['value'] instanceof Builder) { - } elseif ($element['value'] instanceof \DateTime) { - } elseif (is_int($element['value'])) { - } elseif (is_bool($element['value'])) { - } elseif (is_null($element['value'])) { - } elseif (is_float($element['value'])) { - } elseif (is_array($element['value'])) { - // is bind - } - + $expression .= $this->compileWhereElement($element, $query, $first); $first = false; } } @@ -231,6 +214,37 @@ class Grammar extends GrammarAbstract return 'WHERE ' . $expression; } + protected function compileWhereElement(array $element, Builder $query, bool $first = true) : string + { + $expression = ''; + + if(!$first) { + $expression = ' ' . strtoupper($element['boolean']) . ' '; + } + + if (is_string($element['column'])) { + $expression .= $this->compileSystem($element['column'], $query->getPrefix()); + } elseif ($element['column'] instanceof \Closure) { + $expression .= $element['column'](); + } elseif ($element['column'] instanceof Builder) { + $expression .= '(' . $element['column']->toSql() . ')'; + } elseif ($element['column'] instanceof Where) { + $expression .= '(' . $this->compileWhere($element['column'], $query->getPrefix()) . ')'; + } + + if (isset($element['value'])) { + $expression .= ' ' . strtoupper($element['operator']) . ' ' . $this->compileValue($element['value'], $query->getPrefix()); + } + + return $expression; + } + + protected function compileWhere(Where $where, string $prefix = '', bool $first = true) : string + { + + return ''; + } + /** * Compile value. * @@ -247,6 +261,10 @@ class Grammar extends GrammarAbstract protected function compileValue($value, $prefix = '') : string { if (is_string($value)) { + if(strpos($value, ':') === 0) { + return $value; + } + return $this->valueQuotes . $value . $this->valueQuotes; } elseif (is_int($value)) { return $value; diff --git a/DataStorage/Database/Query/QueryType.php b/DataStorage/Database/Query/QueryType.php index db5e364f0..6c6c373d0 100644 --- a/DataStorage/Database/Query/QueryType.php +++ b/DataStorage/Database/Query/QueryType.php @@ -35,4 +35,5 @@ abstract class QueryType extends Enum const UPDATE = 2; const DELETE = 3; const RANDOM = 4; + const RAW = 5; }