schema.php 4.0 KB

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