|
7 | 7 | * @author Muhammet ŞAFAK <[email protected]> |
8 | 8 | * @copyright Copyright © 2022 Muhammet ŞAFAK |
9 | 9 | * @license ./LICENSE MIT |
10 | | - * @version 2.0.8 |
| 10 | + * @version 2.1 |
11 | 11 | * @link https://www.muhammetsafak.com.tr |
12 | 12 | */ |
| 13 | +declare(strict_types=1); |
13 | 14 |
|
14 | 15 | namespace InitPHP\Database; |
15 | 16 |
|
16 | | -use InitPHP\Database\Helpers\{Helper, Parameters, Validation}; |
17 | | -use InitPHP\Database\Exceptions\QueryBuilderException; |
18 | | -use InitPHP\Database\Exceptions\QueryGeneratorException; |
19 | | -use \InitPHP\Database\Exceptions\ValueException; |
| 17 | +use \InitPHP\Database\Helpers\{Helper, Parameters}; |
| 18 | +use \InitPHP\Database\Exceptions\{QueryBuilderException, QueryGeneratorException, ValueException}; |
20 | 19 |
|
21 | 20 | class QueryBuilder |
22 | 21 | { |
@@ -65,6 +64,29 @@ public function exportQB(): array |
65 | 64 | return $this->_STRUCTURE; |
66 | 65 | } |
67 | 66 |
|
| 67 | + /** |
| 68 | + * @param string $key |
| 69 | + * @param string|int|float|bool|null $value |
| 70 | + * @return $this |
| 71 | + */ |
| 72 | + final public function setParameter(string $key, $value): self |
| 73 | + { |
| 74 | + Parameters::set($key, $value); |
| 75 | + return $this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param array $parameters |
| 80 | + * @return $this |
| 81 | + */ |
| 82 | + final public function setParameters(array $parameters = []): self |
| 83 | + { |
| 84 | + foreach ($parameters as $key => $value) { |
| 85 | + Parameters::set($key, $value); |
| 86 | + } |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
68 | 90 | /** |
69 | 91 | * @param string|Raw ...$columns |
70 | 92 | * @return $this |
@@ -368,7 +390,7 @@ final public function join($table, $onStmt = null, string $type = 'INNER'): self |
368 | 390 | $this->_STRUCTURE['join'][$table] = 'NATURAL JOIN ' . $table; |
369 | 391 | break; |
370 | 392 | default: |
371 | | - $this->_STRUCTURE['join'][$table] = $type . ' JOIN ' . $table . ' ON ' . $onStmt; |
| 393 | + $this->_STRUCTURE['join'][$table] = \trim(($type . ' JOIN ' . $table . ' ON ' . $onStmt)); |
372 | 394 | } |
373 | 395 | return $this; |
374 | 396 | } |
@@ -1124,6 +1146,23 @@ final public function raw(string $rawQuery): Raw |
1124 | 1146 | return new Raw($rawQuery); |
1125 | 1147 | } |
1126 | 1148 |
|
| 1149 | + final public function subQuery(\Closure $closure, ?string $alias = null, bool $isIntervalQuery = true): Raw |
| 1150 | + { |
| 1151 | + $queryBuilder = new self(); |
| 1152 | + \call_user_func_array($closure, [$queryBuilder]); |
| 1153 | + |
| 1154 | + if ($alias !== null && $isIntervalQuery !== TRUE) { |
| 1155 | + throw new QueryBuilderException('To define alias to a subquery, it must be an inner query.'); |
| 1156 | + } |
| 1157 | + |
| 1158 | + $rawQuery = ($isIntervalQuery === TRUE ? '(' : '') |
| 1159 | + . $queryBuilder->generateSelectQuery() |
| 1160 | + . ($isIntervalQuery === TRUE ? ')' : '') |
| 1161 | + . ($alias !== null ? ' AS ' . $alias : ''); |
| 1162 | + |
| 1163 | + return $this->raw($rawQuery); |
| 1164 | + } |
| 1165 | + |
1127 | 1166 | final public function generateInsertQuery(): string |
1128 | 1167 | { |
1129 | 1168 | if (!empty($this->_STRUCTURE['table'])) { |
@@ -1533,15 +1572,15 @@ private function whereOrHavingStatementPrepare($column, $value, string $mark = ' |
1533 | 1572 | } |
1534 | 1573 | $values[] = Helper::isSQLParameterOrFunction($val) ? $val : Parameters::add($column, $val); |
1535 | 1574 | } |
1536 | | - $value = \implode(', ', \array_unique($values)); |
| 1575 | + $value = '(' . \implode(', ', \array_unique($values)) . ')'; |
1537 | 1576 | } elseif (Helper::isSQLParameterOrFunction($value)) { |
1538 | 1577 | $value = (string)$value; |
1539 | 1578 | }else{ |
1540 | 1579 | throw new ValueException('An incorrect value was defined.'); |
1541 | 1580 | } |
1542 | 1581 | return $column |
1543 | 1582 | . ($searchMark === 'NOTIN' ? ' NOT ' : ' ') |
1544 | | - . 'IN (' . $value . ')'; |
| 1583 | + . 'IN ' . $value; |
1545 | 1584 | case 'FINDINSET': |
1546 | 1585 | case 'NOTFINDINSET': |
1547 | 1586 | if(\is_array($value)){ |
|
0 commit comments