Grammar.php
1 month ago
MySqlGrammar.php
1 month ago
PostgresGrammar.php
1 month ago
SQLiteGrammar.php
1 month ago
SqlServerGrammar.php
1 month ago
Grammar.php
1147 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Query\Grammars; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Database\Concerns\CompilesJsonPaths; |
| 6 | use IAWPSCOPED\Illuminate\Database\Grammar as BaseGrammar; |
| 7 | use IAWPSCOPED\Illuminate\Database\Query\Builder; |
| 8 | use IAWPSCOPED\Illuminate\Database\Query\JoinClause; |
| 9 | use IAWPSCOPED\Illuminate\Support\Arr; |
| 10 | use RuntimeException; |
| 11 | /** @internal */ |
| 12 | class Grammar extends BaseGrammar |
| 13 | { |
| 14 | use CompilesJsonPaths; |
| 15 | /** |
| 16 | * The grammar specific operators. |
| 17 | * |
| 18 | * @var array |
| 19 | */ |
| 20 | protected $operators = []; |
| 21 | /** |
| 22 | * The grammar specific bitwise operators. |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | protected $bitwiseOperators = []; |
| 27 | /** |
| 28 | * The components that make up a select clause. |
| 29 | * |
| 30 | * @var string[] |
| 31 | */ |
| 32 | protected $selectComponents = ['aggregate', 'columns', 'from', 'indexHint', 'joins', 'wheres', 'groups', 'havings', 'orders', 'limit', 'offset', 'lock']; |
| 33 | /** |
| 34 | * Compile a select query into SQL. |
| 35 | * |
| 36 | * @param \Illuminate\Database\Query\Builder $query |
| 37 | * @return string |
| 38 | */ |
| 39 | public function compileSelect(Builder $query) |
| 40 | { |
| 41 | if (($query->unions || $query->havings) && $query->aggregate) { |
| 42 | return $this->compileUnionAggregate($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($this->compileComponents($query))); |
| 55 | if ($query->unions) { |
| 56 | $sql = $this->wrapUnion($sql) . ' ' . $this->compileUnions($query); |
| 57 | } |
| 58 | $query->columns = $original; |
| 59 | return $sql; |
| 60 | } |
| 61 | /** |
| 62 | * Compile the components necessary for a select clause. |
| 63 | * |
| 64 | * @param \Illuminate\Database\Query\Builder $query |
| 65 | * @return array |
| 66 | */ |
| 67 | protected function compileComponents(Builder $query) |
| 68 | { |
| 69 | $sql = []; |
| 70 | foreach ($this->selectComponents as $component) { |
| 71 | if (isset($query->{$component})) { |
| 72 | $method = 'compile' . \ucfirst($component); |
| 73 | $sql[$component] = $this->{$method}($query, $query->{$component}); |
| 74 | } |
| 75 | } |
| 76 | return $sql; |
| 77 | } |
| 78 | /** |
| 79 | * Compile an aggregated select clause. |
| 80 | * |
| 81 | * @param \Illuminate\Database\Query\Builder $query |
| 82 | * @param array $aggregate |
| 83 | * @return string |
| 84 | */ |
| 85 | protected function compileAggregate(Builder $query, $aggregate) |
| 86 | { |
| 87 | $column = $this->columnize($aggregate['columns']); |
| 88 | // If the query has a "distinct" constraint and we're not asking for all columns |
| 89 | // we need to prepend "distinct" onto the column name so that the query takes |
| 90 | // it into account when it performs the aggregating operations on the data. |
| 91 | if (\is_array($query->distinct)) { |
| 92 | $column = 'distinct ' . $this->columnize($query->distinct); |
| 93 | } elseif ($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 | if ($query->distinct) { |
| 114 | $select = 'select distinct '; |
| 115 | } else { |
| 116 | $select = 'select '; |
| 117 | } |
| 118 | return $select . $this->columnize($columns); |
| 119 | } |
| 120 | /** |
| 121 | * Compile the "from" portion of the query. |
| 122 | * |
| 123 | * @param \Illuminate\Database\Query\Builder $query |
| 124 | * @param string $table |
| 125 | * @return string |
| 126 | */ |
| 127 | protected function compileFrom(Builder $query, $table) |
| 128 | { |
| 129 | return 'from ' . $this->wrapTable($table); |
| 130 | } |
| 131 | /** |
| 132 | * Compile the "join" portions of the query. |
| 133 | * |
| 134 | * @param \Illuminate\Database\Query\Builder $query |
| 135 | * @param array $joins |
| 136 | * @return string |
| 137 | */ |
| 138 | protected function compileJoins(Builder $query, $joins) |
| 139 | { |
| 140 | return \IAWPSCOPED\collect($joins)->map(function ($join) use($query) { |
| 141 | $table = $this->wrapTable($join->table); |
| 142 | $nestedJoins = \is_null($join->joins) ? '' : ' ' . $this->compileJoins($query, $join->joins); |
| 143 | $tableAndNestedJoins = \is_null($join->joins) ? $table : '(' . $table . $nestedJoins . ')'; |
| 144 | return \trim("{$join->type} join {$tableAndNestedJoins} {$this->compileWheres($join)}"); |
| 145 | })->implode(' '); |
| 146 | } |
| 147 | /** |
| 148 | * Compile the "where" portions of the query. |
| 149 | * |
| 150 | * @param \Illuminate\Database\Query\Builder $query |
| 151 | * @return string |
| 152 | */ |
| 153 | public function compileWheres(Builder $query) |
| 154 | { |
| 155 | // Each type of where clause has its own compiler function, which is responsible |
| 156 | // for actually creating the where clauses SQL. This helps keep the code nice |
| 157 | // and maintainable since each clause has a very small method that it uses. |
| 158 | if (\is_null($query->wheres)) { |
| 159 | return ''; |
| 160 | } |
| 161 | // If we actually have some where clauses, we will strip off the first boolean |
| 162 | // operator, which is added by the query builders for convenience so we can |
| 163 | // avoid checking for the first clauses in each of the compilers methods. |
| 164 | if (\count($sql = $this->compileWheresToArray($query)) > 0) { |
| 165 | return $this->concatenateWhereClauses($query, $sql); |
| 166 | } |
| 167 | return ''; |
| 168 | } |
| 169 | /** |
| 170 | * Get an array of all the where clauses for the query. |
| 171 | * |
| 172 | * @param \Illuminate\Database\Query\Builder $query |
| 173 | * @return array |
| 174 | */ |
| 175 | protected function compileWheresToArray($query) |
| 176 | { |
| 177 | return \IAWPSCOPED\collect($query->wheres)->map(function ($where) use($query) { |
| 178 | return $where['boolean'] . ' ' . $this->{"where{$where['type']}"}($query, $where); |
| 179 | })->all(); |
| 180 | } |
| 181 | /** |
| 182 | * Format the where clause statements into one string. |
| 183 | * |
| 184 | * @param \Illuminate\Database\Query\Builder $query |
| 185 | * @param array $sql |
| 186 | * @return string |
| 187 | */ |
| 188 | protected function concatenateWhereClauses($query, $sql) |
| 189 | { |
| 190 | $conjunction = $query instanceof JoinClause ? 'on' : 'where'; |
| 191 | return $conjunction . ' ' . $this->removeLeadingBoolean(\implode(' ', $sql)); |
| 192 | } |
| 193 | /** |
| 194 | * Compile a raw where clause. |
| 195 | * |
| 196 | * @param \Illuminate\Database\Query\Builder $query |
| 197 | * @param array $where |
| 198 | * @return string |
| 199 | */ |
| 200 | protected function whereRaw(Builder $query, $where) |
| 201 | { |
| 202 | return $where['sql']; |
| 203 | } |
| 204 | /** |
| 205 | * Compile a basic where clause. |
| 206 | * |
| 207 | * @param \Illuminate\Database\Query\Builder $query |
| 208 | * @param array $where |
| 209 | * @return string |
| 210 | */ |
| 211 | protected function whereBasic(Builder $query, $where) |
| 212 | { |
| 213 | $value = $this->parameter($where['value']); |
| 214 | $operator = \str_replace('?', '??', $where['operator']); |
| 215 | return $this->wrap($where['column']) . ' ' . $operator . ' ' . $value; |
| 216 | } |
| 217 | /** |
| 218 | * Compile a bitwise operator where clause. |
| 219 | * |
| 220 | * @param \Illuminate\Database\Query\Builder $query |
| 221 | * @param array $where |
| 222 | * @return string |
| 223 | */ |
| 224 | protected function whereBitwise(Builder $query, $where) |
| 225 | { |
| 226 | return $this->whereBasic($query, $where); |
| 227 | } |
| 228 | /** |
| 229 | * Compile a "where in" clause. |
| 230 | * |
| 231 | * @param \Illuminate\Database\Query\Builder $query |
| 232 | * @param array $where |
| 233 | * @return string |
| 234 | */ |
| 235 | protected function whereIn(Builder $query, $where) |
| 236 | { |
| 237 | if (!empty($where['values'])) { |
| 238 | return $this->wrap($where['column']) . ' in (' . $this->parameterize($where['values']) . ')'; |
| 239 | } |
| 240 | return '0 = 1'; |
| 241 | } |
| 242 | /** |
| 243 | * Compile a "where not in" clause. |
| 244 | * |
| 245 | * @param \Illuminate\Database\Query\Builder $query |
| 246 | * @param array $where |
| 247 | * @return string |
| 248 | */ |
| 249 | protected function whereNotIn(Builder $query, $where) |
| 250 | { |
| 251 | if (!empty($where['values'])) { |
| 252 | return $this->wrap($where['column']) . ' not in (' . $this->parameterize($where['values']) . ')'; |
| 253 | } |
| 254 | return '1 = 1'; |
| 255 | } |
| 256 | /** |
| 257 | * Compile a "where not in raw" clause. |
| 258 | * |
| 259 | * For safety, whereIntegerInRaw ensures this method is only used with integer values. |
| 260 | * |
| 261 | * @param \Illuminate\Database\Query\Builder $query |
| 262 | * @param array $where |
| 263 | * @return string |
| 264 | */ |
| 265 | protected function whereNotInRaw(Builder $query, $where) |
| 266 | { |
| 267 | if (!empty($where['values'])) { |
| 268 | return $this->wrap($where['column']) . ' not in (' . \implode(', ', $where['values']) . ')'; |
| 269 | } |
| 270 | return '1 = 1'; |
| 271 | } |
| 272 | /** |
| 273 | * Compile a "where in raw" clause. |
| 274 | * |
| 275 | * For safety, whereIntegerInRaw ensures this method is only used with integer values. |
| 276 | * |
| 277 | * @param \Illuminate\Database\Query\Builder $query |
| 278 | * @param array $where |
| 279 | * @return string |
| 280 | */ |
| 281 | protected function whereInRaw(Builder $query, $where) |
| 282 | { |
| 283 | if (!empty($where['values'])) { |
| 284 | return $this->wrap($where['column']) . ' in (' . \implode(', ', $where['values']) . ')'; |
| 285 | } |
| 286 | return '0 = 1'; |
| 287 | } |
| 288 | /** |
| 289 | * Compile a "where null" clause. |
| 290 | * |
| 291 | * @param \Illuminate\Database\Query\Builder $query |
| 292 | * @param array $where |
| 293 | * @return string |
| 294 | */ |
| 295 | protected function whereNull(Builder $query, $where) |
| 296 | { |
| 297 | return $this->wrap($where['column']) . ' is null'; |
| 298 | } |
| 299 | /** |
| 300 | * Compile a "where not null" clause. |
| 301 | * |
| 302 | * @param \Illuminate\Database\Query\Builder $query |
| 303 | * @param array $where |
| 304 | * @return string |
| 305 | */ |
| 306 | protected function whereNotNull(Builder $query, $where) |
| 307 | { |
| 308 | return $this->wrap($where['column']) . ' is not null'; |
| 309 | } |
| 310 | /** |
| 311 | * Compile a "between" where clause. |
| 312 | * |
| 313 | * @param \Illuminate\Database\Query\Builder $query |
| 314 | * @param array $where |
| 315 | * @return string |
| 316 | */ |
| 317 | protected function whereBetween(Builder $query, $where) |
| 318 | { |
| 319 | $between = $where['not'] ? 'not between' : 'between'; |
| 320 | $min = $this->parameter(\is_array($where['values']) ? \reset($where['values']) : $where['values'][0]); |
| 321 | $max = $this->parameter(\is_array($where['values']) ? \end($where['values']) : $where['values'][1]); |
| 322 | return $this->wrap($where['column']) . ' ' . $between . ' ' . $min . ' and ' . $max; |
| 323 | } |
| 324 | /** |
| 325 | * Compile a "between" where clause. |
| 326 | * |
| 327 | * @param \Illuminate\Database\Query\Builder $query |
| 328 | * @param array $where |
| 329 | * @return string |
| 330 | */ |
| 331 | protected function whereBetweenColumns(Builder $query, $where) |
| 332 | { |
| 333 | $between = $where['not'] ? 'not between' : 'between'; |
| 334 | $min = $this->wrap(\is_array($where['values']) ? \reset($where['values']) : $where['values'][0]); |
| 335 | $max = $this->wrap(\is_array($where['values']) ? \end($where['values']) : $where['values'][1]); |
| 336 | return $this->wrap($where['column']) . ' ' . $between . ' ' . $min . ' and ' . $max; |
| 337 | } |
| 338 | /** |
| 339 | * Compile a "where date" clause. |
| 340 | * |
| 341 | * @param \Illuminate\Database\Query\Builder $query |
| 342 | * @param array $where |
| 343 | * @return string |
| 344 | */ |
| 345 | protected function whereDate(Builder $query, $where) |
| 346 | { |
| 347 | return $this->dateBasedWhere('date', $query, $where); |
| 348 | } |
| 349 | /** |
| 350 | * Compile a "where time" clause. |
| 351 | * |
| 352 | * @param \Illuminate\Database\Query\Builder $query |
| 353 | * @param array $where |
| 354 | * @return string |
| 355 | */ |
| 356 | protected function whereTime(Builder $query, $where) |
| 357 | { |
| 358 | return $this->dateBasedWhere('time', $query, $where); |
| 359 | } |
| 360 | /** |
| 361 | * Compile a "where day" clause. |
| 362 | * |
| 363 | * @param \Illuminate\Database\Query\Builder $query |
| 364 | * @param array $where |
| 365 | * @return string |
| 366 | */ |
| 367 | protected function whereDay(Builder $query, $where) |
| 368 | { |
| 369 | return $this->dateBasedWhere('day', $query, $where); |
| 370 | } |
| 371 | /** |
| 372 | * Compile a "where month" clause. |
| 373 | * |
| 374 | * @param \Illuminate\Database\Query\Builder $query |
| 375 | * @param array $where |
| 376 | * @return string |
| 377 | */ |
| 378 | protected function whereMonth(Builder $query, $where) |
| 379 | { |
| 380 | return $this->dateBasedWhere('month', $query, $where); |
| 381 | } |
| 382 | /** |
| 383 | * Compile a "where year" clause. |
| 384 | * |
| 385 | * @param \Illuminate\Database\Query\Builder $query |
| 386 | * @param array $where |
| 387 | * @return string |
| 388 | */ |
| 389 | protected function whereYear(Builder $query, $where) |
| 390 | { |
| 391 | return $this->dateBasedWhere('year', $query, $where); |
| 392 | } |
| 393 | /** |
| 394 | * Compile a date based where clause. |
| 395 | * |
| 396 | * @param string $type |
| 397 | * @param \Illuminate\Database\Query\Builder $query |
| 398 | * @param array $where |
| 399 | * @return string |
| 400 | */ |
| 401 | protected function dateBasedWhere($type, Builder $query, $where) |
| 402 | { |
| 403 | $value = $this->parameter($where['value']); |
| 404 | return $type . '(' . $this->wrap($where['column']) . ') ' . $where['operator'] . ' ' . $value; |
| 405 | } |
| 406 | /** |
| 407 | * Compile a where clause comparing two columns. |
| 408 | * |
| 409 | * @param \Illuminate\Database\Query\Builder $query |
| 410 | * @param array $where |
| 411 | * @return string |
| 412 | */ |
| 413 | protected function whereColumn(Builder $query, $where) |
| 414 | { |
| 415 | return $this->wrap($where['first']) . ' ' . $where['operator'] . ' ' . $this->wrap($where['second']); |
| 416 | } |
| 417 | /** |
| 418 | * Compile a nested where clause. |
| 419 | * |
| 420 | * @param \Illuminate\Database\Query\Builder $query |
| 421 | * @param array $where |
| 422 | * @return string |
| 423 | */ |
| 424 | protected function whereNested(Builder $query, $where) |
| 425 | { |
| 426 | // Here we will calculate what portion of the string we need to remove. If this |
| 427 | // is a join clause query, we need to remove the "on" portion of the SQL and |
| 428 | // if it is a normal query we need to take the leading "where" of queries. |
| 429 | $offset = $query instanceof JoinClause ? 3 : 6; |
| 430 | return '(' . \substr($this->compileWheres($where['query']), $offset) . ')'; |
| 431 | } |
| 432 | /** |
| 433 | * Compile a where condition with a sub-select. |
| 434 | * |
| 435 | * @param \Illuminate\Database\Query\Builder $query |
| 436 | * @param array $where |
| 437 | * @return string |
| 438 | */ |
| 439 | protected function whereSub(Builder $query, $where) |
| 440 | { |
| 441 | $select = $this->compileSelect($where['query']); |
| 442 | return $this->wrap($where['column']) . ' ' . $where['operator'] . " ({$select})"; |
| 443 | } |
| 444 | /** |
| 445 | * Compile a where exists clause. |
| 446 | * |
| 447 | * @param \Illuminate\Database\Query\Builder $query |
| 448 | * @param array $where |
| 449 | * @return string |
| 450 | */ |
| 451 | protected function whereExists(Builder $query, $where) |
| 452 | { |
| 453 | return 'exists (' . $this->compileSelect($where['query']) . ')'; |
| 454 | } |
| 455 | /** |
| 456 | * Compile a where exists clause. |
| 457 | * |
| 458 | * @param \Illuminate\Database\Query\Builder $query |
| 459 | * @param array $where |
| 460 | * @return string |
| 461 | */ |
| 462 | protected function whereNotExists(Builder $query, $where) |
| 463 | { |
| 464 | return 'not exists (' . $this->compileSelect($where['query']) . ')'; |
| 465 | } |
| 466 | /** |
| 467 | * Compile a where row values condition. |
| 468 | * |
| 469 | * @param \Illuminate\Database\Query\Builder $query |
| 470 | * @param array $where |
| 471 | * @return string |
| 472 | */ |
| 473 | protected function whereRowValues(Builder $query, $where) |
| 474 | { |
| 475 | $columns = $this->columnize($where['columns']); |
| 476 | $values = $this->parameterize($where['values']); |
| 477 | return '(' . $columns . ') ' . $where['operator'] . ' (' . $values . ')'; |
| 478 | } |
| 479 | /** |
| 480 | * Compile a "where JSON boolean" clause. |
| 481 | * |
| 482 | * @param \Illuminate\Database\Query\Builder $query |
| 483 | * @param array $where |
| 484 | * @return string |
| 485 | */ |
| 486 | protected function whereJsonBoolean(Builder $query, $where) |
| 487 | { |
| 488 | $column = $this->wrapJsonBooleanSelector($where['column']); |
| 489 | $value = $this->wrapJsonBooleanValue($this->parameter($where['value'])); |
| 490 | return $column . ' ' . $where['operator'] . ' ' . $value; |
| 491 | } |
| 492 | /** |
| 493 | * Compile a "where JSON contains" clause. |
| 494 | * |
| 495 | * @param \Illuminate\Database\Query\Builder $query |
| 496 | * @param array $where |
| 497 | * @return string |
| 498 | */ |
| 499 | protected function whereJsonContains(Builder $query, $where) |
| 500 | { |
| 501 | $not = $where['not'] ? 'not ' : ''; |
| 502 | return $not . $this->compileJsonContains($where['column'], $this->parameter($where['value'])); |
| 503 | } |
| 504 | /** |
| 505 | * Compile a "JSON contains" statement into SQL. |
| 506 | * |
| 507 | * @param string $column |
| 508 | * @param string $value |
| 509 | * @return string |
| 510 | * |
| 511 | * @throws \RuntimeException |
| 512 | */ |
| 513 | protected function compileJsonContains($column, $value) |
| 514 | { |
| 515 | throw new RuntimeException('This database engine does not support JSON contains operations.'); |
| 516 | } |
| 517 | /** |
| 518 | * Prepare the binding for a "JSON contains" statement. |
| 519 | * |
| 520 | * @param mixed $binding |
| 521 | * @return string |
| 522 | */ |
| 523 | public function prepareBindingForJsonContains($binding) |
| 524 | { |
| 525 | return \json_encode($binding, \JSON_UNESCAPED_UNICODE); |
| 526 | } |
| 527 | /** |
| 528 | * Compile a "where JSON contains key" clause. |
| 529 | * |
| 530 | * @param \Illuminate\Database\Query\Builder $query |
| 531 | * @param array $where |
| 532 | * @return string |
| 533 | */ |
| 534 | protected function whereJsonContainsKey(Builder $query, $where) |
| 535 | { |
| 536 | $not = $where['not'] ? 'not ' : ''; |
| 537 | return $not . $this->compileJsonContainsKey($where['column']); |
| 538 | } |
| 539 | /** |
| 540 | * Compile a "JSON contains key" statement into SQL. |
| 541 | * |
| 542 | * @param string $column |
| 543 | * @return string |
| 544 | * |
| 545 | * @throws \RuntimeException |
| 546 | */ |
| 547 | protected function compileJsonContainsKey($column) |
| 548 | { |
| 549 | throw new RuntimeException('This database engine does not support JSON contains key operations.'); |
| 550 | } |
| 551 | /** |
| 552 | * Compile a "where JSON length" clause. |
| 553 | * |
| 554 | * @param \Illuminate\Database\Query\Builder $query |
| 555 | * @param array $where |
| 556 | * @return string |
| 557 | */ |
| 558 | protected function whereJsonLength(Builder $query, $where) |
| 559 | { |
| 560 | return $this->compileJsonLength($where['column'], $where['operator'], $this->parameter($where['value'])); |
| 561 | } |
| 562 | /** |
| 563 | * Compile a "JSON length" statement into SQL. |
| 564 | * |
| 565 | * @param string $column |
| 566 | * @param string $operator |
| 567 | * @param string $value |
| 568 | * @return string |
| 569 | * |
| 570 | * @throws \RuntimeException |
| 571 | */ |
| 572 | protected function compileJsonLength($column, $operator, $value) |
| 573 | { |
| 574 | throw new RuntimeException('This database engine does not support JSON length operations.'); |
| 575 | } |
| 576 | /** |
| 577 | * Compile a "JSON value cast" statement into SQL. |
| 578 | * |
| 579 | * @param string $value |
| 580 | * @return string |
| 581 | */ |
| 582 | public function compileJsonValueCast($value) |
| 583 | { |
| 584 | return $value; |
| 585 | } |
| 586 | /** |
| 587 | * Compile a "where fulltext" clause. |
| 588 | * |
| 589 | * @param \Illuminate\Database\Query\Builder $query |
| 590 | * @param array $where |
| 591 | * @return string |
| 592 | */ |
| 593 | public function whereFullText(Builder $query, $where) |
| 594 | { |
| 595 | throw new RuntimeException('This database engine does not support fulltext search operations.'); |
| 596 | } |
| 597 | /** |
| 598 | * Compile the "group by" portions of the query. |
| 599 | * |
| 600 | * @param \Illuminate\Database\Query\Builder $query |
| 601 | * @param array $groups |
| 602 | * @return string |
| 603 | */ |
| 604 | protected function compileGroups(Builder $query, $groups) |
| 605 | { |
| 606 | return 'group by ' . $this->columnize($groups); |
| 607 | } |
| 608 | /** |
| 609 | * Compile the "having" portions of the query. |
| 610 | * |
| 611 | * @param \Illuminate\Database\Query\Builder $query |
| 612 | * @return string |
| 613 | */ |
| 614 | protected function compileHavings(Builder $query) |
| 615 | { |
| 616 | return 'having ' . $this->removeLeadingBoolean(\IAWPSCOPED\collect($query->havings)->map(function ($having) { |
| 617 | return $having['boolean'] . ' ' . $this->compileHaving($having); |
| 618 | })->implode(' ')); |
| 619 | } |
| 620 | /** |
| 621 | * Compile a single having clause. |
| 622 | * |
| 623 | * @param array $having |
| 624 | * @return string |
| 625 | */ |
| 626 | protected function compileHaving(array $having) |
| 627 | { |
| 628 | // If the having clause is "raw", we can just return the clause straight away |
| 629 | // without doing any more processing on it. Otherwise, we will compile the |
| 630 | // clause into SQL based on the components that make it up from builder. |
| 631 | if ($having['type'] === 'Raw') { |
| 632 | return $having['sql']; |
| 633 | } elseif ($having['type'] === 'between') { |
| 634 | return $this->compileHavingBetween($having); |
| 635 | } elseif ($having['type'] === 'Null') { |
| 636 | return $this->compileHavingNull($having); |
| 637 | } elseif ($having['type'] === 'NotNull') { |
| 638 | return $this->compileHavingNotNull($having); |
| 639 | } elseif ($having['type'] === 'bit') { |
| 640 | return $this->compileHavingBit($having); |
| 641 | } elseif ($having['type'] === 'Nested') { |
| 642 | return $this->compileNestedHavings($having); |
| 643 | } |
| 644 | return $this->compileBasicHaving($having); |
| 645 | } |
| 646 | /** |
| 647 | * Compile a basic having clause. |
| 648 | * |
| 649 | * @param array $having |
| 650 | * @return string |
| 651 | */ |
| 652 | protected function compileBasicHaving($having) |
| 653 | { |
| 654 | $column = $this->wrap($having['column']); |
| 655 | $parameter = $this->parameter($having['value']); |
| 656 | return $column . ' ' . $having['operator'] . ' ' . $parameter; |
| 657 | } |
| 658 | /** |
| 659 | * Compile a "between" having clause. |
| 660 | * |
| 661 | * @param array $having |
| 662 | * @return string |
| 663 | */ |
| 664 | protected function compileHavingBetween($having) |
| 665 | { |
| 666 | $between = $having['not'] ? 'not between' : 'between'; |
| 667 | $column = $this->wrap($having['column']); |
| 668 | $min = $this->parameter(head($having['values'])); |
| 669 | $max = $this->parameter(\IAWPSCOPED\last($having['values'])); |
| 670 | return $column . ' ' . $between . ' ' . $min . ' and ' . $max; |
| 671 | } |
| 672 | /** |
| 673 | * Compile a having null clause. |
| 674 | * |
| 675 | * @param array $having |
| 676 | * @return string |
| 677 | */ |
| 678 | protected function compileHavingNull($having) |
| 679 | { |
| 680 | $column = $this->wrap($having['column']); |
| 681 | return $column . ' is null'; |
| 682 | } |
| 683 | /** |
| 684 | * Compile a having not null clause. |
| 685 | * |
| 686 | * @param array $having |
| 687 | * @return string |
| 688 | */ |
| 689 | protected function compileHavingNotNull($having) |
| 690 | { |
| 691 | $column = $this->wrap($having['column']); |
| 692 | return $column . ' is not null'; |
| 693 | } |
| 694 | /** |
| 695 | * Compile a having clause involving a bit operator. |
| 696 | * |
| 697 | * @param array $having |
| 698 | * @return string |
| 699 | */ |
| 700 | protected function compileHavingBit($having) |
| 701 | { |
| 702 | $column = $this->wrap($having['column']); |
| 703 | $parameter = $this->parameter($having['value']); |
| 704 | return '(' . $column . ' ' . $having['operator'] . ' ' . $parameter . ') != 0'; |
| 705 | } |
| 706 | /** |
| 707 | * Compile a nested having clause. |
| 708 | * |
| 709 | * @param array $having |
| 710 | * @return string |
| 711 | */ |
| 712 | protected function compileNestedHavings($having) |
| 713 | { |
| 714 | return '(' . \substr($this->compileHavings($having['query']), 7) . ')'; |
| 715 | } |
| 716 | /** |
| 717 | * Compile the "order by" portions of the query. |
| 718 | * |
| 719 | * @param \Illuminate\Database\Query\Builder $query |
| 720 | * @param array $orders |
| 721 | * @return string |
| 722 | */ |
| 723 | protected function compileOrders(Builder $query, $orders) |
| 724 | { |
| 725 | if (!empty($orders)) { |
| 726 | return 'order by ' . \implode(', ', $this->compileOrdersToArray($query, $orders)); |
| 727 | } |
| 728 | return ''; |
| 729 | } |
| 730 | /** |
| 731 | * Compile the query orders to an array. |
| 732 | * |
| 733 | * @param \Illuminate\Database\Query\Builder $query |
| 734 | * @param array $orders |
| 735 | * @return array |
| 736 | */ |
| 737 | protected function compileOrdersToArray(Builder $query, $orders) |
| 738 | { |
| 739 | return \array_map(function ($order) { |
| 740 | return $order['sql'] ?? $this->wrap($order['column']) . ' ' . $order['direction']; |
| 741 | }, $orders); |
| 742 | } |
| 743 | /** |
| 744 | * Compile the random statement into SQL. |
| 745 | * |
| 746 | * @param string|int $seed |
| 747 | * @return string |
| 748 | */ |
| 749 | public function compileRandom($seed) |
| 750 | { |
| 751 | return 'RANDOM()'; |
| 752 | } |
| 753 | /** |
| 754 | * Compile the "limit" portions of the query. |
| 755 | * |
| 756 | * @param \Illuminate\Database\Query\Builder $query |
| 757 | * @param int $limit |
| 758 | * @return string |
| 759 | */ |
| 760 | protected function compileLimit(Builder $query, $limit) |
| 761 | { |
| 762 | return 'limit ' . (int) $limit; |
| 763 | } |
| 764 | /** |
| 765 | * Compile the "offset" portions of the query. |
| 766 | * |
| 767 | * @param \Illuminate\Database\Query\Builder $query |
| 768 | * @param int $offset |
| 769 | * @return string |
| 770 | */ |
| 771 | protected function compileOffset(Builder $query, $offset) |
| 772 | { |
| 773 | return 'offset ' . (int) $offset; |
| 774 | } |
| 775 | /** |
| 776 | * Compile the "union" queries attached to the main query. |
| 777 | * |
| 778 | * @param \Illuminate\Database\Query\Builder $query |
| 779 | * @return string |
| 780 | */ |
| 781 | protected function compileUnions(Builder $query) |
| 782 | { |
| 783 | $sql = ''; |
| 784 | foreach ($query->unions as $union) { |
| 785 | $sql .= $this->compileUnion($union); |
| 786 | } |
| 787 | if (!empty($query->unionOrders)) { |
| 788 | $sql .= ' ' . $this->compileOrders($query, $query->unionOrders); |
| 789 | } |
| 790 | if (isset($query->unionLimit)) { |
| 791 | $sql .= ' ' . $this->compileLimit($query, $query->unionLimit); |
| 792 | } |
| 793 | if (isset($query->unionOffset)) { |
| 794 | $sql .= ' ' . $this->compileOffset($query, $query->unionOffset); |
| 795 | } |
| 796 | return \ltrim($sql); |
| 797 | } |
| 798 | /** |
| 799 | * Compile a single union statement. |
| 800 | * |
| 801 | * @param array $union |
| 802 | * @return string |
| 803 | */ |
| 804 | protected function compileUnion(array $union) |
| 805 | { |
| 806 | $conjunction = $union['all'] ? ' union all ' : ' union '; |
| 807 | return $conjunction . $this->wrapUnion($union['query']->toSql()); |
| 808 | } |
| 809 | /** |
| 810 | * Wrap a union subquery in parentheses. |
| 811 | * |
| 812 | * @param string $sql |
| 813 | * @return string |
| 814 | */ |
| 815 | protected function wrapUnion($sql) |
| 816 | { |
| 817 | return '(' . $sql . ')'; |
| 818 | } |
| 819 | /** |
| 820 | * Compile a union aggregate query into SQL. |
| 821 | * |
| 822 | * @param \Illuminate\Database\Query\Builder $query |
| 823 | * @return string |
| 824 | */ |
| 825 | protected function compileUnionAggregate(Builder $query) |
| 826 | { |
| 827 | $sql = $this->compileAggregate($query, $query->aggregate); |
| 828 | $query->aggregate = null; |
| 829 | return $sql . ' from (' . $this->compileSelect($query) . ') as ' . $this->wrapTable('temp_table'); |
| 830 | } |
| 831 | /** |
| 832 | * Compile an exists statement into SQL. |
| 833 | * |
| 834 | * @param \Illuminate\Database\Query\Builder $query |
| 835 | * @return string |
| 836 | */ |
| 837 | public function compileExists(Builder $query) |
| 838 | { |
| 839 | $select = $this->compileSelect($query); |
| 840 | return "select exists({$select}) as {$this->wrap('exists')}"; |
| 841 | } |
| 842 | /** |
| 843 | * Compile an insert statement into SQL. |
| 844 | * |
| 845 | * @param \Illuminate\Database\Query\Builder $query |
| 846 | * @param array $values |
| 847 | * @return string |
| 848 | */ |
| 849 | public function compileInsert(Builder $query, array $values) |
| 850 | { |
| 851 | // Essentially we will force every insert to be treated as a batch insert which |
| 852 | // simply makes creating the SQL easier for us since we can utilize the same |
| 853 | // basic routine regardless of an amount of records given to us to insert. |
| 854 | $table = $this->wrapTable($query->from); |
| 855 | if (empty($values)) { |
| 856 | return "insert into {$table} default values"; |
| 857 | } |
| 858 | if (!\is_array(\reset($values))) { |
| 859 | $values = [$values]; |
| 860 | } |
| 861 | $columns = $this->columnize(\array_keys(\reset($values))); |
| 862 | // We need to build a list of parameter place-holders of values that are bound |
| 863 | // to the query. Each insert should have the exact same number of parameter |
| 864 | // bindings so we will loop through the record and parameterize them all. |
| 865 | $parameters = \IAWPSCOPED\collect($values)->map(function ($record) { |
| 866 | return '(' . $this->parameterize($record) . ')'; |
| 867 | })->implode(', '); |
| 868 | return "insert into {$table} ({$columns}) values {$parameters}"; |
| 869 | } |
| 870 | /** |
| 871 | * Compile an insert ignore statement into SQL. |
| 872 | * |
| 873 | * @param \Illuminate\Database\Query\Builder $query |
| 874 | * @param array $values |
| 875 | * @return string |
| 876 | * |
| 877 | * @throws \RuntimeException |
| 878 | */ |
| 879 | public function compileInsertOrIgnore(Builder $query, array $values) |
| 880 | { |
| 881 | throw new RuntimeException('This database engine does not support inserting while ignoring errors.'); |
| 882 | } |
| 883 | /** |
| 884 | * Compile an insert and get ID statement into SQL. |
| 885 | * |
| 886 | * @param \Illuminate\Database\Query\Builder $query |
| 887 | * @param array $values |
| 888 | * @param string $sequence |
| 889 | * @return string |
| 890 | */ |
| 891 | public function compileInsertGetId(Builder $query, $values, $sequence) |
| 892 | { |
| 893 | return $this->compileInsert($query, $values); |
| 894 | } |
| 895 | /** |
| 896 | * Compile an insert statement using a subquery into SQL. |
| 897 | * |
| 898 | * @param \Illuminate\Database\Query\Builder $query |
| 899 | * @param array $columns |
| 900 | * @param string $sql |
| 901 | * @return string |
| 902 | */ |
| 903 | public function compileInsertUsing(Builder $query, array $columns, string $sql) |
| 904 | { |
| 905 | return "insert into {$this->wrapTable($query->from)} ({$this->columnize($columns)}) {$sql}"; |
| 906 | } |
| 907 | /** |
| 908 | * Compile an update statement into SQL. |
| 909 | * |
| 910 | * @param \Illuminate\Database\Query\Builder $query |
| 911 | * @param array $values |
| 912 | * @return string |
| 913 | */ |
| 914 | public function compileUpdate(Builder $query, array $values) |
| 915 | { |
| 916 | $table = $this->wrapTable($query->from); |
| 917 | $columns = $this->compileUpdateColumns($query, $values); |
| 918 | $where = $this->compileWheres($query); |
| 919 | return \trim(isset($query->joins) ? $this->compileUpdateWithJoins($query, $table, $columns, $where) : $this->compileUpdateWithoutJoins($query, $table, $columns, $where)); |
| 920 | } |
| 921 | /** |
| 922 | * Compile the columns for an update statement. |
| 923 | * |
| 924 | * @param \Illuminate\Database\Query\Builder $query |
| 925 | * @param array $values |
| 926 | * @return string |
| 927 | */ |
| 928 | protected function compileUpdateColumns(Builder $query, array $values) |
| 929 | { |
| 930 | return \IAWPSCOPED\collect($values)->map(function ($value, $key) { |
| 931 | return $this->wrap($key) . ' = ' . $this->parameter($value); |
| 932 | })->implode(', '); |
| 933 | } |
| 934 | /** |
| 935 | * Compile an update statement without joins into SQL. |
| 936 | * |
| 937 | * @param \Illuminate\Database\Query\Builder $query |
| 938 | * @param string $table |
| 939 | * @param string $columns |
| 940 | * @param string $where |
| 941 | * @return string |
| 942 | */ |
| 943 | protected function compileUpdateWithoutJoins(Builder $query, $table, $columns, $where) |
| 944 | { |
| 945 | return "update {$table} set {$columns} {$where}"; |
| 946 | } |
| 947 | /** |
| 948 | * Compile an update statement with joins into SQL. |
| 949 | * |
| 950 | * @param \Illuminate\Database\Query\Builder $query |
| 951 | * @param string $table |
| 952 | * @param string $columns |
| 953 | * @param string $where |
| 954 | * @return string |
| 955 | */ |
| 956 | protected function compileUpdateWithJoins(Builder $query, $table, $columns, $where) |
| 957 | { |
| 958 | $joins = $this->compileJoins($query, $query->joins); |
| 959 | return "update {$table} {$joins} set {$columns} {$where}"; |
| 960 | } |
| 961 | /** |
| 962 | * Compile an "upsert" statement into SQL. |
| 963 | * |
| 964 | * @param \Illuminate\Database\Query\Builder $query |
| 965 | * @param array $values |
| 966 | * @param array $uniqueBy |
| 967 | * @param array $update |
| 968 | * @return string |
| 969 | * |
| 970 | * @throws \RuntimeException |
| 971 | */ |
| 972 | public function compileUpsert(Builder $query, array $values, array $uniqueBy, array $update) |
| 973 | { |
| 974 | throw new RuntimeException('This database engine does not support upserts.'); |
| 975 | } |
| 976 | /** |
| 977 | * Prepare the bindings for an update statement. |
| 978 | * |
| 979 | * @param array $bindings |
| 980 | * @param array $values |
| 981 | * @return array |
| 982 | */ |
| 983 | public function prepareBindingsForUpdate(array $bindings, array $values) |
| 984 | { |
| 985 | $cleanBindings = Arr::except($bindings, ['select', 'join']); |
| 986 | return \array_values(\array_merge($bindings['join'], $values, Arr::flatten($cleanBindings))); |
| 987 | } |
| 988 | /** |
| 989 | * Compile a delete statement into SQL. |
| 990 | * |
| 991 | * @param \Illuminate\Database\Query\Builder $query |
| 992 | * @return string |
| 993 | */ |
| 994 | public function compileDelete(Builder $query) |
| 995 | { |
| 996 | $table = $this->wrapTable($query->from); |
| 997 | $where = $this->compileWheres($query); |
| 998 | return \trim(isset($query->joins) ? $this->compileDeleteWithJoins($query, $table, $where) : $this->compileDeleteWithoutJoins($query, $table, $where)); |
| 999 | } |
| 1000 | /** |
| 1001 | * Compile a delete statement without joins into SQL. |
| 1002 | * |
| 1003 | * @param \Illuminate\Database\Query\Builder $query |
| 1004 | * @param string $table |
| 1005 | * @param string $where |
| 1006 | * @return string |
| 1007 | */ |
| 1008 | protected function compileDeleteWithoutJoins(Builder $query, $table, $where) |
| 1009 | { |
| 1010 | return "delete from {$table} {$where}"; |
| 1011 | } |
| 1012 | /** |
| 1013 | * Compile a delete statement with joins into SQL. |
| 1014 | * |
| 1015 | * @param \Illuminate\Database\Query\Builder $query |
| 1016 | * @param string $table |
| 1017 | * @param string $where |
| 1018 | * @return string |
| 1019 | */ |
| 1020 | protected function compileDeleteWithJoins(Builder $query, $table, $where) |
| 1021 | { |
| 1022 | $alias = \IAWPSCOPED\last(\explode(' as ', $table)); |
| 1023 | $joins = $this->compileJoins($query, $query->joins); |
| 1024 | return "delete {$alias} from {$table} {$joins} {$where}"; |
| 1025 | } |
| 1026 | /** |
| 1027 | * Prepare the bindings for a delete statement. |
| 1028 | * |
| 1029 | * @param array $bindings |
| 1030 | * @return array |
| 1031 | */ |
| 1032 | public function prepareBindingsForDelete(array $bindings) |
| 1033 | { |
| 1034 | return Arr::flatten(Arr::except($bindings, 'select')); |
| 1035 | } |
| 1036 | /** |
| 1037 | * Compile a truncate table statement into SQL. |
| 1038 | * |
| 1039 | * @param \Illuminate\Database\Query\Builder $query |
| 1040 | * @return array |
| 1041 | */ |
| 1042 | public function compileTruncate(Builder $query) |
| 1043 | { |
| 1044 | return ['truncate table ' . $this->wrapTable($query->from) => []]; |
| 1045 | } |
| 1046 | /** |
| 1047 | * Compile the lock into SQL. |
| 1048 | * |
| 1049 | * @param \Illuminate\Database\Query\Builder $query |
| 1050 | * @param bool|string $value |
| 1051 | * @return string |
| 1052 | */ |
| 1053 | protected function compileLock(Builder $query, $value) |
| 1054 | { |
| 1055 | return \is_string($value) ? $value : ''; |
| 1056 | } |
| 1057 | /** |
| 1058 | * Determine if the grammar supports savepoints. |
| 1059 | * |
| 1060 | * @return bool |
| 1061 | */ |
| 1062 | public function supportsSavepoints() |
| 1063 | { |
| 1064 | return \true; |
| 1065 | } |
| 1066 | /** |
| 1067 | * Compile the SQL statement to define a savepoint. |
| 1068 | * |
| 1069 | * @param string $name |
| 1070 | * @return string |
| 1071 | */ |
| 1072 | public function compileSavepoint($name) |
| 1073 | { |
| 1074 | return 'SAVEPOINT ' . $name; |
| 1075 | } |
| 1076 | /** |
| 1077 | * Compile the SQL statement to execute a savepoint rollback. |
| 1078 | * |
| 1079 | * @param string $name |
| 1080 | * @return string |
| 1081 | */ |
| 1082 | public function compileSavepointRollBack($name) |
| 1083 | { |
| 1084 | return 'ROLLBACK TO SAVEPOINT ' . $name; |
| 1085 | } |
| 1086 | /** |
| 1087 | * Wrap the given JSON selector for boolean values. |
| 1088 | * |
| 1089 | * @param string $value |
| 1090 | * @return string |
| 1091 | */ |
| 1092 | protected function wrapJsonBooleanSelector($value) |
| 1093 | { |
| 1094 | return $this->wrapJsonSelector($value); |
| 1095 | } |
| 1096 | /** |
| 1097 | * Wrap the given JSON boolean value. |
| 1098 | * |
| 1099 | * @param string $value |
| 1100 | * @return string |
| 1101 | */ |
| 1102 | protected function wrapJsonBooleanValue($value) |
| 1103 | { |
| 1104 | return $value; |
| 1105 | } |
| 1106 | /** |
| 1107 | * Concatenate an array of segments, removing empties. |
| 1108 | * |
| 1109 | * @param array $segments |
| 1110 | * @return string |
| 1111 | */ |
| 1112 | protected function concatenate($segments) |
| 1113 | { |
| 1114 | return \implode(' ', \array_filter($segments, function ($value) { |
| 1115 | return (string) $value !== ''; |
| 1116 | })); |
| 1117 | } |
| 1118 | /** |
| 1119 | * Remove the leading boolean from a statement. |
| 1120 | * |
| 1121 | * @param string $value |
| 1122 | * @return string |
| 1123 | */ |
| 1124 | protected function removeLeadingBoolean($value) |
| 1125 | { |
| 1126 | return \preg_replace('/and |or /i', '', $value, 1); |
| 1127 | } |
| 1128 | /** |
| 1129 | * Get the grammar specific operators. |
| 1130 | * |
| 1131 | * @return array |
| 1132 | */ |
| 1133 | public function getOperators() |
| 1134 | { |
| 1135 | return $this->operators; |
| 1136 | } |
| 1137 | /** |
| 1138 | * Get the grammar specific bitwise operators. |
| 1139 | * |
| 1140 | * @return array |
| 1141 | */ |
| 1142 | public function getBitwiseOperators() |
| 1143 | { |
| 1144 | return $this->bitwiseOperators; |
| 1145 | } |
| 1146 | } |
| 1147 |