]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Firebird 2 has a SUBSTRING() builtin, expose it thru a function
authorLele Gaifax <lele@metapensiero.it>
Fri, 11 Apr 2008 22:14:34 +0000 (22:14 +0000)
committerLele Gaifax <lele@metapensiero.it>
Fri, 11 Apr 2008 22:14:34 +0000 (22:14 +0000)
CHANGES
lib/sqlalchemy/databases/firebird.py
test/dialect/firebird.py

diff --git a/CHANGES b/CHANGES
index a045b0bf5a21cb3fc16d4405197993c80d3e4697..fc2c38b0ef76de096cea9bc35b94c1851e626d9b 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -35,7 +35,8 @@ CHANGES
             "AutoTranslat=%s" % odbc_autotranslate
       [ticket:1005]
 
-
+- firebird
+    - Handle the "SUBSTRING(:string FROM :start FOR :length)" builtin.
 
 
 0.4.5
index 11a88bf182bf964c09164a2bd569c8c07f9b313c..5e1dd72bb04c04bcdce5693029a2eac6f2bd3a5d 100644 (file)
@@ -547,6 +547,15 @@ class FBDialect(default.DefaultDialect):
         connection.commit(True)
 
 
+def _substring(s, start, length=None):
+    "Helper function to handle Firebird 2 SUBSTRING builtin"
+
+    if length is None:
+        return "SUBSTRING(%s FROM %s)" % (s, start)
+    else:
+        return "SUBSTRING(%s FROM %s FOR %s)" % (s, start, length)
+
+
 class FBCompiler(sql.compiler.DefaultCompiler):
     """Firebird specific idiosincrasies"""
 
@@ -564,6 +573,9 @@ class FBCompiler(sql.compiler.DefaultCompiler):
         else:
             return self.process(alias.original, **kwargs)
 
+    functions = sql.compiler.DefaultCompiler.functions.copy()
+    functions['substring'] = _substring
+
     def function_argspec(self, func):
         if func.clauses:
             return self.process(func.clause_expr)
index fe4161a1bdfa450f4464499353f5eaa3f7c87900..f929443fd42eb8177f3922d9c79b429c81521fcf 100644 (file)
@@ -85,6 +85,9 @@ class CompileTest(TestBase, AssertsCompiledSQL):
         t = Table('sometable', m, Column('col1', Integer), Column('col2', Integer))
         self.assert_compile(select([func.max(t.c.col1)]), "SELECT max(sometable.col1) AS max_1 FROM sometable")
 
+    def test_substring(self):
+        self.assert_compile(func.substring('abc', 1, 2), "SUBSTRING(:substring_1 FROM :substring_2 FOR :substring_3)")
+        self.assert_compile(func.substring('abc', 1), "SUBSTRING(:substring_1 FROM :substring_2)")
 
 class MiscFBTests(TestBase):