From 2c3c081fb0f481f665f38435586790fc70945b97 Mon Sep 17 00:00:00 2001 From: Lele Gaifax Date: Thu, 13 Dec 2007 15:53:35 +0000 Subject: [PATCH] Use the external strlen UDF for func.length() under Firebird --- lib/sqlalchemy/databases/firebird.py | 6 ++++++ test/dialect/firebird.py | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/sqlalchemy/databases/firebird.py b/lib/sqlalchemy/databases/firebird.py index 59f435c92d..051c8c26b1 100644 --- a/lib/sqlalchemy/databases/firebird.py +++ b/lib/sqlalchemy/databases/firebird.py @@ -422,6 +422,12 @@ class FBCompiler(compiler.DefaultCompiler): """Already taken care of in the `get_select_precolumns` method.""" return "" + def function_string(self, func): + """Use the ``strlen`` UDF for the ``length`` function.""" + if func.name == 'length': + return "strlen%(expr)s" + return super(FBCompiler, self).function_string(func) + class FBSchemaGenerator(compiler.SchemaGenerator): def get_column_specification(self, column, **kwargs): diff --git a/test/dialect/firebird.py b/test/dialect/firebird.py index de1f92793f..f14422eb02 100644 --- a/test/dialect/firebird.py +++ b/test/dialect/firebird.py @@ -73,5 +73,27 @@ class CompileTest(SQLCompileTest): self.assert_compile(select([func.max(t.c.col1)]), "SELECT max(sometable.col1) FROM sometable") +class StrLenTest(PersistTest): + # On FB the length() function is implemented by an external UDF, + # strlen(). Various SA tests fail because they pass a parameter + # to it, and that does not work (it always results the maximum + # string length the UDF was declared to accept). + # This test checks that at least it works ok in other cases. + + def test_strlen(self): + meta = MetaData(testbase.db) + t = Table('t1', meta, + Column('id', Integer, Sequence('t1idseq'), primary_key=True), + Column('name', String(10)) + ) + meta.create_all() + try: + t.insert(values=dict(name='dante')).execute() + t.insert(values=dict(name='alighieri')).execute() + select([func.count(t.c.id)],func.length(t.c.name)==5).execute().fetchone()[0] == 1 + finally: + meta.drop_all() + + if __name__ == '__main__': testbase.main() -- 2.47.3