]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
merge r6504 from 0.5 plus an enhancement to the unit test, [ticket:1611]
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 15 Nov 2009 20:22:57 +0000 (20:22 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 15 Nov 2009 20:22:57 +0000 (20:22 +0000)
CHANGES
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/test_postgresql.py

diff --git a/CHANGES b/CHANGES
index 04d62f1050b432b27e6fdf5dc76bb9117b7a7d11..917ea9b56fed258b153bcd1b4b57b5c32c28eb99 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -749,6 +749,11 @@ CHANGES
       or explicit sequence-stated schema, into account.
       [ticket:1576]
 
+    - Fixed the behavior of extract() to apply operator
+      precedence rules to the "::" operator when applying
+      the "timestamp" cast - ensures proper parenthesization.  
+      [ticket:1611]
+
 - mssql
     - Changed the name of TrustedConnection to
       Trusted_Connection when constructing pyodbc connect
index 9c6de362358d1e023325d96cfec407cdbc1cac94..0c386cc29e2b68fd4b2f9e00f47c679cd3a78a0a 100644 (file)
@@ -314,8 +314,8 @@ class PGCompiler(compiler.SQLCompiler):
 
     def visit_extract(self, extract, **kwargs):
         field = self.extract_map.get(extract.field, extract.field)
-        return "EXTRACT(%s FROM %s::timestamp)" % (
-            field, self.process(extract.expr))
+        return "EXTRACT(%s FROM %s)" % (
+            field, self.process(extract.expr.op('::')(sql.literal_column('timestamp'))))
 
 class PGDDLCompiler(compiler.DDLCompiler):
     def get_column_specification(self, column, **kwargs):
index 152ca40dabc1dfcb253e89ce49328167a1e11747..2a23224414e0357cf5b227136d2f0c30c3724f84 100644 (file)
@@ -104,9 +104,15 @@ class CompileTest(TestBase, AssertsCompiledSQL):
         for field in 'year', 'month', 'day':
             self.assert_compile(
                 select([extract(field, t.c.col1)]),
-                "SELECT EXTRACT(%s FROM t.col1::timestamp) AS anon_1 "
+                "SELECT EXTRACT(%s FROM t.col1 :: timestamp) AS anon_1 "
                 "FROM t" % field)
 
+        for field in 'year', 'month', 'day':
+            self.assert_compile(
+                select([extract(field, func.timestamp() - datetime.timedelta(days =5))]),
+                "SELECT EXTRACT(%s FROM (timestamp() - %%(timestamp_1)s) :: timestamp) AS anon_1"
+                 % field)
+
 class FloatCoercionTest(TablesTest, AssertsExecutionResults):
     __only_on__ = 'postgresql'
     __dialect__ = postgresql.dialect()
@@ -938,6 +944,19 @@ class MiscTest(TestBase, AssertsExecutionResults, AssertsCompiledSQL):
         finally:
             meta1.drop_all()
 
+    def test_extract(self):
+        fivedaysago = datetime.datetime.now() - datetime.timedelta(days=5)
+        for field, exp in (
+                    ('year', fivedaysago.year),
+                    ('month', fivedaysago.month),
+                    ('day', fivedaysago.day),
+            ):
+            r = testing.db.execute(
+                select([extract(field, func.now() + datetime.timedelta(days =-5))])
+            ).scalar()
+            eq_(r, exp)
+
+
     def test_checksfor_sequence(self):
         meta1 = MetaData(testing.db)
         t = Table('mytable', meta1,