PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/fayimora/pp
PHP | 568 lines | 213 code | 66 blank | 289 comment | 5 complexity | c04eeb0fcf3af62035c9bce4172bd3de MD5 | raw file
  1. <?php
  2. namespace Illuminate\Database\Schema\Grammars;
  3. use Illuminate\Support\Fluent;
  4. use Illuminate\Database\Schema\Blueprint;
  5. class PostgresGrammar extends Grammar
  6. {
  7. /**
  8. * The possible column modifiers.
  9. *
  10. * @var array
  11. */
  12. protected $modifiers = ['Increment', 'Nullable', 'Default'];
  13. /**
  14. * The columns available as serials.
  15. *
  16. * @var array
  17. */
  18. protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger'];
  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 information_schema.tables where table_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 "select column_name from information_schema.columns where table_name = '$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 = $blueprint->temporary ? 'create temporary' : 'create';
  49. $sql .= ' table '.$this->wrapTable($blueprint)." ($columns)";
  50. return $sql;
  51. }
  52. /**
  53. * Compile a create table command.
  54. *
  55. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  56. * @param \Illuminate\Support\Fluent $command
  57. * @return string
  58. */
  59. public function compileAdd(Blueprint $blueprint, Fluent $command)
  60. {
  61. $table = $this->wrapTable($blueprint);
  62. $columns = $this->prefixArray('add column', $this->getColumns($blueprint));
  63. return 'alter table '.$table.' '.implode(', ', $columns);
  64. }
  65. /**
  66. * Compile a primary key command.
  67. *
  68. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  69. * @param \Illuminate\Support\Fluent $command
  70. * @return string
  71. */
  72. public function compilePrimary(Blueprint $blueprint, Fluent $command)
  73. {
  74. $columns = $this->columnize($command->columns);
  75. return 'alter table '.$this->wrapTable($blueprint)." add primary key ({$columns})";
  76. }
  77. /**
  78. * Compile a unique key command.
  79. *
  80. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  81. * @param \Illuminate\Support\Fluent $command
  82. * @return string
  83. */
  84. public function compileUnique(Blueprint $blueprint, Fluent $command)
  85. {
  86. $table = $this->wrapTable($blueprint);
  87. $columns = $this->columnize($command->columns);
  88. return "alter table $table add constraint {$command->index} unique ($columns)";
  89. }
  90. /**
  91. * Compile a plain index key command.
  92. *
  93. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  94. * @param \Illuminate\Support\Fluent $command
  95. * @return string
  96. */
  97. public function compileIndex(Blueprint $blueprint, Fluent $command)
  98. {
  99. $columns = $this->columnize($command->columns);
  100. return "create index {$command->index} on ".$this->wrapTable($blueprint)." ({$columns})";
  101. }
  102. /**
  103. * Compile a drop table command.
  104. *
  105. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  106. * @param \Illuminate\Support\Fluent $command
  107. * @return string
  108. */
  109. public function compileDrop(Blueprint $blueprint, Fluent $command)
  110. {
  111. return 'drop table '.$this->wrapTable($blueprint);
  112. }
  113. /**
  114. * Compile a drop table (if exists) command.
  115. *
  116. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  117. * @param \Illuminate\Support\Fluent $command
  118. * @return string
  119. */
  120. public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
  121. {
  122. return 'drop table if exists '.$this->wrapTable($blueprint);
  123. }
  124. /**
  125. * Compile a drop column command.
  126. *
  127. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  128. * @param \Illuminate\Support\Fluent $command
  129. * @return string
  130. */
  131. public function compileDropColumn(Blueprint $blueprint, Fluent $command)
  132. {
  133. $columns = $this->prefixArray('drop column', $this->wrapArray($command->columns));
  134. $table = $this->wrapTable($blueprint);
  135. return 'alter table '.$table.' '.implode(', ', $columns);
  136. }
  137. /**
  138. * Compile a drop primary key command.
  139. *
  140. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  141. * @param \Illuminate\Support\Fluent $command
  142. * @return string
  143. */
  144. public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
  145. {
  146. $table = $blueprint->getTable();
  147. return 'alter table '.$this->wrapTable($blueprint)." drop constraint {$table}_pkey";
  148. }
  149. /**
  150. * Compile a drop unique key command.
  151. *
  152. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  153. * @param \Illuminate\Support\Fluent $command
  154. * @return string
  155. */
  156. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  157. {
  158. $table = $this->wrapTable($blueprint);
  159. return "alter table {$table} drop constraint {$command->index}";
  160. }
  161. /**
  162. * Compile a drop index command.
  163. *
  164. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  165. * @param \Illuminate\Support\Fluent $command
  166. * @return string
  167. */
  168. public function compileDropIndex(Blueprint $blueprint, Fluent $command)
  169. {
  170. return "drop index {$command->index}";
  171. }
  172. /**
  173. * Compile a drop foreign key command.
  174. *
  175. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  176. * @param \Illuminate\Support\Fluent $command
  177. * @return string
  178. */
  179. public function compileDropForeign(Blueprint $blueprint, Fluent $command)
  180. {
  181. $table = $this->wrapTable($blueprint);
  182. return "alter table {$table} drop constraint {$command->index}";
  183. }
  184. /**
  185. * Compile a rename table command.
  186. *
  187. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  188. * @param \Illuminate\Support\Fluent $command
  189. * @return string
  190. */
  191. public function compileRename(Blueprint $blueprint, Fluent $command)
  192. {
  193. $from = $this->wrapTable($blueprint);
  194. return "alter table {$from} rename to ".$this->wrapTable($command->to);
  195. }
  196. /**
  197. * Create the column definition for a char type.
  198. *
  199. * @param \Illuminate\Support\Fluent $column
  200. * @return string
  201. */
  202. protected function typeChar(Fluent $column)
  203. {
  204. return "char({$column->length})";
  205. }
  206. /**
  207. * Create the column definition for a string type.
  208. *
  209. * @param \Illuminate\Support\Fluent $column
  210. * @return string
  211. */
  212. protected function typeString(Fluent $column)
  213. {
  214. return "varchar({$column->length})";
  215. }
  216. /**
  217. * Create the column definition for a text type.
  218. *
  219. * @param \Illuminate\Support\Fluent $column
  220. * @return string
  221. */
  222. protected function typeText(Fluent $column)
  223. {
  224. return 'text';
  225. }
  226. /**
  227. * Create the column definition for a medium text type.
  228. *
  229. * @param \Illuminate\Support\Fluent $column
  230. * @return string
  231. */
  232. protected function typeMediumText(Fluent $column)
  233. {
  234. return 'text';
  235. }
  236. /**
  237. * Create the column definition for a long text type.
  238. *
  239. * @param \Illuminate\Support\Fluent $column
  240. * @return string
  241. */
  242. protected function typeLongText(Fluent $column)
  243. {
  244. return 'text';
  245. }
  246. /**
  247. * Create the column definition for a integer type.
  248. *
  249. * @param \Illuminate\Support\Fluent $column
  250. * @return string
  251. */
  252. protected function typeInteger(Fluent $column)
  253. {
  254. return $column->autoIncrement ? 'serial' : 'integer';
  255. }
  256. /**
  257. * Create the column definition for a big integer type.
  258. *
  259. * @param \Illuminate\Support\Fluent $column
  260. * @return string
  261. */
  262. protected function typeBigInteger(Fluent $column)
  263. {
  264. return $column->autoIncrement ? 'bigserial' : 'bigint';
  265. }
  266. /**
  267. * Create the column definition for a medium integer type.
  268. *
  269. * @param \Illuminate\Support\Fluent $column
  270. * @return string
  271. */
  272. protected function typeMediumInteger(Fluent $column)
  273. {
  274. return $column->autoIncrement ? 'serial' : 'integer';
  275. }
  276. /**
  277. * Create the column definition for a tiny integer type.
  278. *
  279. * @param \Illuminate\Support\Fluent $column
  280. * @return string
  281. */
  282. protected function typeTinyInteger(Fluent $column)
  283. {
  284. return $column->autoIncrement ? 'smallserial' : 'smallint';
  285. }
  286. /**
  287. * Create the column definition for a small integer type.
  288. *
  289. * @param \Illuminate\Support\Fluent $column
  290. * @return string
  291. */
  292. protected function typeSmallInteger(Fluent $column)
  293. {
  294. return $column->autoIncrement ? 'smallserial' : 'smallint';
  295. }
  296. /**
  297. * Create the column definition for a float type.
  298. *
  299. * @param \Illuminate\Support\Fluent $column
  300. * @return string
  301. */
  302. protected function typeFloat(Fluent $column)
  303. {
  304. return $this->typeDouble($column);
  305. }
  306. /**
  307. * Create the column definition for a double type.
  308. *
  309. * @param \Illuminate\Support\Fluent $column
  310. * @return string
  311. */
  312. protected function typeDouble(Fluent $column)
  313. {
  314. return 'double precision';
  315. }
  316. /**
  317. * Create the column definition for a decimal type.
  318. *
  319. * @param \Illuminate\Support\Fluent $column
  320. * @return string
  321. */
  322. protected function typeDecimal(Fluent $column)
  323. {
  324. return "decimal({$column->total}, {$column->places})";
  325. }
  326. /**
  327. * Create the column definition for a boolean type.
  328. *
  329. * @param \Illuminate\Support\Fluent $column
  330. * @return string
  331. */
  332. protected function typeBoolean(Fluent $column)
  333. {
  334. return 'boolean';
  335. }
  336. /**
  337. * Create the column definition for an enum type.
  338. *
  339. * @param \Illuminate\Support\Fluent $column
  340. * @return string
  341. */
  342. protected function typeEnum(Fluent $column)
  343. {
  344. $allowed = array_map(function ($a) {
  345. return "'".$a."'";
  346. }, $column->allowed);
  347. return "varchar(255) check (\"{$column->name}\" in (".implode(', ', $allowed).'))';
  348. }
  349. /**
  350. * Create the column definition for a json type.
  351. *
  352. * @param \Illuminate\Support\Fluent $column
  353. * @return string
  354. */
  355. protected function typeJson(Fluent $column)
  356. {
  357. return 'json';
  358. }
  359. /**
  360. * Create the column definition for a jsonb type.
  361. *
  362. * @param \Illuminate\Support\Fluent $column
  363. * @return string
  364. */
  365. protected function typeJsonb(Fluent $column)
  366. {
  367. return 'jsonb';
  368. }
  369. /**
  370. * Create the column definition for a date type.
  371. *
  372. * @param \Illuminate\Support\Fluent $column
  373. * @return string
  374. */
  375. protected function typeDate(Fluent $column)
  376. {
  377. return 'date';
  378. }
  379. /**
  380. * Create the column definition for a date-time type.
  381. *
  382. * @param \Illuminate\Support\Fluent $column
  383. * @return string
  384. */
  385. protected function typeDateTime(Fluent $column)
  386. {
  387. return 'timestamp(0) without time zone';
  388. }
  389. /**
  390. * Create the column definition for a date-time type.
  391. *
  392. * @param \Illuminate\Support\Fluent $column
  393. * @return string
  394. */
  395. protected function typeDateTimeTz(Fluent $column)
  396. {
  397. return 'timestamp(0) with time zone';
  398. }
  399. /**
  400. * Create the column definition for a time type.
  401. *
  402. * @param \Illuminate\Support\Fluent $column
  403. * @return string
  404. */
  405. protected function typeTime(Fluent $column)
  406. {
  407. return 'time(0) without time zone';
  408. }
  409. /**
  410. * Create the column definition for a time type.
  411. *
  412. * @param \Illuminate\Support\Fluent $column
  413. * @return string
  414. */
  415. protected function typeTimeTz(Fluent $column)
  416. {
  417. return 'time(0) with time zone';
  418. }
  419. /**
  420. * Create the column definition for a timestamp type.
  421. *
  422. * @param \Illuminate\Support\Fluent $column
  423. * @return string
  424. */
  425. protected function typeTimestamp(Fluent $column)
  426. {
  427. if ($column->useCurrent) {
  428. return 'timestamp(0) without time zone default CURRENT_TIMESTAMP(0)';
  429. }
  430. return 'timestamp(0) without time zone';
  431. }
  432. /**
  433. * Create the column definition for a timestamp type.
  434. *
  435. * @param \Illuminate\Support\Fluent $column
  436. * @return string
  437. */
  438. protected function typeTimestampTz(Fluent $column)
  439. {
  440. if ($column->useCurrent) {
  441. return 'timestamp(0) with time zone default CURRENT_TIMESTAMP(0)';
  442. }
  443. return 'timestamp(0) with time zone';
  444. }
  445. /**
  446. * Create the column definition for a binary type.
  447. *
  448. * @param \Illuminate\Support\Fluent $column
  449. * @return string
  450. */
  451. protected function typeBinary(Fluent $column)
  452. {
  453. return 'bytea';
  454. }
  455. /**
  456. * Create the column definition for a uuid type.
  457. *
  458. * @param \Illuminate\Support\Fluent $column
  459. * @return string
  460. */
  461. protected function typeUuid(Fluent $column)
  462. {
  463. return 'uuid';
  464. }
  465. /**
  466. * Get the SQL for a nullable column modifier.
  467. *
  468. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  469. * @param \Illuminate\Support\Fluent $column
  470. * @return string|null
  471. */
  472. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  473. {
  474. return $column->nullable ? ' null' : ' not null';
  475. }
  476. /**
  477. * Get the SQL for a default column modifier.
  478. *
  479. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  480. * @param \Illuminate\Support\Fluent $column
  481. * @return string|null
  482. */
  483. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  484. {
  485. if (! is_null($column->default)) {
  486. return ' default '.$this->getDefaultValue($column->default);
  487. }
  488. }
  489. /**
  490. * Get the SQL for an auto-increment column modifier.
  491. *
  492. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  493. * @param \Illuminate\Support\Fluent $column
  494. * @return string|null
  495. */
  496. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  497. {
  498. if (in_array($column->type, $this->serials) && $column->autoIncrement) {
  499. return ' primary key';
  500. }
  501. }
  502. }