PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Front_End/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php

https://gitlab.com/Sigpot/AirSpot
PHP | 257 lines | 130 code | 43 blank | 84 comment | 16 complexity | 3ccec2bc2c0a27112cd423183667e3b3 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Database\Query\Grammars;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Database\Query\Builder;
  5. use Illuminate\Database\Query\JsonExpression;
  6. class MySqlGrammar extends Grammar
  7. {
  8. /**
  9. * The components that make up a select clause.
  10. *
  11. * @var array
  12. */
  13. protected $selectComponents = [
  14. 'aggregate',
  15. 'columns',
  16. 'from',
  17. 'joins',
  18. 'wheres',
  19. 'groups',
  20. 'havings',
  21. 'orders',
  22. 'limit',
  23. 'offset',
  24. 'lock',
  25. ];
  26. /**
  27. * Compile a select query into SQL.
  28. *
  29. * @param \Illuminate\Database\Query\Builder $query
  30. * @return string
  31. */
  32. public function compileSelect(Builder $query)
  33. {
  34. $sql = parent::compileSelect($query);
  35. if ($query->unions) {
  36. $sql = '('.$sql.') '.$this->compileUnions($query);
  37. }
  38. return $sql;
  39. }
  40. /**
  41. * Compile a single union statement.
  42. *
  43. * @param array $union
  44. * @return string
  45. */
  46. protected function compileUnion(array $union)
  47. {
  48. $joiner = $union['all'] ? ' union all ' : ' union ';
  49. return $joiner.'('.$union['query']->toSql().')';
  50. }
  51. /**
  52. * Compile the random statement into SQL.
  53. *
  54. * @param string $seed
  55. * @return string
  56. */
  57. public function compileRandom($seed)
  58. {
  59. return 'RAND('.$seed.')';
  60. }
  61. /**
  62. * Compile the lock into SQL.
  63. *
  64. * @param \Illuminate\Database\Query\Builder $query
  65. * @param bool|string $value
  66. * @return string
  67. */
  68. protected function compileLock(Builder $query, $value)
  69. {
  70. if (is_string($value)) {
  71. return $value;
  72. }
  73. return $value ? 'for update' : 'lock in share mode';
  74. }
  75. /**
  76. * Compile an update statement into SQL.
  77. *
  78. * @param \Illuminate\Database\Query\Builder $query
  79. * @param array $values
  80. * @return string
  81. */
  82. public function compileUpdate(Builder $query, $values)
  83. {
  84. $table = $this->wrapTable($query->from);
  85. $columns = [];
  86. // Each one of the columns in the update statements needs to be wrapped in the
  87. // keyword identifiers, also a place-holder needs to be created for each of
  88. // the values in the list of bindings so we can make the sets statements.
  89. foreach ($values as $key => $value) {
  90. if ($this->isJsonSelector($key)) {
  91. $columns[] = $this->compileJsonUpdateColumn(
  92. $key, new JsonExpression($value)
  93. );
  94. } else {
  95. $columns[] = $this->wrap($key).' = '.$this->parameter($value);
  96. }
  97. }
  98. $columns = implode(', ', $columns);
  99. // If the query has any "join" clauses, we will setup the joins on the builder
  100. // and compile them so we can attach them to this update, as update queries
  101. // can get join statements to attach to other tables when they're needed.
  102. if (isset($query->joins)) {
  103. $joins = ' '.$this->compileJoins($query, $query->joins);
  104. } else {
  105. $joins = '';
  106. }
  107. // Of course, update queries may also be constrained by where clauses so we'll
  108. // need to compile the where clauses and attach it to the query so only the
  109. // intended records are updated by the SQL statements we generate to run.
  110. $where = $this->compileWheres($query);
  111. $sql = rtrim("update {$table}{$joins} set $columns $where");
  112. if (isset($query->orders)) {
  113. $sql .= ' '.$this->compileOrders($query, $query->orders);
  114. }
  115. if (isset($query->limit)) {
  116. $sql .= ' '.$this->compileLimit($query, $query->limit);
  117. }
  118. return rtrim($sql);
  119. }
  120. /**
  121. * Prepares a JSON column being updated using the JSON_SET function.
  122. *
  123. * @param string $key
  124. * @param \Illuminate\Database\JsonExpression $value
  125. * @return string
  126. */
  127. protected function compileJsonUpdateColumn($key, JsonExpression $value)
  128. {
  129. $path = explode('->', $key);
  130. $field = $this->wrapValue(array_shift($path));
  131. $accessor = '"$.'.implode('.', $path).'"';
  132. return "{$field} = json_set({$field}, {$accessor}, {$value->getValue()})";
  133. }
  134. /**
  135. * Prepare the bindings for an update statement.
  136. *
  137. * @param array $bindings
  138. * @param array $values
  139. * @return array
  140. */
  141. public function prepareBindingsForUpdate(array $bindings, array $values)
  142. {
  143. $index = 0;
  144. foreach ($values as $column => $value) {
  145. if ($this->isJsonSelector($column) && is_bool($value)) {
  146. unset($bindings[$index]);
  147. }
  148. $index++;
  149. }
  150. return $bindings;
  151. }
  152. /**
  153. * Compile a delete statement into SQL.
  154. *
  155. * @param \Illuminate\Database\Query\Builder $query
  156. * @return string
  157. */
  158. public function compileDelete(Builder $query)
  159. {
  160. $table = $this->wrapTable($query->from);
  161. $where = is_array($query->wheres) ? $this->compileWheres($query) : '';
  162. if (isset($query->joins)) {
  163. $joins = ' '.$this->compileJoins($query, $query->joins);
  164. $sql = trim("delete $table from {$table}{$joins} $where");
  165. } else {
  166. $sql = trim("delete from $table $where");
  167. if (isset($query->orders)) {
  168. $sql .= ' '.$this->compileOrders($query, $query->orders);
  169. }
  170. if (isset($query->limit)) {
  171. $sql .= ' '.$this->compileLimit($query, $query->limit);
  172. }
  173. }
  174. return $sql;
  175. }
  176. /**
  177. * Wrap a single string in keyword identifiers.
  178. *
  179. * @param string $value
  180. * @return string
  181. */
  182. protected function wrapValue($value)
  183. {
  184. if ($value === '*') {
  185. return $value;
  186. }
  187. if ($this->isJsonSelector($value)) {
  188. return $this->wrapJsonSelector($value);
  189. }
  190. return '`'.str_replace('`', '``', $value).'`';
  191. }
  192. /**
  193. * Wrap the given JSON selector.
  194. *
  195. * @param string $value
  196. * @return string
  197. */
  198. protected function wrapJsonSelector($value)
  199. {
  200. $path = explode('->', $value);
  201. $field = $this->wrapValue(array_shift($path));
  202. return $field.'->'.'"$.'.implode('.', $path).'"';
  203. }
  204. /**
  205. * Determine if the given string is a JSON selector.
  206. *
  207. * @param string $value
  208. * @return bool
  209. */
  210. protected function isJsonSelector($value)
  211. {
  212. return Str::contains($value, '->');
  213. }
  214. }