Skip to content

Commit

Permalink
Support insertOrIgnoreUsing to \Hyperf\Database\Query\Builder (#6…
Browse files Browse the repository at this point in the history
…783)
  • Loading branch information
zds-s authored May 23, 2024
1 parent f430d57 commit b27d37d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public function compileInsertOrIgnore(Builder $query, array $values)
return $this->compileInsert($query, $values) . ' on conflict do nothing';
}

/**
* Compile an insert ignore statement using a subquery into SQL.
*/
public function compileInsertOrIgnoreUsing(Builder $query, array $columns, string $sql): string
{
return $this->compileInsertUsing($query, $columns, $sql) . ' on conflict do nothing';
}

/**
* Compile an insert and get ID statement into SQL.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Cases/DatabasePostgresBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,29 @@ public function testWhereFullTextForReal()
$this->assertCount(1, $result);
}

#[RequiresPhpExtension('swoole', '< 6.0')]
public function testPostgresInsertOrIgnoreUsingMethod()
{
$builder = $this->getPostgresBuilderWithProcessor();
/**
* @var ConnectionInterface&m\MockInterface $connection
*/
$connection = $builder->getConnection();
$connection->allows('affectingStatement')->andReturnUsing(function ($query, $bindings) {
$this->assertEquals('insert into "table1" ("foo") select "bar" from "table2" where "foreign_id" = ? on conflict do nothing', $query);
$this->assertEquals([5], $bindings);
return 1;
});
$result = $builder->from('table1')->insertOrIgnoreUsing(
['foo'],
function (Builder $query) {
$query->select(['bar'])->from('table2')->where('foreign_id', '=', 5);
}
);

$this->assertEquals(1, $result);
}

protected function getBuilder($connection): PostgresBuilder
{
return new PostgresBuilder($connection);
Expand Down

0 comments on commit b27d37d

Please sign in to comment.