Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
build/
vendor/
bin/
Expand Down
16 changes: 11 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"role":"Contributor"
}
],
"scripts": {
"test": "php vendor/pestphp/pest/bin/pest",
"rector": "php vendor/rector/rector/bin/rector"
},
"autoload":{
"psr-4":{
"NilPortugues\\Sql\\QueryBuilder\\":"src/"
Expand All @@ -32,19 +36,21 @@
},
"require":
{
"php": ">=5.5",
"php": ">=7",
"nilportugues/sql-query-formatter": "~1.2"
},
"require-dev": {
"phpunit/phpunit": "4.*",
"fabpot/php-cs-fixer": "~1.9",
"nilportugues/php_backslasher": "~0.2"
"phpunit/phpunit": "^9.0",
"friendsofphp/php-cs-fixer": "^2.16",
"pestphp/pest": "^0.1.5",
"rector/rector": "^0.7.26"
},
"config":
{
"bin-dir": "bin"
},
"minimum-stability": "stable",
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
Expand Down
25 changes: 25 additions & 0 deletions rector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
imports:
- { resource: "create-rector.yaml", ignore_errors: 'not_found' }

parameters:
# bleeding edge feature
# is_cache_enabled: true

auto_import_names: true

sets:
# - solid
- 'dead-code'

paths:
- src
- tests

exclude_paths:
- "/vendor/"

php_version_features: '7.4'

services:
Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector: null
Rector\SOLID\Rector\Class_\RepeatedLiteralToClassConstantRector: null
3 changes: 2 additions & 1 deletion src/Builder/BuilderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

namespace NilPortugues\Sql\QueryBuilder\Builder;

use Exception;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be one blank line after the last USE statement; 0 found;

/**
* Class BuilderException.
*/
final class BuilderException extends \Exception
final class BuilderException extends Exception
{
}
32 changes: 21 additions & 11 deletions src/Builder/GenericBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@

namespace NilPortugues\Sql\QueryBuilder\Builder;

use NilPortugues\Sql\QueryBuilder\Builder\Syntax\PlaceholderWriter;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\WhereWriter;
use NilPortugues\Sql\QueryFormatter\Formatter;
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete;
use NilPortugues\Sql\QueryBuilder\Manipulation\Intersect;
use NilPortugues\Sql\QueryBuilder\Manipulation\Union;
use NilPortugues\Sql\QueryBuilder\Manipulation\UnionAll;
use NilPortugues\Sql\QueryBuilder\Manipulation\Minus;
use ReflectionClass;
use RuntimeException;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory;
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface;
Expand All @@ -26,21 +36,21 @@ class GenericBuilder implements BuilderInterface
/**
* The placeholder parameter bag.
*
* @var \NilPortugues\Sql\QueryBuilder\Builder\Syntax\PlaceholderWriter
* @var PlaceholderWriter
*/
protected $placeholderWriter;

/**
* The Where writer.
*
* @var \NilPortugues\Sql\QueryBuilder\Builder\Syntax\WhereWriter
* @var WhereWriter
*/
protected $whereWriter;

/**
* The SQL formatter.
*
* @var \NilPortugues\Sql\QueryFormatter\Formatter
* @var Formatter
*/
protected $sqlFormatter;

Expand Down Expand Up @@ -139,31 +149,31 @@ public function update($table = null, array $values = null)
/**
* @param string $table
*
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\Delete
* @return Delete
*/
public function delete($table = null)
{
return $this->injectBuilder(QueryFactory::createDelete($table));
}

/**
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\Intersect
* @return Intersect
*/
public function intersect()
{
return QueryFactory::createIntersect();
}

/**
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\Union
* @return Union
*/
public function union()
{
return QueryFactory::createUnion();
}

/**
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\UnionAll
* @return UnionAll
*/
public function unionAll()
{
Expand All @@ -174,7 +184,7 @@ public function unionAll()
* @param \NilPortugues\Sql\QueryBuilder\Manipulation\Select $first
* @param \NilPortugues\Sql\QueryBuilder\Manipulation\Select $second
*
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\Minus
* @return Minus
*/
public function minus(Select $first, Select $second)
{
Expand All @@ -199,7 +209,7 @@ public function getValues()
public function writeFormatted(QueryInterface $query)
{
if (null === $this->sqlFormatter) {
$this->sqlFormatter = (new \ReflectionClass($this->sqlFormatterClass))->newInstance();
$this->sqlFormatter = (new ReflectionClass($this->sqlFormatterClass))->newInstance();
}

return $this->sqlFormatter->format($this->write($query));
Expand All @@ -211,7 +221,7 @@ public function writeFormatted(QueryInterface $query)
*
* @return string
*
* @throws \RuntimeException
* @throws RuntimeException
*/
public function write(QueryInterface $query, $resetPlaceholders = true)
{
Expand All @@ -227,7 +237,7 @@ public function write(QueryInterface $query, $resetPlaceholders = true)
return $this->queryWriterInstances[$queryPart]->write($query);
}

throw new \RuntimeException('Query builder part not defined.');
throw new RuntimeException('Query builder part not defined.');
}

/**
Expand Down
25 changes: 10 additions & 15 deletions src/Builder/Syntax/ColumnWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,11 @@ 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);
foreach ($valueAsColumns as $alias => $value) {
$value = $this->writer->writePlaceholderValue($value);
$newValueColumn = array($alias => $value);

$newColumns[] = SyntaxFactory::createColumn($newValueColumn, null);
}
$newColumns[] = SyntaxFactory::createColumn($newValueColumn, null);
}

return $newColumns;
Expand All @@ -117,14 +115,12 @@ 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']).')' : '';
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);
}
$newFuncColumn = array($alias => $funcName.$funcArgs);
$newColumns[] = SyntaxFactory::createColumn($newFuncColumn, null);
}

return $newColumns;
Expand Down Expand Up @@ -155,8 +151,7 @@ public function writeColumn(Column $column)
$table = ($alias) ? $this->writer->writeTableAlias($alias) : $this->writer->writeTable($column->getTable());

$columnString = (empty($table)) ? '' : "{$table}.";
$columnString .= $this->writer->writeColumnName($column);

return $columnString;
return $columnString . $this->writer->writeColumnName($column);
}
}
3 changes: 1 addition & 2 deletions src/Builder/Syntax/PlaceholderWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ protected function setValidSqlValue($value)
{
$value = $this->writeNullSqlString($value);
$value = $this->writeStringAsSqlString($value);
$value = $this->writeBooleanSqlString($value);

return $value;
return $this->writeBooleanSqlString($value);
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/Builder/Syntax/WhereWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
class WhereWriter extends AbstractBaseWriter
{
private const SUBJECT = 'subject';

/**
* @var array
*/
Expand Down Expand Up @@ -172,7 +174,7 @@ protected function writeWhereBetweens(Where $where, array &$whereArray)
function (&$between) {

$between = '('
.$this->columnWriter->writeColumn($between['subject'])
.$this->columnWriter->writeColumn($between[self::SUBJECT])
.' BETWEEN '
.$this->writer->writePlaceholderValue($between['a'])
.' AND '
Expand All @@ -198,7 +200,7 @@ protected function writeWhereNotBetweens(Where $where, array &$whereArray)
function (&$between) {

$between = '('
.$this->columnWriter->writeColumn($between['subject'])
.$this->columnWriter->writeColumn($between[self::SUBJECT])
.' NOT BETWEEN '
.$this->writer->writePlaceholderValue($between['a'])
.' AND '
Expand Down Expand Up @@ -227,7 +229,7 @@ function (&$comparison) {
return;
}

$str = $this->writeWherePartialCondition($comparison['subject']);
$str = $this->writeWherePartialCondition($comparison[self::SUBJECT]);
$str .= $this->writer->writeConjunction($comparison['conjunction']);
$str .= $this->writeWherePartialCondition($comparison['target']);

Expand Down Expand Up @@ -286,7 +288,7 @@ protected function writeWhereIsNullable(Where $where, $getMethod, $writeMethod)
$collection,
function (&$collection) use ($writeMethod) {
$collection =
'('.$this->columnWriter->writeColumn($collection['subject'])
'('.$this->columnWriter->writeColumn($collection[self::SUBJECT])
.$this->writer->$writeMethod().')';
}
);
Expand Down Expand Up @@ -322,7 +324,7 @@ protected function writeWhereBooleans(Where $where, array &$whereArray)
\array_walk(
$booleans,
function (&$boolean) use (&$placeholderWriter) {
$column = $this->columnWriter->writeColumn($boolean['subject']);
$column = $this->columnWriter->writeColumn($boolean[self::SUBJECT]);
$value = $this->placeholderWriter->add($boolean['value']);

$boolean = '(ISNULL('.$column.', 0) = '.$value.')';
Expand Down
8 changes: 5 additions & 3 deletions src/Manipulation/AbstractBaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace NilPortugues\Sql\QueryBuilder\Manipulation;

use RuntimeException;
use Exception;
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;
use NilPortugues\Sql\QueryBuilder\Syntax\QueryPartInterface;
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
Expand Down Expand Up @@ -97,12 +99,12 @@ final public function setBuilder(BuilderInterface $builder)
/**
* @return BuilderInterface
*
* @throws \RuntimeException when builder has not been injected
* @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');
throw new RuntimeException('Query builder has not been injected with setBuilder');
}

return $this->builder;
Expand All @@ -117,7 +119,7 @@ public function __toString()
{
try {
return $this->getSql();
} catch (\Exception $e) {
} catch (Exception $e) {
return \sprintf('[%s] %s', \get_class($e), $e->getMessage());
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/Manipulation/AbstractSetQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace NilPortugues\Sql\QueryBuilder\Manipulation;

use NilPortugues\Sql\QueryBuilder\Syntax\Table;
use NilPortugues\Sql\QueryBuilder\Syntax\Where;
use NilPortugues\Sql\QueryBuilder\Syntax\QueryPartInterface;

/**
Expand Down Expand Up @@ -45,7 +47,7 @@ public function getUnions()
/**
* @throws QueryException
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Table
* @return Table
*/
public function getTable()
{
Expand All @@ -57,7 +59,7 @@ public function getTable()
/**
* @throws QueryException
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
* @return Where
*/
public function getWhere()
{
Expand All @@ -69,7 +71,7 @@ public function getWhere()
/**
* @throws QueryException
*
* @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
* @return Where
*/
public function where()
{
Expand Down
Loading