Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

MySqlGrammar.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace Illuminate\Database\Query\Grammars;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Database\Query\Builder;
  5. use Illuminate\Database\Query\JsonExpression;
  6. class MySqlGrammar extends Grammar
  7. {
  8. /**
  9. * The grammar specific operators.
  10. *
  11. * @var array
  12. */
  13. protected $operators = ['sounds like'];
  14. /**
  15. * The components that make up a select clause.
  16. *
  17. * @var array
  18. */
  19. protected $selectComponents = [
  20. 'aggregate',
  21. 'columns',
  22. 'from',
  23. 'joins',
  24. 'wheres',
  25. 'groups',
  26. 'havings',
  27. 'orders',
  28. 'limit',
  29. 'offset',
  30. 'lock',
  31. ];
  32. /**
  33. * Compile a select query into SQL.
  34. *
  35. * @param \Illuminate\Database\Query\Builder $query
  36. * @return string
  37. */
  38. public function compileSelect(Builder $query)
  39. {
  40. if ($query->unions && $query->aggregate) {
  41. return $this->compileUnionAggregate($query);
  42. }
  43. $sql = parent::compileSelect($query);
  44. if ($query->unions) {
  45. $sql = '('.$sql.') '.$this->compileUnions($query);
  46. }
  47. return $sql;
  48. }
  49. /**
  50. * Compile a "JSON contains" statement into SQL.
  51. *
  52. * @param string $column
  53. * @param string $value
  54. * @return string
  55. */
  56. protected function compileJsonContains($column, $value)
  57. {
  58. [$field, $path] = $this->wrapJsonFieldAndPath($column);
  59. return 'json_contains('.$field.', '.$value.$path.')';
  60. }
  61. /**
  62. * Compile a "JSON length" statement into SQL.
  63. *
  64. * @param string $column
  65. * @param string $operator
  66. * @param string $value
  67. * @return string
  68. */
  69. protected function compileJsonLength($column, $operator, $value)
  70. {
  71. [$field, $path] = $this->wrapJsonFieldAndPath($column);
  72. return 'json_length('.$field.$path.') '.$operator.' '.$value;
  73. }
  74. /**
  75. * Compile a single union statement.
  76. *
  77. * @param array $union
  78. * @return string
  79. */
  80. protected function compileUnion(array $union)
  81. {
  82. $conjunction = $union['all'] ? ' union all ' : ' union ';
  83. return $conjunction.'('.$union['query']->toSql().')';
  84. }
  85. /**
  86. * Compile the random statement into SQL.
  87. *
  88. * @param string $seed
  89. * @return string
  90. */
  91. public function compileRandom($seed)
  92. {
  93. return 'RAND('.$seed.')';
  94. }
  95. /**
  96. * Compile the lock into SQL.
  97. *
  98. * @param \Illuminate\Database\Query\Builder $query
  99. * @param bool|string $value
  100. * @return string
  101. */
  102. protected function compileLock(Builder $query, $value)
  103. {
  104. if (! is_string($value)) {
  105. return $value ? 'for update' : 'lock in share mode';
  106. }
  107. return $value;
  108. }
  109. /**
  110. * Compile an update statement into SQL.
  111. *
  112. * @param \Illuminate\Database\Query\Builder $query
  113. * @param array $values
  114. * @return string
  115. */
  116. public function compileUpdate(Builder $query, $values)
  117. {
  118. $table = $this->wrapTable($query->from);
  119. // Each one of the columns in the update statements needs to be wrapped in the
  120. // keyword identifiers, also a place-holder needs to be created for each of
  121. // the values in the list of bindings so we can make the sets statements.
  122. $columns = $this->compileUpdateColumns($values);
  123. // If the query has any "join" clauses, we will setup the joins on the builder
  124. // and compile them so we can attach them to this update, as update queries
  125. // can get join statements to attach to other tables when they're needed.
  126. $joins = '';
  127. if (isset($query->joins)) {
  128. $joins = ' '.$this->compileJoins($query, $query->joins);
  129. }
  130. // Of course, update queries may also be constrained by where clauses so we'll
  131. // need to compile the where clauses and attach it to the query so only the
  132. // intended records are updated by the SQL statements we generate to run.
  133. $where = $this->compileWheres($query);
  134. $sql = rtrim("update {$table}{$joins} set $columns $where");
  135. // If the query has an order by clause we will compile it since MySQL supports
  136. // order bys on update statements. We'll compile them using the typical way
  137. // of compiling order bys. Then they will be appended to the SQL queries.
  138. if (! empty($query->orders)) {
  139. $sql .= ' '.$this->compileOrders($query, $query->orders);
  140. }
  141. // Updates on MySQL also supports "limits", which allow you to easily update a
  142. // single record very easily. This is not supported by all database engines
  143. // so we have customized this update compiler here in order to add it in.
  144. if (isset($query->limit)) {
  145. $sql .= ' '.$this->compileLimit($query, $query->limit);
  146. }
  147. return rtrim($sql);
  148. }
  149. /**
  150. * Compile all of the columns for an update statement.
  151. *
  152. * @param array $values
  153. * @return string
  154. */
  155. protected function compileUpdateColumns($values)
  156. {
  157. return collect($values)->map(function ($value, $key) {
  158. if ($this->isJsonSelector($key)) {
  159. return $this->compileJsonUpdateColumn($key, new JsonExpression($value));
  160. }
  161. return $this->wrap($key).' = '.$this->parameter($value);
  162. })->implode(', ');
  163. }
  164. /**
  165. * Prepares a JSON column being updated using the JSON_SET function.
  166. *
  167. * @param string $key
  168. * @param \Illuminate\Database\Query\JsonExpression $value
  169. * @return string
  170. */
  171. protected function compileJsonUpdateColumn($key, JsonExpression $value)
  172. {
  173. [$field, $path] = $this->wrapJsonFieldAndPath($key);
  174. return "{$field} = json_set({$field}{$path}, {$value->getValue()})";
  175. }
  176. /**
  177. * Prepare the bindings for an update statement.
  178. *
  179. * Booleans, integers, and doubles are inserted into JSON updates as raw values.
  180. *
  181. * @param array $bindings
  182. * @param array $values
  183. * @return array
  184. */
  185. public function prepareBindingsForUpdate(array $bindings, array $values)
  186. {
  187. $values = collect($values)->reject(function ($value, $column) {
  188. return $this->isJsonSelector($column) && is_bool($value);
  189. })->all();
  190. return parent::prepareBindingsForUpdate($bindings, $values);
  191. }
  192. /**
  193. * Compile a delete statement into SQL.
  194. *
  195. * @param \Illuminate\Database\Query\Builder $query
  196. * @return string
  197. */
  198. public function compileDelete(Builder $query)
  199. {
  200. $table = $this->wrapTable($query->from);
  201. $where = is_array($query->wheres) ? $this->compileWheres($query) : '';
  202. return isset($query->joins)
  203. ? $this->compileDeleteWithJoins($query, $table, $where)
  204. : $this->compileDeleteWithoutJoins($query, $table, $where);
  205. }
  206. /**
  207. * Prepare the bindings for a delete statement.
  208. *
  209. * @param array $bindings
  210. * @return array
  211. */
  212. public function prepareBindingsForDelete(array $bindings)
  213. {
  214. $cleanBindings = Arr::except($bindings, ['join', 'select']);
  215. return array_values(
  216. array_merge($bindings['join'], Arr::flatten($cleanBindings))
  217. );
  218. }
  219. /**
  220. * Compile a delete query that does not use joins.
  221. *
  222. * @param \Illuminate\Database\Query\Builder $query
  223. * @param string $table
  224. * @param array $where
  225. * @return string
  226. */
  227. protected function compileDeleteWithoutJoins($query, $table, $where)
  228. {
  229. $sql = trim("delete from {$table} {$where}");
  230. // When using MySQL, delete statements may contain order by statements and limits
  231. // so we will compile both of those here. Once we have finished compiling this
  232. // we will return the completed SQL statement so it will be executed for us.
  233. if (! empty($query->orders)) {
  234. $sql .= ' '.$this->compileOrders($query, $query->orders);
  235. }
  236. if (isset($query->limit)) {
  237. $sql .= ' '.$this->compileLimit($query, $query->limit);
  238. }
  239. return $sql;
  240. }
  241. /**
  242. * Compile a delete query that uses joins.
  243. *
  244. * @param \Illuminate\Database\Query\Builder $query
  245. * @param string $table
  246. * @param array $where
  247. * @return string
  248. */
  249. protected function compileDeleteWithJoins($query, $table, $where)
  250. {
  251. $joins = ' '.$this->compileJoins($query, $query->joins);
  252. $alias = stripos($table, ' as ') !== false
  253. ? explode(' as ', $table)[1] : $table;
  254. return trim("delete {$alias} from {$table}{$joins} {$where}");
  255. }
  256. /**
  257. * Wrap a single string in keyword identifiers.
  258. *
  259. * @param string $value
  260. * @return string
  261. */
  262. protected function wrapValue($value)
  263. {
  264. return $value === '*' ? $value : '`'.str_replace('`', '``', $value).'`';
  265. }
  266. /**
  267. * Wrap the given JSON selector.
  268. *
  269. * @param string $value
  270. * @return string
  271. */
  272. protected function wrapJsonSelector($value)
  273. {
  274. [$field, $path] = $this->wrapJsonFieldAndPath($value);
  275. return 'json_unquote(json_extract('.$field.$path.'))';
  276. }
  277. /**
  278. * Wrap the given JSON selector for boolean values.
  279. *
  280. * @param string $value
  281. * @return string
  282. */
  283. protected function wrapJsonBooleanSelector($value)
  284. {
  285. [$field, $path] = $this->wrapJsonFieldAndPath($value);
  286. return 'json_extract('.$field.$path.')';
  287. }
  288. }