sqlite.php 8.0 KB

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