Skip to content

Commit

Permalink
Defer class loading
Browse files Browse the repository at this point in the history
Nil Portugués committed Sep 23, 2014

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent f3a9b80 commit 9a3d95b
Showing 18 changed files with 58 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -15,5 +15,4 @@
*/
final class BuilderException extends \Exception
{

}
53 changes: 43 additions & 10 deletions src/NilPortugues/SqlQueryBuilder/Builder/GenericBuilder.php
Original file line number Diff line number Diff line change
@@ -9,7 +9,16 @@
*/
namespace NilPortugues\SqlQueryBuilder\Builder;

use NilPortugues\SqlQueryBuilder\Builder\Syntax\DeleteWriter;
use NilPortugues\SqlQueryBuilder\Builder\Syntax\InsertWriter;
use NilPortugues\SqlQueryBuilder\Builder\Syntax\IntersectWriter;
use NilPortugues\SqlQueryBuilder\Builder\Syntax\MinusWriter;
use NilPortugues\SqlQueryBuilder\Builder\Syntax\SelectWriter;
use NilPortugues\SqlQueryBuilder\Builder\Syntax\UnionAllWriter;
use NilPortugues\SqlQueryBuilder\Builder\Syntax\UnionWriter;
use NilPortugues\SqlQueryBuilder\Builder\Syntax\UpdateWriter;
use NilPortugues\SqlQueryBuilder\Builder\Syntax\WriterFactory;
use NilPortugues\SqlQueryBuilder\Manipulation\Delete;
use NilPortugues\SqlQueryBuilder\Manipulation\QueryInterface;
use NilPortugues\SqlQueryBuilder\Manipulation\QueryFactory;
use NilPortugues\SqlQueryBuilder\Manipulation\Select;
@@ -85,16 +94,6 @@ public function __construct()
{
$this->placeholderWriter = WriterFactory::createPlaceholderWriter();

$this->selectWriter = WriterFactory::createSelectWriter($this, $this->placeholderWriter);
$this->updateWriter = WriterFactory::createUpdateWriter($this, $this->placeholderWriter);
$this->deleteWriter = WriterFactory::createDeleteWriter($this, $this->placeholderWriter);
$this->insertWriter = WriterFactory::createInsertWriter($this, $this->placeholderWriter);
$this->whereWriter = WriterFactory::createWhereWriter($this, $this->placeholderWriter);
$this->intersectWriter = WriterFactory::createIntersectWriter($this);
$this->minusWriter = WriterFactory::createMinusWriter($this);
$this->unionWriter = WriterFactory::createUnionWriter($this);
$this->unionAllWriter = WriterFactory::createUnionAllWriter($this);

$this->sqlFormatter = new Formatter();
}

