Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
- New #387: Realize `Schema::loadResultColumn()` method (@Tigrov)
- New #393: Use `DateTimeColumn` class for datetime column types (@Tigrov)
- Enh #396, #409: Refactor constraints (@Tigrov)
- New #394, #395, #398, #425, #435: Implement `Command::upsertReturning()` method (@Tigrov, @vjik)
- New #394, #395, #398, #425, #435, #437: Implement `Command::upsertReturning()` method (@Tigrov, @vjik)
- Enh #394, #395: Refactor `Command::insertWithReturningPks()` method (@Tigrov)
- Chg #399: Rename `insertWithReturningPks()` to `insertReturningPks()` in `Command` and `DMLQueryBuilder` classes (@Tigrov)
- Enh #403: Refactor `DMLQueryBuilder::upsert()`, allow use `EXCLUDED` table alias to access inserted values (@Tigrov)
Expand All @@ -53,7 +53,7 @@
- New #420, #427: Implement `ArrayMergeBuilder`, `LongestBuilder` and `ShortestBuilder` classes (@Tigrov)
- Enh #423: Refactor `DMLQueryBuilder::upsert()` method (@Tigrov)
- Chg #428: Update expression namespaces according to changes in `yiisoft/db` package (@Tigrov)
- Enh #442, #433: Update `DMLQueryBuilder::update()` method to adapt changes in `yiisoft/db` (@rustamwin, @Tigrov)
- Enh #432, #433: Update `DMLQueryBuilder::update()` method to adapt changes in `yiisoft/db` (@rustamwin, @Tigrov)

## 1.2.0 March 21, 2024

Expand Down
15 changes: 5 additions & 10 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Yiisoft\Db\Exception\IntegrityException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\Schema\Column\ColumnInterface;

use function in_array;
use function str_starts_with;
Expand All @@ -22,14 +21,13 @@
*/
final class Command extends AbstractPdoCommand
{
public function insertReturningPks(string $table, array|QueryInterface $columns): array|false
public function insertReturningPks(string $table, array|QueryInterface $columns): array
{
$tableSchema = $this->db->getSchema()->getTableSchema($table);
$primaryKeys = $tableSchema?->getPrimaryKey() ?? [];
$tableColumns = $tableSchema?->getColumns() ?? [];

foreach ($primaryKeys as $name) {
/** @var ColumnInterface $column */
$column = $tableColumns[$name];

if ($column->isAutoIncrement()) {
Expand All @@ -49,9 +47,7 @@ public function insertReturningPks(string $table, array|QueryInterface $columns)
$insertSql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
$this->setSql($insertSql)->bindValues($params);

if ($this->execute() === 0) {
return false;
}
$this->execute();

if (empty($primaryKeys)) {
return [];
Expand All @@ -60,7 +56,6 @@ public function insertReturningPks(string $table, array|QueryInterface $columns)
$result = [];

foreach ($primaryKeys as $name) {
/** @var ColumnInterface $column */
$column = $tableColumns[$name];

if ($column->isAutoIncrement()) {
Expand All @@ -85,7 +80,7 @@ public function upsertReturning(
array|QueryInterface $insertColumns,
array|bool $updateColumns = true,
array|null $returnColumns = null,
): array|false {
): array {
$returnColumns ??= $this->db->getTableSchema($table)?->getColumnNames();

if (empty($returnColumns)) {
Expand All @@ -102,11 +97,11 @@ public function upsertReturning(

/** @psalm-var PDOStatement $this->pdoStatement */
$this->pdoStatement->nextRowset();
/** @psalm-var array<string,mixed>|false $result */
/** @psalm-var array<string,mixed> $result */
$result = $this->pdoStatement->fetch(PDO::FETCH_ASSOC);
$this->pdoStatement->closeCursor();

if (!$this->phpTypecasting || $result === false) {
if (!$this->phpTypecasting) {
return $result;
}

Expand Down
Loading