sqlite.php 7.7 KB

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