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

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,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);
}
}