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