Добавлен цикл проверки сайтов

This commit is contained in:
2024-08-12 15:14:49 +03:00
parent 0b56cd37b5
commit 6b7199a326
115 changed files with 15964 additions and 88 deletions

View File

@ -0,0 +1,18 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder;
/**
* Class BuilderException.
*/
final class BuilderException extends \Exception
{
}

View File

@ -0,0 +1,33 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface;
/**
* Interface BuilderInterface.
*/
interface BuilderInterface
{
/**
* @param QueryInterface $query
*
* @return string
*/
public function write(QueryInterface $query);
/**
* @param QueryInterface $query
*
* @return string
*/
public function writeFormatted(QueryInterface $query);
}

View File

@ -0,0 +1,401 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory;
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryFactory;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Syntax\Table;
/**
* Class Generic.
*/
class GenericBuilder implements BuilderInterface
{
/**
* The placeholder parameter bag.
*
* @var \NilPortugues\Sql\QueryBuilder\Builder\Syntax\PlaceholderWriter
*/
protected $placeholderWriter;
/**
* The Where writer.
*
* @var \NilPortugues\Sql\QueryBuilder\Builder\Syntax\WhereWriter
*/
protected $whereWriter;
/**
* The SQL formatter.
*
* @var \NilPortugues\Sql\QueryFormatter\Formatter
*/
protected $sqlFormatter;
/**
* Class namespace for the query pretty output formatter.
* Required to create the instance only if required.
*
* @var string
*/
protected $sqlFormatterClass = 'NilPortugues\Sql\QueryFormatter\Formatter';
/**
* Array holding the writers for each query part. Methods are called upon request and stored in
* the $queryWriterInstances array.
*
* @var array
*/
protected $queryWriterArray = [
'SELECT' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createSelectWriter',
'INSERT' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createInsertWriter',
'UPDATE' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createUpdateWriter',
'DELETE' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createDeleteWriter',
'INTERSECT' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createIntersectWriter',
'MINUS' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createMinusWriter',
'UNION' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createUnionWriter',
'UNION ALL' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createUnionAllWriter',
];
/**
* Array that stores instances of query writers.
*
* @var array
*/
protected $queryWriterInstances = [
'SELECT' => null,
'INSERT' => null,
'UPDATE' => null,
'DELETE' => null,
'INTERSECT' => null,
'MINUS' => null,
'UNION' => null,
'UNION ALL' => null,
];
/**
* Creates writers.
*/
public function __construct()
{
$this->placeholderWriter = WriterFactory::createPlaceholderWriter();
}
/**
* @param string $table
* @param array $columns
*
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\Select
*/
public function select($table = null, array $columns = null)
{
return $this->injectBuilder(QueryFactory::createSelect($table, $columns));
}
/**
* @param \NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery
*
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery
*/
protected function injectBuilder(AbstractBaseQuery $query)
{
return $query->setBuilder($this);
}
/**
* @param string $table
* @param array $values
*
*@return AbstractBaseQuery
*/
public function insert($table = null, array $values = null)
{
return $this->injectBuilder(QueryFactory::createInsert($table, $values));
}
/**
* @param string $table
* @param array $values
*
*@return AbstractBaseQuery
*/
public function update($table = null, array $values = null)
{
return $this->injectBuilder(QueryFactory::createUpdate($table, $values));
}
/**
* @param string $table
*
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\Delete
*/
public function delete($table = null)
{
return $this->injectBuilder(QueryFactory::createDelete($table));
}
/**
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\Intersect
*/
public function intersect()
{
return QueryFactory::createIntersect();
}
/**
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\Union
*/
public function union()
{
return QueryFactory::createUnion();
}
/**
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\UnionAll
*/
public function unionAll()
{
return QueryFactory::createUnionAll();
}
/**
* @param \NilPortugues\Sql\QueryBuilder\Manipulation\Select $first
* @param \NilPortugues\Sql\QueryBuilder\Manipulation\Select $second
*
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\Minus
*/
public function minus(Select $first, Select $second)
{
return QueryFactory::createMinus($first, $second);
}
/**
* @return array
*/
public function getValues()
{
return $this->placeholderWriter->get();
}
/**
* Returns a SQL string in a readable human-friendly format.
*
* @param QueryInterface $query
*
* @return string
*/
public function writeFormatted(QueryInterface $query)
{
if (null === $this->sqlFormatter) {
$this->sqlFormatter = (new \ReflectionClass($this->sqlFormatterClass))->newInstance();
}
return $this->sqlFormatter->format($this->write($query));
}
/**
* @param QueryInterface $query
* @param bool $resetPlaceholders
*
* @return string
*
* @throws \RuntimeException
*/
public function write(QueryInterface $query, $resetPlaceholders = true)
{
if ($resetPlaceholders) {
$this->placeholderWriter->reset();
}
$queryPart = $query->partName();
if (false === empty($this->queryWriterArray[$queryPart])) {
$this->createQueryObject($queryPart);
return $this->queryWriterInstances[$queryPart]->write($query);
}
throw new \RuntimeException('Query builder part not defined.');
}
/**
* @param Select $select
*
* @return string
*/
public function writeJoin(Select $select)
{
if (null === $this->whereWriter) {
$this->whereWriter = WriterFactory::createWhereWriter($this, $this->placeholderWriter);
}
$sql = ($select->getJoinType()) ? "{$select->getJoinType()} " : '';
$sql .= 'JOIN ';
$sql .= $this->writeTableWithAlias($select->getTable());
$sql .= ' ON ';
$sql .= $this->whereWriter->writeWhere($select->getJoinCondition());
return $sql;
}
/**
* @param Table $table
*
* @return string
*/
public function writeTableWithAlias(Table $table)
{
$alias = ($table->getAlias()) ? " AS {$this->writeTableAlias($table->getAlias())}" : '';
$schema = ($table->getSchema()) ? "{$table->getSchema()}." : '';
return $schema.$this->writeTableName($table).$alias;
}
/**
* @param $alias
*
* @return mixed
*/
public function writeTableAlias($alias)
{
return $alias;
}
/**
* Returns the table name.
*
* @param Table $table
*
* @return string
*/
public function writeTableName(Table $table)
{
return $table->getName();
}
/**
* @param string $alias
*
* @return string
*/
public function writeColumnAlias($alias)
{
return sprintf('"%s"', $alias);
}
/**
* @param Table $table
*
* @return string
*/
public function writeTable(Table $table)
{
$schema = ($table->getSchema()) ? "{$table->getSchema()}." : '';
return $schema.$this->writeTableName($table);
}
/**
* @param array $values
*
* @return array
*/
public function writeValues(array &$values)
{
\array_walk(
$values,
function (&$value) {
$value = $this->writePlaceholderValue($value);
}
);
return $values;
}
/**
* @param $value
*
* @return string
*/
public function writePlaceholderValue($value)
{
return $this->placeholderWriter->add($value);
}
/**
* @param $operator
*
* @return string
*/
public function writeConjunction($operator)
{
return ' '.$operator.' ';
}
/**
* @return string
*/
public function writeIsNull()
{
return ' IS NULL';
}
/**
* @return string
*/
public function writeIsNotNull()
{
return ' IS NOT NULL';
}
/**
* Returns the column name.
*
* @param Column $column
*
* @return string
*/
public function writeColumnName(Column $column)
{
$name = $column->getName();
if ($name === Column::ALL) {
return $this->writeColumnAll();
}
return $name;
}
/**
* @return string
*/
protected function writeColumnAll()
{
return '*';
}
/**
* @param string $queryPart
*/
protected function createQueryObject($queryPart)
{
if (null === $this->queryWriterInstances[$queryPart]) {
$this->queryWriterInstances[$queryPart] = \call_user_func_array(
\explode('::', $this->queryWriterArray[$queryPart]),
[$this, $this->placeholderWriter]
);
}
}
}

View File

@ -0,0 +1,91 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Syntax\Table;
/**
* Class MySqlBuilder.
*/
class MySqlBuilder extends GenericBuilder
{
/**
* {@inheritdoc}
*
* @param Column $column
*
* @return string
*/
public function writeColumnName(Column $column)
{
if ($column->isAll()) {
return '*';
}
if (false !== strpos($column->getName(), '(')) {
return parent::writeColumnName($column);
}
return $this->wrapper(parent::writeColumnName($column));
}
/**
* {@inheritdoc}
*
* @param Table $table
*
* @return string
*/
public function writeTableName(Table $table)
{
return $this->wrapper(parent::writeTableName($table));
}
/**
* {@inheritdoc}
*
* @param $alias
*
* @return string
*/
public function writeTableAlias($alias)
{
return $this->wrapper(parent::writeTableAlias($alias));
}
/**
* {@inheritdoc}
*
* @param $alias
*
* @return string
*/
public function writeColumnAlias($alias)
{
return $this->wrapper($alias);
}
/**
* @param $string
* @param string $char
*
* @return string
*/
protected function wrapper($string, $char = '`')
{
if (0 === strlen($string)) {
return '';
}
return $char.$string.$char;
}
}

View File

@ -0,0 +1,96 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 12/24/14
* Time: 1:14 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
/**
* Class AbstractBaseWriter.
*/
abstract class AbstractBaseWriter
{
/**
* @var GenericBuilder
*/
protected $writer;
/**
* @var PlaceholderWriter
*/
protected $placeholderWriter;
/**
* @var ColumnWriter
*/
protected $columnWriter;
/**
* @param GenericBuilder $writer
* @param PlaceholderWriter $placeholder
*/
public function __construct(GenericBuilder $writer, PlaceholderWriter $placeholder)
{
$this->writer = $writer;
$this->placeholderWriter = $placeholder;
$this->columnWriter = WriterFactory::createColumnWriter($writer, $placeholder);
}
/**
* @param AbstractBaseQuery $class
*
* @return string
*/
public static function writeQueryComment(AbstractBaseQuery $class)
{
$comment = '';
if ('' !== $class->getComment()) {
$comment = $class->getComment();
}
return $comment;
}
/**
* @param AbstractBaseQuery $class
* @param GenericBuilder $writer
* @param PlaceholderWriter $placeholderWriter
* @param array $parts
*/
public static function writeWhereCondition(
AbstractBaseQuery $class,
$writer, PlaceholderWriter
$placeholderWriter,
array &$parts
) {
if (!is_null($class->getWhere())) {
$whereWriter = WriterFactory::createWhereWriter($writer, $placeholderWriter);
$parts[] = "WHERE {$whereWriter->writeWhere($class->getWhere())}";
}
}
/**
* @param AbstractBaseQuery $class
* @param PlaceholderWriter $placeholderWriter
* @param array $parts
*/
public static function writeLimitCondition(
AbstractBaseQuery $class,
PlaceholderWriter $placeholderWriter,
array &$parts
) {
if (!is_null($class->getLimitStart())) {
$start = $placeholderWriter->add($class->getLimitStart());
$parts[] = "LIMIT {$start}";
}
}
}

