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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- New #393: Use `DateTimeColumn` class for datetime column types (@Tigrov)
- New #394, #395, #398: Implement `Command::upsertReturning()` method (@Tigrov)
- Enh #394, #395: Refactor `Command::insertWithReturningPks()` method (@Tigrov)
- Chg #399: Rename `insertWithReturningPks()` to `insertReturningPks()` in `Command` and `DMLQueryBuilder` classes (@Tigrov)

## 1.2.0 March 21, 2024

Expand Down
2 changes: 1 addition & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
final class Command extends AbstractPdoCommand
{
public function insertWithReturningPks(string $table, array|QueryInterface $columns): array|false
public function insertReturningPks(string $table, array|QueryInterface $columns): array|false
{
$tableSchema = $this->db->getSchema()->getTableSchema($table);
$primaryKeys = $tableSchema?->getPrimaryKey() ?? [];
Expand Down
2 changes: 1 addition & 1 deletion src/DMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
final class DMLQueryBuilder extends AbstractDMLQueryBuilder
{
/** @throws NotSupportedException */
public function insertWithReturningPks(string $table, array|QueryInterface $columns, array &$params = []): string
public function insertReturningPks(string $table, array|QueryInterface $columns, array &$params = []): string
{
throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
}
Expand Down
6 changes: 3 additions & 3 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function testGetRawSql(string $sql, array $params, string $expectedRawSql
parent::testGetRawSql($sql, $params, $expectedRawSql);
}

public function testInsertWithReturningPksWithSubqueryAndNoAutoincrement(): void
public function testInsertReturningPksWithSubqueryAndNoAutoincrement(): void
{
$db = $this->getConnection(true);
$command = $db->createCommand();
Expand All @@ -108,10 +108,10 @@ public function testInsertWithReturningPksWithSubqueryAndNoAutoincrement(): void

$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage(
'Yiisoft\Db\Mysql\Command::insertWithReturningPks() is not supported by MySQL for tables without auto increment when inserting sub-query.'
'Yiisoft\Db\Mysql\Command::insertReturningPks() is not supported by MySQL for tables without auto increment when inserting sub-query.'
);

$command->insertWithReturningPks('order_item', $query);
$command->insertReturningPks('order_item', $query);
}

#[DataProviderExternal(CommandProvider::class, 'update')]
Expand Down
21 changes: 11 additions & 10 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,22 +408,23 @@ public function testInsert(
parent::testInsert($table, $columns, $params, $expectedSQL, $expectedParams);
}

#[DataProviderExternal(QueryBuilderProvider::class, 'insertWithReturningPks')]
public function testInsertWithReturningPks(
#[DataProviderExternal(QueryBuilderProvider::class, 'insertReturningPks')]
public function testInsertReturningPks(
string $table,
array|QueryInterface $columns,
array $params,
string $expectedSQL,
array $expectedParams
): void {
$db = $this->getConnection();
$qb = $db->getQueryBuilder();

$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage(
'Yiisoft\Db\Mysql\DMLQueryBuilder::insertWithReturningPks is not supported by MySQL.'
'Yiisoft\Db\Mysql\DMLQueryBuilder::insertReturningPks is not supported by MySQL.'
);

$db = $this->getConnection(true);
$qb = $db->getQueryBuilder();
$qb->insertWithReturningPks($table, $columns, $params);
$qb->insertReturningPks($table, $columns, $params);

$db->close();
}
Expand Down Expand Up @@ -515,7 +516,7 @@ public function testResetSequence(): void
$this->assertSame($sql, $qb->resetSequence('item'));

$command->setSql($sql)->execute();
$insertResult = $command->insertWithReturningPks('item', ['name' => '123', 'category_id' => 1]);
$insertResult = $command->insertReturningPks('item', ['name' => '123', 'category_id' => 1]);
$this->assertEquals(6, $insertResult['id']);

// Key as string.
Expand All @@ -526,7 +527,7 @@ public function testResetSequence(): void
$this->assertSame($sql, $qb->resetSequence('item', '40'));

$command->setSql($sql)->execute();
$insertResult = $command->insertWithReturningPks('item', ['name' => '123', 'category_id' => 1]);
$insertResult = $command->insertReturningPks('item', ['name' => '123', 'category_id' => 1]);
$this->assertEquals(40, $insertResult['id']);

// Change up, key as int.
Expand All @@ -537,7 +538,7 @@ public function testResetSequence(): void
$this->assertSame($sql, $qb->resetSequence('item', 43));

$db->createCommand($sql)->execute();
$insertResult = $command->insertWithReturningPks('item', ['name' => '123', 'category_id' => 1]);
$insertResult = $command->insertReturningPks('item', ['name' => '123', 'category_id' => 1]);
$this->assertEquals(43, $insertResult['id']);

$command->delete('item', ['>=','id', 6])->execute();
Expand All @@ -552,7 +553,7 @@ public function testResetSequence(): void
$this->assertSame($sql, $qb->resetSequence('item'));

$db->createCommand($sql)->execute();
$insertResult = $command->insertWithReturningPks('item', ['name' => '123', 'category_id' => 1]);
$insertResult = $command->insertReturningPks('item', ['name' => '123', 'category_id' => 1]);
$this->assertEquals(6, $insertResult['id']);

$db->close();
Expand Down
2 changes: 1 addition & 1 deletion tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public function testTinyInt1()
)->execute();

$status = 2;
$insertedRow = $db->createCommand()->insertWithReturningPks($tableName, ['status' => $status, 'bool_col' => true]);
$insertedRow = $db->createCommand()->insertReturningPks($tableName, ['status' => $status, 'bool_col' => true]);
$selectedRow = $db->createCommand('SELECT * FROM ' . $tableName . ' WHERE id=:id', ['id' => $insertedRow['id']])->queryOne();

$this->assertEquals($status, $selectedRow['status']);
Expand Down