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