mysql.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 auto
  22. // incrementing keys, no indexes will be created during the first creation
  23. // of the table as they're added in separate commands.
  24. $sql = 'CREATE TABLE '.$this->wrap($table).' ('.$columns.')';
  25. if ( ! is_null($table->engine))
  26. {
  27. $sql .= ' ENGINE = '.$table->engine;
  28. }
  29. return $sql;
  30. }
  31. /**
  32. * Geenrate the SQL statements for a table modification command.
  33. *
  34. * @param Table $table
  35. * @param Fluent $command
  36. * @return array
  37. */
  38. public function add(Table $table, Fluent $command)
  39. {
  40. $columns = $this->columns($table);
  41. // Once we the array of column definitions, we need to add "add" to the
  42. // front of each definition, then we'll concatenate the definitions
  43. // using commas like normal and generate the SQL.
  44. $columns = implode(', ', array_map(function($column)
  45. {
  46. return 'ADD '.$column;
  47. }, $columns));
  48. return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
  49. }
  50. /**
  51. * Create the individual column definitions for the table.
  52. *
  53. * @param Table $table
  54. * @return array
  55. */
  56. protected function columns(Table $table)
  57. {
  58. $columns = array();
  59. foreach ($table->columns as $column)
  60. {
  61. // Each of the data type's have their own definition creation method,
  62. // which is responsible for creating the SQL for the type. This lets
  63. // us to keep the syntax easy and fluent, while translating the
  64. // types to the correct types.
  65. $sql = $this->wrap($column).' '.$this->type($column);
  66. $elements = array('unsigned', 'nullable', 'defaults', 'incrementer');
  67. foreach ($elements as $element)
  68. {
  69. $sql .= $this->$element($table, $column);
  70. }
  71. $columns[] = $sql;
  72. }
  73. return $columns;
  74. }
  75. /**
  76. * Get the SQL syntax for indicating if a column is unsigned.
  77. *
  78. * @param Table $table
  79. * @param Fluent $column
  80. * @return string
  81. */
  82. protected function unsigned(Table $table, Fluent $column)
  83. {
  84. if ($column->type == 'integer' && $column->unsigned)
  85. {
  86. return ' UNSIGNED';
  87. }
  88. }
  89. /**
  90. * Get the SQL syntax for indicating if a column is nullable.
  91. *
  92. * @param Table $table
  93. * @param Fluent $column
  94. * @return string
  95. */
  96. protected function nullable(Table $table, Fluent $column)
  97. {
  98. return ($column->nullable) ? ' NULL' : ' NOT NULL';
  99. }
  100. /**
  101. * Get the SQL syntax for specifying a default value on a column.
  102. *
  103. * @param Table $table
  104. * @param Fluent $column
  105. * @return string
  106. */
  107. protected function defaults(Table $table, Fluent $column)
  108. {
  109. if ( ! is_null($column->default))
  110. {
  111. return " DEFAULT '".$column->default."'";
  112. }
  113. }
  114. /**
  115. * Get the SQL syntax for defining an auto-incrementing column.
  116. *
  117. * @param Table $table
  118. * @param Fluent $column
  119. * @return string
  120. */
  121. protected function incrementer(Table $table, Fluent $column)
  122. {
  123. if ($column->type == 'integer' and $column->increment)
  124. {
  125. return ' AUTO_INCREMENT PRIMARY KEY';
  126. }
  127. }
  128. /**
  129. * Generate the SQL statement for creating a primary key.
  130. *
  131. * @param Table $table
  132. * @param Fluent $command
  133. * @return string
  134. */
  135. public function primary(Table $table, Fluent $command)
  136. {
  137. return $this->key($table, $command->name(null), 'PRIMARY KEY');
  138. }
  139. /**
  140. * Generate the SQL statement for creating a unique index.
  141. *
  142. * @param Table $table
  143. * @param Fluent $command
  144. * @return string
  145. */
  146. public function unique(Table $table, Fluent $command)
  147. {
  148. return $this->key($table, $command, 'UNIQUE');
  149. }
  150. /**
  151. * Generate the SQL statement for creating a full-text index.
  152. *
  153. * @param Table $table
  154. * @param Fluent $command
  155. * @return string
  156. */
  157. public function fulltext(Table $table, Fluent $command)
  158. {
  159. return $this->key($table, $command, 'FULLTEXT');
  160. }
  161. /**
  162. * Generate the SQL statement for creating a regular index.
  163. *
  164. * @param Table $table
  165. * @param Fluent $command
  166. * @return string
  167. */
  168. public function index(Table $table, Fluent $command)
  169. {
  170. return $this->key($table, $command, 'INDEX');
  171. }
  172. /**
  173. * Generate the SQL statement for creating a new index.
  174. *
  175. * @param Table $table
  176. * @param Fluent $command
  177. * @param string $type
  178. * @return string
  179. */
  180. protected function key(Table $table, Fluent $command, $type)
  181. {
  182. $keys = $this->columnize($command->columns);
  183. $name = $command->name;
  184. return 'ALTER TABLE '.$this->wrap($table)." ADD {$type} {$name}({$keys})";
  185. }
  186. /**
  187. * Generate the SQL statement for a rename table command.
  188. *
  189. * @param Table $table
  190. * @param Fluent $command
  191. * @return string
  192. */
  193. public function rename(Table $table, Fluent $command)
  194. {
  195. return 'RENAME TABLE '.$this->wrap($table).' TO '.$this->wrap($command->name);
  196. }
  197. /**
  198. * Generate the SQL statement for a drop table command.
  199. *
  200. * @param Table $table
  201. * @param Fluent $command
  202. * @return string
  203. */
  204. public function drop(Table $table, Fluent $command)
  205. {
  206. return 'DROP TABLE '.$this->wrap($table);
  207. }
  208. /**
  209. * Generate the SQL statement for a drop column command.
  210. *
  211. * @param Table $table
  212. * @param Fluent $command
  213. * @return string
  214. */
  215. public function drop_column(Table $table, Fluent $command)
  216. {
  217. $columns = array_map(array($this, 'wrap'), $command->columns);
  218. // Once we the array of column names, we need to add "drop" to the front
  219. // of each column, then we'll concatenate the columns using commas and
  220. // generate the alter statement SQL.
  221. $columns = implode(', ', array_map(function($column)
  222. {
  223. return 'DROP '.$column;
  224. }, $columns));
  225. return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
  226. }
  227. /**
  228. * Generate the SQL statement for a drop primary key command.
  229. *
  230. * @param Table $table
  231. * @param Fluent $command
  232. * @return string
  233. */
  234. public function drop_primary(Table $table, Fluent $command)
  235. {
  236. return 'ALTER TABLE '.$this->wrap($table).' DROP PRIMARY KEY';
  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_unique(Table $table, Fluent $command)
  246. {
  247. return $this->drop_key($table, $command);
  248. }
  249. /**
  250. * Generate the SQL statement for a drop full-text key command.
  251. *
  252. * @param Table $table
  253. * @param Fluent $command
  254. * @return string
  255. */
  256. public function drop_fulltext(Table $table, Fluent $command)
  257. {
  258. return $this->drop_key($table, $command);
  259. }
  260. /**
  261. * Generate the SQL statement for a drop unqique key command.
  262. *
  263. * @param Table $table
  264. * @param Fluent $command
  265. * @return string
  266. */
  267. public function drop_index(Table $table, Fluent $command)
  268. {
  269. return $this->drop_key($table, $command);
  270. }
  271. /**
  272. * Generate the SQL statement for a drop key command.
  273. *
  274. * @param Table $table
  275. * @param Fluent $command
  276. * @return string
  277. */
  278. protected function drop_key(Table $table, Fluent $command)
  279. {
  280. return 'ALTER TABLE '.$this->wrap($table)." DROP INDEX {$command->name}";
  281. }
  282. /**
  283. * Drop a foreign key constraint from the table.
  284. *
  285. * @param Table $table
  286. * @param Fluent $fluent
  287. * @return string
  288. */
  289. public function drop_foreign(Table $table, Fluent $command)
  290. {
  291. return "ALTER TABLE ".$this->wrap($table)." DROP FOREIGN KEY ".$command->name;
  292. }
  293. /**
  294. * Generate the data-type definition for a string.
  295. *
  296. * @param Fluent $column
  297. * @return string
  298. */
  299. protected function type_string(Fluent $column)
  300. {
  301. return 'VARCHAR('.$column->length.')';
  302. }
  303. /**
  304. * Generate the data-type definition for an integer.
  305. *
  306. * @param Fluent $column
  307. * @return string
  308. */
  309. protected function type_integer(Fluent $column)
  310. {
  311. return 'INT';
  312. }
  313. /**
  314. * Generate the data-type definition for an integer.
  315. *
  316. * @param Fluent $column
  317. * @return string
  318. */
  319. protected function type_float(Fluent $column)
  320. {
  321. return 'FLOAT';
  322. }
  323. /**
  324. * Generate the data-type definintion for a decimal.
  325. *
  326. * @param Fluent $column
  327. * @return string
  328. */
  329. protected function type_decimal(Fluent $column)
  330. {
  331. return "DECIMAL({$column->precision}, {$column->scale})";
  332. }
  333. /**
  334. * Generate the data-type definition for a boolean.
  335. *
  336. * @param Fluent $column
  337. * @return string
  338. */
  339. protected function type_boolean(Fluent $column)
  340. {
  341. return 'TINYINT';
  342. }
  343. /**
  344. * Generate the data-type definition for a date.
  345. *
  346. * @param Fluent $column
  347. * @return string
  348. */
  349. protected function type_date(Fluent $column)
  350. {
  351. return 'DATETIME';
  352. }
  353. /**
  354. * Generate the data-type definition for a timestamp.
  355. *
  356. * @param Fluent $column
  357. * @return string
  358. */
  359. protected function type_timestamp(Fluent $column)
  360. {
  361. return 'TIMESTAMP';
  362. }
  363. /**
  364. * Generate the data-type definition for a text column.
  365. *
  366. * @param Fluent $column
  367. * @return string
  368. */
  369. protected function type_text(Fluent $column)
  370. {
  371. return 'TEXT';
  372. }
  373. /**
  374. * Generate the data-type definition for a blob.
  375. *
  376. * @param Fluent $column
  377. * @return string
  378. */
  379. protected function type_blob(Fluent $column)
  380. {
  381. return 'BLOB';
  382. }
  383. }