mysql.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 nullable.
  77. *
  78. * @param Table $table
  79. * @param Fluent $column
  80. * @return string
  81. */
  82. protected function nullable(Table $table, Fluent $column)
  83. {
  84. return ($column->nullable) ? ' NULL' : ' NOT NULL';
  85. }
  86. /**
  87. * Get the SQL syntax for specifying a default value on a column.
  88. *
  89. * @param Table $table
  90. * @param Fluent $column
  91. * @return string
  92. */
  93. protected function defaults(Table $table, Fluent $column)
  94. {
  95. if ( ! is_null($column->default))
  96. {
  97. return " DEFAULT '".$column->default."'";
  98. }
  99. }
  100. /**
  101. * Get the SQL syntax for defining an auto-incrementing column.
  102. *
  103. * @param Table $table
  104. * @param Fluent $column
  105. * @return string
  106. */
  107. protected function incrementer(Table $table, Fluent $column)
  108. {
  109. if ($column->type == 'integer' and $column->increment)
  110. {
  111. return ' AUTO_INCREMENT PRIMARY KEY';
  112. }
  113. }
  114. /**
  115. * Generate the SQL statement for creating a primary key.
  116. *
  117. * @param Table $table
  118. * @param Fluent $command
  119. * @return string
  120. */
  121. public function primary(Table $table, Fluent $command)
  122. {
  123. return $this->key($table, $command->name(null), 'PRIMARY KEY');
  124. }
  125. /**
  126. * Generate the SQL statement for creating a unique index.
  127. *
  128. * @param Table $table
  129. * @param Fluent $command
  130. * @return string
  131. */
  132. public function unique(Table $table, Fluent $command)
  133. {
  134. return $this->key($table, $command, 'UNIQUE');
  135. }
  136. /**
  137. * Generate the SQL statement for creating a full-text index.
  138. *
  139. * @param Table $table
  140. * @param Fluent $command
  141. * @return string
  142. */
  143. public function fulltext(Table $table, Fluent $command)
  144. {
  145. return $this->key($table, $command, 'FULLTEXT');
  146. }
  147. /**
  148. * Generate the SQL statement for creating a regular index.
  149. *
  150. * @param Table $table
  151. * @param Fluent $command
  152. * @return string
  153. */
  154. public function index(Table $table, Fluent $command)
  155. {
  156. return $this->key($table, $command, 'INDEX');
  157. }
  158. /**
  159. * Generate the SQL statement for creating a new index.
  160. *
  161. * @param Table $table
  162. * @param Fluent $command
  163. * @param string $type
  164. * @return string
  165. */
  166. protected function key(Table $table, Fluent $command, $type)
  167. {
  168. $keys = $this->columnize($command->columns);
  169. $name = $command->name;
  170. return 'ALTER TABLE '.$this->wrap($table)." ADD {$type} {$name}({$keys})";
  171. }
  172. /**
  173. * Generate the SQL statement for a drop table command.
  174. *
  175. * @param Table $table
  176. * @param Fluent $command
  177. * @return string
  178. */
  179. public function drop(Table $table, Fluent $command)
  180. {
  181. return 'DROP TABLE '.$this->wrap($table);
  182. }
  183. /**
  184. * Generate the SQL statement for a drop column command.
  185. *
  186. * @param Table $table
  187. * @param Fluent $command
  188. * @return string
  189. */
  190. public function drop_column(Table $table, Fluent $command)
  191. {
  192. $columns = array_map(array($this, 'wrap'), $command->columns);
  193. // Once we the array of column names, we need to add "drop" to the front
  194. // of each column, then we'll concatenate the columns using commas and
  195. // generate the alter statement SQL.
  196. $columns = implode(', ', array_map(function($column)
  197. {
  198. return 'DROP '.$column;
  199. }, $columns));
  200. return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
  201. }
  202. /**
  203. * Generate the SQL statement for a drop primary key command.
  204. *
  205. * @param Table $table
  206. * @param Fluent $command
  207. * @return string
  208. */
  209. public function drop_primary(Table $table, Fluent $command)
  210. {
  211. return 'ALTER TABLE '.$this->wrap($table).' DROP PRIMARY KEY';
  212. }
  213. /**
  214. * Generate the SQL statement for a drop unqique key command.
  215. *
  216. * @param Table $table
  217. * @param Fluent $command
  218. * @return string
  219. */
  220. public function drop_unique(Table $table, Fluent $command)
  221. {
  222. return $this->drop_key($table, $command);
  223. }
  224. /**
  225. * Generate the SQL statement for a drop full-text key command.
  226. *
  227. * @param Table $table
  228. * @param Fluent $command
  229. * @return string
  230. */
  231. public function drop_fulltext(Table $table, Fluent $command)
  232. {
  233. return $this->drop_key($table, $command);
  234. }
  235. /**
  236. * Generate the SQL statement for a drop unqique key command.
  237. *
  238. * @param Table $table
  239. * @param Fluent $command
  240. * @return string
  241. */
  242. public function drop_index(Table $table, Fluent $command)
  243. {
  244. return $this->drop_key($table, $command);
  245. }
  246. /**
  247. * Generate the SQL statement for a drop key command.
  248. *
  249. * @param Table $table
  250. * @param Fluent $command
  251. * @return string
  252. */
  253. protected function drop_key(Table $table, Fluent $command)
  254. {
  255. return 'ALTER TABLE '.$this->wrap($table)." DROP INDEX {$command->name}";
  256. }
  257. /**
  258. * Generate the data-type definition for a string.
  259. *
  260. * @param Fluent $column
  261. * @return string
  262. */
  263. protected function type_string(Fluent $column)
  264. {
  265. return 'VARCHAR('.$column->length.')';
  266. }
  267. /**
  268. * Generate the data-type definition for an integer.
  269. *
  270. * @param Fluent $column
  271. * @return string
  272. */
  273. protected function type_integer(Fluent $column)
  274. {
  275. return 'INT';
  276. }
  277. /**
  278. * Generate the data-type definition for an integer.
  279. *
  280. * @param Fluent $column
  281. * @return string
  282. */
  283. protected function type_float(Fluent $column)
  284. {
  285. return 'FLOAT';
  286. }
  287. /**
  288. * Generate the data-type definition for a boolean.
  289. *
  290. * @param Fluent $column
  291. * @return string
  292. */
  293. protected function type_boolean(Fluent $column)
  294. {
  295. return 'TINYINT';
  296. }
  297. /**
  298. * Generate the data-type definition for a date.
  299. *
  300. * @param Fluent $column
  301. * @return string
  302. */
  303. protected function type_date(Fluent $column)
  304. {
  305. return 'DATETIME';
  306. }
  307. /**
  308. * Generate the data-type definition for a timestamp.
  309. *
  310. * @param Fluent $column
  311. * @return string
  312. */
  313. protected function type_timestamp(Fluent $column)
  314. {
  315. return 'TIMESTAMP';
  316. }
  317. /**
  318. * Generate the data-type definition for a text column.
  319. *
  320. * @param Fluent $column
  321. * @return string
  322. */
  323. protected function type_text(Fluent $column)
  324. {
  325. return 'TEXT';
  326. }
  327. /**
  328. * Generate the data-type definition for a blob.
  329. *
  330. * @param Fluent $column
  331. * @return string
  332. */
  333. protected function type_blob(Fluent $column)
  334. {
  335. return 'BLOB';
  336. }
  337. }