PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel/database/schema/grammars/mysql.php

https://bitbucket.org/Maron1/taqman
PHP | 421 lines | 160 code | 49 blank | 212 comment | 8 complexity | 9cb1f6c1476a518c4165e7d487109e6a MD5 | raw file
  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. * Generate 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 have 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 || $column->increment))
  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 '".$this->default_value($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 column command.
  199. *
  200. * @param Table $table
  201. * @param Fluent $command
  202. * @return string
  203. */
  204. public function drop_column(Table $table, Fluent $command)
  205. {
  206. $columns = array_map(array($this, 'wrap'), $command->columns);
  207. // Once we the array of column names, we need to add "drop" to the front
  208. // of each column, then we'll concatenate the columns using commas and
  209. // generate the alter statement SQL.
  210. $columns = implode(', ', array_map(function($column)
  211. {
  212. return 'DROP '.$column;
  213. }, $columns));
  214. return 'ALTER TABLE '.$this->wrap($table).' '.$columns;
  215. }
  216. /**
  217. * Generate the SQL statement for a drop primary key command.
  218. *
  219. * @param Table $table
  220. * @param Fluent $command
  221. * @return string
  222. */
  223. public function drop_primary(Table $table, Fluent $command)
  224. {
  225. return 'ALTER TABLE '.$this->wrap($table).' DROP PRIMARY KEY';
  226. }
  227. /**
  228. * Generate the SQL statement for a drop unique key command.
  229. *
  230. * @param Table $table
  231. * @param Fluent $command
  232. * @return string
  233. */
  234. public function drop_unique(Table $table, Fluent $command)
  235. {
  236. return $this->drop_key($table, $command);
  237. }
  238. /**
  239. * Generate the SQL statement for a drop full-text key command.
  240. *
  241. * @param Table $table
  242. * @param Fluent $command
  243. * @return string
  244. */
  245. public function drop_fulltext(Table $table, Fluent $command)
  246. {
  247. return $this->drop_key($table, $command);
  248. }
  249. /**
  250. * Generate the SQL statement for a drop unique key command.
  251. *
  252. * @param Table $table
  253. * @param Fluent $command
  254. * @return string
  255. */
  256. public function drop_index(Table $table, Fluent $command)
  257. {
  258. return $this->drop_key($table, $command);
  259. }
  260. /**
  261. * Generate the SQL statement for a drop key command.
  262. *
  263. * @param Table $table
  264. * @param Fluent $command
  265. * @return string
  266. */
  267. protected function drop_key(Table $table, Fluent $command)
  268. {
  269. return 'ALTER TABLE '.$this->wrap($table)." DROP INDEX {$command->name}";
  270. }
  271. /**
  272. * Drop a foreign key constraint from the table.
  273. *
  274. * @param Table $table
  275. * @param Fluent $command
  276. * @return string
  277. */
  278. public function drop_foreign(Table $table, Fluent $command)
  279. {
  280. return "ALTER TABLE ".$this->wrap($table)." DROP FOREIGN KEY ".$command->name;
  281. }
  282. /**
  283. * Generate the data-type definition for a string.
  284. *
  285. * @param Fluent $column
  286. * @return string
  287. */
  288. protected function type_string(Fluent $column)
  289. {
  290. return 'VARCHAR('.$column->length.')';
  291. }
  292. /**
  293. * Generate the data-type definition for an integer.
  294. *
  295. * @param Fluent $column
  296. * @return string
  297. */
  298. protected function type_integer(Fluent $column)
  299. {
  300. return 'INT';
  301. }
  302. /**
  303. * Generate the data-type definition for an integer.
  304. *
  305. * @param Fluent $column
  306. * @return string
  307. */
  308. protected function type_float(Fluent $column)
  309. {
  310. return 'FLOAT';
  311. }
  312. /**
  313. * Generate the data-type definition for a decimal.
  314. *
  315. * @param Fluent $column
  316. * @return string
  317. */
  318. protected function type_decimal(Fluent $column)
  319. {
  320. return "DECIMAL({$column->precision}, {$column->scale})";
  321. }
  322. /**
  323. * Generate the data-type definition for a boolean.
  324. *
  325. * @param Fluent $column
  326. * @return string
  327. */
  328. protected function type_boolean(Fluent $column)
  329. {
  330. return 'TINYINT(1)';
  331. }
  332. /**
  333. * Generate the data-type definition for a date.
  334. *
  335. * @param Fluent $column
  336. * @return string
  337. */
  338. protected function type_date(Fluent $column)
  339. {
  340. return 'DATETIME';
  341. }
  342. /**
  343. * Generate the data-type definition for a timestamp.
  344. *
  345. * @param Fluent $column
  346. * @return string
  347. */
  348. protected function type_timestamp(Fluent $column)
  349. {
  350. return 'TIMESTAMP';
  351. }
  352. /**
  353. * Generate the data-type definition for a text column.
  354. *
  355. * @param Fluent $column
  356. * @return string
  357. */
  358. protected function type_text(Fluent $column)
  359. {
  360. return 'TEXT';
  361. }
  362. /**
  363. * Generate the data-type definition for a blob.
  364. *
  365. * @param Fluent $column
  366. * @return string
  367. */
  368. protected function type_blob(Fluent $column)
  369. {
  370. return 'BLOB';
  371. }
  372. }