-
-
Notifications
You must be signed in to change notification settings - Fork 40
Add ability to pass FROM
clause to UPDATE
command
#1038
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
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c846471
Add ability to pass `FROM` clause to `UPDATE` command
rustamwin 30e1183
Fix parameters order, add test cases
rustamwin b024bf3
Fix tests
rustamwin a4cf434
Fix CS
rustamwin 2e088fa
Fix psalm
rustamwin 08a5f3a
Improve test cases
rustamwin c495e75
Merge branch 'master' into fix-94
rustamwin 4de75f5
Add changelog
rustamwin e85af52
Merge branch 'master' into fix-94
vjik 28f2461
Minor fix
rustamwin 9b91bbf
Merge remote-tracking branch 'origin/fix-94' into fix-94
rustamwin b639ce3
Minor fix
rustamwin 1f6ca70
Merge branch 'master' into fix-94
vjik c75a2fe
Merge remote-tracking branch 'origin/master' into fix-94
rustamwin 632e5aa
Fix test providers
rustamwin e2999ce
Fix tests
rustamwin 8452445
Merge branch 'master' into fix-94
vjik 673003a
Merge remote-tracking branch 'origin/master' into fix-94
rustamwin 0147a8f
Merge branch 'master' into fix-94
vjik 5b32a20
Merge branch 'master' into fix-94
vjik 155b005
Merge branch 'master' into fix-94
vjik 10c490c
Fix changelog
rustamwin 06a87ae
Bring back $params argument
rustamwin 0064c31
Apply Rector changes (CI)
rustamwin 64b6e53
Apply suggestions from code review
rustamwin e1a0c9e
Merge branch 'master' into fix-94
Tigrov c94a597
Close connections in tests
Tigrov 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
use IteratorAggregate; | ||
use Traversable; | ||
use Yiisoft\Db\Connection\ConnectionInterface; | ||
use Yiisoft\Db\Constant\GettypeResult; | ||
use Yiisoft\Db\Constraint\Index; | ||
use Yiisoft\Db\Exception\Exception; | ||
use InvalidArgumentException; | ||
|
@@ -136,12 +137,21 @@ public function resetSequence(string $table, int|string|null $value = null): str | |
throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.'); | ||
} | ||
|
||
public function update(string $table, array $columns, array|string $condition, array &$params = []): string | ||
{ | ||
public function update( | ||
string $table, | ||
array $columns, | ||
array|ExpressionInterface|string $condition, | ||
array|ExpressionInterface|string|null $from = null, | ||
array &$params = [] | ||
): string { | ||
$updates = $this->prepareUpdateSets($table, $columns, $params); | ||
|
||
$sql = 'UPDATE ' . $this->quoter->quoteTableName($table) . ' SET ' . implode(', ', $updates); | ||
$where = $this->queryBuilder->buildWhere($condition, $params); | ||
if ($from !== null) { | ||
$fromClause = $this->queryBuilder->buildFrom($this->prepareFromTables($from), $params); | ||
$sql .= $fromClause === '' ? '' : ' ' . $fromClause; | ||
} | ||
|
||
return $where === '' ? $sql : $sql . ' ' . $where; | ||
} | ||
|
@@ -493,6 +503,19 @@ protected function prepareUpsertColumns( | |
return [$uniqueNames, $insertNames, null]; | ||
} | ||
|
||
protected function prepareFromTables(array|ExpressionInterface|string $from): array | ||
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. This method identical to 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. Yes, we can but we need to refactor a bit. |
||
{ | ||
/** | ||
* @var array | ||
* @psalm-suppress PossiblyInvalidArgument | ||
*/ | ||
return match (gettype($from)) { | ||
GettypeResult::ARRAY => $from, | ||
GettypeResult::STRING => preg_split('/\s*,\s*/', trim($from), -1, PREG_SPLIT_NO_EMPTY), | ||
default => [$from], | ||
}; | ||
} | ||
|
||
/** | ||
* Returns all column names belonging to constraints enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.) | ||
* for the named table removing constraints which didn't cover the specified column list. | ||
|
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.