From fd8567037269ac937a6b079c6e00022abfc51149 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 1 Mar 2006 19:43:07 +0000 Subject: [PATCH] postgres leaves parenthesis off functions only for no-argument ANSI functions according to a list --- CHANGES | 3 ++- lib/sqlalchemy/databases/postgres.py | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index f0cd2fbc41..5a749f1310 100644 --- a/CHANGES +++ b/CHANGES @@ -25,7 +25,8 @@ check when object attributes are modified or the object is deleted - added "convert_unicode" flag to SQLEngine, will treat all String/CHAR types as Unicode types, with raw-byte/utf-8 translation on the bind parameter and result set side. - +- postgres maintains a list of ANSI functions that must have no parenthesis so +function calls with no arguments work consistently 0.1.2 - fixed a recursive call in schema that was somehow running 994 times then returning normally. broke nothing, slowed down everything. thanks to jpellerin for finding this. diff --git a/lib/sqlalchemy/databases/postgres.py b/lib/sqlalchemy/databases/postgres.py index 67e3d5cf57..13714ba3e6 100644 --- a/lib/sqlalchemy/databases/postgres.py +++ b/lib/sqlalchemy/databases/postgres.py @@ -6,6 +6,7 @@ import sys, StringIO, string, types, re +import sqlalchemy.util as util import sqlalchemy.sql as sql import sqlalchemy.engine as engine import sqlalchemy.schema as schema @@ -101,7 +102,18 @@ class PGBinary(sqltypes.Binary): class PGBoolean(sqltypes.Boolean): def get_col_spec(self): return "BOOLEAN" - + +ANSI_FUNCS = util.HashSet([ +'CURRENT_TIME', +'CURRENT_TIMESTAMP', +'CURRENT_DATE', +'LOCAL_TIME', +'LOCAL_TIMESTAMP', +'CURRENT_USER', +'SESSION_USER', +'USER' +]) + pg2_colspecs = { sqltypes.Integer : PGInteger, sqltypes.Smallinteger : PGSmallInteger, @@ -270,10 +282,11 @@ class PGSQLEngine(ansisql.ANSISQLEngine): class PGCompiler(ansisql.ANSICompiler): def visit_function(self, func): - if len(func.clauses): - super(PGCompiler, self).visit_function(func) - else: + # PG has a bunch of funcs that explicitly need no parenthesis + if func.name.upper() in ANSI_FUNCS and not len(func.clauses): self.strings[func] = func.name + else: + super(PGCompiler, self).visit_function(func) def visit_insert_column(self, column): # Postgres advises against OID usage and turns it off in 8.1, -- 2.47.2