PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/PragmaticLinux/Laravel
PHP | 564 lines | 218 code | 72 blank | 274 comment | 6 complexity | 4bd10a8e1869612f49d1707edfbd9ab7 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. * The possible column modifiers.
  8. *
  9. * @var array
  10. */
  11. protected $modifiers = array('Nullable', 'Default', 'Increment');
  12. /**
  13. * The columns available as serials.
  14. *
  15. * @var array
  16. */
  17. protected $serials = array('bigInteger', 'integer');
  18. /**
  19. * Compile the query to determine if a table exists.
  20. *
  21. * @return string
  22. */
  23. public function compileTableExists()
  24. {
  25. return "select * from sqlite_master where type = 'table' and name = ?";
  26. }
  27. /**
  28. * Compile the query to determine the list of columns.
  29. *
  30. * @param string $table
  31. * @return string
  32. */
  33. public function compileColumnExists($table)
  34. {
  35. return 'pragma table_info('.str_replace('.', '__', $table).')';
  36. }
  37. /**
  38. * Compile a create table command.
  39. *
  40. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  41. * @param \Illuminate\Support\Fluent $command
  42. * @return string
  43. */
  44. public function compileCreate(Blueprint $blueprint, Fluent $command)
  45. {
  46. $columns = implode(', ', $this->getColumns($blueprint));
  47. $sql = 'create table '.$this->wrapTable($blueprint)." ($columns";
  48. // SQLite forces primary keys to be added when the table is initially created
  49. // so we will need to check for a primary key commands and add the columns
  50. // to the table's declaration here so they can be created on the tables.
  51. $sql .= (string) $this->addForeignKeys($blueprint);
  52. $sql .= (string) $this->addPrimaryKeys($blueprint);
  53. return $sql.')';
  54. }
  55. /**
  56. * Get the foreign key syntax for a table creation statement.
  57. *
  58. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  59. * @return string|null
  60. */
  61. protected function addForeignKeys(Blueprint $blueprint)
  62. {
  63. $sql = '';
  64. $foreigns = $this->getCommandsByName($blueprint, 'foreign');
  65. // Once we have all the foreign key commands for the table creation statement
  66. // we'll loop through each of them and add them to the create table SQL we
  67. // are building, since SQLite needs foreign keys on the tables creation.
  68. foreach ($foreigns as $foreign)
  69. {
  70. $sql .= $this->getForeignKey($foreign);
  71. if ( ! is_null($foreign->onDelete))
  72. {
  73. $sql .= " on delete {$foreign->onDelete}";
  74. }
  75. if ( ! is_null($foreign->onUpdate))
  76. {
  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. {
  109. $columns = $this->columnize($primary->columns);
  110. return ", primary key ({$columns})";
  111. }
  112. }
  113. /**
  114. * Compile alter table commands for adding columns
  115. *
  116. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  117. * @param \Illuminate\Support\Fluent $command
  118. * @return array
  119. */
  120. public function compileAdd(Blueprint $blueprint, Fluent $command)
  121. {
  122. $table = $this->wrapTable($blueprint);
  123. $columns = $this->prefixArray('add column', $this->getColumns($blueprint));
  124. $statements = array();
  125. foreach ($columns as $column)
  126. {
  127. $statements[] = 'alter table '.$table.' '.$column;
  128. }
  129. return $statements;
  130. }
  131. /**
  132. * Compile a unique key command.
  133. *
  134. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  135. * @param \Illuminate\Support\Fluent $command
  136. * @return string
  137. */
  138. public function compileUnique(Blueprint $blueprint, Fluent $command)
  139. {
  140. $columns = $this->columnize($command->columns);
  141. $table = $this->wrapTable($blueprint);
  142. return "create unique index {$command->index} on {$table} ({$columns})";
  143. }
  144. /**
  145. * Compile a plain index key command.
  146. *
  147. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  148. * @param \Illuminate\Support\Fluent $command
  149. * @return string
  150. */
  151. public function compileIndex(Blueprint $blueprint, Fluent $command)
  152. {
  153. $columns = $this->columnize($command->columns);
  154. $table = $this->wrapTable($blueprint);
  155. return "create index {$command->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. {
  204. $column = $connection->getDoctrineColumn($blueprint->getTable(), $name);
  205. $tableDiff->removedColumns[$name] = $column;
  206. }
  207. return (array) $schema->getDatabasePlatform()->getAlterTableSQL($tableDiff);
  208. }
  209. /**
  210. * Compile a drop unique key command.
  211. *
  212. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  213. * @param \Illuminate\Support\Fluent $command
  214. * @return string
  215. */
  216. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  217. {
  218. return "drop index {$command->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. return "drop index {$command->index}";
  230. }
  231. /**
  232. * Compile a rename table command.
  233. *
  234. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  235. * @param \Illuminate\Support\Fluent $command
  236. * @return string
  237. */
  238. public function compileRename(Blueprint $blueprint, Fluent $command)
  239. {
  240. $from = $this->wrapTable($blueprint);
  241. return "alter table {$from} rename to ".$this->wrapTable($command->to);
  242. }
  243. /**
  244. * Create the column definition for a char type.
  245. *
  246. * @param \Illuminate\Support\Fluent $column
  247. * @return string
  248. */
  249. protected function typeChar(Fluent $column)
  250. {
  251. return 'varchar';
  252. }
  253. /**
  254. * Create the column definition for a string type.
  255. *
  256. * @param \Illuminate\Support\Fluent $column
  257. * @return string
  258. */
  259. protected function typeString(Fluent $column)
  260. {
  261. return 'varchar';
  262. }
  263. /**
  264. * Create the column definition for a text type.
  265. *
  266. * @param \Illuminate\Support\Fluent $column
  267. * @return string
  268. */
  269. protected function typeText(Fluent $column)
  270. {
  271. return 'text';
  272. }
  273. /**
  274. * Create the column definition for a medium text type.
  275. *
  276. * @param \Illuminate\Support\Fluent $column
  277. * @return string
  278. */
  279. protected function typeMediumText(Fluent $column)
  280. {
  281. return 'text';
  282. }
  283. /**
  284. * Create the column definition for a long text type.
  285. *
  286. * @param \Illuminate\Support\Fluent $column
  287. * @return string
  288. */
  289. protected function typeLongText(Fluent $column)
  290. {
  291. return 'text';
  292. }
  293. /**
  294. * Create the column definition for a integer type.
  295. *
  296. * @param \Illuminate\Support\Fluent $column
  297. * @return string
  298. */
  299. protected function typeInteger(Fluent $column)
  300. {
  301. return 'integer';
  302. }
  303. /**
  304. * Create the column definition for a big integer type.
  305. *
  306. * @param \Illuminate\Support\Fluent $column
  307. * @return string
  308. */
  309. protected function typeBigInteger(Fluent $column)
  310. {
  311. return 'integer';
  312. }
  313. /**
  314. * Create the column definition for a medium integer type.
  315. *
  316. * @param \Illuminate\Support\Fluent $column
  317. * @return string
  318. */
  319. protected function typeMediumInteger(Fluent $column)
  320. {
  321. return 'integer';
  322. }
  323. /**
  324. * Create the column definition for a tiny integer type.
  325. *
  326. * @param \Illuminate\Support\Fluent $column
  327. * @return string
  328. */
  329. protected function typeTinyInteger(Fluent $column)
  330. {
  331. return 'integer';
  332. }
  333. /**
  334. * Create the column definition for a small integer type.
  335. *
  336. * @param \Illuminate\Support\Fluent $column
  337. * @return string
  338. */
  339. protected function typeSmallInteger(Fluent $column)
  340. {
  341. return 'integer';
  342. }
  343. /**
  344. * Create the column definition for a float type.
  345. *
  346. * @param \Illuminate\Support\Fluent $column
  347. * @return string
  348. */
  349. protected function typeFloat(Fluent $column)
  350. {
  351. return 'float';
  352. }
  353. /**
  354. * Create the column definition for a double type.
  355. *
  356. * @param \Illuminate\Support\Fluent $column
  357. * @return string
  358. */
  359. protected function typeDouble(Fluent $column)
  360. {
  361. return 'float';
  362. }
  363. /**
  364. * Create the column definition for a decimal type.
  365. *
  366. * @param \Illuminate\Support\Fluent $column
  367. * @return string
  368. */
  369. protected function typeDecimal(Fluent $column)
  370. {
  371. return 'numeric';
  372. }
  373. /**
  374. * Create the column definition for a boolean type.
  375. *
  376. * @param \Illuminate\Support\Fluent $column
  377. * @return string
  378. */
  379. protected function typeBoolean(Fluent $column)
  380. {
  381. return 'tinyint';
  382. }
  383. /**
  384. * Create the column definition for an enum type.
  385. *
  386. * @param \Illuminate\Support\Fluent $column
  387. * @return string
  388. */
  389. protected function typeEnum(Fluent $column)
  390. {
  391. return 'varchar';
  392. }
  393. /**
  394. * Create the column definition for a json type.
  395. *
  396. * @param \Illuminate\Support\Fluent $column
  397. * @return string
  398. */
  399. protected function typeJson(Fluent $column)
  400. {
  401. return 'text';
  402. }
  403. /**
  404. * Create the column definition for a date type.
  405. *
  406. * @param \Illuminate\Support\Fluent $column
  407. * @return string
  408. */
  409. protected function typeDate(Fluent $column)
  410. {
  411. return 'date';
  412. }
  413. /**
  414. * Create the column definition for a date-time type.
  415. *
  416. * @param \Illuminate\Support\Fluent $column
  417. * @return string
  418. */
  419. protected function typeDateTime(Fluent $column)
  420. {
  421. return 'datetime';
  422. }
  423. /**
  424. * Create the column definition for a time type.
  425. *
  426. * @param \Illuminate\Support\Fluent $column
  427. * @return string
  428. */
  429. protected function typeTime(Fluent $column)
  430. {
  431. return 'time';
  432. }
  433. /**
  434. * Create the column definition for a timestamp type.
  435. *
  436. * @param \Illuminate\Support\Fluent $column
  437. * @return string
  438. */
  439. protected function typeTimestamp(Fluent $column)
  440. {
  441. return 'datetime';
  442. }
  443. /**
  444. * Create the column definition for a binary type.
  445. *
  446. * @param \Illuminate\Support\Fluent $column
  447. * @return string
  448. */
  449. protected function typeBinary(Fluent $column)
  450. {
  451. return 'blob';
  452. }
  453. /**
  454. * Get the SQL for a nullable column modifier.
  455. *
  456. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  457. * @param \Illuminate\Support\Fluent $column
  458. * @return string|null
  459. */
  460. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  461. {
  462. return $column->nullable ? ' null' : ' not null';
  463. }
  464. /**
  465. * Get the SQL for a default column modifier.
  466. *
  467. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  468. * @param \Illuminate\Support\Fluent $column
  469. * @return string|null
  470. */
  471. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  472. {
  473. if ( ! is_null($column->default))
  474. {
  475. return " default ".$this->getDefaultValue($column->default);
  476. }
  477. }
  478. /**
  479. * Get the SQL for an auto-increment column modifier.
  480. *
  481. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  482. * @param \Illuminate\Support\Fluent $column
  483. * @return string|null
  484. */
  485. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  486. {
  487. if (in_array($column->type, $this->serials) && $column->autoIncrement)
  488. {
  489. return ' primary key autoincrement';
  490. }
  491. }
  492. }