/vendor/laravel/framework/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php

https://gitlab.com/techniconline/kmc · PHP · 604 lines · 227 code · 76 blank · 301 comment · 6 complexity · 31f4e90017c4dafb4d3fe59348988784 MD5 · raw file

  1. <?php namespace Illuminate\Database\Schema\Grammars;
  2. use Illuminate\Support\Fluent;
  3. use Illuminate\Database\Connection;
  4. use Illuminate\Database\Schema\Blueprint;
  5. class SQLiteGrammar extends Grammar
  6. {
  7. /**
  8. * The possible column modifiers.
  9. *
  10. * @var array
  11. */
  12. protected $modifiers = array('Nullable', 'Default', 'Increment');
  13. /**
  14. * The columns available as serials.
  15. *
  16. * @var array
  17. */
  18. protected $serials = array('bigInteger', 'integer');
  19. /**
  20. * Compile the query to determine if a table exists.
  21. *
  22. * @return string
  23. */
  24. public function compileTableExists()
  25. {
  26. return "select * from sqlite_master where type = 'table' and name = ?";
  27. }
  28. /**
  29. * Compile the query to determine the list of columns.
  30. *
  31. * @param string $table
  32. * @return string
  33. */
  34. public function compileColumnExists($table)
  35. {
  36. return 'pragma table_info(' . str_replace('.', '__', $table) . ')';
  37. }
  38. /**
  39. * Compile a create table command.
  40. *
  41. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  42. * @param \Illuminate\Support\Fluent $command
  43. * @return string
  44. */
  45. public function compileCreate(Blueprint $blueprint, Fluent $command)
  46. {
  47. $columns = implode(', ', $this->getColumns($blueprint));
  48. $sql = 'create table ' . $this->wrapTable($blueprint) . " ($columns";
  49. // SQLite forces primary keys to be added when the table is initially created
  50. // so we will need to check for a primary key commands and add the columns
  51. // to the table's declaration here so they can be created on the tables.
  52. $sql .= (string)$this->addForeignKeys($blueprint);
  53. $sql .= (string)$this->addPrimaryKeys($blueprint);
  54. return $sql . ')';
  55. }
  56. /**
  57. * Get the foreign key syntax for a table creation statement.
  58. *
  59. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  60. * @return string|null
  61. */
  62. protected function addForeignKeys(Blueprint $blueprint)
  63. {
  64. $sql = '';
  65. $foreigns = $this->getCommandsByName($blueprint, 'foreign');
  66. // Once we have all the foreign key commands for the table creation statement
  67. // we'll loop through each of them and add them to the create table SQL we
  68. // are building, since SQLite needs foreign keys on the tables creation.
  69. foreach ($foreigns as $foreign) {
  70. $sql .= $this->getForeignKey($foreign);
  71. if (!is_null($foreign->onDelete)) {
  72. $sql .= " on delete {$foreign->onDelete}";
  73. }
  74. if (!is_null($foreign->onUpdate)) {
  75. $sql .= " on update {$foreign->onUpdate}";
  76. }
  77. }
  78. return $sql;
  79. }
  80. /**
  81. * Get the SQL for the foreign key.
  82. *
  83. * @param \Illuminate\Support\Fluent $foreign
  84. * @return string
  85. */
  86. protected function getForeignKey($foreign)
  87. {
  88. $on = $this->wrapTable($foreign->on);
  89. // We need to columnize the columns that the foreign key is being defined for
  90. // so that it is a properly formatted list. Once we have done this, we can
  91. // return the foreign key SQL declaration to the calling method for use.
  92. $columns = $this->columnize($foreign->columns);
  93. $onColumns = $this->columnize((array)$foreign->references);
  94. return ", foreign key($columns) references $on($onColumns)";
  95. }
  96. /**
  97. * Get the primary key syntax for a table creation statement.
  98. *
  99. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  100. * @return string|null
  101. */
  102. protected function addPrimaryKeys(Blueprint $blueprint)
  103. {
  104. $primary = $this->getCommandByName($blueprint, 'primary');
  105. if (!is_null($primary)) {
  106. $columns = $this->columnize($primary->columns);
  107. return ", primary key ({$columns})";
  108. }
  109. }
  110. /**
  111. * Compile alter table commands for adding columns.
  112. *
  113. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  114. * @param \Illuminate\Support\Fluent $command
  115. * @return array
  116. */
  117. public function compileAdd(Blueprint $blueprint, Fluent $command)
  118. {
  119. $table = $this->wrapTable($blueprint);
  120. $columns = $this->prefixArray('add column', $this->getColumns($blueprint));
  121. $statements = array();
  122. foreach ($columns as $column) {
  123. $statements[] = 'alter table ' . $table . ' ' . $column;
  124. }
  125. return $statements;
  126. }
  127. /**
  128. * Compile a unique key command.
  129. *
  130. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  131. * @param \Illuminate\Support\Fluent $command
  132. * @return string
  133. */
  134. public function compileUnique(Blueprint $blueprint, Fluent $command)
  135. {
  136. $columns = $this->columnize($command->columns);
  137. $table = $this->wrapTable($blueprint);
  138. return "create unique index {$command->index} on {$table} ({$columns})";
  139. }
  140. /**
  141. * Compile a plain index key command.
  142. *
  143. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  144. * @param \Illuminate\Support\Fluent $command
  145. * @return string
  146. */
  147. public function compileIndex(Blueprint $blueprint, Fluent $command)
  148. {
  149. $columns = $this->columnize($command->columns);
  150. $table = $this->wrapTable($blueprint);
  151. return "create index {$command->index} on {$table} ({$columns})";
  152. }
  153. /**
  154. * Compile a foreign key command.
  155. *
  156. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  157. * @param \Illuminate\Support\Fluent $command
  158. * @return string
  159. */
  160. public function compileForeign(Blueprint $blueprint, Fluent $command)
  161. {
  162. // Handled on table creation...
  163. }
  164. /**
  165. * Compile a drop table command.
  166. *
  167. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  168. * @param \Illuminate\Support\Fluent $command
  169. * @return string
  170. */
  171. public function compileDrop(Blueprint $blueprint, Fluent $command)
  172. {
  173. return 'drop table ' . $this->wrapTable($blueprint);
  174. }
  175. /**
  176. * Compile a drop table (if exists) command.
  177. *
  178. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  179. * @param \Illuminate\Support\Fluent $command
  180. * @return string
  181. */
  182. public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
  183. {
  184. return 'drop table if exists ' . $this->wrapTable($blueprint);
  185. }
  186. /**
  187. * Compile a drop column command.
  188. *
  189. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  190. * @param \Illuminate\Support\Fluent $command
  191. * @param \Illuminate\Database\Connection $connection
  192. * @return array
  193. */
  194. public function compileDropColumn(Blueprint $blueprint, Fluent $command, Connection $connection)
  195. {
  196. $schema = $connection->getDoctrineSchemaManager();
  197. $tableDiff = $this->getDoctrineTableDiff($blueprint, $schema);
  198. foreach ($command->columns as $name) {
  199. $column = $connection->getDoctrineColumn($blueprint->getTable(), $name);
  200. $tableDiff->removedColumns[$name] = $column;
  201. }
  202. return (array)$schema->getDatabasePlatform()->getAlterTableSQL($tableDiff);
  203. }
  204. /**
  205. * Compile a drop unique key command.
  206. *
  207. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  208. * @param \Illuminate\Support\Fluent $command
  209. * @return string
  210. */
  211. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  212. {
  213. return "drop index {$command->index}";
  214. }
  215. /**
  216. * Compile a drop index command.
  217. *
  218. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  219. * @param \Illuminate\Support\Fluent $command
  220. * @return string
  221. */
  222. public function compileDropIndex(Blueprint $blueprint, Fluent $command)
  223. {
  224. return "drop index {$command->index}";
  225. }
  226. /**
  227. * Compile a rename table command.
  228. *
  229. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  230. * @param \Illuminate\Support\Fluent $command
  231. * @return string
  232. */
  233. public function compileRename(Blueprint $blueprint, Fluent $command)
  234. {
  235. $from = $this->wrapTable($blueprint);
  236. return "alter table {$from} rename to " . $this->wrapTable($command->to);
  237. }
  238. /**
  239. * Create the column definition for a char type.
  240. *
  241. * @param \Illuminate\Support\Fluent $column
  242. * @return string
  243. */
  244. protected function typeChar(Fluent $column)
  245. {
  246. return 'varchar';
  247. }
  248. /**
  249. * Create the column definition for a string type.
  250. *
  251. * @param \Illuminate\Support\Fluent $column
  252. * @return string
  253. */
  254. protected function typeString(Fluent $column)
  255. {
  256. return 'varchar';
  257. }
  258. /**
  259. * Create the column definition for a text type.
  260. *
  261. * @param \Illuminate\Support\Fluent $column
  262. * @return string
  263. */
  264. protected function typeText(Fluent $column)
  265. {
  266. return 'text';
  267. }
  268. /**
  269. * Create the column definition for a medium text type.
  270. *
  271. * @param \Illuminate\Support\Fluent $column
  272. * @return string
  273. */
  274. protected function typeMediumText(Fluent $column)
  275. {
  276. return 'text';
  277. }
  278. /**
  279. * Create the column definition for a long text type.
  280. *
  281. * @param \Illuminate\Support\Fluent $column
  282. * @return string
  283. */
  284. protected function typeLongText(Fluent $column)
  285. {
  286. return 'text';
  287. }
  288. /**
  289. * Create the column definition for a integer type.
  290. *
  291. * @param \Illuminate\Support\Fluent $column
  292. * @return string
  293. */
  294. protected function typeInteger(Fluent $column)
  295. {
  296. return 'integer';
  297. }
  298. /**
  299. * Create the column definition for a big integer type.
  300. *
  301. * @param \Illuminate\Support\Fluent $column
  302. * @return string
  303. */
  304. protected function typeBigInteger(Fluent $column)
  305. {
  306. return 'integer';
  307. }
  308. /**
  309. * Create the column definition for a medium integer type.
  310. *
  311. * @param \Illuminate\Support\Fluent $column
  312. * @return string
  313. */
  314. protected function typeMediumInteger(Fluent $column)
  315. {
  316. return 'integer';
  317. }
  318. /**
  319. * Create the column definition for a tiny integer type.
  320. *
  321. * @param \Illuminate\Support\Fluent $column
  322. * @return string
  323. */
  324. protected function typeTinyInteger(Fluent $column)
  325. {
  326. return 'integer';
  327. }
  328. /**
  329. * Create the column definition for a small integer type.
  330. *
  331. * @param \Illuminate\Support\Fluent $column
  332. * @return string
  333. */
  334. protected function typeSmallInteger(Fluent $column)
  335. {
  336. return 'integer';
  337. }
  338. /**
  339. * Create the column definition for a float type.
  340. *
  341. * @param \Illuminate\Support\Fluent $column
  342. * @return string
  343. */
  344. protected function typeFloat(Fluent $column)
  345. {
  346. return 'float';
  347. }
  348. /**
  349. * Create the column definition for a double type.
  350. *
  351. * @param \Illuminate\Support\Fluent $column
  352. * @return string
  353. */
  354. protected function typeDouble(Fluent $column)
  355. {
  356. return 'float';
  357. }
  358. /**
  359. * Create the column definition for a decimal type.
  360. *
  361. * @param \Illuminate\Support\Fluent $column
  362. * @return string
  363. */
  364. protected function typeDecimal(Fluent $column)
  365. {
  366. return 'numeric';
  367. }
  368. /**
  369. * Create the column definition for a boolean type.
  370. *
  371. * @param \Illuminate\Support\Fluent $column
  372. * @return string
  373. */
  374. protected function typeBoolean(Fluent $column)
  375. {
  376. return 'tinyint';
  377. }
  378. /**
  379. * Create the column definition for an enum type.
  380. *
  381. * @param \Illuminate\Support\Fluent $column
  382. * @return string
  383. */
  384. protected function typeEnum(Fluent $column)
  385. {
  386. return 'varchar';
  387. }
  388. /**
  389. * Create the column definition for a json type.
  390. *
  391. * @param \Illuminate\Support\Fluent $column
  392. * @return string
  393. */
  394. protected function typeJson(Fluent $column)
  395. {
  396. return 'text';
  397. }
  398. /**
  399. * Create the column definition for a jsonb type.
  400. *
  401. * @param \Illuminate\Support\Fluent $column
  402. * @return string
  403. */
  404. protected function typeJsonb(Fluent $column)
  405. {
  406. return 'text';
  407. }
  408. /**
  409. * Create the column definition for a date type.
  410. *
  411. * @param \Illuminate\Support\Fluent $column
  412. * @return string
  413. */
  414. protected function typeDate(Fluent $column)
  415. {
  416. return 'date';
  417. }
  418. /**
  419. * Create the column definition for a date-time type.
  420. *
  421. * @param \Illuminate\Support\Fluent $column
  422. * @return string
  423. */
  424. protected function typeDateTime(Fluent $column)
  425. {
  426. return 'datetime';
  427. }
  428. /**
  429. * Create the column definition for a date-time type.
  430. *
  431. * Note: "SQLite does not have a storage class set aside for storing dates and/or times."
  432. * @link https://www.sqlite.org/datatype3.html
  433. *
  434. * @param \Illuminate\Support\Fluent $column
  435. * @return string
  436. */
  437. protected function typeDateTimeTz(Fluent $column)
  438. {
  439. return 'datetime';
  440. }
  441. /**
  442. * Create the column definition for a time type.
  443. *
  444. * @param \Illuminate\Support\Fluent $column
  445. * @return string
  446. */
  447. protected function typeTime(Fluent $column)
  448. {
  449. return 'time';
  450. }
  451. /**
  452. * Create the column definition for a time type.
  453. *
  454. * @param \Illuminate\Support\Fluent $column
  455. * @return string
  456. */
  457. protected function typeTimeTz(Fluent $column)
  458. {
  459. return 'time';
  460. }
  461. /**
  462. * Create the column definition for a timestamp type.
  463. *
  464. * @param \Illuminate\Support\Fluent $column
  465. * @return string
  466. */
  467. protected function typeTimestamp(Fluent $column)
  468. {
  469. return 'datetime';
  470. }
  471. /**
  472. * Create the column definition for a timestamp type.
  473. *
  474. * @param \Illuminate\Support\Fluent $column
  475. * @return string
  476. */
  477. protected function typeTimestampTz(Fluent $column)
  478. {
  479. return 'datetime';
  480. }
  481. /**
  482. * Create the column definition for a binary type.
  483. *
  484. * @param \Illuminate\Support\Fluent $column
  485. * @return string
  486. */
  487. protected function typeBinary(Fluent $column)
  488. {
  489. return 'blob';
  490. }
  491. /**
  492. * Get the SQL for a nullable column modifier.
  493. *
  494. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  495. * @param \Illuminate\Support\Fluent $column
  496. * @return string|null
  497. */
  498. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  499. {
  500. return $column->nullable ? ' null' : ' not null';
  501. }
  502. /**
  503. * Get the SQL for a default column modifier.
  504. *
  505. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  506. * @param \Illuminate\Support\Fluent $column
  507. * @return string|null
  508. */
  509. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  510. {
  511. if (!is_null($column->default)) {
  512. return " default " . $this->getDefaultValue($column->default);
  513. }
  514. }
  515. /**
  516. * Get the SQL for an auto-increment column modifier.
  517. *
  518. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  519. * @param \Illuminate\Support\Fluent $column
  520. * @return string|null
  521. */
  522. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  523. {
  524. if (in_array($column->type, $this->serials) && $column->autoIncrement) {
  525. return ' primary key autoincrement';
  526. }
  527. }
  528. }