PostgresGrammar.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace Illuminate\Database\Query\Grammars;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Database\Query\Builder;
  5. class PostgresGrammar extends Grammar
  6. {
  7. /**
  8. * All of the available clause operators.
  9. *
  10. * @var array
  11. */
  12. protected $operators = [
  13. '=', '<', '>', '<=', '>=', '<>', '!=',
  14. 'like', 'not like', 'between', 'ilike', 'not ilike',
  15. '~', '&', '|', '#', '<<', '>>', '<<=', '>>=',
  16. '&&', '@>', '<@', '?', '?|', '?&', '||', '-', '-', '#-',
  17. 'is distinct from', 'is not distinct from',
  18. ];
  19. /**
  20. * Compile a "where date" clause.
  21. *
  22. * @param \Illuminate\Database\Query\Builder $query
  23. * @param array $where
  24. * @return string
  25. */
  26. protected function whereDate(Builder $query, $where)
  27. {
  28. $value = $this->parameter($where['value']);
  29. return $this->wrap($where['column']).'::date '.$where['operator'].' '.$value;
  30. }
  31. /**
  32. * Compile a "where time" clause.
  33. *
  34. * @param \Illuminate\Database\Query\Builder $query
  35. * @param array $where
  36. * @return string
  37. */
  38. protected function whereTime(Builder $query, $where)
  39. {
  40. $value = $this->parameter($where['value']);
  41. return $this->wrap($where['column']).'::time '.$where['operator'].' '.$value;
  42. }
  43. /**
  44. * Compile a date based where clause.
  45. *
  46. * @param string $type
  47. * @param \Illuminate\Database\Query\Builder $query
  48. * @param array $where
  49. * @return string
  50. */
  51. protected function dateBasedWhere($type, Builder $query, $where)
  52. {
  53. $value = $this->parameter($where['value']);
  54. return 'extract('.$type.' from '.$this->wrap($where['column']).') '.$where['operator'].' '.$value;
  55. }
  56. /**
  57. * Compile a "JSON contains" statement into SQL.
  58. *
  59. * @param string $column
  60. * @param string $value
  61. * @return string
  62. */
  63. protected function compileJsonContains($column, $value)
  64. {
  65. $column = str_replace('->>', '->', $this->wrap($column));
  66. return '('.$column.')::jsonb @> '.$value;
  67. }
  68. /**
  69. * Compile the lock into SQL.
  70. *
  71. * @param \Illuminate\Database\Query\Builder $query
  72. * @param bool|string $value
  73. * @return string
  74. */
  75. protected function compileLock(Builder $query, $value)
  76. {
  77. if (! is_string($value)) {
  78. return $value ? 'for update' : 'for share';
  79. }
  80. return $value;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function compileInsert(Builder $query, array $values)
  86. {
  87. $table = $this->wrapTable($query->from);
  88. return empty($values)
  89. ? "insert into {$table} DEFAULT VALUES"
  90. : parent::compileInsert($query, $values);
  91. }
  92. /**
  93. * Compile an insert and get ID statement into SQL.
  94. *
  95. * @param \Illuminate\Database\Query\Builder $query
  96. * @param array $values
  97. * @param string $sequence
  98. * @return string
  99. */
  100. public function compileInsertGetId(Builder $query, $values, $sequence)
  101. {
  102. if (is_null($sequence)) {
  103. $sequence = 'id';
  104. }
  105. return $this->compileInsert($query, $values).' returning '.$this->wrap($sequence);
  106. }
  107. /**
  108. * Compile an update statement into SQL.
  109. *
  110. * @param \Illuminate\Database\Query\Builder $query
  111. * @param array $values
  112. * @return string
  113. */
  114. public function compileUpdate(Builder $query, $values)
  115. {
  116. $table = $this->wrapTable($query->from);
  117. // Each one of the columns in the update statements needs to be wrapped in the
  118. // keyword identifiers, also a place-holder needs to be created for each of
  119. // the values in the list of bindings so we can make the sets statements.
  120. $columns = $this->compileUpdateColumns($values);
  121. $from = $this->compileUpdateFrom($query);
  122. $where = $this->compileUpdateWheres($query);
  123. return trim("update {$table} set {$columns}{$from} {$where}");
  124. }
  125. /**
  126. * Compile the columns for the update statement.
  127. *
  128. * @param array $values
  129. * @return string
  130. */
  131. protected function compileUpdateColumns($values)
  132. {
  133. // When gathering the columns for an update statement, we'll wrap each of the
  134. // columns and convert it to a parameter value. Then we will concatenate a
  135. // list of the columns that can be added into this update query clauses.
  136. return collect($values)->map(function ($value, $key) {
  137. return $this->wrap($key).' = '.$this->parameter($value);
  138. })->implode(', ');
  139. }
  140. /**
  141. * Compile the "from" clause for an update with a join.
  142. *
  143. * @param \Illuminate\Database\Query\Builder $query
  144. * @return string|null
  145. */
  146. protected function compileUpdateFrom(Builder $query)
  147. {
  148. if (! isset($query->joins)) {
  149. return '';
  150. }
  151. // When using Postgres, updates with joins list the joined tables in the from
  152. // clause, which is different than other systems like MySQL. Here, we will
  153. // compile out the tables that are joined and add them to a from clause.
  154. $froms = collect($query->joins)->map(function ($join) {
  155. return $this->wrapTable($join->table);
  156. })->all();
  157. if (count($froms) > 0) {
  158. return ' from '.implode(', ', $froms);
  159. }
  160. }
  161. /**
  162. * Compile the additional where clauses for updates with joins.
  163. *
  164. * @param \Illuminate\Database\Query\Builder $query
  165. * @return string
  166. */
  167. protected function compileUpdateWheres(Builder $query)
  168. {
  169. $baseWheres = $this->compileWheres($query);
  170. if (! isset($query->joins)) {
  171. return $baseWheres;
  172. }
  173. // Once we compile the join constraints, we will either use them as the where
  174. // clause or append them to the existing base where clauses. If we need to
  175. // strip the leading boolean we will do so when using as the only where.
  176. $joinWheres = $this->compileUpdateJoinWheres($query);
  177. if (trim($baseWheres) == '') {
  178. return 'where '.$this->removeLeadingBoolean($joinWheres);
  179. }
  180. return $baseWheres.' '.$joinWheres;
  181. }
  182. /**
  183. * Compile the "join" clause where clauses for an update.
  184. *
  185. * @param \Illuminate\Database\Query\Builder $query
  186. * @return string
  187. */
  188. protected function compileUpdateJoinWheres(Builder $query)
  189. {
  190. $joinWheres = [];
  191. // Here we will just loop through all of the join constraints and compile them
  192. // all out then implode them. This should give us "where" like syntax after
  193. // everything has been built and then we will join it to the real wheres.
  194. foreach ($query->joins as $join) {
  195. foreach ($join->wheres as $where) {
  196. $method = "where{$where['type']}";
  197. $joinWheres[] = $where['boolean'].' '.$this->$method($query, $where);
  198. }
  199. }
  200. return implode(' ', $joinWheres);
  201. }
  202. /**
  203. * Prepare the bindings for an update statement.
  204. *
  205. * @param array $bindings
  206. * @param array $values
  207. * @return array
  208. */
  209. public function prepareBindingsForUpdate(array $bindings, array $values)
  210. {
  211. // Update statements with "joins" in Postgres use an interesting syntax. We need to
  212. // take all of the bindings and put them on the end of this array since they are
  213. // added to the end of the "where" clause statements as typical where clauses.
  214. $bindingsWithoutJoin = Arr::except($bindings, 'join');
  215. return array_values(
  216. array_merge($values, $bindings['join'], Arr::flatten($bindingsWithoutJoin))
  217. );
  218. }
  219. /**
  220. * Compile a delete statement into SQL.
  221. *
  222. * @param \Illuminate\Database\Query\Builder $query
  223. * @return string
  224. */
  225. public function compileDelete(Builder $query)
  226. {
  227. $table = $this->wrapTable($query->from);
  228. return isset($query->joins)
  229. ? $this->compileDeleteWithJoins($query, $table)
  230. : parent::compileDelete($query);
  231. }
  232. /**
  233. * Compile a delete query that uses joins.
  234. *
  235. * @param \Illuminate\Database\Query\Builder $query
  236. * @param string $table
  237. * @return string
  238. */
  239. protected function compileDeleteWithJoins($query, $table)
  240. {
  241. $using = ' USING '.collect($query->joins)->map(function ($join) {
  242. return $this->wrapTable($join->table);
  243. })->implode(', ');
  244. $where = count($query->wheres) > 0 ? ' '.$this->compileUpdateWheres($query) : '';
  245. return trim("delete from {$table}{$using}{$where}");
  246. }
  247. /**
  248. * Compile a truncate table statement into SQL.
  249. *
  250. * @param \Illuminate\Database\Query\Builder $query
  251. * @return array
  252. */
  253. public function compileTruncate(Builder $query)
  254. {
  255. return ['truncate '.$this->wrapTable($query->from).' restart identity' => []];
  256. }
  257. /**
  258. * Wrap the given JSON selector.
  259. *
  260. * @param string $value
  261. * @return string
  262. */
  263. protected function wrapJsonSelector($value)
  264. {
  265. $path = explode('->', $value);
  266. $field = $this->wrapSegments(explode('.', array_shift($path)));
  267. $wrappedPath = $this->wrapJsonPathAttributes($path);
  268. $attribute = array_pop($wrappedPath);
  269. if (! empty($wrappedPath)) {
  270. return $field.'->'.implode('->', $wrappedPath).'->>'.$attribute;
  271. }
  272. return $field.'->>'.$attribute;
  273. }
  274. /**
  275. * Wrap the attributes of the give JSON path.
  276. *
  277. * @param array $path
  278. * @return array
  279. */
  280. protected function wrapJsonPathAttributes($path)
  281. {
  282. return array_map(function ($attribute) {
  283. return "'$attribute'";
  284. }, $path);
  285. }
  286. }