Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Database\Schema;
4
 
5
use Closure;
6
use Illuminate\Container\Container;
7
use Illuminate\Database\Connection;
8
use InvalidArgumentException;
9
use LogicException;
10
 
11
class Builder
12
{
13
    /**
14
     * The database connection instance.
15
     *
16
     * @var \Illuminate\Database\Connection
17
     */
18
    protected $connection;
19
 
20
    /**
21
     * The schema grammar instance.
22
     *
23
     * @var \Illuminate\Database\Schema\Grammars\Grammar
24
     */
25
    protected $grammar;
26
 
27
    /**
28
     * The Blueprint resolver callback.
29
     *
30
     * @var \Closure
31
     */
32
    protected $resolver;
33
 
34
    /**
35
     * The default string length for migrations.
36
     *
37
     * @var int|null
38
     */
39
    public static $defaultStringLength = 255;
40
 
41
    /**
42
     * The default relationship morph key type.
43
     *
44
     * @var string
45
     */
46
    public static $defaultMorphKeyType = 'int';
47
 
48
    /**
49
     * Indicates whether Doctrine DBAL usage will be prevented if possible when dropping and renaming columns.
50
     *
51
     * @var bool
52
     */
53
    public static $alwaysUsesNativeSchemaOperationsIfPossible = false;
54
 
55
    /**
56
     * Create a new database Schema manager.
57
     *
58
     * @param  \Illuminate\Database\Connection  $connection
59
     * @return void
60
     */
61
    public function __construct(Connection $connection)
62
    {
63
        $this->connection = $connection;
64
        $this->grammar = $connection->getSchemaGrammar();
65
    }
66
 
67
    /**
68
     * Set the default string length for migrations.
69
     *
70
     * @param  int  $length
71
     * @return void
72
     */
73
    public static function defaultStringLength($length)
74
    {
75
        static::$defaultStringLength = $length;
76
    }
77
 
78
    /**
79
     * Set the default morph key type for migrations.
80
     *
81
     * @param  string  $type
82
     * @return void
83
     *
84
     * @throws \InvalidArgumentException
85
     */
86
    public static function defaultMorphKeyType(string $type)
87
    {
88
        if (! in_array($type, ['int', 'uuid', 'ulid'])) {
89
            throw new InvalidArgumentException("Morph key type must be 'int', 'uuid', or 'ulid'.");
90
        }
91
 
92
        static::$defaultMorphKeyType = $type;
93
    }
94
 
95
    /**
96
     * Set the default morph key type for migrations to UUIDs.
97
     *
98
     * @return void
99
     */
100
    public static function morphUsingUuids()
101
    {
102
        return static::defaultMorphKeyType('uuid');
103
    }
104
 
105
    /**
106
     * Set the default morph key type for migrations to ULIDs.
107
     *
108
     * @return void
109
     */
110
    public static function morphUsingUlids()
111
    {
112
        return static::defaultMorphKeyType('ulid');
113
    }
114
 
115
    /**
116
     * Attempt to use native schema operations for dropping and renaming columns, even if Doctrine DBAL is installed.
117
     *
118
     * @param  bool  $value
119
     * @return void
120
     */
121
    public static function useNativeSchemaOperationsIfPossible(bool $value = true)
122
    {
123
        static::$alwaysUsesNativeSchemaOperationsIfPossible = $value;
124
    }
125
 
126
    /**
127
     * Create a database in the schema.
128
     *
129
     * @param  string  $name
130
     * @return bool
131
     *
132
     * @throws \LogicException
133
     */
134
    public function createDatabase($name)
135
    {
136
        throw new LogicException('This database driver does not support creating databases.');
137
    }
138
 
139
    /**
140
     * Drop a database from the schema if the database exists.
141
     *
142
     * @param  string  $name
143
     * @return bool
144
     *
145
     * @throws \LogicException
146
     */
147
    public function dropDatabaseIfExists($name)
148
    {
149
        throw new LogicException('This database driver does not support dropping databases.');
150
    }
151
 
152
    /**
153
     * Determine if the given table exists.
154
     *
155
     * @param  string  $table
156
     * @return bool
157
     */
158
    public function hasTable($table)
159
    {
160
        $table = $this->connection->getTablePrefix().$table;
161
 
162
        return count($this->connection->selectFromWriteConnection(
163
            $this->grammar->compileTableExists(), [$table]
164
        )) > 0;
165
    }
166
 
167
    /**
168
     * Determine if the given table has a given column.
169
     *
170
     * @param  string  $table
171
     * @param  string  $column
172
     * @return bool
173
     */
174
    public function hasColumn($table, $column)
175
    {
176
        return in_array(
177
            strtolower($column), array_map('strtolower', $this->getColumnListing($table))
178
        );
179
    }
180
 
181
    /**
182
     * Determine if the given table has given columns.
183
     *
184
     * @param  string  $table
185
     * @param  array  $columns
186
     * @return bool
187
     */
188
    public function hasColumns($table, array $columns)
189
    {
190
        $tableColumns = array_map('strtolower', $this->getColumnListing($table));
191
 
192
        foreach ($columns as $column) {
193
            if (! in_array(strtolower($column), $tableColumns)) {
194
                return false;
195
            }
196
        }
197
 
198
        return true;
199
    }
200
 
201
    /**
202
     * Execute a table builder callback if the given table has a given column.
203
     *
204
     * @param  string  $table
205
     * @param  string  $column
206
     * @param  \Closure  $callback
207
     * @return void
208
     */
209
    public function whenTableHasColumn(string $table, string $column, Closure $callback)
210
    {
211
        if ($this->hasColumn($table, $column)) {
212
            $this->table($table, fn (Blueprint $table) => $callback($table));
213
        }
214
    }
215
 
216
    /**
217
     * Execute a table builder callback if the given table doesn't have a given column.
218
     *
219
     * @param  string  $table
220
     * @param  string  $column
221
     * @param  \Closure  $callback
222
     * @return void
223
     */
224
    public function whenTableDoesntHaveColumn(string $table, string $column, Closure $callback)
225
    {
226
        if (! $this->hasColumn($table, $column)) {
227
            $this->table($table, fn (Blueprint $table) => $callback($table));
228
        }
229
    }
230
 
231
    /**
232
     * Get the data type for the given column name.
233
     *
234
     * @param  string  $table
235
     * @param  string  $column
236
     * @return string
237
     */
238
    public function getColumnType($table, $column)
239
    {
240
        $table = $this->connection->getTablePrefix().$table;
241
 
242
        return $this->connection->getDoctrineColumn($table, $column)->getType()->getName();
243
    }
244
 
245
    /**
246
     * Get the column listing for a given table.
247
     *
248
     * @param  string  $table
249
     * @return array
250
     */
251
    public function getColumnListing($table)
252
    {
253
        $results = $this->connection->selectFromWriteConnection($this->grammar->compileColumnListing(
254
            $this->connection->getTablePrefix().$table
255
        ));
256
 
257
        return $this->connection->getPostProcessor()->processColumnListing($results);
258
    }
259
 
260
    /**
261
     * Modify a table on the schema.
262
     *
263
     * @param  string  $table
264
     * @param  \Closure  $callback
265
     * @return void
266
     */
267
    public function table($table, Closure $callback)
268
    {
269
        $this->build($this->createBlueprint($table, $callback));
270
    }
271
 
272
    /**
273
     * Create a new table on the schema.
274
     *
275
     * @param  string  $table
276
     * @param  \Closure  $callback
277
     * @return void
278
     */
279
    public function create($table, Closure $callback)
280
    {
281
        $this->build(tap($this->createBlueprint($table), function ($blueprint) use ($callback) {
282
            $blueprint->create();
283
 
284
            $callback($blueprint);
285
        }));
286
    }
287
 
288
    /**
289
     * Drop a table from the schema.
290
     *
291
     * @param  string  $table
292
     * @return void
293
     */
294
    public function drop($table)
295
    {
296
        $this->build(tap($this->createBlueprint($table), function ($blueprint) {
297
            $blueprint->drop();
298
        }));
299
    }
300
 
301
    /**
302
     * Drop a table from the schema if it exists.
303
     *
304
     * @param  string  $table
305
     * @return void
306
     */
307
    public function dropIfExists($table)
308
    {
309
        $this->build(tap($this->createBlueprint($table), function ($blueprint) {
310
            $blueprint->dropIfExists();
311
        }));
312
    }
313
 
314
    /**
315
     * Drop columns from a table schema.
316
     *
317
     * @param  string  $table
318
     * @param  string|array  $columns
319
     * @return void
320
     */
321
    public function dropColumns($table, $columns)
322
    {
323
        $this->table($table, function (Blueprint $blueprint) use ($columns) {
324
            $blueprint->dropColumn($columns);
325
        });
326
    }
327
 
328
    /**
329
     * Drop all tables from the database.
330
     *
331
     * @return void
332
     *
333
     * @throws \LogicException
334
     */
335
    public function dropAllTables()
336
    {
337
        throw new LogicException('This database driver does not support dropping all tables.');
338
    }
339
 
340
    /**
341
     * Drop all views from the database.
342
     *
343
     * @return void
344
     *
345
     * @throws \LogicException
346
     */
347
    public function dropAllViews()
348
    {
349
        throw new LogicException('This database driver does not support dropping all views.');
350
    }
351
 
352
    /**
353
     * Drop all types from the database.
354
     *
355
     * @return void
356
     *
357
     * @throws \LogicException
358
     */
359
    public function dropAllTypes()
360
    {
361
        throw new LogicException('This database driver does not support dropping all types.');
362
    }
363
 
364
    /**
365
     * Get all of the table names for the database.
366
     *
367
     * @return void
368
     *
369
     * @throws \LogicException
370
     */
371
    public function getAllTables()
372
    {
373
        throw new LogicException('This database driver does not support getting all tables.');
374
    }
375
 
376
    /**
377
     * Rename a table on the schema.
378
     *
379
     * @param  string  $from
380
     * @param  string  $to
381
     * @return void
382
     */
383
    public function rename($from, $to)
384
    {
385
        $this->build(tap($this->createBlueprint($from), function ($blueprint) use ($to) {
386
            $blueprint->rename($to);
387
        }));
388
    }
389
 
390
    /**
391
     * Enable foreign key constraints.
392
     *
393
     * @return bool
394
     */
395
    public function enableForeignKeyConstraints()
396
    {
397
        return $this->connection->statement(
398
            $this->grammar->compileEnableForeignKeyConstraints()
399
        );
400
    }
401
 
402
    /**
403
     * Disable foreign key constraints.
404
     *
405
     * @return bool
406
     */
407
    public function disableForeignKeyConstraints()
408
    {
409
        return $this->connection->statement(
410
            $this->grammar->compileDisableForeignKeyConstraints()
411
        );
412
    }
413
 
414
    /**
150 lars 415
     * Disable foreign key constraints during the execution of a callback.
416
     *
417
     * @param  \Closure  $callback
418
     * @return mixed
419
     */
420
    public function withoutForeignKeyConstraints(Closure $callback)
421
    {
422
        $this->disableForeignKeyConstraints();
423
 
424
        $result = $callback();
425
 
426
        $this->enableForeignKeyConstraints();
427
 
428
        return $result;
429
    }
430
 
431
    /**
148 lars 432
     * Execute the blueprint to build / modify the table.
433
     *
434
     * @param  \Illuminate\Database\Schema\Blueprint  $blueprint
435
     * @return void
436
     */
437
    protected function build(Blueprint $blueprint)
438
    {
439
        $blueprint->build($this->connection, $this->grammar);
440
    }
441
 
442
    /**
443
     * Create a new command set with a Closure.
444
     *
445
     * @param  string  $table
446
     * @param  \Closure|null  $callback
447
     * @return \Illuminate\Database\Schema\Blueprint
448
     */
449
    protected function createBlueprint($table, Closure $callback = null)
450
    {
451
        $prefix = $this->connection->getConfig('prefix_indexes')
452
                    ? $this->connection->getConfig('prefix')
453
                    : '';
454
 
455
        if (isset($this->resolver)) {
456
            return call_user_func($this->resolver, $table, $callback, $prefix);
457
        }
458
 
459
        return Container::getInstance()->make(Blueprint::class, compact('table', 'callback', 'prefix'));
460
    }
461
 
462
    /**
463
     * Get the database connection instance.
464
     *
465
     * @return \Illuminate\Database\Connection
466
     */
467
    public function getConnection()
468
    {
469
        return $this->connection;
470
    }
471
 
472
    /**
473
     * Set the database connection instance.
474
     *
475
     * @param  \Illuminate\Database\Connection  $connection
476
     * @return $this
477
     */
478
    public function setConnection(Connection $connection)
479
    {
480
        $this->connection = $connection;
481
 
482
        return $this;
483
    }
484
 
485
    /**
486
     * Set the Schema Blueprint resolver callback.
487
     *
488
     * @param  \Closure  $resolver
489
     * @return void
490
     */
491
    public function blueprintResolver(Closure $resolver)
492
    {
493
        $this->resolver = $resolver;
494
    }
495
}