PageRenderTime 46ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

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