Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions src/Queries/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
use Puzzle\QueryBuilder\Traits\EscaperAware;
use Puzzle\QueryBuilder\Snippet;
use Puzzle\QueryBuilder\Queries\Snippets\Builders;
use Puzzle\QueryBuilder\QueryPartAware;

class Delete implements Query
class Delete implements Query, QueryPartAware
{
use
EscaperAware,
Builders\Join,
Builders\Where,
Builders\OrderBy,
Builders\Limit;
Builders\Limit,
Builders\QueryPart;

private
$from;
Expand All @@ -37,6 +39,10 @@ public function __construct($table = null, ?string $alias = null)

public function toString(): string
{
$snippets = $this->joins;
$snippets[] = $this->from;
$this->ensureNeededTablesArePresent($snippets);

$queryParts = array(
'DELETE',
$this->buildFrom(),
Expand Down
10 changes: 8 additions & 2 deletions src/Queries/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
use Puzzle\QueryBuilder\Snippet;
use Puzzle\QueryBuilder\Queries\Snippets\Builders;
use Puzzle\QueryBuilder\Queries\Snippets\Having;
use Puzzle\QueryBuilder\QueryPartAware;

class Select implements Query
class Select implements Query, QueryPartAware
{
use
EscaperAware,
Builders\Join,
Builders\Where,
Builders\GroupBy,
Builders\OrderBy,
Builders\Limit;
Builders\Limit,
Builders\QueryPart;

private
$select,
Expand All @@ -42,6 +44,10 @@ public function __construct($columns = [])

public function toString(): string
{
$snippets = $this->joins;
$snippets[] = $this->from;
$this->ensureNeededTablesArePresent($snippets);

$queryParts = array(
$this->buildSelect(),
$this->buildFrom(),
Expand Down
60 changes: 60 additions & 0 deletions src/Queries/Snippets/Builders/QueryPart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types = 1);

namespace Puzzle\QueryBuilder\Queries\Snippets\Builders;

use Puzzle\QueryBuilder\Queries\Snippets\NeedTableAware;
use Puzzle\QueryBuilder\Query;

trait QueryPart
{
private
$neededTableNames = [];

public function add(\Puzzle\QueryBuilder\QueryPart $queryPart): Query
{
$queryPart->build($this);

return $this;
}

public function needTable($tableName): Query
{
if(! in_array($tableName, $this->neededTableNames))
{
$this->neededTableNames[] = $tableName;
}

return $this;
}

public function ensureNeededTablesArePresent(array $snippets): void
{
foreach($this->neededTableNames as $tableName)
{
if(! $this->isAtLeastOneSnippetHasNeededTable($tableName, $snippets))
{
throw new \LogicException("One of query parts you used needs $tableName table");
}
}
}

private function isAtLeastOneSnippetHasNeededTable($tableName, array $snippets): bool
{
foreach($snippets as $snippet)
{
if(! $snippet instanceof NeedTableAware)
{
throw new \LogicException('Snippet has not expected NeedTableAware type');
}

if($snippet->hasNeededTable($tableName))
{
return true;
}
}

return false;
}
}
12 changes: 11 additions & 1 deletion src/Queries/Snippets/From.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Puzzle\QueryBuilder\Snippet;

class From implements Snippet
class From implements Snippet, NeedTableAware
{
private
$tableName;
Expand All @@ -28,4 +28,14 @@ public function toString(): string
{
return sprintf('FROM %s', $this->tableName->toString());
}

public function hasNeededTable(string $tableName): bool
{
if($this->tableName->getName() === $tableName || $this->tableName->getAlias() === $tableName)
{
return true;
}

return false;
}
}
12 changes: 11 additions & 1 deletion src/Queries/Snippets/Joins/AbstractJoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use Puzzle\QueryBuilder\Snippet;
use Puzzle\QueryBuilder\Queries\Snippets\Join;
use Puzzle\QueryBuilder\Queries\Snippets;
use Puzzle\QueryBuilder\Queries\Snippets\NeedTableAware;

abstract class AbstractJoin implements Join, Snippet
abstract class AbstractJoin implements Join, Snippet, NeedTableAware
{
private
$table,
Expand Down Expand Up @@ -55,6 +56,15 @@ public function toString(): string
return $joinQueryPart;
}

public function hasNeededTable(string $tableName): bool
{
if($this->table->getName() === $tableName || $this->table->getAlias() === $tableName)
{
return true;
}
return false;
}

abstract protected function getJoinDeclaration(): string;

private function buildUsingConditionClause(): string
Expand Down
10 changes: 10 additions & 0 deletions src/Queries/Snippets/NeedTableAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types = 1);

namespace Puzzle\QueryBuilder\Queries\Snippets;

interface NeedTableAware
{
public function hasNeededTable(string $tableName): bool;
}
10 changes: 10 additions & 0 deletions src/Queries/Snippets/TableName.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ public function toString(): string

return sprintf('%s AS %s', $this->tableName, $this->alias);
}

public function getName(): string
{
return $this->tableName;
}

public function getAlias(): string
{
return $this->alias;
}
}
15 changes: 14 additions & 1 deletion src/Queries/Snippets/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Puzzle\QueryBuilder\Snippet;

class Update implements Snippet
class Update implements Snippet, NeedTableAware
{
private
$tables;
Expand Down Expand Up @@ -51,4 +51,17 @@ public function toString(): string

return sprintf('UPDATE %s', $tablesString);
}

public function hasNeededTable(string $tableName): bool
{
foreach($this->tables as $table)
{
if($table->getName() === $tableName || $table->getAlias() === $tableName)
{
return true;
}
}

return false;
}
}
10 changes: 8 additions & 2 deletions src/Queries/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
use Puzzle\QueryBuilder\Query;
use Puzzle\QueryBuilder\Traits\EscaperAware;
use Puzzle\QueryBuilder\Queries\Snippets\Builders;
use Puzzle\QueryBuilder\QueryPartAware;

class Update implements Query
class Update implements Query, QueryPartAware
{
use
EscaperAware,
Builders\Join,
Builders\Where,
Builders\OrderBy,
Builders\Limit;
Builders\Limit,
Builders\QueryPart;

private
$updatePart,
Expand All @@ -39,6 +41,10 @@ public function __construct($table = null, ?string $alias = null)

public function toString(): string
{
$snippets = $this->joins;
$snippets[] = $this->updatePart;
$this->ensureNeededTablesArePresent($snippets);

$queryParts = array(
$this->buildUpdate(),
$this->buildJoin(),
Expand Down
10 changes: 10 additions & 0 deletions src/QueryPart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types = 1);

namespace Puzzle\QueryBuilder;

interface QueryPart
{
public function build(QueryPartAware $query): void;
}
8 changes: 8 additions & 0 deletions src/QueryPartAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Puzzle\QueryBuilder;

interface QueryPartAware
{
public function add(QueryPart $queryPart): Query;
}
Loading