View File

@ -0,0 +1,51 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 12/24/14
* Time: 12:55 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Syntax\QueryPartInterface;
/**
* Class AbstractSetWriter.
*/
abstract class AbstractSetWriter
{
/**
* @var GenericBuilder
*/
protected $writer;
/**
* @param GenericBuilder $writer
*/
public function __construct(GenericBuilder $writer)
{
$this->writer = $writer;
}
/**
* @param QueryPartInterface $setClass
* @param string $setOperation
* @param $glue
*
* @return string
*/
protected function abstractWrite(QueryPartInterface $setClass, $setOperation, $glue)
{
$selects = [];
foreach ($setClass->$setOperation() as $select) {
$selects[] = $this->writer->write($select, false);
}
return \implode("\n".$glue."\n", $selects);
}
}

View File

@ -0,0 +1,162 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/12/14
* Time: 1:28 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
/**
* Class ColumnWriter.
*/
class ColumnWriter
{
/**
* @var \NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder
*/
protected $writer;
/**
* @var PlaceholderWriter
*/
protected $placeholderWriter;
/**
* @param GenericBuilder $writer
* @param PlaceholderWriter $placeholderWriter
*/
public function __construct(GenericBuilder $writer, PlaceholderWriter $placeholderWriter)
{
$this->writer = $writer;
$this->placeholderWriter = $placeholderWriter;
}
/**
* @param Select $select
*
* @return array
*/
public function writeSelectsAsColumns(Select $select)
{
$selectAsColumns = $select->getColumnSelects();
if (!empty($selectAsColumns)) {
$selectWriter = WriterFactory::createSelectWriter($this->writer, $this->placeholderWriter);
$selectAsColumns = $this->selectColumnToQuery($selectAsColumns, $selectWriter);
}
return $selectAsColumns;
}
/**
* @param array $selectAsColumns
* @param SelectWriter $selectWriter
*
* @return mixed
*/
protected function selectColumnToQuery(array &$selectAsColumns, SelectWriter $selectWriter)
{
\array_walk(
$selectAsColumns,
function (&$column) use (&$selectWriter) {
$keys = \array_keys($column);
$key = \array_pop($keys);
$values = \array_values($column);
$value = $values[0];
if (\is_numeric($key)) {
/* @var Column $value */
$key = $this->writer->writeTableName($value->getTable());
}
$column = $selectWriter->selectToColumn($key, $value);
}
);
return $selectAsColumns;
}
/**
* @param Select $select
*
* @return array
*/
public function writeValueAsColumns(Select $select)
{
$valueAsColumns = $select->getColumnValues();
$newColumns = [];
if (!empty($valueAsColumns)) {
foreach ($valueAsColumns as $alias => $value) {
$value = $this->writer->writePlaceholderValue($value);
$newValueColumn = array($alias => $value);
$newColumns[] = SyntaxFactory::createColumn($newValueColumn, null);
}
}
return $newColumns;
}
/**
* @param Select $select
*
* @return array
*/
public function writeFuncAsColumns(Select $select)
{
$funcAsColumns = $select->getColumnFuncs();
$newColumns = [];
if (!empty($funcAsColumns)) {
foreach ($funcAsColumns as $alias => $value) {
$funcName = $value['func'];
$funcArgs = (!empty($value['args'])) ? '('.implode(', ', $value['args']).')' : '';
$newFuncColumn = array($alias => $funcName.$funcArgs);
$newColumns[] = SyntaxFactory::createColumn($newFuncColumn, null);
}
}
return $newColumns;
}
/**
* @param Column $column
*
* @return string
*/
public function writeColumnWithAlias(Column $column)
{
if (($alias = $column->getAlias()) && !$column->isAll()) {
return $this->writeColumn($column).' AS '.$this->writer->writeColumnAlias($alias);
}
return $this->writeColumn($column);
}
/**
* @param Column $column
*
* @return string
*/
public function writeColumn(Column $column)
{
$alias = $column->getTable()->getAlias();
$table = ($alias) ? $this->writer->writeTableAlias($alias) : $this->writer->writeTable($column->getTable());
$columnString = (empty($table)) ? '' : "{$table}.";
$columnString .= $this->writer->writeColumnName($column);
return $columnString;
}
}

View File

@ -0,0 +1,57 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/11/14
* Time: 1:50 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete;
/**
* Class DeleteWriter.
*/
class DeleteWriter
{
/**
* @var GenericBuilder
*/
protected $writer;
/**
* @var PlaceholderWriter
*/
protected $placeholderWriter;
/**
* @param GenericBuilder $writer
* @param PlaceholderWriter $placeholder
*/
public function __construct(GenericBuilder $writer, PlaceholderWriter $placeholder)
{
$this->writer = $writer;
$this->placeholderWriter = $placeholder;
}
/**
* @param Delete $delete
*
* @return string
*/
public function write(Delete $delete)
{
$table = $this->writer->writeTable($delete->getTable());
$parts = array("DELETE FROM {$table}");
AbstractBaseWriter::writeWhereCondition($delete, $this->writer, $this->placeholderWriter, $parts);
AbstractBaseWriter::writeLimitCondition($delete, $this->placeholderWriter, $parts);
$comment = AbstractBaseWriter::writeQueryComment($delete);
return $comment.implode(' ', $parts);
}
}

View File

