Repository: kayak/pypika (python)

Issue:
[feature request] INSERT IGNORE
Please add the option of using 'INSERT IGNORE', right now all insertions are just 'INSERT'.

Pull request:
Fixes #15 added ignore function

Patch:
diff --git a/pypika/queries.py b/pypika/queries.py
index 885a4d5..9cb6c21 100644
--- a/pypika/queries.py
+++ b/pypika/queries.py
@@ -159,6 +159,7 @@ class QueryBuilder(Selectable, Term):
         self._columns = []
         self._values = []
         self._distinct = False
+        self._ignore = False
 
         self._wheres = None
         self._groupbys = []
@@ -250,6 +251,10 @@ class QueryBuilder(Selectable, Term):
     def distinct(self):
         self._distinct = True
 
+    @builder
+    def ignore(self):
+        self._ignore = True
+
     @builder
     def where(self, criterion):
         self._validate_term(criterion)
@@ -514,8 +519,9 @@ class QueryBuilder(Selectable, Term):
         )
 
     def _insert_sql(self):
-        return 'INSERT INTO {table}'.format(
-            table=self._insert_table
+        return 'INSERT {ignore}INTO {table}'.format(
+            table=self._insert_table,
+            ignore='IGNORE ' if self._ignore else ''
         )
 
     def _columns_sql(self):


Test patch:
diff --git a/pypika/tests/test_inserts.py b/pypika/tests/test_inserts.py
index d7646f2..1a728b4 100644
--- a/pypika/tests/test_inserts.py
+++ b/pypika/tests/test_inserts.py
@@ -72,11 +72,16 @@ class InsertIntoTests(unittest.TestCase):
 
         self.assertEqual('INSERT INTO "abc" ("foo","bar","buz") VALUES (1,\'a\',true)', str(query))
 
-    def test_insert_none_ignored(self):
+    def test_insert_none_skipped(self):
         query = Query.into(self.table_abc).insert()
 
         self.assertEqual('', str(query))
 
+    def test_insert_ignore(self):
+        query = Query.into(self.table_abc).insert(1).ignore()
+
+        self.assertEqual('INSERT IGNORE INTO "abc" VALUES (1)', str(query))
+
 
 class InsertSelectFromTests(unittest.TestCase):
     table_abc, table_efg, table_hij = Tables('abc', 'efg', 'hij')
@@ -127,6 +132,11 @@ class InsertSelectFromTests(unittest.TestCase):
                          'SELECT "efg"."foo","efg"."bar","hij"."fiz","hij"."buz" FROM "efg" '
                          'JOIN "hij" ON "efg"."id"="hij"."abc_id"', str(query))
 
+    def test_insert_star(self):
+        query = Query.into(self.table_abc).from_(self.table_efg).select('*').ignore()
+
+        self.assertEqual('INSERT IGNORE INTO "abc" SELECT * FROM "efg"', str(query))
+
 
 class SelectIntoTests(unittest.TestCase):
     table_abc, table_efg, table_hij = Tables('abc', 'efg', 'hij')