From da452d3ec2f0673cdcedba88f4c2927b0fe02749 Mon Sep 17 00:00:00 2001 From: Adrien Di Mascio Date: Thu, 24 Jul 2008 18:05:40 +0200 Subject: [adbh] define the RANDOM function --- adbh.py | 12 +++++++++++- test/unittest_db.py | 39 ++++++++++++++++----------------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/adbh.py b/adbh.py index 34756dc..880eb8b 100644 --- a/adbh.py +++ b/adbh.py @@ -88,6 +88,14 @@ class LENGTH(FunctionDescr): class DATE(FunctionDescr): rtype = 'Date' +class RANDOM(FunctionDescr): + supported_backends = ('postgres', 'mysql',) + rtype = 'Float' + minargs = maxargs = 0 + name_mapping = {'postgres': 'RANDOM', + 'mysql': 'RAND', + } + class _GenericAdvFuncHelper: """Generic helper, trying to provide generic way to implement specific functionnalities from others DBMS @@ -113,6 +121,7 @@ class _GenericAdvFuncHelper: 'UPPER': UPPER, 'LOWER': LOWER, 'LENGTH': LENGTH, 'DATE': DATE, + 'RANDOM': RANDOM, # keyword function 'IN': IN } @@ -405,7 +414,8 @@ class _SqliteAdvFuncHelper(_GenericAdvFuncHelper): backend_name = 'sqlite' # modifiable but should not be shared FUNCTIONS = _GenericAdvFuncHelper.FUNCTIONS.copy() - + FUNCTIONS.pop('RANDOM') # not defined in sqlite + users_support = groups_support = False ilike_support = False union_parentheses_support = False diff --git a/test/unittest_db.py b/test/unittest_db.py index 9610ae8..91e9ca4 100644 --- a/test/unittest_db.py +++ b/test/unittest_db.py @@ -194,39 +194,32 @@ class DBAPIAdaptersTC(TestCase): def test_auto_register_funcdef(self): - class RANDOM(FunctionDescr): + class MYFUNC(FunctionDescr): supported_backends = ('postgres', 'sqlite',) - rtype = 'Float' - minargs = maxargs = 0 - name_mapping = {'postgres': 'RANDOM', - 'mysql': 'RAND', - 'sqlite': 'SQLITE_RANDOM'} - auto_register_function(RANDOM) + name_mapping = {'postgres': 'MYFUNC', + 'mysql': 'MYF', + 'sqlite': 'SQLITE_MYFUNC'} + auto_register_function(MYFUNC) - pghelper = get_adv_func_helper('postgres') - mshelper = get_adv_func_helper('mysql') - slhelper = get_adv_func_helper('sqlite') - self.failUnless('RANDOM' in pghelper.FUNCTIONS) - self.failUnless('RANDOM' in slhelper.FUNCTIONS) - self.failIf('RANDOM' in mshelper.FUNCTIONS) + self.failUnless('MYFUNC' in pghelper.FUNCTIONS) + self.failUnless('MYFUNC' in slhelper.FUNCTIONS) + self.failIf('MYFUNC' in mshelper.FUNCTIONS) def test_funcname_with_different_backend_names(self): - class RANDOM(FunctionDescr): + class MYFUNC(FunctionDescr): supported_backends = ('postgres', 'mysql', 'sqlite') - rtype = 'Float' - minargs = maxargs = 0 - name_mapping = {'postgres': 'RANDOM', - 'mysql': 'RAND', - 'sqlite': 'SQLITE_RANDOM'} - auto_register_function(RANDOM) + name_mapping = {'postgres': 'MYFUNC', + 'mysql': 'MYF', + 'sqlite': 'SQLITE_MYFUNC'} + auto_register_function(MYFUNC) pghelper = get_adv_func_helper('postgres') mshelper = get_adv_func_helper('mysql') slhelper = get_adv_func_helper('sqlite') - self.assertEquals(pghelper.func_sqlname('RANDOM'), 'RANDOM') - self.assertEquals(mshelper.func_sqlname('RANDOM'), 'RAND') - self.assertEquals(slhelper.func_sqlname('RANDOM'), 'SQLITE_RANDOM') + self.assertEquals(pghelper.func_sqlname('MYFUNC'), 'MYFUNC') + self.assertEquals(mshelper.func_sqlname('MYFUNC'), 'MYF') + self.assertEquals(slhelper.func_sqlname('MYFUNC'), 'SQLITE_MYFUNC') if __name__ == '__main__': -- cgit v1.2.1 From 7be444bc244bc2dab5117c051a8f4b56246a1c42 Mon Sep 17 00:00:00 2001 From: Adrien Di Mascio Date: Fri, 25 Jul 2008 08:53:52 +0200 Subject: oops, test fix --- test/unittest_db.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/unittest_db.py b/test/unittest_db.py index 91e9ca4..9d10aba 100644 --- a/test/unittest_db.py +++ b/test/unittest_db.py @@ -201,6 +201,9 @@ class DBAPIAdaptersTC(TestCase): 'sqlite': 'SQLITE_MYFUNC'} auto_register_function(MYFUNC) + pghelper = get_adv_func_helper('postgres') + mshelper = get_adv_func_helper('mysql') + slhelper = get_adv_func_helper('sqlite') self.failUnless('MYFUNC' in pghelper.FUNCTIONS) self.failUnless('MYFUNC' in slhelper.FUNCTIONS) self.failIf('MYFUNC' in mshelper.FUNCTIONS) -- cgit v1.2.1