schema.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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->driver());
  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 even
  38. // though not all of the commands return an array. This is just in
  39. // case the command needs to run more than one query to do what
  40. // it needs to do what is requested by the developer.
  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 for them, adding the columns to the command.
  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. The developer may specify the index
  67. // type on the fluent column declaration. Here we'll find
  68. // any implicit indexes and add the commands.
  69. foreach ($table->columns as $column)
  70. {
  71. foreach (array('primary', 'unique', 'fulltext', 'index') as $key)
  72. {
  73. if (isset($column->attributes[$key]))
  74. {
  75. $table->$key($column->name, $column->$key);
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * Create the appropriate schema grammar for the driver.
  82. *
  83. * @param string $driver
  84. * @return Grammar
  85. */
  86. public static function grammar($driver)
  87. {
  88. switch ($driver)
  89. {
  90. case 'mysql':
  91. return new Schema\Grammars\MySQL;
  92. case 'pgsql':
  93. return new Schema\Grammars\Postgres;
  94. case 'sqlsrv':
  95. return new Schema\Grammars\SQLServer;
  96. case 'sqlite':
  97. return new Schema\Grammars\SQLite;
  98. }
  99. throw new \Exception("Schema operations not supported for [$driver].");
  100. }
  101. }