Grammar.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. <?php
  2. namespace Illuminate\Database\Query\Grammars;
  3. use RuntimeException;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Str;
  6. use Illuminate\Database\Query\Builder;
  7. use Illuminate\Database\Query\JoinClause;
  8. use Illuminate\Database\Grammar as BaseGrammar;
  9. class Grammar extends BaseGrammar
  10. {
  11. /**
  12. * The grammar specific operators.
  13. *
  14. * @var array
  15. */
  16. protected $operators = [];
  17. /**
  18. * The components that make up a select clause.
  19. *
  20. * @var array
  21. */
  22. protected $selectComponents = [
  23. 'aggregate',
  24. 'columns',
  25. 'from',
  26. 'joins',
  27. 'wheres',
  28. 'groups',
  29. 'havings',
  30. 'orders',
  31. 'limit',
  32. 'offset',
  33. 'unions',
  34. 'lock',
  35. ];
  36. /**
  37. * Compile a select query into SQL.
  38. *
  39. * @param \Illuminate\Database\Query\Builder $query
  40. * @return string
  41. */
  42. public function compileSelect(Builder $query)
  43. {
  44. // If the query does not have any columns set, we'll set the columns to the
  45. // * character to just get all of the columns from the database. Then we
  46. // can build the query and concatenate all the pieces together as one.
  47. $original = $query->columns;
  48. if (is_null($query->columns)) {
  49. $query->columns = ['*'];
  50. }
  51. // To compile the query, we'll spin through each component of the query and
  52. // see if that component exists. If it does we'll just call the compiler
  53. // function for the component which is responsible for making the SQL.
  54. $sql = trim($this->concatenate(
  55. $this->compileComponents($query))
  56. );
  57. $query->columns = $original;
  58. return $sql;
  59. }
  60. /**
  61. * Compile the components necessary for a select clause.
  62. *
  63. * @param \Illuminate\Database\Query\Builder $query
  64. * @return array
  65. */
  66. protected function compileComponents(Builder $query)
  67. {
  68. $sql = [];
  69. foreach ($this->selectComponents as $component) {
  70. // To compile the query, we'll spin through each component of the query and
  71. // see if that component exists. If it does we'll just call the compiler
  72. // function for the component which is responsible for making the SQL.
  73. if (! is_null($query->$component)) {
  74. $method = 'compile'.ucfirst($component);
  75. $sql[$component] = $this->$method($query, $query->$component);
  76. }
  77. }
  78. return $sql;
  79. }
  80. /**
  81. * Compile an aggregated select clause.
  82. *
  83. * @param \Illuminate\Database\Query\Builder $query
  84. * @param array $aggregate
  85. * @return string
  86. */
  87. protected function compileAggregate(Builder $query, $aggregate)
  88. {
  89. $column = $this->columnize($aggregate['columns']);
  90. // If the query has a "distinct" constraint and we're not asking for all columns
  91. // we need to prepend "distinct" onto the column name so that the query takes
  92. // it into account when it performs the aggregating operations on the data.
  93. if ($query->distinct && $column !== '*') {
  94. $column = 'distinct '.$column;
  95. }
  96. return 'select '.$aggregate['function'].'('.$column.') as aggregate';
  97. }
  98. /**
  99. * Compile the "select *" portion of the query.
  100. *
  101. * @param \Illuminate\Database\Query\Builder $query
  102. * @param array $columns
  103. * @return string|null
  104. */
  105. protected function compileColumns(Builder $query, $columns)
  106. {
  107. // If the query is actually performing an aggregating select, we will let that
  108. // compiler handle the building of the select clauses, as it will need some
  109. // more syntax that is best handled by that function to keep things neat.
  110. if (! is_null($query->aggregate)) {
  111. return;
  112. }
  113. $select = $query->distinct ? 'select distinct ' : 'select ';
  114. return $select.$this->columnize($columns);
  115. }
  116. /**
  117. * Compile the "from" portion of the query.
  118. *
  119. * @param \Illuminate\Database\Query\Builder $query
  120. * @param string $table
  121. * @return string
  122. */
  123. protected function compileFrom(Builder $query, $table)
  124. {
  125. return 'from '.$this->wrapTable($table);
  126. }
  127. /**
  128. * Compile the "join" portions of the query.
  129. *
  130. * @param \Illuminate\Database\Query\Builder $query
  131. * @param array $joins
  132. * @return string
  133. */
  134. protected function compileJoins(Builder $query, $joins)
  135. {
  136. return collect($joins)->map(function ($join) use ($query) {
  137. $table = $this->wrapTable($join->table);
  138. $nestedJoins = is_null($join->joins) ? '' : ' '.$this->compileJoins($query, $join->joins);
  139. return trim("{$join->type} join {$table}{$nestedJoins} {$this->compileWheres($join)}");
  140. })->implode(' ');
  141. }
  142. /**
  143. * Compile the "where" portions of the query.
  144. *
  145. * @param \Illuminate\Database\Query\Builder $query
  146. * @return string
  147. */
  148. protected function compileWheres(Builder $query)
  149. {
  150. // Each type of where clauses has its own compiler function which is responsible
  151. // for actually creating the where clauses SQL. This helps keep the code nice
  152. // and maintainable since each clause has a very small method that it uses.
  153. if (is_null($query->wheres)) {
  154. return '';
  155. }
  156. // If we actually have some where clauses, we will strip off the first boolean
  157. // operator, which is added by the query builders for convenience so we can
  158. // avoid checking for the first clauses in each of the compilers methods.
  159. if (count($sql = $this->compileWheresToArray($query)) > 0) {
  160. return $this->concatenateWhereClauses($query, $sql);
  161. }
  162. return '';
  163. }
  164. /**
  165. * Get an array of all the where clauses for the query.
  166. *
  167. * @param \Illuminate\Database\Query\Builder $query
  168. * @return array
  169. */
  170. protected function compileWheresToArray($query)
  171. {
  172. return collect($query->wheres)->map(function ($where) use ($query) {
  173. return $where['boolean'].' '.$this->{"where{$where['type']}"}($query, $where);
  174. })->all();
  175. }
  176. /**
  177. * Format the where clause statements into one string.
  178. *
  179. * @param \Illuminate\Database\Query\Builder $query
  180. * @param array $sql
  181. * @return string
  182. */
  183. protected function concatenateWhereClauses($query, $sql)
  184. {
  185. $conjunction = $query instanceof JoinClause ? 'on' : 'where';
  186. return $conjunction.' '.$this->removeLeadingBoolean(implode(' ', $sql));
  187. }
  188. /**
  189. * Compile a raw where clause.
  190. *
  191. * @param \Illuminate\Database\Query\Builder $query
  192. * @param array $where
  193. * @return string
  194. */
  195. protected function whereRaw(Builder $query, $where)
  196. {
  197. return $where['sql'];
  198. }
  199. /**
  200. * Compile a basic where clause.
  201. *
  202. * @param \Illuminate\Database\Query\Builder $query
  203. * @param array $where
  204. * @return string
  205. */
  206. protected function whereBasic(Builder $query, $where)
  207. {
  208. $value = $this->parameter($where['value']);
  209. return $this->wrap($where['column']).' '.$where['operator'].' '.$value;
  210. }
  211. /**
  212. * Compile a "where in" clause.
  213. *
  214. * @param \Illuminate\Database\Query\Builder $query
  215. * @param array $where
  216. * @return string
  217. */
  218. protected function whereIn(Builder $query, $where)
  219. {
  220. if (! empty($where['values'])) {
  221. return $this->wrap($where['column']).' in ('.$this->parameterize($where['values']).')';
  222. }
  223. return '0 = 1';
  224. }
  225. /**
  226. * Compile a "where not in" clause.
  227. *
  228. * @param \Illuminate\Database\Query\Builder $query
  229. * @param array $where
  230. * @return string
  231. */
  232. protected function whereNotIn(Builder $query, $where)
  233. {
  234. if (! empty($where['values'])) {
  235. return $this->wrap($where['column']).' not in ('.$this->parameterize($where['values']).')';
  236. }
  237. return '1 = 1';
  238. }
  239. /**
  240. * Compile a where in sub-select clause.
  241. *
  242. * @param \Illuminate\Database\Query\Builder $query
  243. * @param array $where
  244. * @return string
  245. */
  246. protected function whereInSub(Builder $query, $where)
  247. {
  248. return $this->wrap($where['column']).' in ('.$this->compileSelect($where['query']).')';
  249. }
  250. /**
  251. * Compile a where not in sub-select clause.
  252. *
  253. * @param \Illuminate\Database\Query\Builder $query
  254. * @param array $where
  255. * @return string
  256. */
  257. protected function whereNotInSub(Builder $query, $where)
  258. {
  259. return $this->wrap($where['column']).' not in ('.$this->compileSelect($where['query']).')';
  260. }
  261. /**
  262. * Compile a "where null" clause.
  263. *
  264. * @param \Illuminate\Database\Query\Builder $query
  265. * @param array $where
  266. * @return string
  267. */
  268. protected function whereNull(Builder $query, $where)
  269. {
  270. return $this->wrap($where['column']).' is null';
  271. }
  272. /**
  273. * Compile a "where not null" clause.
  274. *
  275. * @param \Illuminate\Database\Query\Builder $query
  276. * @param array $where
  277. * @return string
  278. */
  279. protected function whereNotNull(Builder $query, $where)
  280. {
  281. return $this->wrap($where['column']).' is not null';
  282. }
  283. /**
  284. * Compile a "between" where clause.
  285. *
  286. * @param \Illuminate\Database\Query\Builder $query
  287. * @param array $where
  288. * @return string
  289. */
  290. protected function whereBetween(Builder $query, $where)
  291. {
  292. $between = $where['not'] ? 'not between' : 'between';
  293. $min = $this->parameter(reset($where['values']));
  294. $max = $this->parameter(end($where['values']));
  295. return $this->wrap($where['column']).' '.$between.' '.$min.' and '.$max;
  296. }
  297. /**
  298. * Compile a "where date" clause.
  299. *
  300. * @param \Illuminate\Database\Query\Builder $query
  301. * @param array $where
  302. * @return string
  303. */
  304. protected function whereDate(Builder $query, $where)
  305. {
  306. return $this->dateBasedWhere('date', $query, $where);
  307. }
  308. /**
  309. * Compile a "where time" clause.
  310. *
  311. * @param \Illuminate\Database\Query\Builder $query
  312. * @param array $where
  313. * @return string
  314. */
  315. protected function whereTime(Builder $query, $where)
  316. {
  317. return $this->dateBasedWhere('time', $query, $where);
  318. }
  319. /**
  320. * Compile a "where day" clause.
  321. *
  322. * @param \Illuminate\Database\Query\Builder $query
  323. * @param array $where
  324. * @return string
  325. */
  326. protected function whereDay(Builder $query, $where)
  327. {
  328. return $this->dateBasedWhere('day', $query, $where);
  329. }
  330. /**
  331. * Compile a "where month" clause.
  332. *
  333. * @param \Illuminate\Database\Query\Builder $query
  334. * @param array $where
  335. * @return string
  336. */
  337. protected function whereMonth(Builder $query, $where)
  338. {
  339. return $this->dateBasedWhere('month', $query, $where);
  340. }
  341. /**
  342. * Compile a "where year" clause.
  343. *
  344. * @param \Illuminate\Database\Query\Builder $query
  345. * @param array $where
  346. * @return string
  347. */
  348. protected function whereYear(Builder $query, $where)
  349. {
  350. return $this->dateBasedWhere('year', $query, $where);
  351. }
  352. /**
  353. * Compile a date based where clause.
  354. *
  355. * @param string $type
  356. * @param \Illuminate\Database\Query\Builder $query
  357. * @param array $where
  358. * @return string
  359. */
  360. protected function dateBasedWhere($type, Builder $query, $where)
  361. {
  362. $value = $this->parameter($where['value']);
  363. return $type.'('.$this->wrap($where['column']).') '.$where['operator'].' '.$value;
  364. }
  365. /**
  366. * Compile a where clause comparing two columns..
  367. *
  368. * @param \Illuminate\Database\Query\Builder $query
  369. * @param array $where
  370. * @return string
  371. */
  372. protected function whereColumn(Builder $query, $where)
  373. {
  374. return $this->wrap($where['first']).' '.$where['operator'].' '.$this->wrap($where['second']);
  375. }
  376. /**
  377. * Compile a nested where clause.
  378. *
  379. * @param \Illuminate\Database\Query\Builder $query
  380. * @param array $where
  381. * @return string
  382. */
  383. protected function whereNested(Builder $query, $where)
  384. {
  385. // Here we will calculate what portion of the string we need to remove. If this
  386. // is a join clause query, we need to remove the "on" portion of the SQL and
  387. // if it is a normal query we need to take the leading "where" of queries.
  388. $offset = $query instanceof JoinClause ? 3 : 6;
  389. return '('.substr($this->compileWheres($where['query']), $offset).')';
  390. }
  391. /**
  392. * Compile a where condition with a sub-select.
  393. *
  394. * @param \Illuminate\Database\Query\Builder $query
  395. * @param array $where
  396. * @return string
  397. */
  398. protected function whereSub(Builder $query, $where)
  399. {
  400. $select = $this->compileSelect($where['query']);
  401. return $this->wrap($where['column']).' '.$where['operator']." ($select)";
  402. }
  403. /**
  404. * Compile a where exists clause.
  405. *
  406. * @param \Illuminate\Database\Query\Builder $query
  407. * @param array $where
  408. * @return string
  409. */
  410. protected function whereExists(Builder $query, $where)
  411. {
  412. return 'exists ('.$this->compileSelect($where['query']).')';
  413. }
  414. /**
  415. * Compile a where exists clause.
  416. *
  417. * @param \Illuminate\Database\Query\Builder $query
  418. * @param array $where
  419. * @return string
  420. */
  421. protected function whereNotExists(Builder $query, $where)
  422. {
  423. return 'not exists ('.$this->compileSelect($where['query']).')';
  424. }
  425. /**
  426. * Compile a where row values condition.
  427. *
  428. * @param \Illuminate\Database\Query\Builder $query
  429. * @param array $where
  430. * @return string
  431. */
  432. protected function whereRowValues(Builder $query, $where)
  433. {
  434. $values = $this->parameterize($where['values']);
  435. return '('.implode(', ', $where['columns']).') '.$where['operator'].' ('.$values.')';
  436. }
  437. /**
  438. * Compile a "where JSON contains" clause.
  439. *
  440. * @param \Illuminate\Database\Query\Builder $query
  441. * @param array $where
  442. * @return string
  443. */
  444. protected function whereJsonContains(Builder $query, $where)
  445. {
  446. $not = $where['not'] ? 'not ' : '';
  447. return $not.$this->compileJsonContains(
  448. $where['column'], $this->parameter($where['value'])
  449. );
  450. }
  451. /**
  452. * Compile a "JSON contains" statement into SQL.
  453. *
  454. * @param string $column
  455. * @param string $value
  456. * @return string
  457. * @throws \RuntimeException
  458. */
  459. protected function compileJsonContains($column, $value)
  460. {
  461. throw new RuntimeException('This database engine does not support JSON contains operations.');
  462. }
  463. /**
  464. * Prepare the binding for a "JSON contains" statement.
  465. *
  466. * @param mixed $binding
  467. * @return string
  468. */
  469. public function prepareBindingForJsonContains($binding)
  470. {
  471. return json_encode($binding);
  472. }
  473. /**
  474. * Compile the "group by" portions of the query.
  475. *
  476. * @param \Illuminate\Database\Query\Builder $query
  477. * @param array $groups
  478. * @return string
  479. */
  480. protected function compileGroups(Builder $query, $groups)
  481. {
  482. return 'group by '.$this->columnize($groups);
  483. }
  484. /**
  485. * Compile the "having" portions of the query.
  486. *
  487. * @param \Illuminate\Database\Query\Builder $query
  488. * @param array $havings
  489. * @return string
  490. */
  491. protected function compileHavings(Builder $query, $havings)
  492. {
  493. $sql = implode(' ', array_map([$this, 'compileHaving'], $havings));
  494. return 'having '.$this->removeLeadingBoolean($sql);
  495. }
  496. /**
  497. * Compile a single having clause.
  498. *
  499. * @param array $having
  500. * @return string
  501. */
  502. protected function compileHaving(array $having)
  503. {
  504. // If the having clause is "raw", we can just return the clause straight away
  505. // without doing any more processing on it. Otherwise, we will compile the
  506. // clause into SQL based on the components that make it up from builder.
  507. if ($having['type'] === 'Raw') {
  508. return $having['boolean'].' '.$having['sql'];
  509. }
  510. return $this->compileBasicHaving($having);
  511. }
  512. /**
  513. * Compile a basic having clause.
  514. *
  515. * @param array $having
  516. * @return string
  517. */
  518. protected function compileBasicHaving($having)
  519. {
  520. $column = $this->wrap($having['column']);
  521. $parameter = $this->parameter($having['value']);
  522. return $having['boolean'].' '.$column.' '.$having['operator'].' '.$parameter;
  523. }
  524. /**
  525. * Compile the "order by" portions of the query.
  526. *
  527. * @param \Illuminate\Database\Query\Builder $query
  528. * @param array $orders
  529. * @return string
  530. */
  531. protected function compileOrders(Builder $query, $orders)
  532. {
  533. if (! empty($orders)) {
  534. return 'order by '.implode(', ', $this->compileOrdersToArray($query, $orders));
  535. }
  536. return '';
  537. }
  538. /**
  539. * Compile the query orders to an array.
  540. *
  541. * @param \Illuminate\Database\Query\Builder $query
  542. * @param array $orders
  543. * @return array
  544. */
  545. protected function compileOrdersToArray(Builder $query, $orders)
  546. {
  547. return array_map(function ($order) {
  548. return ! isset($order['sql'])
  549. ? $this->wrap($order['column']).' '.$order['direction']
  550. : $order['sql'];
  551. }, $orders);
  552. }
  553. /**
  554. * Compile the random statement into SQL.
  555. *
  556. * @param string $seed
  557. * @return string
  558. */
  559. public function compileRandom($seed)
  560. {
  561. return 'RANDOM()';
  562. }
  563. /**
  564. * Compile the "limit" portions of the query.
  565. *
  566. * @param \Illuminate\Database\Query\Builder $query
  567. * @param int $limit
  568. * @return string
  569. */
  570. protected function compileLimit(Builder $query, $limit)
  571. {
  572. return 'limit '.(int) $limit;
  573. }
  574. /**
  575. * Compile the "offset" portions of the query.
  576. *
  577. * @param \Illuminate\Database\Query\Builder $query
  578. * @param int $offset
  579. * @return string
  580. */
  581. protected function compileOffset(Builder $query, $offset)
  582. {
  583. return 'offset '.(int) $offset;
  584. }
  585. /**
  586. * Compile the "union" queries attached to the main query.
  587. *
  588. * @param \Illuminate\Database\Query\Builder $query
  589. * @return string
  590. */
  591. protected function compileUnions(Builder $query)
  592. {
  593. $sql = '';
  594. foreach ($query->unions as $union) {
  595. $sql .= $this->compileUnion($union);
  596. }
  597. if (! empty($query->unionOrders)) {
  598. $sql .= ' '.$this->compileOrders($query, $query->unionOrders);
  599. }
  600. if (isset($query->unionLimit)) {
  601. $sql .= ' '.$this->compileLimit($query, $query->unionLimit);
  602. }
  603. if (isset($query->unionOffset)) {
  604. $sql .= ' '.$this->compileOffset($query, $query->unionOffset);
  605. }
  606. return ltrim($sql);
  607. }
  608. /**
  609. * Compile a single union statement.
  610. *
  611. * @param array $union
  612. * @return string
  613. */
  614. protected function compileUnion(array $union)
  615. {
  616. $conjunction = $union['all'] ? ' union all ' : ' union ';
  617. return $conjunction.$union['query']->toSql();
  618. }
  619. /**
  620. * Compile an exists statement into SQL.
  621. *
  622. * @param \Illuminate\Database\Query\Builder $query
  623. * @return string
  624. */
  625. public function compileExists(Builder $query)
  626. {
  627. $select = $this->compileSelect($query);
  628. return "select exists({$select}) as {$this->wrap('exists')}";
  629. }
  630. /**
  631. * Compile an insert statement into SQL.
  632. *
  633. * @param \Illuminate\Database\Query\Builder $query
  634. * @param array $values
  635. * @return string
  636. */
  637. public function compileInsert(Builder $query, array $values)
  638. {
  639. // Essentially we will force every insert to be treated as a batch insert which
  640. // simply makes creating the SQL easier for us since we can utilize the same
  641. // basic routine regardless of an amount of records given to us to insert.
  642. $table = $this->wrapTable($query->from);
  643. if (! is_array(reset($values))) {
  644. $values = [$values];
  645. }
  646. $columns = $this->columnize(array_keys(reset($values)));
  647. // We need to build a list of parameter place-holders of values that are bound
  648. // to the query. Each insert should have the exact same amount of parameter
  649. // bindings so we will loop through the record and parameterize them all.
  650. $parameters = collect($values)->map(function ($record) {
  651. return '('.$this->parameterize($record).')';
  652. })->implode(', ');
  653. return "insert into $table ($columns) values $parameters";
  654. }
  655. /**
  656. * Compile an insert and get ID statement into SQL.
  657. *
  658. * @param \Illuminate\Database\Query\Builder $query
  659. * @param array $values
  660. * @param string $sequence
  661. * @return string
  662. */
  663. public function compileInsertGetId(Builder $query, $values, $sequence)
  664. {
  665. return $this->compileInsert($query, $values);
  666. }
  667. /**
  668. * Compile an update statement into SQL.
  669. *
  670. * @param \Illuminate\Database\Query\Builder $query
  671. * @param array $values
  672. * @return string
  673. */
  674. public function compileUpdate(Builder $query, $values)
  675. {
  676. $table = $this->wrapTable($query->from);
  677. // Each one of the columns in the update statements needs to be wrapped in the
  678. // keyword identifiers, also a place-holder needs to be created for each of
  679. // the values in the list of bindings so we can make the sets statements.
  680. $columns = collect($values)->map(function ($value, $key) {
  681. return $this->wrap($key).' = '.$this->parameter($value);
  682. })->implode(', ');
  683. // If the query has any "join" clauses, we will setup the joins on the builder
  684. // and compile them so we can attach them to this update, as update queries
  685. // can get join statements to attach to other tables when they're needed.
  686. $joins = '';
  687. if (isset($query->joins)) {
  688. $joins = ' '.$this->compileJoins($query, $query->joins);
  689. }
  690. // Of course, update queries may also be constrained by where clauses so we'll
  691. // need to compile the where clauses and attach it to the query so only the
  692. // intended records are updated by the SQL statements we generate to run.
  693. $wheres = $this->compileWheres($query);
  694. return trim("update {$table}{$joins} set $columns $wheres");
  695. }
  696. /**
  697. * Prepare the bindings for an update statement.
  698. *
  699. * @param array $bindings
  700. * @param array $values
  701. * @return array
  702. */
  703. public function prepareBindingsForUpdate(array $bindings, array $values)
  704. {
  705. $cleanBindings = Arr::except($bindings, ['join', 'select']);
  706. return array_values(
  707. array_merge($bindings['join'], $values, Arr::flatten($cleanBindings))
  708. );
  709. }
  710. /**
  711. * Compile a delete statement into SQL.
  712. *
  713. * @param \Illuminate\Database\Query\Builder $query
  714. * @return string
  715. */
  716. public function compileDelete(Builder $query)
  717. {
  718. $wheres = is_array($query->wheres) ? $this->compileWheres($query) : '';
  719. return trim("delete from {$this->wrapTable($query->from)} $wheres");
  720. }
  721. /**
  722. * Prepare the bindings for a delete statement.
  723. *
  724. * @param array $bindings
  725. * @return array
  726. */
  727. public function prepareBindingsForDelete(array $bindings)
  728. {
  729. return Arr::flatten($bindings);
  730. }
  731. /**
  732. * Compile a truncate table statement into SQL.
  733. *
  734. * @param \Illuminate\Database\Query\Builder $query
  735. * @return array
  736. */
  737. public function compileTruncate(Builder $query)
  738. {
  739. return ['truncate '.$this->wrapTable($query->from) => []];
  740. }
  741. /**
  742. * Compile the lock into SQL.
  743. *
  744. * @param \Illuminate\Database\Query\Builder $query
  745. * @param bool|string $value
  746. * @return string
  747. */
  748. protected function compileLock(Builder $query, $value)
  749. {
  750. return is_string($value) ? $value : '';
  751. }
  752. /**
  753. * Determine if the grammar supports savepoints.
  754. *
  755. * @return bool
  756. */
  757. public function supportsSavepoints()
  758. {
  759. return true;
  760. }
  761. /**
  762. * Compile the SQL statement to define a savepoint.
  763. *
  764. * @param string $name
  765. * @return string
  766. */
  767. public function compileSavepoint($name)
  768. {
  769. return 'SAVEPOINT '.$name;
  770. }
  771. /**
  772. * Compile the SQL statement to execute a savepoint rollback.
  773. *
  774. * @param string $name
  775. * @return string
  776. */
  777. public function compileSavepointRollBack($name)
  778. {
  779. return 'ROLLBACK TO SAVEPOINT '.$name;
  780. }
  781. /**
  782. * Wrap a value in keyword identifiers.
  783. *
  784. * @param \Illuminate\Database\Query\Expression|string $value
  785. * @param bool $prefixAlias
  786. * @return string
  787. */
  788. public function wrap($value, $prefixAlias = false)
  789. {
  790. if ($this->isExpression($value)) {
  791. return $this->getValue($value);
  792. }
  793. // If the value being wrapped has a column alias we will need to separate out
  794. // the pieces so we can wrap each of the segments of the expression on its
  795. // own, and then join these both back together using the "as" connector.
  796. if (strpos(strtolower($value), ' as ') !== false) {
  797. return $this->wrapAliasedValue($value, $prefixAlias);
  798. }
  799. // If the given value is a JSON selector we will wrap it differently than a
  800. // traditional value. We will need to split this path and wrap each part
  801. // wrapped, etc. Otherwise, we will simply wrap the value as a string.
  802. if ($this->isJsonSelector($value)) {
  803. return $this->wrapJsonSelector($value);
  804. }
  805. return $this->wrapSegments(explode('.', $value));
  806. }
  807. /**
  808. * Wrap the given JSON selector.
  809. *
  810. * @param string $value
  811. * @return string
  812. */
  813. protected function wrapJsonSelector($value)
  814. {
  815. throw new RuntimeException('This database engine does not support JSON operations.');
  816. }
  817. /**
  818. * Determine if the given string is a JSON selector.
  819. *
  820. * @param string $value
  821. * @return bool
  822. */
  823. protected function isJsonSelector($value)
  824. {
  825. return Str::contains($value, '->');
  826. }
  827. /**
  828. * Concatenate an array of segments, removing empties.
  829. *
  830. * @param array $segments
  831. * @return string
  832. */
  833. protected function concatenate($segments)
  834. {
  835. return implode(' ', array_filter($segments, function ($value) {
  836. return (string) $value !== '';
  837. }));
  838. }
  839. /**
  840. * Remove the leading boolean from a statement.
  841. *
  842. * @param string $value
  843. * @return string
  844. */
  845. protected function removeLeadingBoolean($value)
  846. {
  847. return preg_replace('/and |or /i', '', $value, 1);
  848. }
  849. /**
  850. * Get the grammar specific operators.
  851. *
  852. * @return array
  853. */
  854. public function getOperators()
  855. {
  856. return $this->operators;
  857. }
  858. }