schema.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. return static::execute($table);
  16. }
  17. /**
  18. * Create a new database table schema.
  19. *
  20. * @param string $table
  21. * @param Closure $callback
  22. * @return void
  23. */
  24. public static function create($table, $callback)
  25. {
  26. $table = new Schema\Table($table);
  27. // To indicate that the table is new and needs to be created, we'll run
  28. // the "create" command on the table instance. This tells schema it is
  29. // not simply a column modification operation.
  30. $table->create();
  31. call_user_func($callback, $table);
  32. return static::execute($table);
  33. }
  34. /**
  35. * Drop a database table from the schema.
  36. *
  37. * @param string $table
  38. * @param string $connection
  39. * @return void
  40. */
  41. public static function drop($table, $connection = null)
  42. {
  43. $table = new Schema\Table($table);
  44. $table->on($connection);
  45. // To indicate that the table needs to be dropped, we will run the
  46. // "drop" command on the table instance and pass the instance to
  47. // the execute method as calling a Closure isn't needed.
  48. $table->drop();
  49. return static::execute($table);
  50. }
  51. /**
  52. * Execute the given schema operation against the database.
  53. *
  54. * @param Schema\Table $table
  55. * @return void
  56. */
  57. public static function execute($table)
  58. {
  59. // The implications method is responsible for finding any fluently
  60. // defined indexes on the schema table and adding the explicit
  61. // commands that are needed to tbe schema instance.
  62. static::implications($table);
  63. foreach ($table->commands as $command)
  64. {
  65. $connection = DB::connection($table->connection);
  66. $grammar = static::grammar($connection);
  67. // Each grammar has a function that corresponds to the command type and
  68. // is for building that command's SQL. This lets the SQL syntax builds
  69. // stay granular across various database systems.
  70. if (method_exists($grammar, $method = $command->type))
  71. {
  72. $statements = $grammar->$method($table, $command);
  73. // Once we have the statements, we will cast them to an array even
  74. // though not all of the commands return an array just in case it
  75. // needs multiple queries to complete.
  76. foreach ((array) $statements as $statement)
  77. {
  78. $connection->query($statement);
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * Add any implicit commands to the schema table operation.
  85. *
  86. * @param Schema\Table $table
  87. * @return void
  88. */
  89. protected static function implications($table)
  90. {
  91. // If the developer has specified columns for the table and the table is
  92. // not being created, we'll assume they simply want to add the columns
  93. // to the table and generate the add command.
  94. if (count($table->columns) > 0 and ! $table->creating())
  95. {
  96. $command = new Fluent(array('type' => 'add'));
  97. array_unshift($table->commands, $command);
  98. }
  99. // For some extra syntax sugar, we'll check for any implicit indexes
  100. // on the table since the developer may specify the index type on
  101. // the fluent column declaration for convenience.
  102. foreach ($table->columns as $column)
  103. {
  104. foreach (array('primary', 'unique', 'fulltext', 'index') as $key)
  105. {
  106. if (isset($column->$key))
  107. {
  108. if ($column->$key === true)
  109. {
  110. $table->$key($column->name);
  111. }
  112. else
  113. {
  114. $table->$key($column->name, $column->$key);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * Create the appropriate schema grammar for the driver.
  122. *
  123. * @param Connection $connection
  124. * @return Grammar
  125. */
  126. public static function grammar(Connection $connection)
  127. {
  128. $driver = $connection->driver();
  129. switch ($driver)
  130. {
  131. case 'mysql':
  132. return new Schema\Grammars\MySQL($connection);
  133. case 'pgsql':
  134. return new Schema\Grammars\Postgres($connection);
  135. case 'sqlsrv':
  136. return new Schema\Grammars\SQLServer($connection);
  137. case 'sqlite':
  138. return new Schema\Grammars\SQLite($connection);
  139. }
  140. throw new \Exception("Schema operations not supported for [$driver].");
  141. }
  142. }