Skip to content

Commit

Permalink
[12.x] Fix accessing Connection property in Grammar classes (#54487)
Browse files Browse the repository at this point in the history
* fix connection references

* formatting

* formatting

* formatting
  • Loading branch information
hafezdivandari authored Feb 12, 2025
1 parent aef71d6 commit f248edb
Show file tree
Hide file tree
Showing 21 changed files with 132 additions and 381 deletions.
21 changes: 1 addition & 20 deletions Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ public function useDefaultQueryGrammar()
*/
protected function getDefaultQueryGrammar()
{
($grammar = new QueryGrammar)->setConnection($this);

return $grammar;
return new QueryGrammar($this);
}

/**
Expand Down Expand Up @@ -1626,26 +1624,9 @@ public function setTablePrefix($prefix)
{
$this->tablePrefix = $prefix;

$this->getQueryGrammar()->setTablePrefix($prefix);

return $this;
}

/**
* Set the table prefix and return the grammar.
*
* @template TGrammar of \Illuminate\Database\Grammar
*
* @param TGrammar $grammar
* @return TGrammar
*/
public function withTablePrefix(Grammar $grammar)
{
$grammar->setTablePrefix($this->tablePrefix);

return $grammar;
}

/**
* Execute the given callback without table prefix.
*
Expand Down
48 changes: 21 additions & 27 deletions Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ abstract class Grammar
protected $connection;

/**
* The grammar table prefix.
* Create a new grammar instance.
*
* @var string
* @param \Illuminate\Database\Connection $connection
* @return void
*/
protected $tablePrefix = '';
public function __construct(Connection $connection)
{
$this->connection = $connection;
}

/**
* Wrap an array of values.
Expand All @@ -49,15 +53,15 @@ public function wrapTable($table, $prefix = null)
return $this->getValue($table);
}

$prefix ??= $this->connection->getTablePrefix();

// If the table being wrapped has an alias we'll need to separate the pieces
// so we can prefix the table and then wrap each of the segments on their
// own and then join these both back together using the "as" connector.
if (stripos($table, ' as ') !== false) {
return $this->wrapAliasedTable($table);
return $this->wrapAliasedTable($table, $prefix);
}

$prefix ??= $this->tablePrefix;

// If the table being wrapped has a custom schema name specified, we need to
// prefix the last segment as the table name then wrap each segment alone
// and eventually join them both back together using the dot connector.
Expand Down Expand Up @@ -118,13 +122,16 @@ protected function wrapAliasedValue($value)
* Wrap a table that has an alias.
*
* @param string $value
* @param string|null $prefix
* @return string
*/
protected function wrapAliasedTable($value)
protected function wrapAliasedTable($value, $prefix = null)
{
$segments = preg_split('/\s+as\s+/i', $value);

return $this->wrapTable($segments[0]).' as '.$this->wrapValue($this->tablePrefix.$segments[1]);
$prefix ??= $this->connection->getTablePrefix();

return $this->wrapTable($segments[0], $prefix).' as '.$this->wrapValue($prefix.$segments[1]);
}

/**
Expand Down Expand Up @@ -238,10 +245,6 @@ public function quoteString($value)
*/
public function escape($value, $binary = false)
{
if (is_null($this->connection)) {
throw new RuntimeException("The database driver's grammar implementation does not support escaping values.");
}

return $this->connection->escape($value, $binary);
}

Expand Down Expand Up @@ -284,35 +287,26 @@ public function getDateFormat()
/**
* Get the grammar's table prefix.
*
* @deprecated Use DB::getTablePrefix()
*
* @return string
*/
public function getTablePrefix()
{
return $this->tablePrefix;
return $this->connection->getTablePrefix();
}

/**
* Set the grammar's table prefix.
*
* @deprecated Use DB::setTablePrefix()
*
* @param string $prefix
* @return $this
*/
public function setTablePrefix($prefix)
{
$this->tablePrefix = $prefix;

return $this;
}

/**
* Set the grammar's database connection.
*
* @param \Illuminate\Database\Connection $connection
* @return $this
*/
public function setConnection($connection)
{
$this->connection = $connection;
$this->connection->setTablePrefix($prefix);

return $this;
}
Expand Down
8 changes: 2 additions & 6 deletions MariaDbConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public function getServerVersion(): string
*/
protected function getDefaultQueryGrammar()
{
($grammar = new QueryGrammar)->setConnection($this);

return $this->withTablePrefix($grammar);
return new QueryGrammar($this);
}

/**
Expand All @@ -73,9 +71,7 @@ public function getSchemaBuilder()
*/
protected function getDefaultSchemaGrammar()
{
($grammar = new SchemaGrammar)->setConnection($this);

return $this->withTablePrefix($grammar);
return new SchemaGrammar($this);
}

/**
Expand Down
8 changes: 2 additions & 6 deletions MySqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ public function getServerVersion(): string
*/
protected function getDefaultQueryGrammar()
{
($grammar = new QueryGrammar)->setConnection($this);

return $this->withTablePrefix($grammar);
return new QueryGrammar($this);
}

/**
Expand All @@ -147,9 +145,7 @@ public function getSchemaBuilder()
*/
protected function getDefaultSchemaGrammar()
{
($grammar = new SchemaGrammar)->setConnection($this);

return $this->withTablePrefix($grammar);
return new SchemaGrammar($this);
}

/**
Expand Down
8 changes: 2 additions & 6 deletions PostgresConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ protected function isUniqueConstraintError(Exception $exception)
*/
protected function getDefaultQueryGrammar()
{
($grammar = new QueryGrammar)->setConnection($this);

return $this->withTablePrefix($grammar);
return new QueryGrammar($this);
}

/**
Expand All @@ -88,9 +86,7 @@ public function getSchemaBuilder()
*/
protected function getDefaultSchemaGrammar()
{
($grammar = new SchemaGrammar)->setConnection($this);

return $this->withTablePrefix($grammar);
return new SchemaGrammar($this);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,12 @@ protected function compileDeleteWithJoinsOrLimit(Builder $query)
*/
public function compileTruncate(Builder $query)
{
[$schema, $table] = $this->connection->getSchemaBuilder()->parseSchemaAndTable($query->from);
[$schema, $table] = $query->getConnection()->getSchemaBuilder()->parseSchemaAndTable($query->from);

$schema = $schema ? $this->wrapValue($schema).'.' : '';

return [
'delete from '.$schema.'sqlite_sequence where name = ?' => [$this->getTablePrefix().$table],
'delete from '.$schema.'sqlite_sequence where name = ?' => [$query->getConnection()->getTablePrefix().$table],
'delete from '.$this->wrapTable($query->from) => [],
];
}
Expand Down
8 changes: 2 additions & 6 deletions SQLiteConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ protected function isUniqueConstraintError(Exception $exception)
*/
protected function getDefaultQueryGrammar()
{
($grammar = new QueryGrammar)->setConnection($this);

return $this->withTablePrefix($grammar);
return new QueryGrammar($this);
}

/**
Expand All @@ -188,9 +186,7 @@ public function getSchemaBuilder()
*/
protected function getDefaultSchemaGrammar()
{
($grammar = new SchemaGrammar)->setConnection($this);

return $this->withTablePrefix($grammar);
return new SchemaGrammar($this);
}

/**
Expand Down
32 changes: 14 additions & 18 deletions Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ class Blueprint
*/
protected $table;

/**
* The prefix of the table.
*
* @var string
*/
protected $prefix;

/**
* The columns that should be added to the table.
*
Expand Down Expand Up @@ -103,15 +96,13 @@ class Blueprint
* @param \Illuminate\Database\Connection $connection
* @param string $table
* @param \Closure|null $callback
* @param string $prefix
* @return void
*/
public function __construct(Connection $connection, $table, ?Closure $callback = null, $prefix = '')
public function __construct(Connection $connection, $table, ?Closure $callback = null)
{
$this->connection = $connection;
$this->grammar = $connection->getSchemaGrammar();
$this->table = $table;
$this->prefix = $prefix;

if (! is_null($callback)) {
$callback($this);
Expand Down Expand Up @@ -158,7 +149,7 @@ public function toSql()
$this->state->update($command);
}

if (! is_null($sql = $this->grammar->$method($this, $command, $this->connection))) {
if (! is_null($sql = $this->grammar->$method($this, $command))) {
$statements = array_merge($statements, (array) $sql);
}
}
Expand Down Expand Up @@ -290,7 +281,7 @@ public function addAlterCommands()
return;
}

$alterCommands = $this->grammar->getAlterCommands($this->connection);
$alterCommands = $this->grammar->getAlterCommands();

[$commands, $lastCommandWasAlter, $hasAlterCommand] = [
[], false, false,
Expand All @@ -313,7 +304,7 @@ public function addAlterCommands()
}

if ($hasAlterCommand) {
$this->state = new BlueprintState($this, $this->connection, $this->grammar);
$this->state = new BlueprintState($this, $this->connection);
}

$this->commands = $commands;
Expand Down Expand Up @@ -1703,9 +1694,13 @@ protected function dropIndexCommand($command, $type, $index)
*/
protected function createIndexName($type, array $columns)
{
$table = str_contains($this->table, '.')
? substr_replace($this->table, '.'.$this->prefix, strrpos($this->table, '.'), 1)
: $this->prefix.$this->table;
$table = $this->table;

if ($this->connection->getConfig('prefix_indexes')) {
$table = str_contains($this->table, '.')
? substr_replace($this->table, '.'.$this->connection->getTablePrefix(), strrpos($this->table, '.'), 1)
: $this->connection->getTablePrefix().$this->table;
}

$index = strtolower($table.'_'.implode('_', $columns).'_'.$type);

Expand Down Expand Up @@ -1824,11 +1819,13 @@ public function getTable()
/**
* Get the table prefix.
*
* @deprecated Use DB::getTablePrefix()
*
* @return string
*/
public function getPrefix()
{
return $this->prefix;
return $this->connection->getTablePrefix();
}

/**
Expand All @@ -1854,7 +1851,6 @@ public function getCommands()
/**
* Determine if the blueprint has state.
*
* @param mixed $name
* @return bool
*/
private function hasState(): bool
Expand Down
12 changes: 1 addition & 11 deletions Schema/BlueprintState.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Database\Connection;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Grammars\Grammar;
use Illuminate\Support\Collection;
use Illuminate\Support\Fluent;
use Illuminate\Support\Str;
Expand All @@ -25,13 +24,6 @@ class BlueprintState
*/
protected $connection;

/**
* The grammar instance.
*
* @var \Illuminate\Database\Schema\Grammars\Grammar
*/
protected $grammar;

/**
* The columns.
*
Expand Down Expand Up @@ -65,14 +57,12 @@ class BlueprintState
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Database\Connection $connection
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
* @return void
*/
public function __construct(Blueprint $blueprint, Connection $connection, Grammar $grammar)
public function __construct(Blueprint $blueprint, Connection $connection)
{
$this->blueprint = $blueprint;
$this->connection = $connection;
$this->grammar = $grammar;

$schema = $connection->getSchemaBuilder();
$table = $blueprint->getTable();
Expand Down
Loading

0 comments on commit f248edb

Please sign in to comment.