@ -0,0 +1,103 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/11/14
* Time: 1:51 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Insert;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryException;
/**
* Class InsertWriter.
*/
class InsertWriter
{
/**
* @var GenericBuilder
*/
protected $writer;
/**
* @var ColumnWriter
*/
protected $columnWriter;
/**
* @param GenericBuilder $writer
* @param PlaceholderWriter $placeholder
*/
public function __construct(GenericBuilder $writer, PlaceholderWriter $placeholder)
{
$this->writer = $writer;
$this->columnWriter = WriterFactory::createColumnWriter($this->writer, $placeholder);
}
/**
* @param Insert $insert
*
* @throws QueryException
*
* @return string
*/
public function write(Insert $insert)
{
$columns = $insert->getColumns();
if (empty($columns)) {
throw new QueryException('No columns were defined for the current schema.');
}
$columns = $this->writeQueryColumns($columns);
$values = $this->writeQueryValues($insert->getValues());
$table = $this->writer->writeTable($insert->getTable());
$comment = AbstractBaseWriter::writeQueryComment($insert);
return $comment."INSERT INTO {$table} ($columns) VALUES ($values)";
}
/**
* @param $columns
*
* @return string
*/
protected function writeQueryColumns($columns)
{
return $this->writeCommaSeparatedValues($columns, $this->columnWriter, 'writeColumn');
}
/**
* @param $collection
* @param $writer
* @param string $method
*
* @return string
*/
protected function writeCommaSeparatedValues($collection, $writer, $method)
{
\array_walk(
$collection,
function (&$data) use ($writer, $method) {
$data = $writer->$method($data);
}
);
return \implode(', ', $collection);
}
/**
* @param $values
*
* @return string
*/
protected function writeQueryValues($values)
{
return $this->writeCommaSeparatedValues($values, $this->writer, 'writePlaceholderValue');
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:15 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Manipulation\Intersect;
/**
* Class IntersectWriter.
*/
class IntersectWriter extends AbstractSetWriter
{
/**
* @param Intersect $intersect
*
* @return string
*/
public function write(Intersect $intersect)
{
return $this->abstractWrite($intersect, 'getIntersects', Intersect::INTERSECT);
}
}

View File

@ -0,0 +1,46 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:15 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Minus;
/**
* Class MinusWriter.
*/
class MinusWriter
{
/**
* @var GenericBuilder
*/
protected $writer;
/**
* @param GenericBuilder $writer
*/
public function __construct(GenericBuilder $writer)
{
$this->writer = $writer;
}
/**
* @param Minus $minus
*
* @return string
*/
public function write(Minus $minus)
{
$first = $this->writer->write($minus->getFirst());
$second = $this->writer->write($minus->getSecond());
return $first."\n".Minus::MINUS."\n".$second;
}
}

View File

@ -0,0 +1,147 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/4/14
* Time: 12:02 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
/**
* Class PlaceholderWriter.
*/
class PlaceholderWriter
{
/**
* @var int
*/
protected $counter = 1;
/**
* @var array
*/
protected $placeholders = [];
/**
* @return array
*/
public function get()
{
return $this->placeholders;
}
/**
* @return $this
*/
public function reset()
{
$this->counter = 1;
$this->placeholders = [];
return $this;
}
/**
* @param $value
*
* @return string
*/
public function add($value)
{
$placeholderKey = ':v'.$this->counter;
$this->placeholders[$placeholderKey] = $this->setValidSqlValue($value);
++$this->counter;
return $placeholderKey;
}
/**
* @param $value
*
* @return string
*/
protected function setValidSqlValue($value)
{
$value = $this->writeNullSqlString($value);
$value = $this->writeStringAsSqlString($value);
$value = $this->writeBooleanSqlString($value);
return $value;
}
/**
* @param $value
*
* @return string
*/
protected function writeNullSqlString($value)
{
if (\is_null($value) || (\is_string($value) && empty($value))) {
$value = $this->writeNull();
}
return $value;
}
/**
* @return string
*/
protected function writeNull()
{
return 'NULL';
}
/**
* @param string $value
*
* @return string
*/
protected function writeStringAsSqlString($value)
{
if (\is_string($value)) {
$value = $this->writeString($value);
}
return $value;
}
/**
* @param string $value
*
* @return string
*/
protected function writeString($value)
{
return $value;
}
/**
* @param string $value
*
* @return string
*/
protected function writeBooleanSqlString($value)
{
if (\is_bool($value)) {
$value = $this->writeBoolean($value);
}
return $value;
}
/**
* @param bool $value
*
* @return string
*/
protected function writeBoolean($value)
{
$value = \filter_var($value, FILTER_VALIDATE_BOOLEAN);
return ($value) ? '1' : '0';
}
}

View File

@ -0,0 +1,397 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/11/14
* Time: 1:50 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
/**
* Class SelectWriter.
*/
class SelectWriter extends AbstractBaseWriter
{
/**
* @param $alias
* @param Select $select
*
* @return Column
*/
public function selectToColumn($alias, Select $select)
{
$selectAsColumn = $this->write($select);
if (!empty($selectAsColumn)) {
$selectAsColumn = '('.$selectAsColumn.')';
}
$column = array($alias => $selectAsColumn);
return SyntaxFactory::createColumn($column, null);
}
/**
* @param Select $select
*
* @return string
*/
public function write(Select $select)
{
if ($select->isJoinSelect()) {
return $this->writer->writeJoin($select);
}
return $this->writeSelectQuery($select);
}
/**
* @param Select $select
*
* @return string
*/
protected function writeSelectQuery(Select $select)
{
$parts = ['SELECT'];
if ($select->isDistinct()) {
$parts[] = 'DISTINCT';
}
$this->writeSelectColumns($select, $parts);
$this->writeSelectFrom($select, $parts);
$this->writeSelectJoins($select, $parts);
$this->writeSelectWhere($select, $parts);
$this->writeSelectGroupBy($select, $parts);
$this->writeSelectHaving($select, $parts);
$this->writeSelectOrderBy($select, $parts);
$this->writeSelectLimit($select, $parts);
return AbstractBaseWriter::writeQueryComment($select).implode(' ', \array_filter($parts));
}
/**
* @param Select $select
* @param string[] $parts
*
* @return $this
*/
public function writeSelectColumns(Select $select, array &$parts)
{
if ($select->isCount() === false) {
$columns = $this->writeColumnAlias(
$select->getAllColumns(),
$this->columnWriter->writeSelectsAsColumns($select),
$this->columnWriter->writeValueAsColumns($select),
$this->columnWriter->writeFuncAsColumns($select)
);
$parts = \array_merge($parts, [implode(', ', $columns)]);
return $this;
}
$columns = $select->getColumns();
$column = \array_pop($columns);
$columnList = $column->getName();
$parts = \array_merge($parts, [$columnList]);
return $this;
}
/**
* @param $tableColumns
* @param $selectAsColumns
* @param $valueAsColumns
* @param $funcAsColumns
*
* @return array
*/
protected function writeColumnAlias($tableColumns, $selectAsColumns, $valueAsColumns, $funcAsColumns)
{
$columns = \array_merge($tableColumns, $selectAsColumns, $valueAsColumns, $funcAsColumns);
\array_walk(
$columns,
function (&$column) {
$column = $this->columnWriter->writeColumnWithAlias($column);
}
);
return $columns;
}
/**
* @param Select $select
* @param string[] $parts
*
* @return $this
*/
public function writeSelectFrom(Select $select, array &$parts)
{
$parts = \array_merge(
$parts,
['FROM '.$this->writer->writeTableWithAlias($select->getTable())]
);
return $this;
}
/**
* @param Select $select
* @param array $parts
*
* @return $this
*/
public function writeSelectJoins(Select $select, array &$parts)
{
$parts = \array_merge(
$parts,
[$this->writeSelectAggrupation($select, $this->writer, 'getAllJoins', 'writeJoin', ' ')]
);
return $this;
}
/**
* @param Select $select
* @param $writer
* @param string $getMethod
* @param string $writeMethod
* @param string $glue
* @param string $prepend
*
* @return string
*/
protected function writeSelectAggrupation(Select $select, $writer, $getMethod, $writeMethod, $glue, $prepend = '')
{
$str = '';
$joins = $select->$getMethod();
if (!empty($joins)) {
\array_walk(
$joins,
function (&$join) use ($writer, $writeMethod) {
$join = $writer->$writeMethod($join);
}
);
$str = $prepend.implode($glue, $joins);
}
return $str;
}
/**
* @param Select $select
* @param array $parts
*
* @return $this
*/
public function writeSelectWhere(Select $select, array &$parts)
{
$str = '';
$wheres = $this->writeSelectWheres($select->getAllWheres());
$wheres = \array_filter($wheres);
if (\count($wheres) > 0) {
$str = 'WHERE ';
$separator = ' '.$this->writer->writeConjunction($select->getWhereOperator()).' ';
$str .= \implode($separator, $wheres);
}
$parts = \array_merge($parts, [$str]);
return $this;
}
/**
* @param array $wheres
*
* @return array
*/
protected function writeSelectWheres(array $wheres)
{
$whereWriter = WriterFactory::createWhereWriter($this->writer, $this->placeholderWriter);
\array_walk(
$wheres,
function (&$where) use (&$whereWriter) {
$where = $whereWriter->writeWhere($where);
}
);
return $wheres;
}
/**
* @param Select $select
* @param array $parts
*
* @return $this
*/
public function writeSelectGroupBy(Select $select, array &$parts)
{
$groupBy = $this->writeSelectAggrupation(
$select,
$this->columnWriter,
'getGroupBy',
'writeColumn',
', ',
'GROUP BY '
);
$parts = \array_merge($parts, [$groupBy]);
return $this;
}
/**
* @param Select $select
* @param array $parts
*
* @return $this
*/
public function writeSelectHaving(Select $select, array &$parts)
{
$str = '';
$havingArray = $select->getAllHavings();
if (\count($havingArray) > 0) {
$placeholder = $this->placeholderWriter;
$writer = $this->writer;
$str = 'HAVING ';
$separator = ' '.$select->getHavingOperator().' ';
$havingArray = $this->getHavingConditions($havingArray, $select, $writer, $placeholder);
$str .= \implode($separator, $havingArray);
}
$parts = \array_merge($parts, [$str]);
return $this;
}
/**
* @param array $havingArray
* @param Select $select
* @param GenericBuilder $writer
* @param PlaceholderWriter $placeholder
*
* @return mixed
*/
protected function getHavingConditions(
array &$havingArray,
Select $select,
GenericBuilder $writer,
PlaceholderWriter $placeholder
) {
\array_walk(
$havingArray,
function (&$having) use ($select, $writer, $placeholder) {
$whereWriter = WriterFactory::createWhereWriter($writer, $placeholder);
$clauses = $whereWriter->writeWhereClauses($having);
$having = \implode($this->writer->writeConjunction($select->getHavingOperator()), $clauses);
}
);
return $havingArray;
}
/**
* @param Select $select
* @param array $parts
*
* @return $this
*/
protected function writeSelectOrderBy(Select $select, array &$parts)
{
$str = '';
if (\count($select->getAllOrderBy())) {
$orderByArray = $select->getAllOrderBy();
\array_walk(
$orderByArray,
function (&$orderBy) {
$orderBy = $this->writeOrderBy($orderBy);
}
);
$str = 'ORDER BY ';
$str .= \implode(', ', $orderByArray);
}
$parts = \array_merge($parts, [$str]);
return $this;
}
/**
* @param OrderBy $orderBy
*
* @return string
*/
public function writeOrderBy(OrderBy $orderBy)
{
$column = $this->columnWriter->writeColumn($orderBy->getColumn());
return $column.' '.$orderBy->getDirection();
}
/**
* @param Select $select
* @param array $parts
*
* @return $this
*/
protected function writeSelectLimit(Select $select, array &$parts)
{
$mask = $this->getStartingLimit($select).$this->getLimitCount($select);
$limit = '';
if ($mask !== '00') {
$start = $this->placeholderWriter->add($select->getLimitStart());
$count = $this->placeholderWriter->add($select->getLimitCount());
$limit = "LIMIT {$start}, {$count}";
}
$parts = \array_merge($parts, [$limit]);
return $this;
}
/**
* @param Select $select
*
* @return string
*/
protected function getStartingLimit(Select $select)
{
return (null === $select->getLimitStart() || 0 == $select->getLimitStart()) ? '0' : '1';
}
/**
* @param Select $select
*
* @return string
*/
protected function getLimitCount(Select $select)
{
return (null === $select->getLimitCount()) ? '0' : '1';
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:15 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Manipulation\UnionAll;
/**
* Class UnionAllWriter.
*/
class UnionAllWriter extends AbstractSetWriter
{
/**
* @param UnionAll $unionAll
*
* @return string
*/
public function write(UnionAll $unionAll)
{
return $this->abstractWrite($unionAll, 'getUnions', UnionAll::UNION_ALL);
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:15 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Manipulation\Union;
/**
* Class UnionWriter.
*/
class UnionWriter extends AbstractSetWriter
{
/**
* @param Union $union
*
* @return string
*/
public function write(Union $union)
{
return $this->abstractWrite($union, 'getUnions', Union::UNION);
}
}

View File

@ -0,0 +1,67 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/11/14
* Time: 1:51 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryException;
use NilPortugues\Sql\QueryBuilder\Manipulation\Update;
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
/**
* Class UpdateWriter.
*/
class UpdateWriter extends AbstractBaseWriter
{
/**
* @param Update $update
*
* @throws QueryException
*
* @return string
*/
public function write(Update $update)
{
$values = $update->getValues();
if (empty($values)) {
throw new QueryException('No values to update in Update query.');
}
$parts = array(
'UPDATE '.$this->writer->writeTable($update->getTable()).' SET ',
$this->writeUpdateValues($update),
);
AbstractBaseWriter::writeWhereCondition($update, $this->writer, $this->placeholderWriter, $parts);
AbstractBaseWriter::writeLimitCondition($update, $this->placeholderWriter, $parts);
$comment = AbstractBaseWriter::writeQueryComment($update);
return $comment.implode(' ', $parts);
}
/**
* @param Update $update
*
* @return string
*/
protected function writeUpdateValues(Update $update)
{
$assigns = [];
foreach ($update->getValues() as $column => $value) {
$newColumn = array($column);
$column = $this->columnWriter->writeColumn(SyntaxFactory::createColumn($newColumn, $update->getTable()));
$value = $this->writer->writePlaceholderValue($value);
$assigns[] = "$column = $value";
}
return \implode(', ', $assigns);
}
}

View File

@ -0,0 +1,400 @@
<?php
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
use NilPortugues\Sql\QueryBuilder\Syntax\Where;
/**
* Class WhereWriter.
*/
class WhereWriter extends AbstractBaseWriter
{
/**
* @var array
*/
protected $matchMode = [
'natural' => '(MATCH({{columnNames}}) AGAINST({{columnValues}}))',
'boolean' => '(MATCH({{columnNames}}) AGAINST({{columnValues}} IN BOOLEAN MODE))',
'query_expansion' => '(MATCH({{columnNames}}) AGAINST({{columnValues}} WITH QUERY EXPANSION))',
];
/**
* @param Where $where
*
* @return string
*/
public function writeWhere(Where $where)
{
$clauses = $this->writeWhereClauses($where);
$clauses = \array_filter($clauses);
if (empty($clauses)) {
return '';
}
return \implode($this->writer->writeConjunction($where->getConjunction()), $clauses);
}
/**
* @param Where $where
*
* @return array
*/
public function writeWhereClauses(Where $where)
{
$whereArray = [];
$this->writeWhereMatches($where, $whereArray);
$this->writeWhereIns($where, $whereArray);
$this->writeWhereNotIns($where, $whereArray);
$this->writeWhereBetweens($where, $whereArray);
$this->writeWhereNotBetweens($where, $whereArray);
$this->writeWhereComparisons($where, $whereArray);
$this->writeWhereIsNulls($where, $whereArray);
$this->writeWhereIsNotNulls($where, $whereArray);
$this->writeWhereBooleans($where, $whereArray);
$this->writeExists($where, $whereArray);
$this->writeNotExists($where, $whereArray);
$this->writeSubWheres($where, $whereArray);
return $whereArray;
}
/**
* @param Where $where
* @param array $whereArray
*
* @return array
*/
protected function writeWhereMatches(Where $where, array &$whereArray)
{
$matches = [];
foreach ($where->getMatches() as $values) {
$columns = SyntaxFactory::createColumns($values['columns'], $where->getTable());
$columnNames = $this->getColumnNames($columns);
$columnValues = array(\implode(' ', $values['values']));
$columnValues = \implode(', ', $this->writer->writeValues($columnValues));
$matches[] = \str_replace(
['{{columnNames}}', '{{columnValues}}'],
[$columnNames, $columnValues],
$this->matchMode[$values['mode']]
);
}
$whereArray = \array_merge($whereArray, $matches);
}
/**
* @param $columns
*
* @return string
*/
protected function getColumnNames($columns)
{
$columnNames = [];
foreach ($columns as &$column) {
$columnNames[] = $this->columnWriter->writeColumn($column);
}
return \implode(', ', $columnNames);
}
/**
* @param Where $where
* @param array $whereArray
*
* @return array
*/
protected function writeWhereIns(Where $where, array &$whereArray)
{
$whereArray = \array_merge(
$whereArray,
$this->writeWhereIn($where, 'getIns', 'IN')
);
}
/**
* @param Where $where
* @param string $method
* @param string $operation
*
* @return array
*/
protected function writeWhereIn(Where $where, $method, $operation)
{
$collection = [];
foreach ($where->$method() as $column => $values) {
$newColumn = array($column);
$column = SyntaxFactory::createColumn($newColumn, $where->getTable());
$column = $this->columnWriter->writeColumn($column);
$values = $this->writer->writeValues($values);
$values = \implode(', ', $values);
$collection[] = "({$column} $operation ({$values}))";
}
return $collection;
}
/**
* @param Where $where
* @param array $whereArray
*
* @return array
*/
protected function writeWhereNotIns(Where $where, array &$whereArray)
{
$whereArray = \array_merge(
$whereArray,
$this->writeWhereIn($where, 'getNotIns', 'NOT IN')
);
}
/**
* @param Where $where
* @param array $whereArray
*
* @return array
*/
protected function writeWhereBetweens(Where $where, array &$whereArray)
{
$between = $where->getBetweens();
\array_walk(
$between,
function (&$between) {
$between = '('
.$this->columnWriter->writeColumn($between['subject'])
.' BETWEEN '
.$this->writer->writePlaceholderValue($between['a'])
.' AND '
.$this->writer->writePlaceholderValue($between['b'])
.')';
}
);
$whereArray = \array_merge($whereArray, $between);
}
/**
* @param Where $where
* @param array $whereArray
*
* @return array
*/
protected function writeWhereNotBetweens(Where $where, array &$whereArray)
{
$between = $where->getNotBetweens();
\array_walk(
$between,
function (&$between) {
$between = '('
.$this->columnWriter->writeColumn($between['subject'])
.' NOT BETWEEN '
.$this->writer->writePlaceholderValue($between['a'])
.' AND '
.$this->writer->writePlaceholderValue($between['b'])
.')';
}
);
$whereArray = \array_merge($whereArray, $between);
}
/**
* @param Where $where
* @param array $whereArray
*
* @return array
*/
protected function writeWhereComparisons(Where $where, array &$whereArray)
{
$comparisons = $where->getComparisons();
\array_walk(
$comparisons,
function (&$comparison) {
if (!is_array($comparison)) {
return;
}
$str = $this->writeWherePartialCondition($comparison['subject']);
$str .= $this->writer->writeConjunction($comparison['conjunction']);
$str .= $this->writeWherePartialCondition($comparison['target']);
$comparison = "($str)";
}
);
$whereArray = \array_merge($whereArray, $comparisons);
}
/**
* @param $subject
*
* @return string
*/
protected function writeWherePartialCondition(&$subject)
{
if ($subject instanceof Column) {
$str = $this->columnWriter->writeColumn($subject);
} elseif ($subject instanceof Select) {
$selectWriter = WriterFactory::createSelectWriter($this->writer, $this->placeholderWriter);
$str = '('.$selectWriter->write($subject).')';
} else {
$str = $this->writer->writePlaceholderValue($subject);
}
return $str;
}
/**
* @param Where $where
* @param array $whereArray
*
* @return array
*/
protected function writeWhereIsNulls(Where $where, array &$whereArray)
{
$whereArray = \array_merge(
$whereArray,
$this->writeWhereIsNullable($where, 'getNull', 'writeIsNull')
);
}
/**
* @param Where $where
* @param string $getMethod
* @param string $writeMethod
*
* @return array
*/
protected function writeWhereIsNullable(Where $where, $getMethod, $writeMethod)
{
$collection = $where->$getMethod();
\array_walk(
$collection,
function (&$collection) use ($writeMethod) {
$collection =
'('.$this->columnWriter->writeColumn($collection['subject'])
.$this->writer->$writeMethod().')';
}
);
return $collection;
}
/**
* @param Where $where
* @param array $whereArray
*
* @return array
*/
protected function writeWhereIsNotNulls(Where $where, array &$whereArray)
{
$whereArray = \array_merge(
$whereArray,
$this->writeWhereIsNullable($where, 'getNotNull', 'writeIsNotNull')
);
}
/**
* @param Where $where
* @param array $whereArray
*
* @return array
*/
protected function writeWhereBooleans(Where $where, array &$whereArray)
{
$booleans = $where->getBooleans();
$placeholderWriter = $this->placeholderWriter;
\array_walk(
$booleans,
function (&$boolean) use (&$placeholderWriter) {
$column = $this->columnWriter->writeColumn($boolean['subject']);
$value = $this->placeholderWriter->add($boolean['value']);
$boolean = '(ISNULL('.$column.', 0) = '.$value.')';
}
);
$whereArray = \array_merge($whereArray, $booleans);
}
/**
* @param Where $where
* @param array $whereArray
*
* @return array
*/
protected function writeExists(Where $where, array &$whereArray)
{
$whereArray = \array_merge(
$whereArray,
$this->writeExistence($where, 'getExists', 'EXISTS')
);
}
/**
* @param Where $where
* @param string $method
* @param string $operation
*
* @return array
*/
protected function writeExistence(Where $where, $method, $operation)
{
$exists = [];
foreach ($where->$method() as $select) {
$exists[] = "$operation (".$this->writer->write($select, false).')';
}
return $exists;
}
/**
* @param Where $where
* @param array $whereArray
*
* @return array
*/
protected function writeNotExists(Where $where, array &$whereArray)
{
$whereArray = \array_merge(
$whereArray,
$this->writeExistence($where, 'getNotExists', 'NOT EXISTS')
);
}
/**
* @param Where $where
* @param array $whereArray
*
* @return array
*/
protected function writeSubWheres(Where $where, array &$whereArray)
{
$subWheres = $where->getSubWheres();
\array_walk(
$subWheres,
function (&$subWhere) {
$subWhere = "({$this->writeWhere($subWhere)})";
}
);
$whereArray = \array_merge($whereArray, $subWheres);
}
}

View File

@ -0,0 +1,133 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/12/14
* Time: 2:11 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
/**
* Class WriterFactory.
*/
final class WriterFactory
{
/**
* @param GenericBuilder $writer
* @param PlaceholderWriter $placeholderWriter
*
* @return ColumnWriter
*/
public static function createColumnWriter(GenericBuilder $writer, PlaceholderWriter $placeholderWriter)
{
return new ColumnWriter($writer, $placeholderWriter);
}
/**
* @param GenericBuilder $writer
* @param PlaceholderWriter $placeholderWriter
*
* @return WhereWriter
*/
public static function createWhereWriter(GenericBuilder $writer, PlaceholderWriter $placeholderWriter)
{
return new WhereWriter($writer, $placeholderWriter);
}
/**
* @param GenericBuilder $writer
* @param PlaceholderWriter $placeholderWriter
*
* @return SelectWriter
*/
public static function createSelectWriter(GenericBuilder $writer, PlaceholderWriter $placeholderWriter)
{
return new SelectWriter($writer, $placeholderWriter);
}
/**
* @param GenericBuilder $writer
* @param PlaceholderWriter $placeholderWriter
*
* @return InsertWriter
*/
public static function createInsertWriter(GenericBuilder $writer, PlaceholderWriter $placeholderWriter)
{
return new InsertWriter($writer, $placeholderWriter);
}
/**
* @param GenericBuilder $writer
* @param PlaceholderWriter $placeholderWriter
*
* @return UpdateWriter
*/
public static function createUpdateWriter(GenericBuilder $writer, PlaceholderWriter $placeholderWriter)
{
return new UpdateWriter($writer, $placeholderWriter);
}
/**
* @param GenericBuilder $writer
* @param PlaceholderWriter $placeholderWriter
*
* @return DeleteWriter
*/
public static function createDeleteWriter(GenericBuilder $writer, PlaceholderWriter $placeholderWriter)
{
return new DeleteWriter($writer, $placeholderWriter);
}
/**
* @return PlaceholderWriter
*/
public static function createPlaceholderWriter()
{
return new PlaceholderWriter();
}
/**
* @param GenericBuilder $writer
*
* @return IntersectWriter
*/
public static function createIntersectWriter(GenericBuilder $writer)
{
return new IntersectWriter($writer);
}
/**
* @param GenericBuilder $writer
*
* @return MinusWriter
*/
public static function createMinusWriter(GenericBuilder $writer)
{
return new MinusWriter($writer);
}
/**
* @param GenericBuilder $writer
*
* @return UnionWriter
*/
public static function createUnionWriter(GenericBuilder $writer)
{
return new UnionWriter($writer);
}
/**
* @param GenericBuilder $writer
*
* @return UnionAllWriter
*/
public static function createUnionAllWriter(GenericBuilder $writer)
{
return new UnionAllWriter($writer);
}
}

View File

@ -0,0 +1,277 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;
use NilPortugues\Sql\QueryBuilder\Syntax\QueryPartInterface;
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
use NilPortugues\Sql\QueryBuilder\Syntax\Table;
use NilPortugues\Sql\QueryBuilder\Syntax\Where;
// Builder injects itself into query for convestion to SQL string.
use NilPortugues\Sql\QueryBuilder\Builder\BuilderInterface;
/**
* Class AbstractBaseQuery.
*/
abstract class AbstractBaseQuery implements QueryInterface, QueryPartInterface
{
/**
* @var string
*/
protected $comment = '';
/**
* @var \NilPortugues\Sql\QueryBuilder\Builder\BuilderInterface
*/
protected $builder;
/**
* @var string
*/
protected $table;
/**
* @var string
*/
protected $whereOperator = 'AND';
/**
* @var Where
*/
protected $where;
/**
* @var array
*/
protected $joins = [];
/**
* @var int
*/
protected $limitStart;
/**
* @var int
*/
protected $limitCount;
/**
* @var array
*/
protected $orderBy = [];
/**
* @return Where
*/
protected function filter()
{
if (!isset($this->where)) {
$this->where = QueryFactory::createWhere($this);
}
return $this->where;
}
/**
* Stores the builder that created this query.
*
* @param BuilderInterface $builder
*
* @return $this
*/
final public function setBuilder(BuilderInterface $builder)
{
$this->builder = $builder;
return $this;
}
/**
* @return BuilderInterface
*
* @throws \RuntimeException when builder has not been injected
*/
final public function getBuilder()
{
if (!$this->builder) {
throw new \RuntimeException('Query builder has not been injected with setBuilder');
}
return $this->builder;
}
/**
* Converts this query into an SQL string by using the injected builder.
*
* @return string
*/
public function __toString()
{
try {
return $this->getSql();
} catch (\Exception $e) {
return \sprintf('[%s] %s', \get_class($e), $e->getMessage());
}
}
/**
* Converts this query into an SQL string by using the injected builder.
* Optionally can return the SQL with formatted structure.
*
* @param bool $formatted
*
* @return string
*/
public function getSql($formatted = false)
{
if ($formatted) {
return $this->getBuilder()->writeFormatted($this);
}
return $this->getBuilder()->write($this);
}
/**
* @return string
*/
abstract public function partName();
/**
* @return Where
*/
public function getWhere()
{
return $this->where;
}
/**
* @param Where $where
*
* @return $this
*/
public function setWhere(Where $where)
{
$this->where = $where;
return $this;
}
/**
* @return Table
*/
public function getTable()
{
$newTable = array($this->table);
return \is_null($this->table) ? null : SyntaxFactory::createTable($newTable);
}
/**
* @param string $table
*
* @return $this
*/
public function setTable($table)
{
$this->table = (string) $table;
return $this;
}
/**
* @param string $whereOperator
*
* @return Where
*/
public function where($whereOperator = 'AND')
{
if (!isset($this->where)) {
$this->where = $this->filter();
}
$this->where->conjunction($whereOperator);
return $this->where;
}
/**
* @return string
*/
public function getWhereOperator()
{
if (!isset($this->where)) {
$this->where = $this->filter();
}
return $this->where->getConjunction();
}
/**
* @param string $column
* @param string $direction
* @param null $table
*
* @return $this
*/
public function orderBy($column, $direction = OrderBy::ASC, $table = null)
{
$newColumn = array($column);
$column = SyntaxFactory::createColumn($newColumn, \is_null($table) ? $this->getTable() : $table);
$this->orderBy[] = new OrderBy($column, $direction);
return $this;
}
/**
* @return int
*/
public function getLimitCount()
{
return $this->limitCount;
}
/**
* @return int
*/
public function getLimitStart()
{
return $this->limitStart;
}
/**
* @param string $comment
*
* @return $this
*/
public function setComment($comment)
{
// Make each line of the comment prefixed with "--",
// and remove any trailing whitespace.
$comment = '-- '.str_replace("\n", "\n-- ", \rtrim($comment));
// Trim off any trailing "-- ", to ensure that the comment is valid.
$this->comment = \rtrim($comment, '- ');
if ($this->comment) {
$this->comment .= "\n";
}
return $this;
}
/**
* @return string
*/
public function getComment()
{
return $this->comment;
}
}

View File

@ -0,0 +1,57 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 12/24/14
* Time: 12:30 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
/**
* Class AbstractCreationalQuery.
*/
abstract class AbstractCreationalQuery extends AbstractBaseQuery
{
/**
* @var array
*/
protected $values = [];
/**
* @param string $table
* @param array $values
*/
public function __construct($table = null, array $values = null)
{
if (isset($table)) {
$this->setTable($table);
}
if (!empty($values)) {
$this->setValues($values);
}
}
/**
* @return array
*/
public function getValues()
{
return $this->values;
}
/**
* @param array $values
*
* @return $this
*/
public function setValues(array $values)
{
$this->values = $values;
return $this;
}
}

View File

@ -0,0 +1,80 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 12/24/14
* Time: 12:30 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Syntax\QueryPartInterface;
/**
* Class AbstractSetQuery.
*/
abstract class AbstractSetQuery implements QueryInterface, QueryPartInterface
{
/**
* @var array
*/
protected $union = [];
/**
* @param Select $select
*
* @return $this
*/
public function add(Select $select)
{
$this->union[] = $select;
return $this;
}
/**
* @return array
*/
public function getUnions()
{
return $this->union;
}
/**
* @throws QueryException
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Table
*/
public function getTable()
{
throw new QueryException(
\sprintf('%s does not support tables', $this->partName())
);
}
/**
* @throws QueryException
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
*/
public function getWhere()
{
throw new QueryException(
\sprintf('%s does not support WHERE.', $this->partName())
);
}
/**
* @throws QueryException
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
*/
public function where()
{
throw new QueryException(
\sprintf('%s does not support the WHERE statement.', $this->partName())
);
}
}

View File

@ -0,0 +1,263 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 12/25/14
* Time: 12:12 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
/**
* Class ColumnQuery.
*/
class ColumnQuery
{
/**
* @var array
*/
protected $columns = [];
/**
* @var array
*/
protected $columnSelects = [];
/**
* @var array
*/
protected $columnValues = [];
/**
* @var array
*/
protected $columnFuncs = [];
/**
* @var bool
*/
protected $isCount = false;
/**
* @var Select
*/
protected $select;
/**
* @var JoinQuery
*/
protected $joinQuery;
/**
* @param Select $select
* @param JoinQuery $joinQuery
* @param array $columns
*/
public function __construct(Select $select, JoinQuery $joinQuery, array $columns = null)
{
$this->select = $select;
$this->joinQuery = $joinQuery;
if (!isset($columns)) {
$columns = array(Column::ALL);
}
if (\count($columns)) {
$this->setColumns($columns);
}
}
/**
* @param $start
* @param int $count
*
* @return Select
*/
public function limit($start, $count = 0)
{
return $this->select->limit($start, $count);
}
/**
* @param string $whereOperator
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
*/
public function where($whereOperator = 'AND')
{
return $this->select->where($whereOperator);
}
/**
* @param string $column
* @param string $direction
* @param null $table
*
* @return Select
*/
public function orderBy($column, $direction = OrderBy::ASC, $table = null)
{
return $this->select->orderBy($column, $direction, $table);
}
/**
* @param string[] $columns
*
* @return Select
*/
public function groupBy(array $columns)
{
return $this->select->groupBy($columns);
}
/**
* Allows setting a Select query as a column value.
*
* @param array $column
*
* @return $this
*/
public function setSelectAsColumn(array $column)
{
$this->columnSelects[] = $column;
return $this;
}
/**
* @return array
*/
public function getColumnSelects()
{
return $this->columnSelects;
}
/**
* Allows setting a value to the select statement.
*
* @param string $value
* @param string $alias
*
* @return $this
*/
public function setValueAsColumn($value, $alias)
{
$this->columnValues[$alias] = $value;
return $this;
}
/**
* @return array
*/
public function getColumnValues()
{
return $this->columnValues;
}
/**
* Allows calculation on columns using predefined SQL functions.
*
* @param string $funcName
* @param string[] $arguments
* @param string $alias
*
* @return $this
*/
public function setFunctionAsColumn($funcName, array $arguments, $alias)
{
$this->columnFuncs[$alias] = ['func' => $funcName, 'args' => $arguments];
return $this;
}
/**
* @return array
*/
public function getColumnFuncs()
{
return $this->columnFuncs;
}
/**
* @param string $columnName
* @param string $alias
*
* @return $this
*/
public function count($columnName = '*', $alias = '')
{
$table = $this->select->getTable();
$count = 'COUNT(';
$count .= ($columnName !== '*') ? "$table.{$columnName}" : '*';
$count .= ')';
if (isset($alias) && \strlen($alias) > 0) {
$count .= ' AS "'.$alias.'"';
}
$this->columns = array($count);
$this->isCount = true;
return $this;
}
/**
* @return bool
*/
public function isCount()
{
return $this->isCount;
}
/**
* @return array
*/
public function getAllColumns()
{
$columns = $this->getColumns();
foreach ($this->joinQuery->getJoins() as $join) {
$joinCols = $join->getAllColumns();
$columns = \array_merge($columns, $joinCols);
}
return $columns;
}
/**
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Column
*
* @throws QueryException
*/
public function getColumns()
{
if (\is_null($this->select->getTable())) {
throw new QueryException('No table specified for the Select instance');
}
return SyntaxFactory::createColumns($this->columns, $this->select->getTable());
}
/**
* Sets the column names used to write the SELECT statement.
* If key is set, key is the column's alias. Value is always the column names.
*
* @param array $columns
*
* @return $this
*/
public function setColumns(array $columns)
{
$this->columns = $columns;
return $this;
}
}

View File

@ -0,0 +1,60 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
/**
* Class Delete.
*/
class Delete extends AbstractBaseQuery
{
/**
* @var int
*/
protected $limitStart;
/**
* @param string $table
*/
public function __construct($table = null)
{
if (isset($table)) {
$this->setTable($table);
}
}
/**
* @return string
*/
public function partName()
{
return 'DELETE';
}
/**
* @return int
*/
public function getLimitStart()
{
return $this->limitStart;
}
/**
* @param int $start
*
* @return $this
*/
public function limit($start)
{
$this->limitStart = $start;
return $this;
}
}

View File

@ -0,0 +1,37 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
/**
* Class Insert.
*/
class Insert extends AbstractCreationalQuery
{
/**
* @return string
*/
public function partName()
{
return 'INSERT';
}
/**
* @return array
*/
public function getColumns()
{
$columns = \array_keys($this->values);
return SyntaxFactory::createColumns($columns, $this->getTable());
}
}

View File

@ -0,0 +1,84 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:11 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Syntax\QueryPartInterface;
/**
* Class Intersect.
*/
class Intersect implements QueryInterface, QueryPartInterface
{
const INTERSECT = 'INTERSECT';
/**
* @var array
*/
protected $intersect = [];
/**
* @return string
*/
public function partName()
{
return 'INTERSECT';
}
/**
* @param Select $select
*
* @return $this
*/
public function add(Select $select)
{
$this->intersect[] = $select;
return $this;
}
/**
* @return array
*/
public function getIntersects()
{
return $this->intersect;
}
/**
* @throws QueryException
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Table
*/
public function getTable()
{
throw new QueryException('INTERSECT does not support tables');
}
/**
* @throws QueryException
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
*/
public function getWhere()
{
throw new QueryException('INTERSECT does not support WHERE.');
}
/**
* @throws QueryException
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
*/
public function where()
{
throw new QueryException('INTERSECT does not support the WHERE statement.');
}
}

View File

@ -0,0 +1,308 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 12/25/14
* Time: 11:41 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Syntax\Where;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
/**
* Class JoinQuery.
*/
class JoinQuery
{
const JOIN_LEFT = 'LEFT';
const JOIN_RIGHT = 'RIGHT';
const JOIN_INNER = 'INNER';
const JOIN_CROSS = 'CROSS';
/**
* @var Where
*/
protected $joinCondition;
/**
* @var bool
*/
protected $isJoin = false;
/**
* @var string
*/
protected $joinType;
/**
* @var array
*/
protected $joins = [];
/**
* @var Select
*/
protected $select;
/**
* @param Select $select
*/
public function __construct(Select $select)
{
$this->select = $select;
}
/**
* @param string $table
*
* @return $this
*/
public function setTable($table)
{
$this->select->setTable($table);
return $this;
}
/**
* @param string $table
* @param mixed $selfColumn
* @param mixed $refColumn
* @param string[] $columns
*
* @return Select
*/
public function leftJoin($table, $selfColumn = null, $refColumn = null, $columns = [])
{
return $this->join($table, $selfColumn, $refColumn, $columns, self::JOIN_LEFT);
}
/**
* @param string $table
* @param mixed $selfColumn
* @param mixed $refColumn
* @param string[] $columns
* @param string $joinType
*
* @return Select
*/
public function join(
$table,
$selfColumn = null,
$refColumn = null,
$columns = [],
$joinType = null
) {
if (!isset($this->joins[$table])) {
$select = QueryFactory::createSelect($table);
$select->setColumns($columns);
$select->setJoinType($joinType);
$select->setParentQuery($this->select);
$this->addJoin($select, $selfColumn, $refColumn);
}
return $this->joins[$table];
}
/**
* @param Select $select
* @param mixed $selfColumn
* @param mixed $refColumn
*
* @return Select
*/
public function addJoin(Select $select, $selfColumn, $refColumn)
{
$select->isJoin(true);
$table = $select->getTable()->getName();
if (!isset($this->joins[$table])) {
if (!$selfColumn instanceof Column) {
$newColumn = array($selfColumn);
$selfColumn = SyntaxFactory::createColumn(
$newColumn,
$this->select->getTable()
);
}
$select->joinCondition()->equals($refColumn, $selfColumn);
$this->joins[$table] = $select;
}
return $this->joins[$table];
}
/**
* Transforms Select in a joint.
*
* @param bool $isJoin
*
* @return $this
*/
public function setJoin($isJoin = true)
{
$this->isJoin = $isJoin;
return $this;
}
/**
* @param string $table
* @param mixed $selfColumn
* @param mixed $refColumn
* @param string[] $columns
*
* @internal param null $selectClass
*
* @return Select
*/
public function rightJoin($table, $selfColumn = null, $refColumn = null, $columns = [])
{
return $this->join($table, $selfColumn, $refColumn, $columns, self::JOIN_RIGHT);
}
/**
* @param string $table
* @param mixed $selfColumn
* @param mixed $refColumn
* @param string[] $columns
*
* @return Select
*/
public function crossJoin($table, $selfColumn = null, $refColumn = null, $columns = [])
{
return $this->join($table, $selfColumn, $refColumn, $columns, self::JOIN_CROSS);
}
/**
* @param string $table
* @param mixed $selfColumn
* @param mixed $refColumn
* @param string[] $columns
*
* @return Select
*/
public function innerJoin($table, $selfColumn = null, $refColumn = null, $columns = [])
{
return $this->join($table, $selfColumn, $refColumn, $columns, self::JOIN_INNER);
}
/**
* Alias to joinCondition.
*
* @return Where
*/
public function on()
{
return $this->joinCondition();
}
/**
* WHERE constrains used for the ON clause of a (LEFT/RIGHT/INNER/CROSS) JOIN.
*
* @return Where
*/
public function joinCondition()
{
if (!isset($this->joinCondition)) {
$this->joinCondition = QueryFactory::createWhere($this->select);
}
return $this->joinCondition;
}
/**
* @return bool
*/
public function isJoinSelect()
{
return $this->isJoin;
}
/**
* @return bool
*/
public function isJoin()
{
return $this->isJoin;
}
/**
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
*/
public function getJoinCondition()
{
return $this->joinCondition;
}
/**
* @param \NilPortugues\Sql\QueryBuilder\Syntax\Where $joinCondition
*
* @return $this
*/
public function setJoinCondition($joinCondition)
{
$this->joinCondition = $joinCondition;
return $this;
}
/**
* @return string
*/
public function getJoinType()
{
return $this->joinType;
}
/**
* @param string $joinType
*
* @return $this
*/
public function setJoinType($joinType)
{
$this->joinType = $joinType;
return $this;
}
/**
* @return array
*/
public function getJoins()
{
return $this->joins;
}
/**
* @param array $joins
*
* @return $this
*/
public function setJoins($joins)
{
$this->joins = $joins;
return $this;
}
/**
* @return array
*/
public function getAllJoins()
{
$joins = $this->joins;
foreach ($this->joins as $join) {
$joins = \array_merge($joins, $join->getAllJoins());
}
return $joins;
}
}

View File

@ -0,0 +1,95 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:11 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Syntax\QueryPartInterface;
/**
* Class Minus.
*/
class Minus implements QueryInterface, QueryPartInterface
{
const MINUS = 'MINUS';
/**
* @var Select
*/
protected $first;
/**
* @var Select
*/
protected $second;
/**
* @return string
*/
public function partName()
{
return 'MINUS';
}
/***
* @param Select $first
* @param Select $second
*/
public function __construct(Select $first, Select $second)
{
$this->first = $first;
$this->second = $second;
}
/**
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\Select
*/
public function getFirst()
{
return $this->first;
}
/**
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\Select
*/
public function getSecond()
{
return $this->second;
}
/**
* @throws QueryException
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Table
*/
public function getTable()
{
throw new QueryException('MINUS does not support tables');
}
/**
* @throws QueryException
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
*/
public function getWhere()
{
throw new QueryException('MINUS does not support WHERE.');
}
/**
* @throws QueryException
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
*/
public function where()
{
throw new QueryException('MINUS does not support the WHERE statement.');
}
}

View File

@ -0,0 +1,18 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
/**
* Class QueryException.
*/
final class QueryException extends \Exception
{
}

View File

@ -0,0 +1,107 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Syntax\Where;
/**
* Class QueryFactory.
*/
final class QueryFactory
{
/**
* @param string $table
* @param array $columns
*
* @return Select
*/
public static function createSelect($table = null, array $columns = null)
{
return new Select($table, $columns);
}
/**
* @param string $table
* @param array $values
*
* @return Insert
*/
public static function createInsert($table = null, array $values = null)
{
return new Insert($table, $values);
}
/**
* @param string $table
* @param array $values
*
* @return Update
*/
public static function createUpdate($table = null, array $values = null)
{
return new Update($table, $values);
}
/**
* @param string $table
*
* @return Delete
*/
public static function createDelete($table = null)
{
return new Delete($table);
}
/**
* @param QueryInterface $query
*
* @return Where
*/
public static function createWhere(QueryInterface $query)
{
return new Where($query);
}
/**
* @return Intersect
*/
public static function createIntersect()
{
return new Intersect();
}
/**
* @param Select $first
* @param Select $second
*
* @return Minus
*/
public static function createMinus(Select $first, Select $second)
{
return new Minus($first, $second);
}
/**
* @return Union
*/
public static function createUnion()
{
return new Union();
}
/**
* @return UnionAll
*/
public static function createUnionAll()
{
return new UnionAll();
}
}

View File

@ -0,0 +1,37 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
/**
* Interface QueryInterface.
*/
interface QueryInterface
{
/**
* @return string
*/
public function partName();
/**
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Table
*/
public function getTable();
/**
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
*/
public function getWhere();
/**
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
*/
public function where();
}

View File

@ -0,0 +1,546 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
use NilPortugues\Sql\QueryBuilder\Syntax\Table;
use NilPortugues\Sql\QueryBuilder\Syntax\Where;
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;
/**
* Class Select.
*/
class Select extends AbstractBaseQuery
{
/**
* @var Table
*/
protected $table;
/**
* @var array
*/
protected $groupBy = [];
/**
* @var string
*/
protected $camelCaseTableName = '';
/**
* @var Where
*/
protected $having;
/**
* @var string
*/
protected $havingOperator = 'AND';
/**
* @var bool
*/
protected $isDistinct = false;
/**
* @var Where
*/
protected $where;
/**
* @var JoinQuery
*/
protected $joinQuery;
/**
* @var ColumnQuery
*/
protected $columnQuery;
/**
* @var ParentQuery
*/
protected $parentQuery;
/**
* @param string $table
* @param array $columns
*/
public function __construct($table = null, array $columns = null)
{
if (isset($table)) {
$this->setTable($table);
}
$this->joinQuery = new JoinQuery($this);
$this->columnQuery = new ColumnQuery($this, $this->joinQuery, $columns);
}
/**
* This __clone method will create an exact clone but without the object references due to the fact these
* are lost in the process of serialization and un-serialization.
*
* @return Select
*/
public function __clone()
{
return \unserialize(\serialize($this));
}
/**
* @return string
*/
public function partName()
{
return 'SELECT';
}
/**
* @param string $table
* @param string $selfColumn
* @param string $refColumn
* @param string[] $columns
*
* @return Select
*/
public function leftJoin($table, $selfColumn = null, $refColumn = null, $columns = [])
{
return $this->joinQuery->leftJoin($table, $selfColumn, $refColumn, $columns);
}
/**
* @param string $table
* @param string $selfColumn
* @param string $refColumn
* @param string[] $columns
* @param string $joinType
*
* @return Select
*/
public function join(
$table,
$selfColumn = null,
$refColumn = null,
$columns = [],
$joinType = null
) {
return $this->joinQuery->join($table, $selfColumn, $refColumn, $columns, $joinType);
}
/**
* WHERE constrains used for the ON clause of a (LEFT/RIGHT/INNER/CROSS) JOIN.
*
* @return Where
*/
public function joinCondition()
{
return $this->joinQuery->joinCondition();
}
/**
* @param Select $select
* @param string $selfColumn
* @param string $refColumn
*
* @return Select
*/
public function addJoin(Select $select, $selfColumn, $refColumn)
{
return $this->joinQuery->addJoin($select, $selfColumn, $refColumn);
}
/**
* Transforms Select in a joint.
*
* @param bool $isJoin
*
* @return JoinQuery
*/
public function isJoin($isJoin = true)
{
return $this->joinQuery->setJoin($isJoin);
}
/**
* @param string $table
* @param string $selfColumn
* @param string $refColumn
* @param string[] $columns
*
* @internal param null $selectClass
*
* @return Select
*/
public function rightJoin($table, $selfColumn = null, $refColumn = null, $columns = [])
{
return $this->joinQuery->rightJoin($table, $selfColumn, $refColumn, $columns);
}
/**
* @param string $table
* @param string $selfColumn
* @param string $refColumn
* @param string[] $columns
*
* @return Select
*/
public function crossJoin($table, $selfColumn = null, $refColumn = null, $columns = [])
{
return $this->joinQuery->crossJoin($table, $selfColumn, $refColumn, $columns);
}
/**
* @param string $table
* @param string $selfColumn
* @param string $refColumn
* @param string[] $columns
*
* @return Select
*/
public function innerJoin($table, $selfColumn = null, $refColumn = null, $columns = [])
{
return $this->joinQuery->innerJoin($table, $selfColumn, $refColumn, $columns);
}
/**
* Alias to joinCondition.
*
* @return Where
*/
public function on()
{
return $this->joinQuery->joinCondition();
}
/**
* @return bool
*/
public function isJoinSelect()
{
return $this->joinQuery->isJoin();
}
/**
* @return array
*/
public function getAllColumns()
{
return $this->columnQuery->getAllColumns();
}
/**
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Column
*
* @throws QueryException
*/
public function getColumns()
{
return $this->columnQuery->getColumns();
}
/**
* Sets the column names used to write the SELECT statement.
* If key is set, key is the column's alias. Value is always the column names.
*
* @param string[] $columns
*
* @return ColumnQuery
*/
public function setColumns(array $columns)
{
return $this->columnQuery->setColumns($columns);
}
/**
* Allows setting a Select query as a column value.
*
* @param array $column
*
* @return ColumnQuery
*/
public function setSelectAsColumn(array $column)
{
return $this->columnQuery->setSelectAsColumn($column);
}
/**
* @return array
*/
public function getColumnSelects()
{
return $this->columnQuery->getColumnSelects();
}
/**
* Allows setting a value to the select statement.
*
* @param string $value
* @param string $alias
*
* @return ColumnQuery
*/
public function setValueAsColumn($value, $alias)
{
return $this->columnQuery->setValueAsColumn($value, $alias);
}
/**
* @return array
*/
public function getColumnValues()
{
return $this->columnQuery->getColumnValues();
}
/**
* Allows calculation on columns using predefined SQL functions.
*
* @param string $funcName
* @param string[] $arguments
* @param string $alias
*
* @return ColumnQuery
*/
public function setFunctionAsColumn($funcName, array $arguments, $alias)
{
return $this->columnQuery->setFunctionAsColumn($funcName, $arguments, $alias);
}
/**
* @return array
*/
public function getColumnFuncs()
{
return $this->columnQuery->getColumnFuncs();
}
/**
* Returns all the Where conditions to the BuilderInterface class in order to write the SQL WHERE statement.
*
* @return array
*/
public function getAllWheres()
{
return $this->getAllOperation($this->where, 'getAllWheres');
}
/**
* @param null|Where $data
* @param string $operation
*
* @return array
*/
protected function getAllOperation($data, $operation)
{
$collection = [];
if (!is_null($data)) {
$collection[] = $data;
}
foreach ($this->joinQuery->getJoins() as $join) {
$collection = \array_merge($collection, $join->$operation());
}
return $collection;
}
/**
* @return array
*/
public function getAllHavings()
{
return $this->getAllOperation($this->having, 'getAllHavings');
}
/**
* @param string $columnName
* @param string $alias
*
* @return ColumnQuery
*/
public function count($columnName = '*', $alias = '')
{
return $this->columnQuery->count($columnName, $alias);
}
/**
* @return bool
*/
public function isCount()
{
return $this->columnQuery->isCount();
}
/**
* @param int $start
* @param $count
*
* @return $this
*/
public function limit($start, $count = 0)
{
$this->limitStart = $start;
$this->limitCount = $count;
return $this;
}
/**
* @return array
*/
public function getAllJoins()
{
return $this->joinQuery->getAllJoins();
}
/**
* @return array
*/
public function getGroupBy()
{
return SyntaxFactory::createColumns($this->groupBy, $this->getTable());
}
/**
* @param string[] $columns
*
* @return $this
*/
public function groupBy(array $columns)
{
$this->groupBy = $columns;
return $this;
}
/**
* @return Where
*/
public function getJoinCondition()
{
return $this->joinQuery->getJoinCondition();
}
/**
* @return string
*/
public function getJoinType()
{
return $this->joinQuery->getJoinType();
}
/**
* @param string|null $joinType
*
* @return $this
*/
public function setJoinType($joinType)
{
$this->joinQuery->setJoinType($joinType);
return $this;
}
/**
* @param $havingOperator
*
* @throws QueryException
*
* @return Where
*/
public function having($havingOperator = 'AND')
{
if (!isset($this->having)) {
$this->having = QueryFactory::createWhere($this);
}
if (!in_array($havingOperator, array(Where::CONJUNCTION_AND, Where::CONJUNCTION_OR))) {
throw new QueryException(
"Invalid conjunction specified, must be one of AND or OR, but '".$havingOperator."' was found."
);
}
$this->havingOperator = $havingOperator;
return $this->having;
}
/**
* @return string
*/
public function getHavingOperator()
{
return $this->havingOperator;
}
/**
* @return $this
*/
public function distinct()
{
$this->isDistinct = true;
return $this;
}
/**
* @return bool
*/
public function isDistinct()
{
return $this->isDistinct;
}
/**
* @return array
*/
public function getAllOrderBy()
{
return $this->orderBy;
}
/**
* @return ParentQuery
*/
public function getParentQuery()
{
return $this->parentQuery;
}
/**
* @param Select $parentQuery
*
* @return $this
*/
public function setParentQuery(Select $parentQuery)
{
$this->parentQuery = $parentQuery;
return $this;
}
/**
* @param string $column
* @param string $direction
* @param null $table
*
* @return $this
*/
public function orderBy($column, $direction = OrderBy::ASC, $table = null)
{
$current = parent::orderBy($column, $direction, $table);
if ($this->getParentQuery() != null) {
$this->getParentQuery()->orderBy($column, $direction, \is_null($table) ? $this->getTable() : $table);
}
return $current;
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:11 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
/**
* Class Union.
*/
class Union extends AbstractSetQuery
{
const UNION = 'UNION';
/**
* @return string
*/
public function partName()
{
return 'UNION';
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:11 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
/**
* Class UnionAll.
*/
class UnionAll extends AbstractSetQuery
{
const UNION_ALL = 'UNION ALL';
/**
* @return string
*/
public function partName()
{
return 'UNION ALL';
}
}

View File

@ -0,0 +1,55 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
/**
* Class Update.
*/
class Update extends AbstractCreationalQuery
{
/**
* @var int
*/
protected $limitStart;
/**
* @var array
*/
protected $orderBy = [];
/**
* @return string
*/
public function partName()
{
return 'UPDATE';
}
/**
* @return int
*/
public function getLimitStart()
{
return $this->limitStart;
}
/**
* @param int $start
*
* @return $this
*/
public function limit($start)
{
$this->limitStart = $start;
return $this;
}
}

View File

@ -0,0 +1,139 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Syntax;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryException;
/**
* Class Column.
*/
class Column implements QueryPartInterface
{
const ALL = '*';
/**
* @var Table
*/
protected $table;
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $alias;
/**
* @param string $name
* @param string $table
* @param string $alias
*/
public function __construct($name, $table, $alias = '')
{
$this->setName($name);
$this->setTable($table);
$this->setAlias($alias);
}
/**
* @return string
*/
public function partName()
{
return 'COLUMN';
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*
* @return $this
*/
public function setName($name)
{
$this->name = (string) $name;
return $this;
}
/**
* @return Table
*/
public function getTable()
{
return $this->table;
}
/**
* @param string $table
*
* @return $this
*/
public function setTable($table)
{
$newTable = array($table);
$this->table = SyntaxFactory::createTable($newTable);
return $this;
}
/**
* @return string
*/
public function getAlias()
{
return $this->alias;
}
/**
* @param null|string $alias
*
* @return $this
*
* @throws QueryException
*/
public function setAlias($alias)
{
if (0 == \strlen($alias)) {
$this->alias = null;
return $this;
}
if ($this->isAll()) {
throw new QueryException("Can't use alias because column name is ALL (*)");
}
$this->alias = (string) $alias;
return $this;
}
/**
* Check whether column name is '*' or not.
*
* @return bool
*/
public function isAll()
{
return $this->getName() == self::ALL;
}
}

View File

@ -0,0 +1,92 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Syntax;
/**
* Class OrderBy.
*/
class OrderBy
{
const ASC = 'ASC';
const DESC = 'DESC';
/**
* @var Column
*/
protected $column;
/**
* @var string
*/
protected $direction;
/**
* @var bool
*/
protected $useAlias;
/**
* @param Column $column
* @param string $direction
*/
public function __construct(Column $column, $direction)
{
$this->setColumn($column);
$this->setDirection($direction);
}
/**
* @return Column
*/
public function getColumn()
{
return $this->column;
}
/**
* @param Column $column
*
* @return $this
*/
public function setColumn($column)
{
$this->column = $column;
return $this;
}
/**
* @return string
*/
public function getDirection()
{
return $this->direction;
}
/**
* @param string $direction
*
* @throws \InvalidArgumentException
*
* @return $this
*/
public function setDirection($direction)
{
if (!in_array($direction, array(self::ASC, self::DESC))) {
throw new \InvalidArgumentException(
"Specified direction '$direction' is not allowed. Only ASC or DESC are allowed."
);
}
$this->direction = $direction;
return $this;
}
}

View File

@ -0,0 +1,22 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/8/14
* Time: 5:32 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Syntax;
/**
* Interface QueryPartInterface.
*/
interface QueryPartInterface
{
/**
* @return string
*/
public function partName();
}

View File

@ -0,0 +1,93 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Syntax;
/**
* Class SyntaxFactory.
*/
final class SyntaxFactory
{
/**
* Creates a collection of Column objects.
*
* @param array $arguments
* @param Table|null $table
*
* @return array
*/
public static function createColumns(array &$arguments, $table = null)
{
$createdColumns = [];
foreach ($arguments as $index => $column) {
if (!is_object($column)) {
$newColumn = array($column);
$column = self::createColumn($newColumn, $table);
if (!is_numeric($index)) {
$column->setAlias($index);
}
$createdColumns[] = $column;
} else if ($column instanceof Column) {
$createdColumns[] = $column;
}
}
return \array_filter($createdColumns);
}
/**
* Creates a Column object.
*
* @param array $argument
* @param null|Table $table
*
* @return Column
*/
public static function createColumn(array &$argument, $table = null)
{
$columnName = \array_values($argument);
$columnName = $columnName[0];
$columnAlias = \array_keys($argument);
$columnAlias = $columnAlias[0];
if (\is_numeric($columnAlias) || \strpos($columnName, '*') !== false) {
$columnAlias = null;
}
return new Column($columnName, (string) $table, $columnAlias);
}
/**
* Creates a Table object.
*
* @param string[] $table
*
* @return Table
*/
public static function createTable($table)
{
$tableName = $table;
if (\is_array($table)) {
$tableName = \current($table);
$tableAlias = \key($table);
}
$newTable = new Table($tableName);
if (isset($tableAlias) && !is_numeric($tableAlias)) {
$newTable->setAlias($tableAlias);
}
return $newTable;
}
}

View File

@ -0,0 +1,138 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Syntax;
/**
* Class Table.
*/
class Table
{
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $alias;
/**
* @var string
*/
protected $schema;
/**
* @var bool
*/
protected $view = false;
/**
* @param $name
* @param string $schema
*/
public function __construct($name, $schema = null)
{
$this->name = $name;
if (!is_null($schema)) {
$this->schema = $schema;
}
}
/**
* @return string
*/
public function __toString()
{
return (string) $this->name;
}
/**
* @param bool $view
*
* @return $this
*/
public function setView($view)
{
$this->view = $view;
return $this;
}
/**
* @return bool
*/
public function isView()
{
return $this->view;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getAlias()
{
return $this->alias;
}
/**
* @return string
*/
public function getCompleteName()
{
$alias = ($this->alias) ? " AS {$this->alias}" : '';
$schema = ($this->schema) ? "{$this->schema}." : '';
return $schema.$this->name.$alias;
}
/**
* @param string $alias
*
* @return $this
*/
public function setAlias($alias)
{
$this->alias = $alias;
return $this;
}
/**
* @return string
*/
public function getSchema()
{
return $this->schema;
}
/**
* @param string
* @param string $schema
*
* @return $this
*/
public function setSchema($schema)
{
$this->schema = $schema;
return $this;
}
}

View File

@ -0,0 +1,646 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Sql\QueryBuilder\Syntax;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryException;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryFactory;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
/**
* Class Where.
*/
class Where
{
const OPERATOR_GREATER_THAN_OR_EQUAL = '>=';
const OPERATOR_GREATER_THAN = '>';
const OPERATOR_LESS_THAN_OR_EQUAL = '<=';
const OPERATOR_LESS_THAN = '<';
const OPERATOR_LIKE = 'LIKE';
const OPERATOR_NOT_LIKE = 'NOT LIKE';
const OPERATOR_EQUAL = '=';
const OPERATOR_NOT_EQUAL = '<>';
const CONJUNCTION_AND = 'AND';
const CONJUNCTION_AND_NOT = 'AND NOT';
const CONJUNCTION_OR = 'OR';
const CONJUNCTION_OR_NOT = 'OR NOT';
const CONJUNCTION_EXISTS = 'EXISTS';
const CONJUNCTION_NOT_EXISTS = 'NOT EXISTS';
/**
* @var array
*/
protected $comparisons = [];
/**
* @var array
*/
protected $betweens = [];
/**
* @var array
*/
protected $isNull = [];
/**
* @var array
*/
protected $isNotNull = [];
/**
* @var array
*/
protected $booleans = [];
/**
* @var array
*/
protected $match = [];
/**
* @var array
*/
protected $ins = [];
/**
* @var array
*/
protected $notIns = [];
/**
* @var array
*/
protected $subWheres = [];
/**
* @var string
*/
protected $conjunction = self::CONJUNCTION_AND;
/**
* @var QueryInterface
*/
protected $query;
/**
* @var Table
*/
protected $table;
/**
* @var array
*/
protected $exists = [];
/**
* @var array
*/
protected $notExists = [];
/**
* @var array
*/
protected $notBetweens = [];
/**
* @param QueryInterface $query
*/
public function __construct(QueryInterface $query)
{
$this->query = $query;
}
/**
* Deep copy for nested references.
*
* @return mixed
*/
public function __clone()
{
return \unserialize(\serialize($this));
}
/**
* @return bool
*/
public function isEmpty()
{
$empty = \array_merge(
$this->comparisons,
$this->booleans,
$this->betweens,
$this->isNotNull,
$this->isNull,
$this->ins,
$this->notIns,
$this->subWheres,
$this->exists
);
return 0 == \count($empty);
}
/**
* @return string
*/
public function getConjunction()
{
return $this->conjunction;
}
/**
* @param string $operator
*
* @return $this
*
* @throws QueryException
*/
public function conjunction($operator)
{
if (false === \in_array(
$operator,
[self::CONJUNCTION_AND, self::CONJUNCTION_OR, self::CONJUNCTION_OR_NOT, self::CONJUNCTION_AND_NOT]
)
) {
throw new QueryException(
"Invalid conjunction specified, must be one of AND or OR, but '".$operator."' was found."
);
}
$this->conjunction = $operator;
return $this;
}
/**
* @return array
*/
public function getSubWheres()
{
return $this->subWheres;
}
/**
* @param $operator
*
* @return Where
*/
public function subWhere($operator = 'OR')
{
/** @var Where $filter */
$filter = QueryFactory::createWhere($this->query);
$filter->conjunction($operator);
$filter->setTable($this->getTable());
$this->subWheres[] = $filter;
return $filter;
}
/**
* @return Table
*/
public function getTable()
{
return $this->query->getTable();
}
/**
* Used for subWhere query building.
*
* @param Table $table string
*
* @return $this
*/
public function setTable($table)
{
$this->table = $table;
return $this;
}
/**
* equals alias.
*
* @param $column
* @param int $value
*
* @return static
*/
public function eq($column, $value)
{
return $this->equals($column, $value);
}
/**
* @param $column
* @param $value
*
* @return static
*/
public function equals($column, $value)
{
return $this->compare($column, $value, self::OPERATOR_EQUAL);
}
/**
* @param $column
* @param $value
* @param string $operator
*
* @return $this
*/
protected function compare($column, $value, $operator)
{
$column = $this->prepareColumn($column);
$this->comparisons[] = [
'subject' => $column,
'conjunction' => $operator,
'target' => $value,
];
return $this;
}
/**
* @param $column
*
* @return Column|Select
*/
protected function prepareColumn($column)
{
//This condition handles the "Select as a a column" special case.
//or when compare column is customized.
if ($column instanceof Select || $column instanceof Column) {
return $column;
}
$newColumn = [$column];
return SyntaxFactory::createColumn($newColumn, $this->getTable());
}
/**
* @param string $column
* @param int $value
*
* @return static
*/
public function notEquals($column, $value)
{
return $this->compare($column, $value, self::OPERATOR_NOT_EQUAL);
}
/**
* @param string $column
* @param int $value
*
* @return static
*/
public function greaterThan($column, $value)
{
return $this->compare($column, $value, self::OPERATOR_GREATER_THAN);
}
/**
* @param string $column
* @param int $value
*
* @return static
*/
public function greaterThanOrEqual($column, $value)
{
return $this->compare($column, $value, self::OPERATOR_GREATER_THAN_OR_EQUAL);
}
/**
* @param string $column
* @param int $value
*
* @return static
*/
public function lessThan($column, $value)
{
return $this->compare($column, $value, self::OPERATOR_LESS_THAN);
}
/**
* @param string $column
* @param int $value
*
* @return static
*/
public function lessThanOrEqual($column, $value)
{
return $this->compare($column, $value, self::OPERATOR_LESS_THAN_OR_EQUAL);
}
/**
* @param string $column
* @param $value
*
* @return static
*/
public function like($column, $value)
{
return $this->compare($column, $value, self::OPERATOR_LIKE);
}
/**
* @param string $column
* @param int $value
*
* @return static
*/
public function notLike($column, $value)
{
return $this->compare($column, $value, self::OPERATOR_NOT_LIKE);
}
/**
* @param string[] $columns
* @param mixed[] $values
*
* @return static
*/
public function match(array $columns, array $values)
{
return $this->genericMatch($columns, $values, 'natural');
}
/**
* @param string[] $columns
* @param mixed[] $values
* @param string $mode
*
* @return $this
*/
protected function genericMatch(array &$columns, array &$values, $mode)
{
$this->match[] = [
'columns' => $columns,
'values' => $values,
'mode' => $mode,
];
return $this;
}
/**
* @param string $literal
*
* @return $this
*/
public function asLiteral($literal)
{
$this->comparisons[] = $literal;
return $this;
}
/**
* @param string[] $columns
* @param mixed[] $values
*
* @return $this
*/
public function matchBoolean(array $columns, array $values)
{
return $this->genericMatch($columns, $values, 'boolean');
}
/**
* @param string[] $columns
* @param mixed[] $values
*
* @return $this
*/
public function matchWithQueryExpansion(array $columns, array $values)
{
return $this->genericMatch($columns, $values, 'query_expansion');
}
/**
* @param string $column
* @param int[] $values
*
* @return $this
*/
public function in($column, array $values)
{
$this->ins[$column] = $values;
return $this;
}
/**
* @param string $column
* @param int[] $values
*
* @return $this
*/
public function notIn($column, array $values)
{
$this->notIns[$column] = $values;
return $this;
}
/**
* @param string $column
* @param int $a
* @param int $b
*
* @return $this
*/
public function between($column, $a, $b)
{
$column = $this->prepareColumn($column);
$this->betweens[] = ['subject' => $column, 'a' => $a, 'b' => $b];
return $this;
}
/**
* @param string $column
* @param int $a
* @param int $b
*
* @return $this
*/
public function notBetween($column, $a, $b)
{
$column = $this->prepareColumn($column);
$this->notBetweens[] = ['subject' => $column, 'a' => $a, 'b' => $b];
return $this;
}
/**
* @param string $column
*
* @return static
*/
public function isNull($column)
{
$column = $this->prepareColumn($column);
$this->isNull[] = ['subject' => $column];
return $this;
}
/**
* @param string $column
*
* @return $this
*/
public function isNotNull($column)
{
$column = $this->prepareColumn($column);
$this->isNotNull[] = ['subject' => $column];
return $this;
}
/**
* @param string $column
* @param int $value
*
* @return $this
*/
public function addBitClause($column, $value)
{
$column = $this->prepareColumn($column);
$this->booleans[] = ['subject' => $column, 'value' => $value];
return $this;
}
/**
* @param Select $select
*
* @return $this
*/
public function exists(Select $select)
{
$this->exists[] = $select;
return $this;
}
/**
* @return array
*/
public function getExists()
{
return $this->exists;
}
/**
* @param Select $select
*
* @return $this
*/
public function notExists(Select $select)
{
$this->notExists[] = $select;
return $this;
}
/**
* @return array
*/
public function getNotExists()
{
return $this->notExists;
}
/**
* @return array
*/
public function getMatches()
{
return $this->match;
}
/**
* @return array
*/
public function getIns()
{
return $this->ins;
}
/**
* @return array
*/
public function getNotIns()
{
return $this->notIns;
}
/**
* @return array
*/
public function getBetweens()
{
return $this->betweens;
}
/**
* @return array
*/
public function getNotBetweens()
{
return $this->notBetweens;
}
/**
* @return array
*/
public function getBooleans()
{
return $this->booleans;
}
/**
* @return array
*/
public function getComparisons()
{
return $this->comparisons;
}
/**
* @return array
*/
public function getNotNull()
{
return $this->isNotNull;
}
/**
* @return array
*/
public function getNull()
{
return $this->isNull;
}
/**
* @return QueryInterface
*/
public function end()
{
return $this->query;
}
}