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

https://gitlab.com/techniconline/kmc · PHP · 545 lines · 200 code · 62 blank · 283 comment · 3 complexity · 389d2a151715cc6e0e273763565e4bc4 MD5 · raw file

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