Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore() method to common Insert class #173

Merged
merged 1 commit into from
Feb 4, 2022
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
15 changes: 15 additions & 0 deletions src/Common/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,21 @@ public function addRow(array $cols = array())
return $this;
}

/**
*
* Adds IGNORE flag depending on DB syntax.
*
* @param bool $enable Set or unset flag (default true).
* @throws Exception
* @return \Aura\SqlQuery\Sqlite\Insert
*
*/
public function ignore($enable = true)
{
// override in child classes
throw new Exception(get_class($this) . " doesn't support IGNORE flag");
}

/**
*
* Finishes off the current row in a bulk insert, collecting the bulk
Expand Down
16 changes: 16 additions & 0 deletions src/Sqlite/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,28 @@ public function orFail($enable = true)
*
* Adds or removes OR IGNORE flag.
*
* @deprecated use ignore instead
* @param bool $enable Set or unset flag (default true).
*
* @return $this
*
*/
public function orIgnore($enable = true)
{
$this->ignore($enable);
return $this;
}

/**
*
* Adds or removes OR IGNORE flag.
*
* @param bool $enable Set or unset flag (default true).
*
* @return $this
*
*/
public function ignore($enable = true)
{
$this->setFlag('OR IGNORE', $enable);
return $this;
Expand Down
9 changes: 9 additions & 0 deletions tests/Pgsql/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ public function testGetLastInsertIdName_default()
$expect = 'table_col_seq';
$this->assertSame($expect, $actual);
}

public function testIgnore()
{
$this->setExpectedException(
'Aura\SqlQuery\Exception',
"Aura\SqlQuery\Pgsql\Insert doesn't support IGNORE flag"
);
$this->query->ignore();
}
}
14 changes: 14 additions & 0 deletions tests/Sqlite/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ public function testOrIgnore()
$this->assertSameSql($expect, $actual);
}

public function testIgnore()
{
$this->query->ignore()
->into('t1')
->cols(array('c1', 'c2', 'c3'))
->set('c4', 'NOW()')
->set('c5', null);

$actual = $this->query->__toString();
$expect = sprintf($this->expected_sql_with_flag, 'OR IGNORE');

$this->assertSameSql($expect, $actual);
}

public function testOrReplace()
{
$this->query->orReplace()
Expand Down