PostgresGrammar.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. <?php
  2. namespace Illuminate\Database\Query\Grammars;
  3. use Illuminate\Database\Query\Builder;
  4. use Illuminate\Database\Query\JoinLateralClause;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Str;
  7. class PostgresGrammar extends Grammar
  8. {
  9. /**
  10. * All of the available clause operators.
  11. *
  12. * @var string[]
  13. */
  14. protected $operators = [
  15. '=', '<', '>', '<=', '>=', '<>', '!=',
  16. 'like', 'not like', 'between', 'ilike', 'not ilike',
  17. '~', '&', '|', '#', '<<', '>>', '<<=', '>>=',
  18. '&&', '@>', '<@', '?', '?|', '?&', '||', '-', '@?', '@@', '#-',
  19. 'is distinct from', 'is not distinct from',
  20. ];
  21. /**
  22. * The grammar specific bitwise operators.
  23. *
  24. * @var array
  25. */
  26. protected $bitwiseOperators = [
  27. '~', '&', '|', '#', '<<', '>>', '<<=', '>>=',
  28. ];
  29. /**
  30. * Compile a basic where clause.
  31. *
  32. * @param \Illuminate\Database\Query\Builder $query
  33. * @param array $where
  34. * @return string
  35. */
  36. protected function whereBasic(Builder $query, $where)
  37. {
  38. if (str_contains(strtolower($where['operator']), 'like')) {
  39. return sprintf(
  40. '%s::text %s %s',
  41. $this->wrap($where['column']),
  42. $where['operator'],
  43. $this->parameter($where['value'])
  44. );
  45. }
  46. return parent::whereBasic($query, $where);
  47. }
  48. /**
  49. * Compile a bitwise operator where clause.
  50. *
  51. * @param \Illuminate\Database\Query\Builder $query
  52. * @param array $where
  53. * @return string
  54. */
  55. protected function whereBitwise(Builder $query, $where)
  56. {
  57. $value = $this->parameter($where['value']);
  58. $operator = str_replace('?', '??', $where['operator']);
  59. return '('.$this->wrap($where['column']).' '.$operator.' '.$value.')::bool';
  60. }
  61. /**
  62. * Compile a "where date" clause.
  63. *
  64. * @param \Illuminate\Database\Query\Builder $query
  65. * @param array $where
  66. * @return string
  67. */
  68. protected function whereDate(Builder $query, $where)
  69. {
  70. $value = $this->parameter($where['value']);
  71. return $this->wrap($where['column']).'::date '.$where['operator'].' '.$value;
  72. }
  73. /**
  74. * Compile a "where time" clause.
  75. *
  76. * @param \Illuminate\Database\Query\Builder $query
  77. * @param array $where
  78. * @return string
  79. */
  80. protected function whereTime(Builder $query, $where)
  81. {
  82. $value = $this->parameter($where['value']);
  83. return $this->wrap($where['column']).'::time '.$where['operator'].' '.$value;
  84. }
  85. /**
  86. * Compile a date based where clause.
  87. *
  88. * @param string $type
  89. * @param \Illuminate\Database\Query\Builder $query
  90. * @param array $where
  91. * @return string
  92. */
  93. protected function dateBasedWhere($type, Builder $query, $where)
  94. {
  95. $value = $this->parameter($where['value']);
  96. return 'extract('.$type.' from '.$this->wrap($where['column']).') '.$where['operator'].' '.$value;
  97. }
  98. /**
  99. * Compile a "where fulltext" clause.
  100. *
  101. * @param \Illuminate\Database\Query\Builder $query
  102. * @param array $where
  103. * @return string
  104. */
  105. public function whereFullText(Builder $query, $where)
  106. {
  107. $language = $where['options']['language'] ?? 'english';
  108. if (! in_array($language, $this->validFullTextLanguages())) {
  109. $language = 'english';
  110. }
  111. $columns = collect($where['columns'])->map(function ($column) use ($language) {
  112. return "to_tsvector('{$language}', {$this->wrap($column)})";
  113. })->implode(' || ');
  114. $mode = 'plainto_tsquery';
  115. if (($where['options']['mode'] ?? []) === 'phrase') {
  116. $mode = 'phraseto_tsquery';
  117. }
  118. if (($where['options']['mode'] ?? []) === 'websearch') {
  119. $mode = 'websearch_to_tsquery';
  120. }
  121. return "({$columns}) @@ {$mode}('{$language}', {$this->parameter($where['value'])})";
  122. }
  123. /**
  124. * Get an array of valid full text languages.
  125. *
  126. * @return array
  127. */
  128. protected function validFullTextLanguages()
  129. {
  130. return [
  131. 'simple',
  132. 'arabic',
  133. 'danish',
  134. 'dutch',
  135. 'english',
  136. 'finnish',
  137. 'french',
  138. 'german',
  139. 'hungarian',
  140. 'indonesian',
  141. 'irish',
  142. 'italian',
  143. 'lithuanian',
  144. 'nepali',
  145. 'norwegian',
  146. 'portuguese',
  147. 'romanian',
  148. 'russian',
  149. 'spanish',
  150. 'swedish',
  151. 'tamil',
  152. 'turkish',
  153. ];
  154. }
  155. /**
  156. * Compile the "select *" portion of the query.
  157. *
  158. * @param \Illuminate\Database\Query\Builder $query
  159. * @param array $columns
  160. * @return string|null
  161. */
  162. protected function compileColumns(Builder $query, $columns)
  163. {
  164. // If the query is actually performing an aggregating select, we will let that
  165. // compiler handle the building of the select clauses, as it will need some
  166. // more syntax that is best handled by that function to keep things neat.
  167. if (! is_null($query->aggregate)) {
  168. return;
  169. }
  170. if (is_array($query->distinct)) {
  171. $select = 'select distinct on ('.$this->columnize($query->distinct).') ';
  172. } elseif ($query->distinct) {
  173. $select = 'select distinct ';
  174. } else {
  175. $select = 'select ';
  176. }
  177. return $select.$this->columnize($columns);
  178. }
  179. /**
  180. * Compile a "JSON contains" statement into SQL.
  181. *
  182. * @param string $column
  183. * @param string $value
  184. * @return string
  185. */
  186. protected function compileJsonContains($column, $value)
  187. {
  188. $column = str_replace('->>', '->', $this->wrap($column));
  189. return '('.$column.')::jsonb @> '.$value;
  190. }
  191. /**
  192. * Compile a "JSON contains key" statement into SQL.
  193. *
  194. * @param string $column
  195. * @return string
  196. */
  197. protected function compileJsonContainsKey($column)
  198. {
  199. $segments = explode('->', $column);
  200. $lastSegment = array_pop($segments);
  201. if (filter_var($lastSegment, FILTER_VALIDATE_INT) !== false) {
  202. $i = $lastSegment;
  203. } elseif (preg_match('/\[(-?[0-9]+)\]$/', $lastSegment, $matches)) {
  204. $segments[] = Str::beforeLast($lastSegment, $matches[0]);
  205. $i = $matches[1];
  206. }
  207. $column = str_replace('->>', '->', $this->wrap(implode('->', $segments)));
  208. if (isset($i)) {
  209. return vsprintf('case when %s then %s else false end', [
  210. 'jsonb_typeof(('.$column.")::jsonb) = 'array'",
  211. 'jsonb_array_length(('.$column.')::jsonb) >= '.($i < 0 ? abs($i) : $i + 1),
  212. ]);
  213. }
  214. $key = "'".str_replace("'", "''", $lastSegment)."'";
  215. return 'coalesce(('.$column.')::jsonb ?? '.$key.', false)';
  216. }
  217. /**
  218. * Compile a "JSON length" statement into SQL.
  219. *
  220. * @param string $column
  221. * @param string $operator
  222. * @param string $value
  223. * @return string
  224. */
  225. protected function compileJsonLength($column, $operator, $value)
  226. {
  227. $column = str_replace('->>', '->', $this->wrap($column));
  228. return 'jsonb_array_length(('.$column.')::jsonb) '.$operator.' '.$value;
  229. }
  230. /**
  231. * Compile a single having clause.
  232. *
  233. * @param array $having
  234. * @return string
  235. */
  236. protected function compileHaving(array $having)
  237. {
  238. if ($having['type'] === 'Bitwise') {
  239. return $this->compileHavingBitwise($having);
  240. }
  241. return parent::compileHaving($having);
  242. }
  243. /**
  244. * Compile a having clause involving a bitwise operator.
  245. *
  246. * @param array $having
  247. * @return string
  248. */
  249. protected function compileHavingBitwise($having)
  250. {
  251. $column = $this->wrap($having['column']);
  252. $parameter = $this->parameter($having['value']);
  253. return '('.$column.' '.$having['operator'].' '.$parameter.')::bool';
  254. }
  255. /**
  256. * Compile the lock into SQL.
  257. *
  258. * @param \Illuminate\Database\Query\Builder $query
  259. * @param bool|string $value
  260. * @return string
  261. */
  262. protected function compileLock(Builder $query, $value)
  263. {
  264. if (! is_string($value)) {
  265. return $value ? 'for update' : 'for share';
  266. }
  267. return $value;
  268. }
  269. /**
  270. * Compile an insert ignore statement into SQL.
  271. *
  272. * @param \Illuminate\Database\Query\Builder $query
  273. * @param array $values
  274. * @return string
  275. */
  276. public function compileInsertOrIgnore(Builder $query, array $values)
  277. {
  278. return $this->compileInsert($query, $values).' on conflict do nothing';
  279. }
  280. /**
  281. * Compile an insert ignore statement using a subquery into SQL.
  282. *
  283. * @param \Illuminate\Database\Query\Builder $query
  284. * @param array $columns
  285. * @param string $sql
  286. * @return string
  287. */
  288. public function compileInsertOrIgnoreUsing(Builder $query, array $columns, string $sql)
  289. {
  290. return $this->compileInsertUsing($query, $columns, $sql).' on conflict do nothing';
  291. }
  292. /**
  293. * Compile an insert and get ID statement into SQL.
  294. *
  295. * @param \Illuminate\Database\Query\Builder $query
  296. * @param array $values
  297. * @param string $sequence
  298. * @return string
  299. */
  300. public function compileInsertGetId(Builder $query, $values, $sequence)
  301. {
  302. return $this->compileInsert($query, $values).' returning '.$this->wrap($sequence ?: 'id');
  303. }
  304. /**
  305. * Compile an update statement into SQL.
  306. *
  307. * @param \Illuminate\Database\Query\Builder $query
  308. * @param array $values
  309. * @return string
  310. */
  311. public function compileUpdate(Builder $query, array $values)
  312. {
  313. if (isset($query->joins) || isset($query->limit)) {
  314. return $this->compileUpdateWithJoinsOrLimit($query, $values);
  315. }
  316. return parent::compileUpdate($query, $values);
  317. }
  318. /**
  319. * Compile the columns for an update statement.
  320. *
  321. * @param \Illuminate\Database\Query\Builder $query
  322. * @param array $values
  323. * @return string
  324. */
  325. protected function compileUpdateColumns(Builder $query, array $values)
  326. {
  327. return collect($values)->map(function ($value, $key) {
  328. $column = last(explode('.', $key));
  329. if ($this->isJsonSelector($key)) {
  330. return $this->compileJsonUpdateColumn($column, $value);
  331. }
  332. return $this->wrap($column).' = '.$this->parameter($value);
  333. })->implode(', ');
  334. }
  335. /**
  336. * Compile an "upsert" statement into SQL.
  337. *
  338. * @param \Illuminate\Database\Query\Builder $query
  339. * @param array $values
  340. * @param array $uniqueBy
  341. * @param array $update
  342. * @return string
  343. */
  344. public function compileUpsert(Builder $query, array $values, array $uniqueBy, array $update)
  345. {
  346. $sql = $this->compileInsert($query, $values);
  347. $sql .= ' on conflict ('.$this->columnize($uniqueBy).') do update set ';
  348. $columns = collect($update)->map(function ($value, $key) {
  349. return is_numeric($key)
  350. ? $this->wrap($value).' = '.$this->wrapValue('excluded').'.'.$this->wrap($value)
  351. : $this->wrap($key).' = '.$this->parameter($value);
  352. })->implode(', ');
  353. return $sql.$columns;
  354. }
  355. /**
  356. * Compile a "lateral join" clause.
  357. *
  358. * @param \Illuminate\Database\Query\JoinLateralClause $join
  359. * @param string $expression
  360. * @return string
  361. */
  362. public function compileJoinLateral(JoinLateralClause $join, string $expression): string
  363. {
  364. return trim("{$join->type} join lateral {$expression} on true");
  365. }
  366. /**
  367. * Prepares a JSON column being updated using the JSONB_SET function.
  368. *
  369. * @param string $key
  370. * @param mixed $value
  371. * @return string
  372. */
  373. protected function compileJsonUpdateColumn($key, $value)
  374. {
  375. $segments = explode('->', $key);
  376. $field = $this->wrap(array_shift($segments));
  377. $path = "'{".implode(',', $this->wrapJsonPathAttributes($segments, '"'))."}'";
  378. return "{$field} = jsonb_set({$field}::jsonb, {$path}, {$this->parameter($value)})";
  379. }
  380. /**
  381. * Compile an update from statement into SQL.
  382. *
  383. * @param \Illuminate\Database\Query\Builder $query
  384. * @param array $values
  385. * @return string
  386. */
  387. public function compileUpdateFrom(Builder $query, $values)
  388. {
  389. $table = $this->wrapTable($query->from);
  390. // Each one of the columns in the update statements needs to be wrapped in the
  391. // keyword identifiers, also a place-holder needs to be created for each of
  392. // the values in the list of bindings so we can make the sets statements.
  393. $columns = $this->compileUpdateColumns($query, $values);
  394. $from = '';
  395. if (isset($query->joins)) {
  396. // When using Postgres, updates with joins list the joined tables in the from
  397. // clause, which is different than other systems like MySQL. Here, we will
  398. // compile out the tables that are joined and add them to a from clause.
  399. $froms = collect($query->joins)->map(function ($join) {
  400. return $this->wrapTable($join->table);
  401. })->all();
  402. if (count($froms) > 0) {
  403. $from = ' from '.implode(', ', $froms);
  404. }
  405. }
  406. $where = $this->compileUpdateWheres($query);
  407. return trim("update {$table} set {$columns}{$from} {$where}");
  408. }
  409. /**
  410. * Compile the additional where clauses for updates with joins.
  411. *
  412. * @param \Illuminate\Database\Query\Builder $query
  413. * @return string
  414. */
  415. protected function compileUpdateWheres(Builder $query)
  416. {
  417. $baseWheres = $this->compileWheres($query);
  418. if (! isset($query->joins)) {
  419. return $baseWheres;
  420. }
  421. // Once we compile the join constraints, we will either use them as the where
  422. // clause or append them to the existing base where clauses. If we need to
  423. // strip the leading boolean we will do so when using as the only where.
  424. $joinWheres = $this->compileUpdateJoinWheres($query);
  425. if (trim($baseWheres) == '') {
  426. return 'where '.$this->removeLeadingBoolean($joinWheres);
  427. }
  428. return $baseWheres.' '.$joinWheres;
  429. }
  430. /**
  431. * Compile the "join" clause where clauses for an update.
  432. *
  433. * @param \Illuminate\Database\Query\Builder $query
  434. * @return string
  435. */
  436. protected function compileUpdateJoinWheres(Builder $query)
  437. {
  438. $joinWheres = [];
  439. // Here we will just loop through all of the join constraints and compile them
  440. // all out then implode them. This should give us "where" like syntax after
  441. // everything has been built and then we will join it to the real wheres.
  442. foreach ($query->joins as $join) {
  443. foreach ($join->wheres as $where) {
  444. $method = "where{$where['type']}";
  445. $joinWheres[] = $where['boolean'].' '.$this->$method($query, $where);
  446. }
  447. }
  448. return implode(' ', $joinWheres);
  449. }
  450. /**
  451. * Prepare the bindings for an update statement.
  452. *
  453. * @param array $bindings
  454. * @param array $values
  455. * @return array
  456. */
  457. public function prepareBindingsForUpdateFrom(array $bindings, array $values)
  458. {
  459. $values = collect($values)->map(function ($value, $column) {
  460. return is_array($value) || ($this->isJsonSelector($column) && ! $this->isExpression($value))
  461. ? json_encode($value)
  462. : $value;
  463. })->all();
  464. $bindingsWithoutWhere = Arr::except($bindings, ['select', 'where']);
  465. return array_values(
  466. array_merge($values, $bindings['where'], Arr::flatten($bindingsWithoutWhere))
  467. );
  468. }
  469. /**
  470. * Compile an update statement with joins or limit into SQL.
  471. *
  472. * @param \Illuminate\Database\Query\Builder $query
  473. * @param array $values
  474. * @return string
  475. */
  476. protected function compileUpdateWithJoinsOrLimit(Builder $query, array $values)
  477. {
  478. $table = $this->wrapTable($query->from);
  479. $columns = $this->compileUpdateColumns($query, $values);
  480. $alias = last(preg_split('/\s+as\s+/i', $query->from));
  481. $selectSql = $this->compileSelect($query->select($alias.'.ctid'));
  482. return "update {$table} set {$columns} where {$this->wrap('ctid')} in ({$selectSql})";
  483. }
  484. /**
  485. * Prepare the bindings for an update statement.
  486. *
  487. * @param array $bindings
  488. * @param array $values
  489. * @return array
  490. */
  491. public function prepareBindingsForUpdate(array $bindings, array $values)
  492. {
  493. $values = collect($values)->map(function ($value, $column) {
  494. return is_array($value) || ($this->isJsonSelector($column) && ! $this->isExpression($value))
  495. ? json_encode($value)
  496. : $value;
  497. })->all();
  498. $cleanBindings = Arr::except($bindings, 'select');
  499. return array_values(
  500. array_merge($values, Arr::flatten($cleanBindings))
  501. );
  502. }
  503. /**
  504. * Compile a delete statement into SQL.
  505. *
  506. * @param \Illuminate\Database\Query\Builder $query
  507. * @return string
  508. */
  509. public function compileDelete(Builder $query)
  510. {
  511. if (isset($query->joins) || isset($query->limit)) {
  512. return $this->compileDeleteWithJoinsOrLimit($query);
  513. }
  514. return parent::compileDelete($query);
  515. }
  516. /**
  517. * Compile a delete statement with joins or limit into SQL.
  518. *
  519. * @param \Illuminate\Database\Query\Builder $query
  520. * @return string
  521. */
  522. protected function compileDeleteWithJoinsOrLimit(Builder $query)
  523. {
  524. $table = $this->wrapTable($query->from);
  525. $alias = last(preg_split('/\s+as\s+/i', $query->from));
  526. $selectSql = $this->compileSelect($query->select($alias.'.ctid'));
  527. return "delete from {$table} where {$this->wrap('ctid')} in ({$selectSql})";
  528. }
  529. /**
  530. * Compile a truncate table statement into SQL.
  531. *
  532. * @param \Illuminate\Database\Query\Builder $query
  533. * @return array
  534. */
  535. public function compileTruncate(Builder $query)
  536. {
  537. return ['truncate '.$this->wrapTable($query->from).' restart identity cascade' => []];
  538. }
  539. /**
  540. * Wrap the given JSON selector.
  541. *
  542. * @param string $value
  543. * @return string
  544. */
  545. protected function wrapJsonSelector($value)
  546. {
  547. $path = explode('->', $value);
  548. $field = $this->wrapSegments(explode('.', array_shift($path)));
  549. $wrappedPath = $this->wrapJsonPathAttributes($path);
  550. $attribute = array_pop($wrappedPath);
  551. if (! empty($wrappedPath)) {
  552. return $field.'->'.implode('->', $wrappedPath).'->>'.$attribute;
  553. }
  554. return $field.'->>'.$attribute;
  555. }
  556. /**
  557. * Wrap the given JSON selector for boolean values.
  558. *
  559. * @param string $value
  560. * @return string
  561. */
  562. protected function wrapJsonBooleanSelector($value)
  563. {
  564. $selector = str_replace(
  565. '->>', '->',
  566. $this->wrapJsonSelector($value)
  567. );
  568. return '('.$selector.')::jsonb';
  569. }
  570. /**
  571. * Wrap the given JSON boolean value.
  572. *
  573. * @param string $value
  574. * @return string
  575. */
  576. protected function wrapJsonBooleanValue($value)
  577. {
  578. return "'".$value."'::jsonb";
  579. }
  580. /**
  581. * Wrap the attributes of the given JSON path.
  582. *
  583. * @param array $path
  584. * @return array
  585. */
  586. protected function wrapJsonPathAttributes($path)
  587. {
  588. $quote = func_num_args() === 2 ? func_get_arg(1) : "'";
  589. return collect($path)->map(function ($attribute) {
  590. return $this->parseJsonPathArrayKeys($attribute);
  591. })->collapse()->map(function ($attribute) use ($quote) {
  592. return filter_var($attribute, FILTER_VALIDATE_INT) !== false
  593. ? $attribute
  594. : $quote.$attribute.$quote;
  595. })->all();
  596. }
  597. /**
  598. * Parse the given JSON path attribute for array keys.
  599. *
  600. * @param string $attribute
  601. * @return array
  602. */
  603. protected function parseJsonPathArrayKeys($attribute)
  604. {
  605. if (preg_match('/(\[[^\]]+\])+$/', $attribute, $parts)) {
  606. $key = Str::beforeLast($attribute, $parts[0]);
  607. preg_match_all('/\[([^\]]+)\]/', $parts[0], $keys);
  608. return collect([$key])
  609. ->merge($keys[1])
  610. ->diff('')
  611. ->values()
  612. ->all();
  613. }
  614. return [$attribute];
  615. }
  616. /**
  617. * Substitute the given bindings into the given raw SQL query.
  618. *
  619. * @param string $sql
  620. * @param array $bindings
  621. * @return string
  622. */
  623. public function substituteBindingsIntoRawSql($sql, $bindings)
  624. {
  625. $query = parent::substituteBindingsIntoRawSql($sql, $bindings);
  626. foreach ($this->operators as $operator) {
  627. if (! str_contains($operator, '?')) {
  628. continue;
  629. }
  630. $query = str_replace(str_replace('?', '??', $operator), $operator, $query);
  631. }
  632. return $query;
  633. }
  634. }