Skip to content

Commit

Permalink
Refactro common code into Element::hasValidSqlCache() protected method
Browse files Browse the repository at this point in the history
Signed-off-by: pine3ree <pine3ree@gmail.com>
  • Loading branch information
pine3ree committed Oct 13, 2023
1 parent 5dfd138 commit 64c8a88
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Sql/Clause/ConditionalClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getSQL(DriverInterface $driver = null, Params $params = null): s
return $this->sql = '';
}

if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down
9 changes: 6 additions & 3 deletions src/Sql/Clause/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,19 @@ public function getSpecification()

public function getSQL(DriverInterface $driver = null, Params $params = null): string
{
if (isset($this->sql) && $driver === $this->driver && $params === null) {
if (isset($this->sql)
&& $driver === $this->driver
&& $params === null
&& isset($this->params)
) {
return $this->sql;
}

$this->driver = $driver; // Set last used driver argument
$this->params = null; // Reset previously collected params, if any

$driver = $driver ?? Driver::ansi();
$params = $params ?? ($this->params = new Params());

$table = $driver->quoteIdentifier($this->table);
if (!empty($this->alias)) {
Expand All @@ -156,8 +161,6 @@ public function getSQL(DriverInterface $driver = null, Params $params = null): s
return $this->sql = "{$join} {$table}";
}

$params = $params ?? ($this->params = new Params());

$specification_sql = '';
if ($this->specification instanceof Literal) {
$specification_sql = $this->specification->getSQL();
Expand Down
9 changes: 9 additions & 0 deletions src/Sql/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@ protected function clearSQL(): void
}
}

protected function hasValidSqlCache(?Driver $driver, ?Params $params): bool
{
return (isset($this->sql)
&& isset($this->params)
&& $this->driver === $driver
&& $params === null
);
}

/**
* Detach the clone from the orginal element's parent, clear the internal
* sql cache, if any, and emove any previously collected params.
Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Predicate/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected static function assertValidLimit($value): void

public function getSQL(DriverInterface $driver = null, Params $params = null): string
{
if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Predicate/CompareTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static function assertValidComparisonOperator(string $operator): void

public function getSQL(DriverInterface $driver = null, Params $params = null): string
{
if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Predicate/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected static function assertValidComparisonValue($value): void

public function getSQL(DriverInterface $driver = null, Params $params = null): string
{
if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Predicate/Exists.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(Select $select)

public function getSQL(DriverInterface $driver = null, Params $params = null): string
{
if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Predicate/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function assertValidSubstitution(string $name, $value): void

public function getSQL(DriverInterface $driver = null, Params $params = null): string
{
if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Predicate/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected static function assertValidValueList($valueList)
*/
public function getSQL(DriverInterface $driver = null, Params $params = null): string
{
if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Predicate/Is.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected static function createInvalidArgumentException($value)

public function getSQL(DriverInterface $driver = null, Params $params = null): string
{
if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Predicate/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected static function assertValidEscapeCharacter(string $escape): void

public function getSQL(DriverInterface $driver = null, Params $params = null): string
{
if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Predicate/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ public function getSQL(DriverInterface $driver = null, Params $params = null): s
return $this->sql = '';
}

if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Statement/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function from(string $table): self

public function getSQL(DriverInterface $driver = null, Params $params = null): string
{
if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Sql/Statement/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public function select(Select $select): self

public function getSQL(DriverInterface $driver = null, Params $params = null): string
{
if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand All @@ -329,7 +329,7 @@ public function getSQL(DriverInterface $driver = null, Params $params = null): s
return $this->sql = $driver->getInsertSQL($this, $params);
}

return $this->generateSQL($driver, $params);
return $this->sql = $this->generateSQL($driver, $params);
}

protected function generateSQL(DriverInterface $driver, Params $params): string
Expand All @@ -351,10 +351,10 @@ protected function generateSQL(DriverInterface $driver, Params $params): string
$column_list = empty($columns) ? "" : "{$columns} ";

if ($this->select instanceof Select) {
return $this->sql = "{$insert} " . Sql::INTO . " {$table} {$column_list}{$values}";
return "{$insert} " . Sql::INTO . " {$table} {$column_list}{$values}";
}

return $this->sql = "{$insert} " . Sql::INTO . " {$table} {$column_list}" . Sql::VALUES . " {$values}";
return "{$insert} " . Sql::INTO . " {$table} {$column_list}" . Sql::VALUES . " {$values}";
}

private function getColumnsSQL(DriverInterface $driver): string
Expand Down
16 changes: 9 additions & 7 deletions src/Sql/Statement/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,12 @@ public function aggregate(string $sqlAggregateFunc, string $identifier, string $

private function getColumnsSQL(DriverInterface $driver, Params $params): string
{
if (isset($this->sqls['columns']) && $this->driver_unchanged) {

if (isset($this->sqls['columns'])
&& $driver === $this->driver
&& $params === null
&& isset($this->params)
) {
return $this->sqls['columns'];
}

Expand Down Expand Up @@ -774,7 +779,7 @@ private function getGroupBySQL(DriverInterface $driver): string
}

// Partial caching is possibile as there are no params to import here
if (isset($this->sqls['group']) && $this->driver_unchanged) {
if (isset($this->sqls['group']) && $driver === $this->driver) {
return $this->sqls['group'];
}

Expand Down Expand Up @@ -875,7 +880,7 @@ private function getOrderBySQL(DriverInterface $driver): string
}

// Partial caching is possibile as there are no params to import here
if (isset($this->sqls['order']) && $this->driver_unchanged) {
if (isset($this->sqls['order']) && $driver === $this->driver) {
return $this->sqls['order'];
}

Expand Down Expand Up @@ -1044,10 +1049,7 @@ private function getUnionOrIntersectSQL(DriverInterface $driver, Params $params,
*/
public function getSQL(DriverInterface $driver = null, Params $params = null, bool $pretty = false): string
{
if (isset($this->sql)
&& $this->driver_unchanged = $driver === $this->driver
&& $params === null
) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Statement/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function set($column_or_set, $value = null): self

public function getSQL(DriverInterface $driver = null, Params $params = null): string
{
if (isset($this->sql) && $driver === $this->driver && $params === null) {
if ($this->hasValidSqlCache($driver, $params)) {
return $this->sql;
}

Expand Down

0 comments on commit 64c8a88

Please sign in to comment.