Skip to content

Commit b27d37d

Browse files
authored
Support insertOrIgnoreUsing to \Hyperf\Database\Query\Builder (#6783)
1 parent f430d57 commit b27d37d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/Query/Grammars/PostgresGrammar.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function compileInsertOrIgnore(Builder $query, array $values)
4646
return $this->compileInsert($query, $values) . ' on conflict do nothing';
4747
}
4848

49+
/**
50+
* Compile an insert ignore statement using a subquery into SQL.
51+
*/
52+
public function compileInsertOrIgnoreUsing(Builder $query, array $columns, string $sql): string
53+
{
54+
return $this->compileInsertUsing($query, $columns, $sql) . ' on conflict do nothing';
55+
}
56+
4957
/**
5058
* Compile an insert and get ID statement into SQL.
5159
*

tests/Cases/DatabasePostgresBuilderTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,29 @@ public function testWhereFullTextForReal()
236236
$this->assertCount(1, $result);
237237
}
238238

239+
#[RequiresPhpExtension('swoole', '< 6.0')]
240+
public function testPostgresInsertOrIgnoreUsingMethod()
241+
{
242+
$builder = $this->getPostgresBuilderWithProcessor();
243+
/**
244+
* @var ConnectionInterface&m\MockInterface $connection
245+
*/
246+
$connection = $builder->getConnection();
247+
$connection->allows('affectingStatement')->andReturnUsing(function ($query, $bindings) {
248+
$this->assertEquals('insert into "table1" ("foo") select "bar" from "table2" where "foreign_id" = ? on conflict do nothing', $query);
249+
$this->assertEquals([5], $bindings);
250+
return 1;
251+
});
252+
$result = $builder->from('table1')->insertOrIgnoreUsing(
253+
['foo'],
254+
function (Builder $query) {
255+
$query->select(['bar'])->from('table2')->where('foreign_id', '=', 5);
256+
}
257+
);
258+
259+
$this->assertEquals(1, $result);
260+
}
261+
239262
protected function getBuilder($connection): PostgresBuilder
240263
{
241264
return new PostgresBuilder($connection);

0 commit comments

Comments
 (0)