123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- <?php namespace Laravel\Database\Schema\Grammars;
- use Laravel\Fluent;
- use Laravel\Database\Schema\Table;
- class MySQL extends Grammar {
-
- public $wrapper = '`%s`';
-
- public function create(Table $table, Fluent $command)
- {
- $columns = implode(', ', $this->columns($table));
-
-
-
- $sql = 'CREATE TABLE '.$this->wrap($table).' ('.$columns.')';
- if ( ! is_null($table->engine))
- {
- $sql .= ' ENGINE = '.$table->engine;
- }
- return $sql;
- }
-
- public function add(Table $table, Fluent $command)
- {
- $columns = $this->columns($table);
-
-
-
- $columns = implode(', ', array_map(function($column)
- {
- return 'ADD '.$column;
- }, $columns));
- return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
- }
-
- protected function columns(Table $table)
- {
- $columns = array();
- foreach ($table->columns as $column)
- {
-
-
-
-
- $sql = $this->wrap($column).' '.$this->type($column);
- $elements = array('unsigned', 'nullable', 'defaults', 'incrementer');
- foreach ($elements as $element)
- {
- $sql .= $this->$element($table, $column);
- }
- $columns[] = $sql;
- }
- return $columns;
- }
-
- protected function nullable(Table $table, Fluent $column)
- {
- return ($column->nullable) ? ' NULL' : ' NOT NULL';
- }
-
- protected function defaults(Table $table, Fluent $column)
- {
- if ( ! is_null($column->default))
- {
- return " DEFAULT '".$column->default."'";
- }
- }
-
- protected function incrementer(Table $table, Fluent $column)
- {
- if ($column->type == 'integer' and $column->increment)
- {
- return ' AUTO_INCREMENT PRIMARY KEY';
- }
- }
-
- public function primary(Table $table, Fluent $command)
- {
- return $this->key($table, $command->name(null), 'PRIMARY KEY');
- }
-
- public function unique(Table $table, Fluent $command)
- {
- return $this->key($table, $command, 'UNIQUE');
- }
-
- public function fulltext(Table $table, Fluent $command)
- {
- return $this->key($table, $command, 'FULLTEXT');
- }
-
- public function index(Table $table, Fluent $command)
- {
- return $this->key($table, $command, 'INDEX');
- }
-
- protected function key(Table $table, Fluent $command, $type)
- {
- $keys = $this->columnize($command->columns);
- $name = $command->name;
- return 'ALTER TABLE '.$this->wrap($table)." ADD {$type} {$name}({$keys})";
- }
-
- public function drop(Table $table, Fluent $command)
- {
- return 'DROP TABLE '.$this->wrap($table);
- }
-
- public function drop_column(Table $table, Fluent $command)
- {
- $columns = array_map(array($this, 'wrap'), $command->columns);
-
-
-
- $columns = implode(', ', array_map(function($column)
- {
- return 'DROP '.$column;
- }, $columns));
- return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
- }
-
- public function drop_primary(Table $table, Fluent $command)
- {
- return 'ALTER TABLE '.$this->wrap($table).' DROP PRIMARY KEY';
- }
-
- public function drop_unique(Table $table, Fluent $command)
- {
- return $this->drop_key($table, $command);
- }
-
- public function drop_fulltext(Table $table, Fluent $command)
- {
- return $this->drop_key($table, $command);
- }
-
- public function drop_index(Table $table, Fluent $command)
- {
- return $this->drop_key($table, $command);
- }
-
- protected function drop_key(Table $table, Fluent $command)
- {
- return 'ALTER TABLE '.$this->wrap($table)." DROP INDEX {$command->name}";
- }
-
- protected function type_string(Fluent $column)
- {
- return 'VARCHAR('.$column->length.')';
- }
-
- protected function type_integer(Fluent $column)
- {
- return 'INT';
- }
-
- protected function type_float(Fluent $column)
- {
- return 'FLOAT';
- }
-
- protected function type_boolean(Fluent $column)
- {
- return 'TINYINT';
- }
-
- protected function type_date(Fluent $column)
- {
- return 'DATETIME';
- }
-
- protected function type_timestamp(Fluent $column)
- {
- return 'TIMESTAMP';
- }
-
- protected function type_text(Fluent $column)
- {
- return 'TEXT';
- }
-
- protected function type_blob(Fluent $column)
- {
- return 'BLOB';
- }
- }
|