mysql.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php namespace Laravel\Database\Schema\Grammars;
  2. use Laravel\Fluent;
  3. use Laravel\Database\Schema\Table;
  4. class MySQL extends Grammar {
  5. /**
  6. * The keyword identifier for the database system.
  7. *
  8. * @var string
  9. */
  10. public $wrapper = '`%s`';
  11. /**
  12. * Generate the SQL statements for a table creation command.
  13. *
  14. * @param Table $table
  15. * @param Fluent $command
  16. * @return array
  17. */
  18. public function create(Table $table, Fluent $command)
  19. {
  20. $columns = implode(', ', $this->columns($table));
  21. // First we will generate the base table creation statement. Other than incrementing
  22. // keys, no indexes will be created during the first creation of the table since
  23. // they will be added in separate commands.
  24. $sql = 'CREATE TABLE '.$this->wrap($table).' ('.$columns.')';
  25. // MySQL supports various "engines" for database tables. If an engine ws specified
  26. // by the developer, we will set it after adding the columns the table creation
  27. // statement. Some engines support extra indexes.
  28. if ( ! is_null($table->engine))
  29. {
  30. $sql .= ' ENGINE = '.$table->engine;
  31. }
  32. return $sql;
  33. }
  34. /**
  35. * Geenrate the SQL statements for a table modification command.
  36. *
  37. * @param Table $table
  38. * @param Fluent $command
  39. * @return array
  40. */
  41. public function add(Table $table, Fluent $command)
  42. {
  43. $columns = $this->columns($table);
  44. // Once we the array of column definitions, we need to add "add" to the front
  45. // of each definition, then we'll concatenate the definitions using commas
  46. // like normal and generate the SQL.
  47. $columns = implode(', ', array_map(function($column)
  48. {
  49. return 'ADD '.$column;
  50. }, $columns));
  51. return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
  52. }
  53. /**
  54. * Create the individual column definitions for the table.
  55. *
  56. * @param Table $table
  57. * @return array
  58. */
  59. protected function columns(Table $table)
  60. {
  61. $columns = array();
  62. foreach ($table->columns as $column)
  63. {
  64. // Each of the data type's have their own definition creation method,
  65. // which is responsible for creating the SQL for the type. This lets
  66. // us to keep the syntax easy and fluent, while translating the
  67. // types to the correct types.
  68. $sql = $this->wrap($column).' '.$this->type($column);
  69. $elements = array('nullable', 'defaults', 'incrementer');
  70. foreach ($elements as $element)
  71. {
  72. $sql .= $this->$element($table, $column);
  73. }
  74. $columns[] = $sql;
  75. }
  76. return $columns;
  77. }
  78. /**
  79. * Get the SQL syntax for indicating if a column is nullable.
  80. *
  81. * @param Table $table
  82. * @param Fluent $column
  83. * @return string
  84. */
  85. protected function nullable(Table $table, Fluent $column)
  86. {
  87. return ($column->nullable) ? ' NULL' : ' NOT NULL';
  88. }
  89. /**
  90. * Get the SQL syntax for specifying a default value on a column.
  91. *
  92. * @param Table $table
  93. * @param Fluent $column
  94. * @return string
  95. */
  96. protected function defaults(Table $table, Fluent $column)
  97. {
  98. if ( ! is_null($column->default))
  99. {
  100. return " DEFAULT '".$column->default."'";
  101. }
  102. }
  103. /**
  104. * Get the SQL syntax for defining an auto-incrementing column.
  105. *
  106. * @param Table $table
  107. * @param Fluent $column
  108. * @return string
  109. */
  110. protected function incrementer(Table $table, Fluent $column)
  111. {
  112. if ($column->type == 'integer' and $column->increment)
  113. {
  114. return ' AUTO_INCREMENT PRIMARY KEY';
  115. }
  116. }
  117. /**
  118. * Generate the SQL statement for creating a primary key.
  119. *
  120. * @param Table $table
  121. * @param Fluent $command
  122. * @return string
  123. */
  124. public function primary(Table $table, Fluent $command)
  125. {
  126. return $this->key($table, $command->name(null), 'PRIMARY KEY');
  127. }
  128. /**
  129. * Generate the SQL statement for creating a unique index.
  130. *
  131. * @param Table $table
  132. * @param Fluent $command
  133. * @return string
  134. */
  135. public function unique(Table $table, Fluent $command)
  136. {
  137. return $this->key($table, $command, 'UNIQUE');
  138. }
  139. /**
  140. * Generate the SQL statement for creating a full-text index.
  141. *
  142. * @param Table $table
  143. * @param Fluent $command
  144. * @return string
  145. */
  146. public function fulltext(Table $table, Fluent $command)
  147. {
  148. return $this->key($table, $command, 'FULLTEXT');
  149. }
  150. /**
  151. * Generate the SQL statement for creating a regular index.
  152. *
  153. * @param Table $table
  154. * @param Fluent $command
  155. * @return string
  156. */
  157. public function index(Table $table, Fluent $command)
  158. {
  159. return $this->key($table, $command, 'INDEX');
  160. }
  161. /**
  162. * Generate the SQL statement for creating a new index.
  163. *
  164. * @param Table $table
  165. * @param Fluent $command
  166. * @param string $type
  167. * @return string
  168. */
  169. protected function key(Table $table, Fluent $command, $type)
  170. {
  171. $keys = $this->columnize($command->columns);
  172. $name = $command->name;
  173. return 'ALTER TABLE '.$this->wrap($table)." ADD {$type} {$name}({$keys})";
  174. }
  175. /**
  176. * Generate the SQL statement for a drop table command.
  177. *
  178. * @param Table $table
  179. * @param Fluent $command
  180. * @return string
  181. */
  182. public function drop(Table $table, Fluent $command)
  183. {
  184. return 'DROP TABLE '.$this->wrap($table);
  185. }
  186. /**
  187. * Generate the SQL statement for a drop column command.
  188. *
  189. * @param Table $table
  190. * @param Fluent $command
  191. * @return string
  192. */
  193. public function drop_column(Table $table, Fluent $command)
  194. {
  195. $columns = array_map(array($this, 'wrap'), $command->columns);
  196. // Once we the array of column names, we need to add "drop" to the front
  197. // of each column, then we'll concatenate the columns using commas and
  198. // generate the alter statement SQL.
  199. $columns = implode(', ', array_map(function($column)
  200. {
  201. return 'DROP '.$column;
  202. }, $columns));
  203. return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
  204. }
  205. /**
  206. * Generate the SQL statement for a drop primary key command.
  207. *
  208. * @param Table $table
  209. * @param Fluent $command
  210. * @return string
  211. */
  212. public function drop_primary(Table $table, Fluent $command)
  213. {
  214. return 'ALTER TABLE '.$this->wrap($table).' DROP PRIMARY KEY';
  215. }
  216. /**
  217. * Generate the SQL statement for a drop unqique key command.
  218. *
  219. * @param Table $table
  220. * @param Fluent $command
  221. * @return string
  222. */
  223. public function drop_unique(Table $table, Fluent $command)
  224. {
  225. return $this->drop_key($table, $command);
  226. }
  227. /**
  228. * Generate the SQL statement for a drop full-text key command.
  229. *
  230. * @param Table $table
  231. * @param Fluent $command
  232. * @return string
  233. */
  234. public function drop_fulltext(Table $table, Fluent $command)
  235. {
  236. return $this->drop_key($table, $command);
  237. }
  238. /**
  239. * Generate the SQL statement for a drop unqique key command.
  240. *
  241. * @param Table $table
  242. * @param Fluent $command
  243. * @return string
  244. */
  245. public function drop_index(Table $table, Fluent $command)
  246. {
  247. return $this->drop_key($table, $command);
  248. }
  249. /**
  250. * Generate the SQL statement for a drop key command.
  251. *
  252. * @param Table $table
  253. * @param Fluent $command
  254. * @return string
  255. */
  256. protected function drop_key(Table $table, Fluent $command)
  257. {
  258. return 'ALTER TABLE '.$this->wrap($table)." DROP INDEX {$command->name}";
  259. }
  260. /**
  261. * Generate the data-type definition for a string.
  262. *
  263. * @param Fluent $column
  264. * @return string
  265. */
  266. protected function type_string(Fluent $column)
  267. {
  268. return 'VARCHAR('.$column->length.')';
  269. }
  270. /**
  271. * Generate the data-type definition for an integer.
  272. *
  273. * @param Fluent $column
  274. * @return string
  275. */
  276. protected function type_integer(Fluent $column)
  277. {
  278. return 'INT';
  279. }
  280. /**
  281. * Generate the data-type definition for an integer.
  282. *
  283. * @param Fluent $column
  284. * @return string
  285. */
  286. protected function type_float(Fluent $column)
  287. {
  288. return 'FLOAT';
  289. }
  290. /**
  291. * Generate the data-type definition for a boolean.
  292. *
  293. * @param Fluent $column
  294. * @return string
  295. */
  296. protected function type_boolean(Fluent $column)
  297. {
  298. return 'TINYINT';
  299. }
  300. /**
  301. * Generate the data-type definition for a date.
  302. *
  303. * @param Fluent $column
  304. * @return string
  305. */
  306. protected function type_date(Fluent $column)
  307. {
  308. return 'DATETIME';
  309. }
  310. /**
  311. * Generate the data-type definition for a timestamp.
  312. *
  313. * @param Fluent $column
  314. * @return string
  315. */
  316. protected function type_timestamp(Fluent $column)
  317. {
  318. return 'TIMESTAMP';
  319. }
  320. /**
  321. * Generate the data-type definition for a text column.
  322. *
  323. * @param Fluent $column
  324. * @return string
  325. */
  326. protected function type_text(Fluent $column)
  327. {
  328. return 'TEXT';
  329. }
  330. /**
  331. * Generate the data-type definition for a blob.
  332. *
  333. * @param Fluent $column
  334. * @return string
  335. */
  336. protected function type_blob(Fluent $column)
  337. {
  338. return 'BLOB';
  339. }
  340. }