sqlite.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 key 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 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 to 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($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 drop table command.
  176. *
  177. * @param Table $table
  178. * @param Fluent $command
  179. * @return string
  180. */
  181. public function drop(Table $table, Fluent $command)
  182. {
  183. return 'DROP TABLE '.$this->wrap($table);
  184. }
  185. /**
  186. * Generate the SQL statement for a drop unique key command.
  187. *
  188. * @param Table $table
  189. * @param Fluent $command
  190. * @return string
  191. */
  192. public function drop_unique(Table $table, Fluent $command)
  193. {
  194. return $this->drop_key($table, $command);
  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_index(Table $table, Fluent $command)
  204. {
  205. return $this->drop_key($table, $command);
  206. }
  207. /**
  208. * Generate the SQL statement for a drop key command.
  209. *
  210. * @param Table $table
  211. * @param Fluent $command
  212. * @return string
  213. */
  214. protected function drop_key(Table $table, Fluent $command)
  215. {
  216. return 'DROP INDEX '.$this->wrap($command->name);
  217. }
  218. /**
  219. * Generate the data-type definition for a string.
  220. *
  221. * @param Fluent $column
  222. * @return string
  223. */
  224. protected function type_string(Fluent $column)
  225. {
  226. return 'VARCHAR';
  227. }
  228. /**
  229. * Generate the data-type definition for an integer.
  230. *
  231. * @param Fluent $column
  232. * @return string
  233. */
  234. protected function type_integer(Fluent $column)
  235. {
  236. return 'INTEGER';
  237. }
  238. /**
  239. * Generate the data-type definition for an integer.
  240. *
  241. * @param Fluent $column
  242. * @return string
  243. */
  244. protected function type_float(Fluent $column)
  245. {
  246. return 'FLOAT';
  247. }
  248. /**
  249. * Generate the data-type definition for a decimal.
  250. *
  251. * @param Fluent $column
  252. * @return string
  253. */
  254. protected function type_decimal(Fluent $column)
  255. {
  256. return 'FLOAT';
  257. }
  258. /**
  259. * Generate the data-type definition for a boolean.
  260. *
  261. * @param Fluent $column
  262. * @return string
  263. */
  264. protected function type_boolean(Fluent $column)
  265. {
  266. return 'INTEGER';
  267. }
  268. /**
  269. * Generate the data-type definition for a date.
  270. *
  271. * @param Fluent $column
  272. * @return string
  273. */
  274. protected function type_date(Fluent $column)
  275. {
  276. return 'DATETIME';
  277. }
  278. /**
  279. * Generate the data-type definition for a timestamp.
  280. *
  281. * @param Fluent $column
  282. * @return string
  283. */
  284. protected function type_timestamp(Fluent $column)
  285. {
  286. return 'DATETIME';
  287. }
  288. /**
  289. * Generate the data-type definition for a text column.
  290. *
  291. * @param Fluent $column
  292. * @return string
  293. */
  294. protected function type_text(Fluent $column)
  295. {
  296. return 'TEXT';
  297. }
  298. /**
  299. * Generate the data-type definition for a blob.
  300. *
  301. * @param Fluent $column
  302. * @return string
  303. */
  304. protected function type_blob(Fluent $column)
  305. {
  306. return 'BLOB';
  307. }
  308. }