Skip to content

Commit c78fa3d

Browse files
[12.x] Fix accessing Connection property in Grammar classes (#54487)
* fix connection references * formatting * formatting * formatting
1 parent a4864ff commit c78fa3d

File tree

49 files changed

+479
-718
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+479
-718
lines changed

config/database.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
'url' => env('DB_URL'),
3737
'database' => env('DB_DATABASE', database_path('database.sqlite')),
3838
'prefix' => '',
39+
'prefix_indexes' => null,
3940
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
4041
'busy_timeout' => null,
4142
'journal_mode' => null,

src/Illuminate/Database/Connection.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,7 @@ public function useDefaultQueryGrammar()
248248
*/
249249
protected function getDefaultQueryGrammar()
250250
{
251-
($grammar = new QueryGrammar)->setConnection($this);
252-
253-
return $grammar;
251+
return new QueryGrammar($this);
254252
}
255253

256254
/**
@@ -1626,26 +1624,9 @@ public function setTablePrefix($prefix)
16261624
{
16271625
$this->tablePrefix = $prefix;
16281626

1629-
$this->getQueryGrammar()->setTablePrefix($prefix);
1630-
16311627
return $this;
16321628
}
16331629

1634-
/**
1635-
* Set the table prefix and return the grammar.
1636-
*
1637-
* @template TGrammar of \Illuminate\Database\Grammar
1638-
*
1639-
* @param TGrammar $grammar
1640-
* @return TGrammar
1641-
*/
1642-
public function withTablePrefix(Grammar $grammar)
1643-
{
1644-
$grammar->setTablePrefix($this->tablePrefix);
1645-
1646-
return $grammar;
1647-
}
1648-
16491630
/**
16501631
* Execute the given callback without table prefix.
16511632
*

src/Illuminate/Database/Grammar.php

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ abstract class Grammar
1919
protected $connection;
2020

2121
/**
22-
* The grammar table prefix.
22+
* Create a new grammar instance.
2323
*
24-
* @var string
24+
* @param \Illuminate\Database\Connection $connection
25+
* @return void
2526
*/
26-
protected $tablePrefix = '';
27+
public function __construct(Connection $connection)
28+
{
29+
$this->connection = $connection;
30+
}
2731

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

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

59-
$prefix ??= $this->tablePrefix;
60-
6165
// If the table being wrapped has a custom schema name specified, we need to
6266
// prefix the last segment as the table name then wrap each segment alone
6367
// and eventually join them both back together using the dot connector.
@@ -118,13 +122,16 @@ protected function wrapAliasedValue($value)
118122
* Wrap a table that has an alias.
119123
*
120124
* @param string $value
125+
* @param string|null $prefix
121126
* @return string
122127
*/
123-
protected function wrapAliasedTable($value)
128+
protected function wrapAliasedTable($value, $prefix = null)
124129
{
125130
$segments = preg_split('/\s+as\s+/i', $value);
126131

127-
return $this->wrapTable($segments[0]).' as '.$this->wrapValue($this->tablePrefix.$segments[1]);
132+
$prefix ??= $this->connection->getTablePrefix();
133+
134+
return $this->wrapTable($segments[0], $prefix).' as '.$this->wrapValue($prefix.$segments[1]);
128135
}
129136

130137
/**
@@ -238,10 +245,6 @@ public function quoteString($value)
238245
*/
239246
public function escape($value, $binary = false)
240247
{
241-
if (is_null($this->connection)) {
242-
throw new RuntimeException("The database driver's grammar implementation does not support escaping values.");
243-
}
244-
245248
return $this->connection->escape($value, $binary);
246249
}
247250

@@ -284,35 +287,26 @@ public function getDateFormat()
284287
/**
285288
* Get the grammar's table prefix.
286289
*
290+
* @deprecated Use DB::getTablePrefix()
291+
*
287292
* @return string
288293
*/
289294
public function getTablePrefix()
290295
{
291-
return $this->tablePrefix;
296+
return $this->connection->getTablePrefix();
292297
}
293298

294299
/**
295300
* Set the grammar's table prefix.
296301
*
302+
* @deprecated Use DB::setTablePrefix()
303+
*
297304
* @param string $prefix
298305
* @return $this
299306
*/
300307
public function setTablePrefix($prefix)
301308
{
302-
$this->tablePrefix = $prefix;
303-
304-
return $this;
305-
}
306-
307-
/**
308-
* Set the grammar's database connection.
309-
*
310-
* @param \Illuminate\Database\Connection $connection
311-
* @return $this
312-
*/
313-
public function setConnection($connection)
314-
{
315-
$this->connection = $connection;
309+
$this->connection->setTablePrefix($prefix);
316310

317311
return $this;
318312
}

src/Illuminate/Database/MariaDbConnection.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public function getServerVersion(): string
4747
*/
4848
protected function getDefaultQueryGrammar()
4949
{
50-
($grammar = new QueryGrammar)->setConnection($this);
51-
52-
return $this->withTablePrefix($grammar);
50+
return new QueryGrammar($this);
5351
}
5452

5553
/**
@@ -73,9 +71,7 @@ public function getSchemaBuilder()
7371
*/
7472
protected function getDefaultSchemaGrammar()
7573
{
76-
($grammar = new SchemaGrammar)->setConnection($this);
77-
78-
return $this->withTablePrefix($grammar);
74+
return new SchemaGrammar($this);
7975
}
8076

8177
/**

src/Illuminate/Database/MySqlConnection.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ public function getServerVersion(): string
121121
*/
122122
protected function getDefaultQueryGrammar()
123123
{
124-
($grammar = new QueryGrammar)->setConnection($this);
125-
126-
return $this->withTablePrefix($grammar);
124+
return new QueryGrammar($this);
127125
}
128126

129127
/**
@@ -147,9 +145,7 @@ public function getSchemaBuilder()
147145
*/
148146
protected function getDefaultSchemaGrammar()
149147
{
150-
($grammar = new SchemaGrammar)->setConnection($this);
151-
152-
return $this->withTablePrefix($grammar);
148+
return new SchemaGrammar($this);
153149
}
154150

155151
/**

src/Illuminate/Database/PostgresConnection.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ protected function isUniqueConstraintError(Exception $exception)
6262
*/
6363
protected function getDefaultQueryGrammar()
6464
{
65-
($grammar = new QueryGrammar)->setConnection($this);
66-
67-
return $this->withTablePrefix($grammar);
65+
return new QueryGrammar($this);
6866
}
6967

7068
/**
@@ -88,9 +86,7 @@ public function getSchemaBuilder()
8886
*/
8987
protected function getDefaultSchemaGrammar()
9088
{
91-
($grammar = new SchemaGrammar)->setConnection($this);
92-
93-
return $this->withTablePrefix($grammar);
89+
return new SchemaGrammar($this);
9490
}
9591

9692
/**

src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,12 @@ protected function compileDeleteWithJoinsOrLimit(Builder $query)
439439
*/
440440
public function compileTruncate(Builder $query)
441441
{
442-
[$schema, $table] = $this->connection->getSchemaBuilder()->parseSchemaAndTable($query->from);
442+
[$schema, $table] = $query->getConnection()->getSchemaBuilder()->parseSchemaAndTable($query->from);
443443

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

446446
return [
447-
'delete from '.$schema.'sqlite_sequence where name = ?' => [$this->getTablePrefix().$table],
447+
'delete from '.$schema.'sqlite_sequence where name = ?' => [$query->getConnection()->getTablePrefix().$table],
448448
'delete from '.$this->wrapTable($query->from) => [],
449449
];
450450
}

src/Illuminate/Database/SQLiteConnection.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ protected function isUniqueConstraintError(Exception $exception)
162162
*/
163163
protected function getDefaultQueryGrammar()
164164
{
165-
($grammar = new QueryGrammar)->setConnection($this);
166-
167-
return $this->withTablePrefix($grammar);
165+
return new QueryGrammar($this);
168166
}
169167

170168
/**
@@ -188,9 +186,7 @@ public function getSchemaBuilder()
188186
*/
189187
protected function getDefaultSchemaGrammar()
190188
{
191-
($grammar = new SchemaGrammar)->setConnection($this);
192-
193-
return $this->withTablePrefix($grammar);
189+
return new SchemaGrammar($this);
194190
}
195191

196192
/**

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ class Blueprint
3434
*/
3535
protected $table;
3636

37-
/**
38-
* The prefix of the table.
39-
*
40-
* @var string
41-
*/
42-
protected $prefix;
43-
4437
/**
4538
* The columns that should be added to the table.
4639
*
@@ -103,15 +96,13 @@ class Blueprint
10396
* @param \Illuminate\Database\Connection $connection
10497
* @param string $table
10598
* @param \Closure|null $callback
106-
* @param string $prefix
10799
* @return void
108100
*/
109-
public function __construct(Connection $connection, $table, ?Closure $callback = null, $prefix = '')
101+
public function __construct(Connection $connection, $table, ?Closure $callback = null)
110102
{
111103
$this->connection = $connection;
112104
$this->grammar = $connection->getSchemaGrammar();
113105
$this->table = $table;
114-
$this->prefix = $prefix;
115106

116107
if (! is_null($callback)) {
117108
$callback($this);
@@ -158,7 +149,7 @@ public function toSql()
158149
$this->state->update($command);
159150
}
160151

161-
if (! is_null($sql = $this->grammar->$method($this, $command, $this->connection))) {
152+
if (! is_null($sql = $this->grammar->$method($this, $command))) {
162153
$statements = array_merge($statements, (array) $sql);
163154
}
164155
}
@@ -290,7 +281,7 @@ public function addAlterCommands()
290281
return;
291282
}
292283

293-
$alterCommands = $this->grammar->getAlterCommands($this->connection);
284+
$alterCommands = $this->grammar->getAlterCommands();
294285

295286
[$commands, $lastCommandWasAlter, $hasAlterCommand] = [
296287
[], false, false,
@@ -313,7 +304,7 @@ public function addAlterCommands()
313304
}
314305

315306
if ($hasAlterCommand) {
316-
$this->state = new BlueprintState($this, $this->connection, $this->grammar);
307+
$this->state = new BlueprintState($this, $this->connection);
317308
}
318309

319310
$this->commands = $commands;
@@ -1703,9 +1694,13 @@ protected function dropIndexCommand($command, $type, $index)
17031694
*/
17041695
protected function createIndexName($type, array $columns)
17051696
{
1706-
$table = str_contains($this->table, '.')
1707-
? substr_replace($this->table, '.'.$this->prefix, strrpos($this->table, '.'), 1)
1708-
: $this->prefix.$this->table;
1697+
$table = $this->table;
1698+
1699+
if ($this->connection->getConfig('prefix_indexes')) {
1700+
$table = str_contains($this->table, '.')
1701+
? substr_replace($this->table, '.'.$this->connection->getTablePrefix(), strrpos($this->table, '.'), 1)
1702+
: $this->connection->getTablePrefix().$this->table;
1703+
}
17091704

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

@@ -1824,11 +1819,13 @@ public function getTable()
18241819
/**
18251820
* Get the table prefix.
18261821
*
1822+
* @deprecated Use DB::getTablePrefix()
1823+
*
18271824
* @return string
18281825
*/
18291826
public function getPrefix()
18301827
{
1831-
return $this->prefix;
1828+
return $this->connection->getTablePrefix();
18321829
}
18331830

18341831
/**
@@ -1854,7 +1851,6 @@ public function getCommands()
18541851
/**
18551852
* Determine if the blueprint has state.
18561853
*
1857-
* @param mixed $name
18581854
* @return bool
18591855
*/
18601856
private function hasState(): bool

src/Illuminate/Database/Schema/BlueprintState.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Database\Connection;
66
use Illuminate\Database\Query\Expression;
7-
use Illuminate\Database\Schema\Grammars\Grammar;
87
use Illuminate\Support\Collection;
98
use Illuminate\Support\Fluent;
109
use Illuminate\Support\Str;
@@ -25,13 +24,6 @@ class BlueprintState
2524
*/
2625
protected $connection;
2726

28-
/**
29-
* The grammar instance.
30-
*
31-
* @var \Illuminate\Database\Schema\Grammars\Grammar
32-
*/
33-
protected $grammar;
34-
3527
/**
3628
* The columns.
3729
*
@@ -65,14 +57,12 @@ class BlueprintState
6557
*
6658
* @param \Illuminate\Database\Schema\Blueprint $blueprint
6759
* @param \Illuminate\Database\Connection $connection
68-
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
6960
* @return void
7061
*/
71-
public function __construct(Blueprint $blueprint, Connection $connection, Grammar $grammar)
62+
public function __construct(Blueprint $blueprint, Connection $connection)
7263
{
7364
$this->blueprint = $blueprint;
7465
$this->connection = $connection;
75-
$this->grammar = $grammar;
7666

7767
$schema = $connection->getSchemaBuilder();
7868
$table = $blueprint->getTable();

0 commit comments

Comments
 (0)