schema.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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
  31. // and is responsible for building that's commands SQL. This lets
  32. // the SQL generation stay very granular and makes it simply to
  33. // add new database systems to the schema system.
  34. if (method_exists($grammar, $method = $command->type))
  35. {
  36. $statements = $grammar->$method($table, $command);
  37. // Once we have the statements, we will cast them to an array
  38. // even though not all of the commands return an array just
  39. // in case the command needs to run more than one query to
  40. // do what it needs to do.
  41. foreach ((array) $statements as $statement)
  42. {
  43. $connection->statement($statement);
  44. }
  45. }
  46. }
  47. }
  48. /**
  49. * Add any implicit commands to the schema table operation.
  50. *
  51. * @param Schema\Table $table
  52. * @return void
  53. */
  54. protected static function implications($table)
  55. {
  56. // If the developer has specified columns for the table and the
  57. // table is not being created, we will assume they simply want
  58. // to add the columns to the table, and will generate an add
  59. // command on the schema automatically.
  60. if (count($table->columns) > 0 and ! $table->creating())
  61. {
  62. $command = new Fluent(array('type' => 'add'));
  63. array_unshift($table->commands, $command);
  64. }
  65. // For some extra syntax sugar, we'll check for any implicit
  66. // indexes on the table since the developer may specify the
  67. // index type on the fluent column declaration.
  68. foreach ($table->columns as $column)
  69. {
  70. foreach (array('primary', 'unique', 'fulltext', 'index') as $key)
  71. {
  72. if (isset($column->attributes[$key]))
  73. {
  74. $table->$key($column->name, $column->$key);
  75. }
  76. }
  77. }
  78. }
  79. /**
  80. * Create the appropriate schema grammar for the driver.
  81. *
  82. * @param Connection $connection
  83. * @return Grammar
  84. */
  85. public static function grammar(Connection $connection)
  86. {
  87. $driver = $connection->driver();
  88. switch ($driver)
  89. {
  90. case 'mysql':
  91. return new Schema\Grammars\MySQL($connection);
  92. case 'pgsql':
  93. return new Schema\Grammars\Postgres($connection);
  94. case 'sqlsrv':
  95. return new Schema\Grammars\SQLServer($connection);
  96. case 'sqlite':
  97. return new Schema\Grammars\SQLite($connection);
  98. }
  99. throw new \Exception("Schema operations not supported for [$driver].");
  100. }
  101. }