From 020bad71def1bf4bc4fc0a7bbd319124093a2f08 Mon Sep 17 00:00:00 2001 From: Maksims Tarleckis Date: Thu, 16 Jan 2020 14:32:13 +0200 Subject: [PATCH] Add ignore() method to common Insert class --- src/Common/Insert.php | 15 +++++++++++++++ src/Sqlite/Insert.php | 16 ++++++++++++++++ tests/Pgsql/InsertTest.php | 9 +++++++++ tests/Sqlite/InsertTest.php | 14 ++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/src/Common/Insert.php b/src/Common/Insert.php index feb8123..3c763c7 100644 --- a/src/Common/Insert.php +++ b/src/Common/Insert.php @@ -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 diff --git a/src/Sqlite/Insert.php b/src/Sqlite/Insert.php index 27f0eb2..238d3bc 100644 --- a/src/Sqlite/Insert.php +++ b/src/Sqlite/Insert.php @@ -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; diff --git a/tests/Pgsql/InsertTest.php b/tests/Pgsql/InsertTest.php index c5ed196..b271da3 100644 --- a/tests/Pgsql/InsertTest.php +++ b/tests/Pgsql/InsertTest.php @@ -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(); + } } diff --git a/tests/Sqlite/InsertTest.php b/tests/Sqlite/InsertTest.php index 689640d..5375543 100644 --- a/tests/Sqlite/InsertTest.php +++ b/tests/Sqlite/InsertTest.php @@ -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()