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