| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Database\Schema\Grammars;
|
|
|
4 |
|
|
|
5 |
use Doctrine\DBAL\Schema\AbstractSchemaManager as SchemaManager;
|
|
|
6 |
use Doctrine\DBAL\Schema\TableDiff;
|
|
|
7 |
use Illuminate\Database\Concerns\CompilesJsonPaths;
|
|
|
8 |
use Illuminate\Database\Connection;
|
|
|
9 |
use Illuminate\Database\Grammar as BaseGrammar;
|
|
|
10 |
use Illuminate\Database\Query\Expression;
|
|
|
11 |
use Illuminate\Database\Schema\Blueprint;
|
|
|
12 |
use Illuminate\Support\Fluent;
|
|
|
13 |
use LogicException;
|
|
|
14 |
use RuntimeException;
|
|
|
15 |
|
|
|
16 |
abstract class Grammar extends BaseGrammar
|
|
|
17 |
{
|
|
|
18 |
use CompilesJsonPaths;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* If this Grammar supports schema changes wrapped in a transaction.
|
|
|
22 |
*
|
|
|
23 |
* @var bool
|
|
|
24 |
*/
|
|
|
25 |
protected $transactions = false;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* The commands to be executed outside of create or alter command.
|
|
|
29 |
*
|
|
|
30 |
* @var array
|
|
|
31 |
*/
|
|
|
32 |
protected $fluentCommands = [];
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Compile a create database command.
|
|
|
36 |
*
|
|
|
37 |
* @param string $name
|
|
|
38 |
* @param \Illuminate\Database\Connection $connection
|
|
|
39 |
* @return void
|
|
|
40 |
*
|
|
|
41 |
* @throws \LogicException
|
|
|
42 |
*/
|
|
|
43 |
public function compileCreateDatabase($name, $connection)
|
|
|
44 |
{
|
|
|
45 |
throw new LogicException('This database driver does not support creating databases.');
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Compile a drop database if exists command.
|
|
|
50 |
*
|
|
|
51 |
* @param string $name
|
|
|
52 |
* @return void
|
|
|
53 |
*
|
|
|
54 |
* @throws \LogicException
|
|
|
55 |
*/
|
|
|
56 |
public function compileDropDatabaseIfExists($name)
|
|
|
57 |
{
|
|
|
58 |
throw new LogicException('This database driver does not support dropping databases.');
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Compile a rename column command.
|
|
|
63 |
*
|
|
|
64 |
* @param \Illuminate\Database\Schema\Blueprint $blueprint
|
|
|
65 |
* @param \Illuminate\Support\Fluent $command
|
|
|
66 |
* @param \Illuminate\Database\Connection $connection
|
|
|
67 |
* @return array|string
|
|
|
68 |
*/
|
|
|
69 |
public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection)
|
|
|
70 |
{
|
|
|
71 |
return RenameColumn::compile($this, $blueprint, $command, $connection);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Compile a change column command into a series of SQL statements.
|
|
|
76 |
*
|
|
|
77 |
* @param \Illuminate\Database\Schema\Blueprint $blueprint
|
|
|
78 |
* @param \Illuminate\Support\Fluent $command
|
|
|
79 |
* @param \Illuminate\Database\Connection $connection
|
|
|
80 |
* @return array
|
|
|
81 |
*
|
|
|
82 |
* @throws \RuntimeException
|
|
|
83 |
*/
|
|
|
84 |
public function compileChange(Blueprint $blueprint, Fluent $command, Connection $connection)
|
|
|
85 |
{
|
|
|
86 |
return ChangeColumn::compile($this, $blueprint, $command, $connection);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Compile a fulltext index key command.
|
|
|
91 |
*
|
|
|
92 |
* @param \Illuminate\Database\Schema\Blueprint $blueprint
|
|
|
93 |
* @param \Illuminate\Support\Fluent $command
|
|
|
94 |
* @return string
|
|
|
95 |
*
|
|
|
96 |
* @throws \RuntimeException
|
|
|
97 |
*/
|
|
|
98 |
public function compileFulltext(Blueprint $blueprint, Fluent $command)
|
|
|
99 |
{
|
|
|
100 |
throw new RuntimeException('This database driver does not support fulltext index creation.');
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Compile a drop fulltext index command.
|
|
|
105 |
*
|
|
|
106 |
* @param \Illuminate\Database\Schema\Blueprint $blueprint
|
|
|
107 |
* @param \Illuminate\Support\Fluent $command
|
|
|
108 |
* @return string
|
|
|
109 |
*
|
|
|
110 |
* @throws \RuntimeException
|
|
|
111 |
*/
|
|
|
112 |
public function compileDropFullText(Blueprint $blueprint, Fluent $command)
|
|
|
113 |
{
|
|
|
114 |
throw new RuntimeException('This database driver does not support fulltext index removal.');
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Compile a foreign key command.
|
|
|
119 |
*
|
|
|
120 |
* @param \Illuminate\Database\Schema\Blueprint $blueprint
|
|
|
121 |
* @param \Illuminate\Support\Fluent $command
|
|
|
122 |
* @return string
|
|
|
123 |
*/
|
|
|
124 |
public function compileForeign(Blueprint $blueprint, Fluent $command)
|
|
|
125 |
{
|
|
|
126 |
// We need to prepare several of the elements of the foreign key definition
|
|
|
127 |
// before we can create the SQL, such as wrapping the tables and convert
|
|
|
128 |
// an array of columns to comma-delimited strings for the SQL queries.
|
|
|
129 |
$sql = sprintf('alter table %s add constraint %s ',
|
|
|
130 |
$this->wrapTable($blueprint),
|
|
|
131 |
$this->wrap($command->index)
|
|
|
132 |
);
|
|
|
133 |
|
|
|
134 |
// Once we have the initial portion of the SQL statement we will add on the
|
|
|
135 |
// key name, table name, and referenced columns. These will complete the
|
|
|
136 |
// main portion of the SQL statement and this SQL will almost be done.
|
|
|
137 |
$sql .= sprintf('foreign key (%s) references %s (%s)',
|
|
|
138 |
$this->columnize($command->columns),
|
|
|
139 |
$this->wrapTable($command->on),
|
|
|
140 |
$this->columnize((array) $command->references)
|
|
|
141 |
);
|
|
|
142 |
|
|
|
143 |
// Once we have the basic foreign key creation statement constructed we can
|
|
|
144 |
// build out the syntax for what should happen on an update or delete of
|
|
|
145 |
// the affected columns, which will get something like "cascade", etc.
|
|
|
146 |
if (! is_null($command->onDelete)) {
|
|
|
147 |
$sql .= " on delete {$command->onDelete}";
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
if (! is_null($command->onUpdate)) {
|
|
|
151 |
$sql .= " on update {$command->onUpdate}";
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
return $sql;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Compile the blueprint's column definitions.
|
|
|
159 |
*
|
|
|
160 |
* @param \Illuminate\Database\Schema\Blueprint $blueprint
|
|
|
161 |
* @return array
|
|
|
162 |
*/
|
|
|
163 |
protected function getColumns(Blueprint $blueprint)
|
|
|
164 |
{
|
|
|
165 |
$columns = [];
|
|
|
166 |
|
|
|
167 |
foreach ($blueprint->getAddedColumns() as $column) {
|
|
|
168 |
// Each of the column types has their own compiler functions, which are tasked
|
|
|
169 |
// with turning the column definition into its SQL format for this platform
|
|
|
170 |
// used by the connection. The column's modifiers are compiled and added.
|
|
|
171 |
$sql = $this->wrap($column).' '.$this->getType($column);
|
|
|
172 |
|
|
|
173 |
$columns[] = $this->addModifiers($sql, $blueprint, $column);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
return $columns;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* Get the SQL for the column data type.
|
|
|
181 |
*
|
|
|
182 |
* @param \Illuminate\Support\Fluent $column
|
|
|
183 |
* @return string
|
|
|
184 |
*/
|
|
|
185 |
protected function getType(Fluent $column)
|
|
|
186 |
{
|
|
|
187 |
return $this->{'type'.ucfirst($column->type)}($column);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Create the column definition for a generated, computed column type.
|
|
|
192 |
*
|
|
|
193 |
* @param \Illuminate\Support\Fluent $column
|
|
|
194 |
* @return void
|
|
|
195 |
*
|
|
|
196 |
* @throws \RuntimeException
|
|
|
197 |
*/
|
|
|
198 |
protected function typeComputed(Fluent $column)
|
|
|
199 |
{
|
|
|
200 |
throw new RuntimeException('This database driver does not support the computed type.');
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Add the column modifiers to the definition.
|
|
|
205 |
*
|
|
|
206 |
* @param string $sql
|
|
|
207 |
* @param \Illuminate\Database\Schema\Blueprint $blueprint
|
|
|
208 |
* @param \Illuminate\Support\Fluent $column
|
|
|
209 |
* @return string
|
|
|
210 |
*/
|
|
|
211 |
protected function addModifiers($sql, Blueprint $blueprint, Fluent $column)
|
|
|
212 |
{
|
|
|
213 |
foreach ($this->modifiers as $modifier) {
|
|
|
214 |
if (method_exists($this, $method = "modify{$modifier}")) {
|
|
|
215 |
$sql .= $this->{$method}($blueprint, $column);
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
return $sql;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Get the primary key command if it exists on the blueprint.
|
|
|
224 |
*
|
|
|
225 |
* @param \Illuminate\Database\Schema\Blueprint $blueprint
|
|
|
226 |
* @param string $name
|
|
|
227 |
* @return \Illuminate\Support\Fluent|null
|
|
|
228 |
*/
|
|
|
229 |
protected function getCommandByName(Blueprint $blueprint, $name)
|
|
|
230 |
{
|
|
|
231 |
$commands = $this->getCommandsByName($blueprint, $name);
|
|
|
232 |
|
|
|
233 |
if (count($commands) > 0) {
|
|
|
234 |
return reset($commands);
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
/**
|
|
|
239 |
* Get all of the commands with a given name.
|
|
|
240 |
*
|
|
|
241 |
* @param \Illuminate\Database\Schema\Blueprint $blueprint
|
|
|
242 |
* @param string $name
|
|
|
243 |
* @return array
|
|
|
244 |
*/
|
|
|
245 |
protected function getCommandsByName(Blueprint $blueprint, $name)
|
|
|
246 |
{
|
|
|
247 |
return array_filter($blueprint->getCommands(), function ($value) use ($name) {
|
|
|
248 |
return $value->name == $name;
|
|
|
249 |
});
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
/**
|
|
|
253 |
* Add a prefix to an array of values.
|
|
|
254 |
*
|
|
|
255 |
* @param string $prefix
|
|
|
256 |
* @param array $values
|
|
|
257 |
* @return array
|
|
|
258 |
*/
|
|
|
259 |
public function prefixArray($prefix, array $values)
|
|
|
260 |
{
|
|
|
261 |
return array_map(function ($value) use ($prefix) {
|
|
|
262 |
return $prefix.' '.$value;
|
|
|
263 |
}, $values);
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
/**
|
|
|
267 |
* Wrap a table in keyword identifiers.
|
|
|
268 |
*
|
|
|
269 |
* @param mixed $table
|
|
|
270 |
* @return string
|
|
|
271 |
*/
|
|
|
272 |
public function wrapTable($table)
|
|
|
273 |
{
|
|
|
274 |
return parent::wrapTable(
|
|
|
275 |
$table instanceof Blueprint ? $table->getTable() : $table
|
|
|
276 |
);
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
* Wrap a value in keyword identifiers.
|
|
|
281 |
*
|
|
|
282 |
* @param \Illuminate\Database\Query\Expression|string $value
|
|
|
283 |
* @param bool $prefixAlias
|
|
|
284 |
* @return string
|
|
|
285 |
*/
|
|
|
286 |
public function wrap($value, $prefixAlias = false)
|
|
|
287 |
{
|
|
|
288 |
return parent::wrap(
|
|
|
289 |
$value instanceof Fluent ? $value->name : $value, $prefixAlias
|
|
|
290 |
);
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
/**
|
|
|
294 |
* Format a value so that it can be used in "default" clauses.
|
|
|
295 |
*
|
|
|
296 |
* @param mixed $value
|
|
|
297 |
* @return string
|
|
|
298 |
*/
|
|
|
299 |
protected function getDefaultValue($value)
|
|
|
300 |
{
|
|
|
301 |
if ($value instanceof Expression) {
|
|
|
302 |
return $value;
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
return is_bool($value)
|
|
|
306 |
? "'".(int) $value."'"
|
|
|
307 |
: "'".(string) $value."'";
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
/**
|
|
|
311 |
* Create an empty Doctrine DBAL TableDiff from the Blueprint.
|
|
|
312 |
*
|
|
|
313 |
* @param \Illuminate\Database\Schema\Blueprint $blueprint
|
|
|
314 |
* @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
|
|
|
315 |
* @return \Doctrine\DBAL\Schema\TableDiff
|
|
|
316 |
*/
|
|
|
317 |
public function getDoctrineTableDiff(Blueprint $blueprint, SchemaManager $schema)
|
|
|
318 |
{
|
|
|
319 |
$table = $this->getTablePrefix().$blueprint->getTable();
|
|
|
320 |
|
|
|
321 |
return tap(new TableDiff($table), function ($tableDiff) use ($schema, $table) {
|
|
|
322 |
$tableDiff->fromTable = $schema->listTableDetails($table);
|
|
|
323 |
});
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
/**
|
|
|
327 |
* Get the fluent commands for the grammar.
|
|
|
328 |
*
|
|
|
329 |
* @return array
|
|
|
330 |
*/
|
|
|
331 |
public function getFluentCommands()
|
|
|
332 |
{
|
|
|
333 |
return $this->fluentCommands;
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
/**
|
|
|
337 |
* Check if this Grammar supports schema changes wrapped in a transaction.
|
|
|
338 |
*
|
|
|
339 |
* @return bool
|
|
|
340 |
*/
|
|
|
341 |
public function supportsSchemaTransactions()
|
|
|
342 |
{
|
|
|
343 |
return $this->transactions;
|
|
|
344 |
}
|
|
|
345 |
}
|