@@ -190,34 +189,64 @@ public function write(QueryInterface $query, $resetPlaceholders = true)
switch ($query->partName()) {

case 'SELECT':
if (false === ($this->selectWriter instanceof SelectWriter)) {
$this->selectWriter = WriterFactory::createSelectWriter($this, $this->placeholderWriter);
}

$sql = $this->selectWriter->writeSelect($query);
break;

case 'INSERT':
if (false === ($this->insertWriter instanceof InsertWriter)) {
$this->insertWriter = WriterFactory::createInsertWriter($this, $this->placeholderWriter);
}

$sql = $this->insertWriter->writeInsert($query);
break;

case 'UPDATE':
if (false === ($this->updateWriter instanceof UpdateWriter)) {
$this->updateWriter = WriterFactory::createUpdateWriter($this, $this->placeholderWriter);
}

$sql = $this->updateWriter->writeUpdate($query);
break;

case 'DELETE':
if (false === ($this->deleteWriter instanceof DeleteWriter)) {
$this->deleteWriter = WriterFactory::createDeleteWriter($this, $this->placeholderWriter);
}

$sql = $this->deleteWriter->writeDelete($query);
break;

case 'INTERSECT':
if (false === ($this->intersectWriter instanceof IntersectWriter)) {
$this->intersectWriter = WriterFactory::createIntersectWriter($this);
}

$sql = $this->intersectWriter->writeIntersect($query);
break;

case 'MINUS':
if (false === ($this->minusWriter instanceof MinusWriter)) {
$this->minusWriter = WriterFactory::createMinusWriter($this);
}

$sql = $this->minusWriter->writeMinus($query);
break;

case 'UNION':
if (false === ($this->unionWriter instanceof UnionWriter)) {
$this->unionWriter = WriterFactory::createUnionWriter($this);
}
$sql = $this->unionWriter->writeUnion($query);
break;

case 'UNION ALL':
if (false === ($this->unionAllWriter instanceof UnionAllWriter)) {
$this->unionAllWriter = WriterFactory::createUnionAllWriter($this);
}
$sql = $this->unionAllWriter->writeUnionAll($query);
break;
}
@@ -244,6 +273,10 @@ public function writeFormatted(QueryInterface $query)
*/
public function writeJoin(Select $select)
{
if (false === ($this->whereWriter instanceof Delete)) {
$this->whereWriter = WriterFactory::createWhereWriter($this, $this->placeholderWriter);
}

$sql = ($select->getJoinType()) ? "{$select->getJoinType()} " : "";
$sql .= "JOIN ";
$sql .= $this->writeTableWithAlias($select->getTable());
Original file line number Diff line number Diff line change
@@ -68,7 +68,6 @@ public function writeSelectsAsColumns(Select $select)
$selectAsColumns = $select->getColumnSelects();

if (!empty($selectAsColumns)) {

$selectWriter = WriterFactory::createSelectWriter($this->writer, $this->placeholderWriter);

array_walk(
@@ -103,7 +102,6 @@ public function writeValueAsColumns(Select $select)
$newColumns = array();

if (!empty($valueAsColumns)) {

foreach ($valueAsColumns as $alias => $value) {
$value = $this->writer->writePlaceholderValue($value);
$newValueColumn = array($alias => $value);
@@ -126,9 +124,7 @@ public function writeFuncAsColumns(Select $select)
$newColumns = array();

if (!empty($funcAsColumns)) {

foreach ($funcAsColumns as $alias => $value) {

$funcName = $value['func'];
$funcArgs = (!empty($value['args'])) ? "(".implode(', ', $value['args']).")" : '';

@@ -153,5 +149,4 @@ public function writeColumnWithAlias(Column $column)

return $this->writeColumn($column);
}

}
Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@ public function __construct(GenericBuilder $writer, PlaceholderWriter $placehold
{
$this->writer = $writer;
$this->placeholderWriter = $placeholder;

}

/**
Original file line number Diff line number Diff line change
@@ -107,7 +107,6 @@ public function writeSelect(Select $select)
public function writeSelectColumns(Select $select)
{
if ($select->isCount() === false) {

$tableColumns = $select->getAllColumns();
$selectAsColumns = $this->columnWriter->writeSelectsAsColumns($select);
$valueAsColumns = $this->columnWriter->writeValueAsColumns($select);
@@ -123,9 +122,7 @@ function (&$column) {
);

$columnList = implode(", ", $columns);

} else {

$columns = $select->getColumns();
$column = array_pop($columns);
$columnList = $column->getName();
@@ -155,7 +152,6 @@ public function writeSelectJoins(Select $select)
$joins = $select->getAllJoins();

if (!empty($joins)) {

array_walk(
$joins,
function (&$join) {
@@ -182,7 +178,6 @@ public function writeSelectWhere(Select $select)
$wheres = array_filter($wheres);

if (count($wheres) > 0) {

$str = "WHERE ";
$separator = " ".$this->writer->writeConjunction($select->getWhereOperator())." ";

@@ -221,7 +216,6 @@ public function writeSelectGroupBy(Select $select)
{
$str = "";
if (count($select->getGroupBy())) {

$groupCols = $select->getGroupBy();

array_walk(
@@ -248,7 +242,6 @@ public function writeSelectHaving(Select $select)
$str = "";

if (count($havingArray = $select->getAllHavings()) > 0) {

$placeholder = $this->placeholderWriter;
$writer = $this->writer;

@@ -280,7 +273,6 @@ protected function writeSelectOrderBy(Select $select)
{
$str = "";
if (count($select->getAllOrderBy())) {

$orderByArray = $select->getAllOrderBy();
array_walk(
$orderByArray,
Original file line number Diff line number Diff line change
@@ -67,7 +67,6 @@ public function writeUpdate(Update $update)
);

if (!is_null($update->getWhere())) {

$whereWriter = WriterFactory::createWhereWriter($this->writer, $this->placeholderWriter);
$parts[] = " WHERE {$whereWriter->writeWhere($update->getWhere())}";
}
@@ -89,7 +88,6 @@ private function writeUpdateValues(Update $update)
{
$assigns = array();
foreach ($update->getValues() as $column => $value) {

$newColumn = array($column);
$column = $this->columnWriter->writeColumn(SyntaxFactory::createColumn($newColumn, $update->getTable()));

Original file line number Diff line number Diff line change
@@ -111,7 +111,6 @@ protected function writeWhereMatches(Where $where)
$matches = array();

foreach ($where->getMatches() as $values) {

$columns = SyntaxFactory::createColumns($values['columns'], $where->getTable());

$columnNames = array();
@@ -152,7 +151,6 @@ protected function writeWhereIns(Where $where)
$ins = array();

foreach ($where->getIns() as $column => $values) {

$newColumn = array($column);
$column = SyntaxFactory::createColumn($newColumn, $where->getTable());
$column = $this->columnWriter->writeColumn($column);
@@ -176,7 +174,6 @@ protected function writeWhereNotIns(Where $where)
$notIns = $where->getNotIns();

foreach ($notIns as $column => &$values) {

$newColumn = array($column);
$column = SyntaxFactory::createColumn($newColumn, $where->getTable());
$column = $this->columnWriter->writeColumn($column);
@@ -247,15 +244,11 @@ 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->writeSelect($subject).')';

} else {
$str = $this->writer->writePlaceholderValue($subject);

}

return $str;
1 change: 0 additions & 1 deletion src/NilPortugues/SqlQueryBuilder/Manipulation/Delete.php
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@
*/
class Delete extends BaseQuery
{

/**
* @var int
*/
Original file line number Diff line number Diff line change
@@ -15,5 +15,4 @@
*/
final class QueryException extends \Exception
{

}
9 changes: 3 additions & 6 deletions src/NilPortugues/SqlQueryBuilder/Manipulation/Select.php
Original file line number Diff line number Diff line change
@@ -168,15 +168,13 @@ public function join(
$key = $table->getCompleteName();

if (!isset($this->joins[$key])) {

$select = QueryFactory::createSelect($table);
$select->setColumns($columns);
$select->setJoinType($joinType);
$this->addJoin($select, $selfColumn, $refColumn);
}

return $this->joins[$key];

}

/**
@@ -206,7 +204,6 @@ public function addJoin(Select $select, $selfColumn, $refColumn)
$key = $select->getTable()->getCompleteName();

if (!isset($this->joins[$key])) {

$newColumn = array($selfColumn);
$select->joinCondition()->equals($refColumn, SyntaxFactory::createColumn($newColumn, $this->getTable()));
$this->joins[$key] = $select;
@@ -378,9 +375,9 @@ public function getColumnValues()
/**
* Allows calculation on columns using predefined SQL functions.
*
* @param string $funcName
* @param string $funcName
* @param string[] $arguments
* @param string $alias
* @param string $alias
*
* @return $this
*/
@@ -448,7 +445,7 @@ public function count($columnName = '*', $alias = '')
{
$count = 'COUNT(';
$count .= ($columnName !== '*') ? "$this->table.{$columnName}" : '*';
$count .=')';
$count .= ')';

if (isset($alias) && strlen($alias)>0) {
$count .= " AS '{$alias}'";
1 change: 0 additions & 1 deletion src/NilPortugues/SqlQueryBuilder/Syntax/SyntaxFactory.php
Original file line number Diff line number Diff line change
@@ -28,7 +28,6 @@ public static function createColumns(array &$arguments, $table = null)
$createdColumns = array();

foreach ($arguments as $index => $column) {

if (!is_object($column)) {
$newColumn = array($column);
$column = self::createColumn($newColumn, $table);
24 changes: 12 additions & 12 deletions src/NilPortugues/SqlQueryBuilder/Syntax/Where.php
Original file line number Diff line number Diff line change
@@ -363,7 +363,7 @@ public function match(array $columns, array $values)
}

/**
* @param string[] $columns
* @param string[] $columns
* @param integer[] $values
* @return $this
*/
@@ -379,7 +379,7 @@ public function matchBoolean(array $columns, array $values)
}

/**
* @param string[] $columns
* @param string[] $columns
* @param integer[] $values
* @return $this
*/
@@ -388,14 +388,14 @@ public function matchWithQueryExpansion(array $columns, array $values)
$this->match[] = array(
'columns' => $columns,
'values' => $values,
'mode' => 'query_expansion'
'mode' => 'query_expansion',
);

return $this;
}

/**
* @param string $column
* @param string $column
* @param integer[] $values
* @return $this
*/
@@ -407,7 +407,7 @@ public function in($column, array $values)
}

/**
* @param string $column
* @param string $column
* @param integer[] $values
* @return $this
*/
@@ -419,9 +419,9 @@ public function notIn($column, array $values)
}

/**
* @param string $column
* @param integer $a
* @param integer $b
* @param string $column
* @param integer $a
* @param integer $b
* @return $this
*/
public function between($column, $a, $b)
@@ -446,7 +446,7 @@ public function isNull($column)
}

/**
* @param string $column
* @param string $column
* @return $this
*/
public function isNotNull($column)
@@ -458,8 +458,8 @@ public function isNotNull($column)
}

/**
* @param string $column
* @param integer $value
* @param string $column
* @param integer $value
* @return $this
*/
public function addBitClause($column, $value)
@@ -479,7 +479,7 @@ public function exists(Select $select)
{
$this->exists[] = $select;

return $this;
return $this;
}

/**
Original file line number Diff line number Diff line change
@@ -30,7 +30,6 @@ class MySqlBuilderTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->writer = new MySqlBuilder();

}

/**
@@ -39,7 +38,6 @@ protected function setUp()
protected function tearDown()
{
$this->writer = null;

}

/**
Original file line number Diff line number Diff line change
@@ -102,7 +102,6 @@ public function it_should_write_column_with_alias()
*/
public function it_should_be_able_to_write_column_as_a_select_statement()
{

$selectRole = new Select();
$selectRole
->setTable('role')
@@ -147,7 +146,6 @@ public function it_should_be_able_to_write_column_as_a_value_statement()

$expected = array(':v1' => 10, ':v2' => 1);
$this->assertEquals($expected, $this->writer->getValues());

}

/**
Original file line number Diff line number Diff line change
@@ -87,6 +87,5 @@ public function it_should_write_delete_row_with_where_condition_and_limit_1()

$expected = array(':v1' => 10, ':v2' => 20, ':v3' => 30, ':v4' => 1);
$this->assertEquals($expected, $this->writer->getValues());

}
}
Original file line number Diff line number Diff line change
@@ -349,7 +349,6 @@ public function it_should_be_able_to_do_an_add_with_multiple_joins()
$this->query->setTable('user');

for ($i = 1; $i <= 5; $i++) {

//Select QueryInterface for "news" table
$select = new Select();
$select
Original file line number Diff line number Diff line change
@@ -61,7 +61,6 @@ public function it_should_allow_where_conditions()
*/
public function it_should_allow_where_or_conditions()
{

$this->query
->setTable('user')
->where('OR')
@@ -272,7 +271,6 @@ public function it_should_be_able_to_let_where_statement_write_between_condition

$expected = array(':v1' => 1, ':v2' => 2);
$this->assertEquals($expected, $this->writer->getValues());

}

/**
Original file line number Diff line number Diff line change
@@ -34,7 +34,6 @@ class DeleteTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{

$this->query = new Delete();
}

0 comments on commit 9a3d95b

Please sign in to comment.