schema.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php namespace Laravel\Database;
  2. use Laravel\Fluent;
  3. use Laravel\Database as DB;
  4. class Schema {
  5. /**
  6. * Begin a fluent schema operation on a database table.
  7. *
  8. * @param string $table
  9. * @param Closure $callback
  10. * @return void
  11. */
  12. public static function table($table, $callback)
  13. {
  14. call_user_func($callback, $table = new Schema\Table($table));
  15. static::implications($table);
  16. return static::execute($table);
  17. }
  18. /**
  19. * Execute the given schema operation against the database.
  20. *
  21. * @param Schema\Table $table
  22. * @return void
  23. */
  24. public static function execute($table)
  25. {
  26. foreach ($table->commands as $command)
  27. {
  28. $connection = DB::connection($table->connection);
  29. $grammar = static::grammar($connection);
  30. // Each grammar has a function that corresponds to the command type and is for
  31. // building that command's SQL. This lets the SQL generation stay granular
  32. // and flexible across various database systems.
  33. if (method_exists($grammar, $method = $command->type))
  34. {
  35. $statements = $grammar->$method($table, $command);
  36. // Once we have the statements, we will cast them to an array even though
  37. // not all of the commands return an array just in case the command
  38. // needs multiple queries to complete its database work.
  39. foreach ((array) $statements as $statement)
  40. {
  41. $connection->statement($statement);
  42. }
  43. }
  44. }
  45. }
  46. /**
  47. * Add any implicit commands to the schema table operation.
  48. *
  49. * @param Schema\Table $table
  50. * @return void
  51. */
  52. protected static function implications($table)
  53. {
  54. // If the developer has specified columns for the table and the table is
  55. // not being created, we'll assume they simply want to add the columns
  56. // to the table and generate the add command.
  57. if (count($table->columns) > 0 and ! $table->creating())
  58. {
  59. $command = new Fluent(array('type' => 'add'));
  60. array_unshift($table->commands, $command);
  61. }
  62. // For some extra syntax sugar, we'll check for any implicit indexes
  63. // on the table since the developer may specify the index type on
  64. // the fluent column declaration for convenience.
  65. foreach ($table->columns as $column)
  66. {
  67. foreach (array('primary', 'unique', 'fulltext', 'index') as $key)
  68. {
  69. if (isset($column->attributes[$key]))
  70. {
  71. $table->$key($column->name, $column->$key);
  72. }
  73. }
  74. }
  75. }
  76. /**
  77. * Create the appropriate schema grammar for the driver.
  78. *
  79. * @param Connection $connection
  80. * @return Grammar
  81. */
  82. public static function grammar(Connection $connection)
  83. {
  84. $driver = $connection->driver();
  85. switch ($driver)
  86. {
  87. case 'mysql':
  88. return new Schema\Grammars\MySQL($connection);
  89. case 'pgsql':
  90. return new Schema\Grammars\Postgres($connection);
  91. case 'sqlsrv':
  92. return new Schema\Grammars\SQLServer($connection);
  93. case 'sqlite':
  94. return new Schema\Grammars\SQLite($connection);
  95. }
  96. throw new \Exception("Schema operations not supported for [$driver].");
  97. }
  98. }