]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fixed empty (zero column) sqlite inserts, allowing inserts on
authorJason Kirtland <jek@discorporate.us>
Fri, 19 Oct 2007 07:02:48 +0000 (07:02 +0000)
committerJason Kirtland <jek@discorporate.us>
Fri, 19 Oct 2007 07:02:48 +0000 (07:02 +0000)
autoincrementing single column tables.

CHANGES
lib/sqlalchemy/databases/sqlite.py
test/dialect/sqlite.py

diff --git a/CHANGES b/CHANGES
index f86adc4c6cc320263d7158166440288bfa8f84a8..eab3ec13744ba2b3b345842af93e4cd191b67410 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,13 +1,19 @@
 =======
 CHANGES
 =======
+
 0.4.1
 -----
-- added test coverage for unknown type reflection, fixed
-  sqlite/mysql handling of type reflection for unknown types
+
+- Added test coverage for unknown type reflection, fixed sqlite/mysql
+  handling of type reflection for unknown types.
+
+- Fixed empty (zero column) sqlite inserts, allowing inserts on
+  autoincrementing single column tables.
   
 0.4.0
 -----
+
 - (see 0.4.0beta1 for the start of major changes against 0.3, 
   as well as http://www.sqlalchemy.org/trac/wiki/WhatsNewIn04 )
 
index 99a1d56503eeb68e3355a454402ef195879245ee..6666f60669dffd75e2f3a89174ecb1079804cde7 100644 (file)
@@ -362,6 +362,21 @@ class SQLiteCompiler(compiler.DefaultCompiler):
         # sqlite has no "FOR UPDATE" AFAICT
         return ''
 
+    def visit_insert(self, insert_stmt):
+        self.isinsert = True
+        colparams = self._get_colparams(insert_stmt)
+        preparer = self.preparer
+
+        if not colparams:
+            return "INSERT INTO %s DEFAULT VALUES" % (
+                (preparer.format_table(insert_stmt.table),))
+        else:
+            return ("INSERT INTO %s (%s) VALUES (%s)" %
+                    (preparer.format_table(insert_stmt.table),
+                     ', '.join([preparer.format_column(c[0])
+                                for c in colparams]),
+                     ', '.join([c[1] for c in colparams])))
+
 
 class SQLiteSchemaGenerator(compiler.SchemaGenerator):
 
index 43ee9e6a352fe0b28564b7e8a3d3f6136f2f0c7c..e401859bc2582e8edda7704cb28f0a09c40226b1 100644 (file)
@@ -3,6 +3,7 @@
 import testbase
 import datetime
 from sqlalchemy import *
+from sqlalchemy import exceptions
 from sqlalchemy.databases import sqlite
 from testlib import *
 
@@ -85,5 +86,67 @@ class DialectTest(AssertMixin):
             testbase.db.execute("drop table django_content_type")
 
 
+class InsertTest(AssertMixin):
+    """Tests inserts and autoincrement."""
+
+    def _test_empty_insert(self, table, expect=1):
+        try:
+            table.create()
+            for wanted in (expect, expect * 2):
+
+                table.insert().execute()
+
+                rows = table.select().execute().fetchall()
+                print rows
+                self.assertEquals(len(rows), wanted)
+        finally:
+            table.drop()
+
+    @testing.supported('sqlite')
+    def test_empty_insert_pk1(self):
+        self._test_empty_insert(
+            Table('a', MetaData(testbase.db),
+                  Column('id', Integer, primary_key=True)))
+
+    @testing.supported('sqlite')
+    def test_empty_insert_pk2(self):
+        self.assertRaises(
+            exceptions.DBAPIError,
+            self._test_empty_insert,
+            Table('b', MetaData(testbase.db),
+                  Column('x', Integer, primary_key=True),
+                  Column('y', Integer, primary_key=True)))
+
+    @testing.supported('sqlite')
+    def test_empty_insert_pk3(self):
+        self.assertRaises(
+            exceptions.DBAPIError,
+            self._test_empty_insert,
+            Table('c', MetaData(testbase.db),
+                  Column('x', Integer, primary_key=True),
+                  Column('y', Integer, PassiveDefault('123'),
+                         primary_key=True)))
+
+    @testing.supported('sqlite')
+    def test_empty_insert_pk4(self):
+        self._test_empty_insert(
+            Table('d', MetaData(testbase.db),
+                  Column('x', Integer, primary_key=True),
+                  Column('y', Integer, PassiveDefault('123'))))
+
+    @testing.supported('sqlite')
+    def test_empty_insert_nopk1(self):
+        self._test_empty_insert(
+            Table('e', MetaData(testbase.db),
+                  Column('id', Integer)))
+    
+    @testing.supported('sqlite')
+    def test_empty_insert_nopk2(self):
+        self._test_empty_insert(
+            Table('f', MetaData(testbase.db),
+                  Column('x', Integer),
+                  Column('y', Integer)))
+
+
 if __name__ == "__main__":
     testbase.main()