Skip to content

[12.x] Fix accessing Connection property in Grammar classes #54487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
1 change: 1 addition & 0 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
'url' => env('DB_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'prefix_indexes' => null,
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
'busy_timeout' => null,
'journal_mode' => null,
Expand Down
21 changes: 1 addition & 20 deletions src/Illuminate/Database/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 src/Illuminate/Database/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 src/Illuminate/Database/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 src/Illuminate/Database/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 src/Illuminate/Database/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 src/Illuminate/Database/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 src/Illuminate/Database/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 src/Illuminate/Database/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 src/Illuminate/Database/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