]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Use the external strlen UDF for func.length() under Firebird
authorLele Gaifax <lele@metapensiero.it>
Thu, 13 Dec 2007 15:53:35 +0000 (15:53 +0000)
committerLele Gaifax <lele@metapensiero.it>
Thu, 13 Dec 2007 15:53:35 +0000 (15:53 +0000)
lib/sqlalchemy/databases/firebird.py
test/dialect/firebird.py

index 59f435c92db37d892b206ed93fcb745af854de34..051c8c26b1e372dbce2ca8f1b32bbf7efd6c6a36 100644 (file)
@@ -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):
index de1f92793f588fbcacce31da85983426df172924..f14422eb02ea6114fcf76627b3671a8701a665f5 100644 (file)
@@ -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()