]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- column defaults and onupdates, executing inline, will add parenthesis
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 6 Sep 2007 16:37:37 +0000 (16:37 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 6 Sep 2007 16:37:37 +0000 (16:37 +0000)
  for subqueries and other parenthesis-requiring expressions

CHANGES
lib/sqlalchemy/sql/compiler.py
test/sql/select.py

diff --git a/CHANGES b/CHANGES
index 9fa7ad5988ab41d04918fcea55dc2c2ec37af710..6282b9cdeeae3d162559dfcfbef2d8491515dab5 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -21,6 +21,9 @@ CHANGES
   
 - Fixed reflection of the empty string for mysql enums.
 
+- column defaults and onupdates, executing inline,  will add parenthesis 
+  for subqueries and other parenthesis-requiring expressions
+
 0.4.0beta5
 ----------
 
index f1d0e329490390e66a7616ded229c48fd2d6e1bd..2b4786cb25624a9ddddcdd1ec7b61812a4f22ea2 100644 (file)
@@ -678,7 +678,7 @@ class DefaultCompiler(engine.Compiled, visitors.ClauseVisitor):
                         self.prefetch.add(c)
                     elif isinstance(c.default, schema.ColumnDefault):
                         if isinstance(c.default.arg, sql.ClauseElement):
-                            values.append((c, self.process(c.default.arg)))
+                            values.append((c, self.process(c.default.arg.self_group())))
                             self.postfetch.add(c)
                         else:
                             values.append((c, create_bind_param(c, None)))
@@ -693,7 +693,7 @@ class DefaultCompiler(engine.Compiled, visitors.ClauseVisitor):
                 elif self.isupdate:
                     if isinstance(c.onupdate, schema.ColumnDefault):
                         if isinstance(c.onupdate.arg, sql.ClauseElement):
-                            values.append((c, self.process(c.onupdate.arg)))
+                            values.append((c, self.process(c.onupdate.arg.self_group())))
                             self.postfetch.add(c)
                         else:
                             values.append((c, create_bind_param(c, None)))
index edca33bc0b60ae04433399b76c252cc52d1ae698..f0af7e238a1299b030cafe829dd5c9af74902e1f 100644 (file)
@@ -1187,7 +1187,33 @@ class CRUDTest(SQLCompileTest):
         s = select([table2.c.othername], table2.c.otherid == table1.c.myid)
         u = table1.delete(table1.c.name==s)
         self.assert_compile(u, "DELETE FROM mytable WHERE mytable.name = (SELECT myothertable.othername FROM myothertable WHERE myothertable.otherid = mytable.myid)")
+
+class InlineDefaultTest(SQLCompileTest):
+    def test_insert(self):
+        m = MetaData()
+        foo =  Table('foo', m,
+            Column('id', Integer))
             
+        t = Table('test', m,
+            Column('col1', Integer, default=func.foo(1)),
+            Column('col2', Integer, default=select([func.coalesce(func.max(foo.c.id))])),
+            )
+
+        self.assert_compile(t.insert(inline=True, values={}), "INSERT INTO test (col1, col2) VALUES (foo(:foo), (SELECT coalesce(max(foo.id)) FROM foo))")
+
+    def test_update(self):
+        m = MetaData()
+        foo =  Table('foo', m,
+            Column('id', Integer))
+
+        t = Table('test', m,
+            Column('col1', Integer, onupdate=func.foo(1)),
+            Column('col2', Integer, onupdate=select([func.coalesce(func.max(foo.c.id))])),
+            Column('col3', String(30))
+            )
+
+        self.assert_compile(t.update(inline=True, values={'col3':'foo'}), "UPDATE test SET col1=foo(:foo), col2=(SELECT coalesce(max(foo.id)) FROM foo), col3=:col3")
+        
 class SchemaTest(SQLCompileTest):
     def testselect(self):
         # these tests will fail with the MS-SQL compiler since it will alias schema-qualified tables