-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend QueryBuilder for possibility to use "into" and "from" with sta… #6114
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,9 @@ | |
|
||
use function array_key_exists; | ||
use function array_keys; | ||
use function array_map; | ||
use function array_unshift; | ||
use function array_values; | ||
use function count; | ||
use function func_get_args; | ||
use function func_num_args; | ||
|
@@ -69,7 +71,6 @@ class QueryBuilder | |
'distinct' => false, | ||
'from' => [], | ||
'join' => [], | ||
'set' => [], | ||
'where' => null, | ||
'groupBy' => [], | ||
'having' => null, | ||
|
@@ -585,7 +586,7 @@ public function getMaxResults() | |
/** | ||
* Either appends to or replaces a single, generic query part. | ||
* | ||
* The available parts are: 'select', 'from', 'set', 'where', | ||
* The available parts are: 'select', 'from', 'where', | ||
* 'groupBy', 'having' and 'orderBy'. | ||
* | ||
* @param string $sqlPartName | ||
|
@@ -610,7 +611,6 @@ public function add($sqlPartName, $sqlPart, $append = false) | |
$sqlPartName === 'orderBy' | ||
|| $sqlPartName === 'groupBy' | ||
|| $sqlPartName === 'select' | ||
|| $sqlPartName === 'set' | ||
) { | ||
foreach ($sqlPart as $part) { | ||
$this->sqlParts[$sqlPartName][] = $part; | ||
|
@@ -755,10 +755,9 @@ public function delete($delete = null, $alias = null) | |
return $this; | ||
} | ||
|
||
return $this->add('from', [ | ||
'table' => $delete, | ||
'alias' => $alias, | ||
]); | ||
$this->sqlParts['from'] = [['table' => $delete, 'alias' => $alias]]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
|
@@ -777,18 +776,13 @@ public function delete($delete = null, $alias = null) | |
* | ||
* @return $this This QueryBuilder instance. | ||
*/ | ||
public function update($update = null, $alias = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apps that currently calls |
||
public function update($update, $alias = null) | ||
{ | ||
$this->type = self::UPDATE; | ||
|
||
if ($update === null) { | ||
return $this; | ||
} | ||
$this->sqlParts['from'] = [['table' => $update, 'alias' => $alias]]; | ||
|
||
return $this->add('from', [ | ||
'table' => $update, | ||
'alias' => $alias, | ||
]); | ||
return $this; | ||
} | ||
|
||
/** | ||
|
@@ -818,7 +812,28 @@ public function insert($insert = null) | |
return $this; | ||
} | ||
|
||
return $this->add('from', ['table' => $insert]); | ||
$this->sqlParts['from'] = [['table' => $insert]]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Turns the query being built into an insert query that inserts into | ||
* a certain table | ||
* | ||
* <code> | ||
* $qb = $conn->createQueryBuilder() | ||
* ->insert() | ||
* ->into('users') | ||
* </code> | ||
* | ||
* @param string $insert The table into which the rows should be inserted. | ||
* | ||
* @return $this This QueryBuilder instance. | ||
*/ | ||
public function into($insert) | ||
{ | ||
return $this->insert($insert); | ||
} | ||
|
||
/** | ||
|
@@ -838,10 +853,9 @@ public function insert($insert = null) | |
*/ | ||
public function from($from, $alias = null) | ||
{ | ||
return $this->add('from', [ | ||
'table' => $from, | ||
'alias' => $alias, | ||
], true); | ||
Comment on lines
-841
to
-844
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason for not calling |
||
$this->sqlParts['from'][] = ['table' => $from, 'alias' => $alias]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
|
@@ -970,7 +984,7 @@ public function rightJoin($fromAlias, $join, $alias, $condition = null) | |
*/ | ||
public function set($key, $value) | ||
{ | ||
return $this->add('set', $key . ' = ' . $value, true); | ||
return $this->setValue($key, $value); | ||
} | ||
|
||
/** | ||
|
@@ -1423,7 +1437,7 @@ private function isLimitQuery(): bool | |
*/ | ||
private function getSQLForInsert(): string | ||
{ | ||
return 'INSERT INTO ' . $this->sqlParts['from']['table'] . | ||
return 'INSERT INTO ' . $this->sqlParts['from'][0]['table'] . | ||
' (' . implode(', ', array_keys($this->sqlParts['values'])) . ')' . | ||
' VALUES(' . implode(', ', $this->sqlParts['values']) . ')'; | ||
} | ||
|
@@ -1433,11 +1447,15 @@ private function getSQLForInsert(): string | |
*/ | ||
private function getSQLForUpdate(): string | ||
{ | ||
$table = $this->sqlParts['from']['table'] | ||
. ($this->sqlParts['from']['alias'] ? ' ' . $this->sqlParts['from']['alias'] : ''); | ||
$table = $this->sqlParts['from'][0]['table'] | ||
. ($this->sqlParts['from'][0]['alias'] ? ' ' . $this->sqlParts['from'][0]['alias'] : ''); | ||
|
||
return 'UPDATE ' . $table | ||
. ' SET ' . implode(', ', $this->sqlParts['set']) | ||
. ' SET ' . implode(', ', array_map( | ||
(static fn ($k, $v) => $k . ' = ' . $v), | ||
array_keys($this->sqlParts['values']), | ||
array_values($this->sqlParts['values']), | ||
)) | ||
. ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : ''); | ||
} | ||
|
||
|
@@ -1446,8 +1464,8 @@ private function getSQLForUpdate(): string | |
*/ | ||
private function getSQLForDelete(): string | ||
{ | ||
$table = $this->sqlParts['from']['table'] | ||
. ($this->sqlParts['from']['alias'] ? ' ' . $this->sqlParts['from']['alias'] : ''); | ||
$table = $this->sqlParts['from'][0]['table'] | ||
. ($this->sqlParts['from'][0]['alias'] ? ' ' . $this->sqlParts['from'][0]['alias'] : ''); | ||
|
||
return 'DELETE FROM ' . $table | ||
. ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : ''); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for this change? What happens to existing apps that call
$qb->add('set', …)
?