Skip to content

Commit ca15be4

Browse files
committed
Added insert ignore support
1 parent 32cce63 commit ca15be4

6 files changed

Lines changed: 115 additions & 0 deletions

File tree

src/Illuminate/Database/Query/Builder.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,6 +2647,33 @@ public function insert(array $values)
26472647
);
26482648
}
26492649

2650+
/**
2651+
* Insert ignore a new record into the database.
2652+
*
2653+
* @param array $values
2654+
* @return int
2655+
*/
2656+
public function insertOrIgnore(array $values)
2657+
{
2658+
if (empty($values)) {
2659+
return 0;
2660+
}
2661+
2662+
if (! is_array(reset($values))) {
2663+
$values = [$values];
2664+
} else {
2665+
foreach ($values as $key => $value) {
2666+
ksort($value);
2667+
$values[$key] = $value;
2668+
}
2669+
}
2670+
2671+
return $this->connection->affectingStatement(
2672+
$this->grammar->compileInsertOrIgnore($this, $values),
2673+
$this->cleanBindings(Arr::flatten($values, 1))
2674+
);
2675+
}
2676+
26502677
/**
26512678
* Insert a new record and get the value of the primary key.
26522679
*

src/Illuminate/Database/Query/Grammars/Grammar.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,18 @@ public function compileInsert(Builder $query, array $values)
872872
return "insert into $table ($columns) values $parameters";
873873
}
874874

875+
/**
876+
* Compile an insert ignore statement into SQL.
877+
*
878+
* @param \Illuminate\Database\Query\Builder $query
879+
* @param array $values
880+
* @return string
881+
*/
882+
public function compileInsertOrIgnore(Builder $query, array $values)
883+
{
884+
throw new RuntimeException('This database engine does not support insert or ignore.');
885+
}
886+
875887
/**
876888
* Compile an insert and get ID statement into SQL.
877889
*

src/Illuminate/Database/Query/Grammars/MySqlGrammar.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ public function compileSelect(Builder $query)
5454
return $sql;
5555
}
5656

57+
/**
58+
* Compile an insert ignore statement into SQL.
59+
*
60+
* @param \Illuminate\Database\Query\Builder $query
61+
* @param array $values
62+
* @return string
63+
*/
64+
public function compileInsertOrIgnore(Builder $query, array $values)
65+
{
66+
return substr_replace($this->compileInsert($query, $values), ' ignore', 6, 0);
67+
}
68+
5769
/**
5870
* Compile a "JSON contains" statement into SQL.
5971
*

src/Illuminate/Database/Query/Grammars/PostgresGrammar.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ public function compileInsert(Builder $query, array $values)
195195
: parent::compileInsert($query, $values);
196196
}
197197

198+
/**
199+
* Compile an insert ignore statement into SQL.
200+
*
201+
* @param \Illuminate\Database\Query\Builder $query
202+
* @param array $values
203+
* @return string
204+
*/
205+
public function compileInsertOrIgnore(Builder $query, array $values)
206+
{
207+
return $this->compileInsert($query, $values).' on conflict do nothing';
208+
}
209+
198210
/**
199211
* Compile an insert and get ID statement into SQL.
200212
*

src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ public function compileInsert(Builder $query, array $values)
178178
: parent::compileInsert($query, $values);
179179
}
180180

181+
/**
182+
* Compile an insert ignore statement into SQL.
183+
*
184+
* @param \Illuminate\Database\Query\Builder $query
185+
* @param array $values
186+
* @return string
187+
*/
188+
public function compileInsertOrIgnore(Builder $query, array $values)
189+
{
190+
return substr_replace($this->compileInsert($query, $values), ' or ignore', 6, 0);
191+
}
192+
181193
/**
182194
* Compile an update statement into SQL.
183195
*

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,46 @@ function (Builder $query) {
18671867
$this->assertTrue($result);
18681868
}
18691869

1870+
public function testInsertOrIgnoreMethod()
1871+
{
1872+
$this->expectException(RuntimeException::class);
1873+
$this->expectExceptionMessage('This database engine does not support insert or ignore.');
1874+
$builder = $this->getBuilder();
1875+
$builder->from('users')->insertOrIgnore(['email' => 'foo']);
1876+
}
1877+
1878+
public function testMySqlInsertOrIgnoreMethod()
1879+
{
1880+
$builder = $this->getMySqlBuilder();
1881+
$builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert ignore into `users` (`email`) values (?)', ['foo'])->andReturn(1);
1882+
$result = $builder->from('users')->insertOrIgnore(['email' => 'foo']);
1883+
$this->assertEquals(1, $result);
1884+
}
1885+
1886+
public function testPostgresInsertOrIgnoreMethod()
1887+
{
1888+
$builder = $this->getPostgresBuilder();
1889+
$builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into "users" ("email") values (?) on conflict do nothing', ['foo'])->andReturn(1);
1890+
$result = $builder->from('users')->insertOrIgnore(['email' => 'foo']);
1891+
$this->assertEquals(1, $result);
1892+
}
1893+
1894+
public function testSQLiteInsertOrIgnoreMethod()
1895+
{
1896+
$builder = $this->getSQLiteBuilder();
1897+
$builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert or ignore into "users" ("email") values (?)', ['foo'])->andReturn(1);
1898+
$result = $builder->from('users')->insertOrIgnore(['email' => 'foo']);
1899+
$this->assertEquals(1, $result);
1900+
}
1901+
1902+
public function testSqlServerInsertOrIgnoreMethod()
1903+
{
1904+
$this->expectException(RuntimeException::class);
1905+
$this->expectExceptionMessage('This database engine does not support insert or ignore.');
1906+
$builder = $this->getSqlServerBuilder();
1907+
$builder->from('users')->insertOrIgnore(['email' => 'foo']);
1908+
}
1909+
18701910
public function testInsertGetIdMethod()
18711911
{
18721912
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)