Skip to content

Commit

Permalink
Added insert ignore support
Browse files Browse the repository at this point in the history
  • Loading branch information
dunhamjared committed Aug 19, 2019
1 parent 32cce63 commit ca15be4
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2647,6 +2647,33 @@ public function insert(array $values)
);
}

/**
* Insert ignore a new record into the database.
*
* @param array $values
* @return int
*/
public function insertOrIgnore(array $values)
{
if (empty($values)) {
return 0;
}

if (! is_array(reset($values))) {
$values = [$values];
} else {
foreach ($values as $key => $value) {
ksort($value);
$values[$key] = $value;
}
}

return $this->connection->affectingStatement(
$this->grammar->compileInsertOrIgnore($this, $values),
$this->cleanBindings(Arr::flatten($values, 1))
);
}

/**
* Insert a new record and get the value of the primary key.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,18 @@ public function compileInsert(Builder $query, array $values)
return "insert into $table ($columns) values $parameters";
}

/**
* Compile an insert ignore statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
*/
public function compileInsertOrIgnore(Builder $query, array $values)
{
throw new RuntimeException('This database engine does not support insert or ignore.');
}

/**
* Compile an insert and get ID statement into SQL.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ public function compileSelect(Builder $query)
return $sql;
}

/**
* Compile an insert ignore statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
*/
public function compileInsertOrIgnore(Builder $query, array $values)
{
return substr_replace($this->compileInsert($query, $values), ' ignore', 6, 0);
}

/**
* Compile a "JSON contains" statement into SQL.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ public function compileInsert(Builder $query, array $values)
: parent::compileInsert($query, $values);
}

/**
* Compile an insert ignore statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
*/
public function compileInsertOrIgnore(Builder $query, array $values)
{
return $this->compileInsert($query, $values).' on conflict do nothing';
}

/**
* Compile an insert and get ID statement into SQL.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ public function compileInsert(Builder $query, array $values)
: parent::compileInsert($query, $values);
}

/**
* Compile an insert ignore statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
*/
public function compileInsertOrIgnore(Builder $query, array $values)
{
return substr_replace($this->compileInsert($query, $values), ' or ignore', 6, 0);
}

/**
* Compile an update statement into SQL.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,46 @@ function (Builder $query) {
$this->assertTrue($result);
}

public function testInsertOrIgnoreMethod()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('This database engine does not support insert or ignore.');
$builder = $this->getBuilder();
$builder->from('users')->insertOrIgnore(['email' => 'foo']);
}

public function testMySqlInsertOrIgnoreMethod()
{
$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert ignore into `users` (`email`) values (?)', ['foo'])->andReturn(1);
$result = $builder->from('users')->insertOrIgnore(['email' => 'foo']);
$this->assertEquals(1, $result);
}

public function testPostgresInsertOrIgnoreMethod()
{
$builder = $this->getPostgresBuilder();
$builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into "users" ("email") values (?) on conflict do nothing', ['foo'])->andReturn(1);
$result = $builder->from('users')->insertOrIgnore(['email' => 'foo']);
$this->assertEquals(1, $result);
}

public function testSQLiteInsertOrIgnoreMethod()
{
$builder = $this->getSQLiteBuilder();
$builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert or ignore into "users" ("email") values (?)', ['foo'])->andReturn(1);
$result = $builder->from('users')->insertOrIgnore(['email' => 'foo']);
$this->assertEquals(1, $result);
}

public function testSqlServerInsertOrIgnoreMethod()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('This database engine does not support insert or ignore.');
$builder = $this->getSqlServerBuilder();
$builder->from('users')->insertOrIgnore(['email' => 'foo']);
}

public function testInsertGetIdMethod()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit ca15be4

Please sign in to comment.