diff options
author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> | 2009-04-22 09:05:24 +0200 |
---|---|---|
committer | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> | 2009-04-22 09:05:24 +0200 |
commit | d8b1bfeb9fd9a533a8f2899c4fcc65f5e867e33b (patch) | |
tree | 6af98988bea6e20008c9b62d5aef5beae6903adc | |
parent | be2af3174e06512f0a35a653b4f00a0be86a9e78 (diff) | |
download | logilab-common-d8b1bfeb9fd9a533a8f2899c4fcc65f5e867e33b.tar.gz |
[adbh] remove trailing whitespaces + fix mysqldb boolean handling
Some versions of MySQLdb don't understand True and False and need
explicit 1 or 0 values (boolean being stored as TINYINT(1))
-rw-r--r-- | adbh.py | 122 |
1 files changed, 61 insertions, 61 deletions
@@ -27,7 +27,7 @@ class metafunc(type): dict['name'] = name.upper() return type.__new__(mcs, name, bases, dict) - + class FunctionDescr(object): __metaclass__ = metafunc @@ -49,7 +49,7 @@ class FunctionDescr(object): def backend_name(self, backend): return self.name_mapping.get(backend, self.name) backend_name = classmethod(backend_name) - + #@classmethod def check_nbargs(cls, nbargs): if cls.minargs is not None and \ @@ -62,12 +62,12 @@ class FunctionDescr(object): class AggrFunctionDescr(FunctionDescr): aggregat = True - rtype = None + rtype = None class MAX(AggrFunctionDescr): pass class MIN(AggrFunctionDescr): pass class SUM(AggrFunctionDescr): pass -class COUNT(AggrFunctionDescr): +class COUNT(AggrFunctionDescr): rtype = 'Int' class AVG(AggrFunctionDescr): rtype = 'Float' @@ -98,9 +98,9 @@ class _GenericAdvFuncHelper: specific functionnalities from others DBMS An exception is raised when the functionality is not emulatable - """ + """ # DBMS resources descriptors and accessors - + backend_name = None # overriden in subclasses ('postgres', 'sqlite', etc.) needs_from_clause = False union_parentheses_support = True @@ -130,8 +130,8 @@ class _GenericAdvFuncHelper: 'Float' : 'float', 'Decimal' : 'decimal', 'Boolean' : 'boolean', - 'Date' : 'date', - 'Time' : 'time', + 'Date' : 'date', + 'Time' : 'time', 'Datetime' : 'timestamp', 'Interval' : 'interval', 'Password' : 'bytea', @@ -147,7 +147,7 @@ class _GenericAdvFuncHelper: '%s is already registered' % funcdef.name cls.FUNCTIONS[funcdef.name] = funcdef register_function = classmethod(register_function) - + #@classmethod def function_description(cls, funcname): """return the description (`FunctionDescription`) for a RQL function""" @@ -156,7 +156,7 @@ class _GenericAdvFuncHelper: except KeyError: raise UnsupportedFunction(funcname) function_description = classmethod(function_description) - + #@obsolete('use users_support attribute') def support_users(self): """return True if the DBMS support users (this is usually @@ -164,8 +164,8 @@ class _GenericAdvFuncHelper: """ return self.users_support support_user = obsolete('use users_support attribute')(support_users) - - #@obsolete('use groups_support attribute') + + #@obsolete('use groups_support attribute') def support_groups(self): """return True if the DBMS support groups""" return self.groups_support @@ -174,7 +174,7 @@ class _GenericAdvFuncHelper: def func_sqlname(self, funcname): funcdef = self.function_description(funcname) return funcdef.backend_name(self.backend_name) - + def system_database(self): """return the system database for the given driver""" raise NotImplementedError('not supported by this DBMS') @@ -183,27 +183,27 @@ class _GenericAdvFuncHelper: keepownership=True): """return a command to backup the given database""" raise NotImplementedError('not supported by this DBMS') - + def restore_commands(self, dbname, dbhost, dbuser, backupfile, encoding='utf-8', keepownership=True, drop=True): """return a list of commands to restore a backup the given database""" raise NotImplementedError('not supported by this DBMS') - + # helpers to standardize SQL according to the database - + def sql_current_date(self): return 'CURRENT_DATE' - + def sql_current_time(self): return 'CURRENT_TIME' - + def sql_current_timestamp(self): return 'CURRENT_TIMESTAMP' - + def sql_create_sequence(self, seq_name): return '''CREATE TABLE %s (last INTEGER); INSERT INTO %s VALUES (0);''' % (seq_name, seq_name) - + def sql_create_index(self, table, column, unique=False): idx = self._index_name(table, column, unique) if unique: @@ -217,10 +217,10 @@ INSERT INTO %s VALUES (0);''' % (seq_name, seq_name) return 'ALTER TABLE %s DROP CONSTRAINT %s' % (table, idx) else: return 'DROP INDEX %s' % idx - + def sql_drop_sequence(self, seq_name): return 'DROP TABLE %s;' % seq_name - + def sqls_increment_sequence(self, seq_name): return ('UPDATE %s SET last=last+1;' % seq_name, 'SELECT last FROM %s;' % seq_name) @@ -228,13 +228,13 @@ INSERT INTO %s VALUES (0);''' % (seq_name, seq_name) def sql_temporary_table(self, table_name, table_schema, drop_on_commit=True): return "CREATE TEMPORARY TABLE %s (%s);" % (table_name, table_schema) - + def boolean_value(self, value): if value: return 'TRUE' else: return 'FALSE' - + def increment_sequence(self, cursor, seq_name): for sql in self.sqls_increment_sequence(seq_name): cursor.execute(sql) @@ -254,15 +254,15 @@ INSERT INTO %s VALUES (0);''' % (seq_name, seq_name) return '%s_%s_key' % (table.lower(), column.lower()) else: return '%s_%s_idx' % (table.lower(), column.lower()) - + def create_index(self, cursor, table, column, unique=False): if not self.index_exists(cursor, table, column, unique): cursor.execute(self.sql_create_index(table, column, unique)) - + def drop_index(self, cursor, table, column, unique=False): if self.index_exists(cursor, table, column, unique): cursor.execute(self.sql_drop_index(table, column, unique)) - + def index_exists(self, cursor, table, column, unique=False): idx = self._index_name(table, column, unique) return idx in self.list_indices(cursor, table) @@ -270,27 +270,27 @@ INSERT INTO %s VALUES (0);''' % (seq_name, seq_name) def user_exists(self, cursor, username): """return True if a user with the given username exists""" return username in self.list_users(cursor) - + def list_users(self, cursor): """return the list of existing database users""" raise NotImplementedError('not supported by this DBMS') - + def create_database(self, cursor, dbname, owner=None, encoding='utf-8'): """create a new database""" raise NotImplementedError('not supported by this DBMS') - + def list_databases(self): """return the list of existing databases""" raise NotImplementedError('not supported by this DBMS') - + def list_tables(self, cursor): """return the list of tables of a database""" raise NotImplementedError('not supported by this DBMS') - + def list_indices(self, cursor, table=None): """return the list of indices of a database, only for the given table if specified""" raise NotImplementedError('not supported by this DBMS') - + def pgdbcmd(cmd, dbhost, dbuser): @@ -308,11 +308,11 @@ class _PGAdvFuncHelper(_GenericAdvFuncHelper): backend_name = 'postgres' # modifiable but should not be shared FUNCTIONS = _GenericAdvFuncHelper.FUNCTIONS.copy() - + def system_database(self): """return the system database for the given driver""" return 'template1' - + def backup_command(self, dbname, dbhost, dbuser, backupfile, keepownership=True): """return a command to backup the given database""" @@ -326,7 +326,7 @@ class _PGAdvFuncHelper(_GenericAdvFuncHelper): cmd.append('--file=%s' % backupfile) cmd.append(dbname) return ' '.join(cmd) - + def restore_commands(self, dbname, dbhost, dbuser, backupfile, encoding='utf-8', keepownership=True, drop=True): """return a list of commands to restore a backup the given database""" @@ -345,24 +345,24 @@ class _PGAdvFuncHelper(_GenericAdvFuncHelper): cmd.append(backupfile) cmds.append(' '.join(cmd)) return cmds - + def sql_create_sequence(self, seq_name): return 'CREATE SEQUENCE %s;' % seq_name - + def sql_drop_sequence(self, seq_name): return 'DROP SEQUENCE %s;' % seq_name - + def sqls_increment_sequence(self, seq_name): return ("SELECT nextval('%s');" % seq_name,) - + def sql_temporary_table(self, table_name, table_schema, drop_on_commit=True): if not drop_on_commit: return "CREATE TEMPORARY TABLE %s (%s);" % (table_name, - table_schema) + table_schema) return "CREATE TEMPORARY TABLE %s (%s) ON COMMIT DROP;" % (table_name, table_schema) - + def create_database(self, cursor, dbname, owner=None, encoding='utf-8'): """create a new database""" sql = "CREATE DATABASE %(dbname)s" @@ -391,7 +391,7 @@ class _PGAdvFuncHelper(_GenericAdvFuncHelper): """return the list of existing databases""" cursor.execute('SELECT datname FROM pg_database') return [r[0] for r in cursor.fetchall()] - + def list_tables(self, cursor): """return the list of tables of a database""" cursor.execute("SELECT tablename FROM pg_tables") @@ -405,7 +405,7 @@ class _PGAdvFuncHelper(_GenericAdvFuncHelper): cursor.execute(sql) return [r[0] for r in cursor.fetchall()] - + class _SqliteAdvFuncHelper(_GenericAdvFuncHelper): """Generic helper, trying to provide generic way to implement specific functionnalities from others DBMS @@ -421,7 +421,7 @@ class _SqliteAdvFuncHelper(_GenericAdvFuncHelper): ilike_support = False union_parentheses_support = False intersect_all_support = False - + def sql_create_index(self, table, column, unique=False): idx = self._index_name(table, column, unique) if unique: @@ -431,7 +431,7 @@ class _SqliteAdvFuncHelper(_GenericAdvFuncHelper): def sql_drop_index(self, table, column, unique=False): return 'DROP INDEX %s' % self._index_name(table, column, unique) - + def list_tables(self, cursor): """return the list of tables of a database""" # filter type='table' else we get indices as well @@ -446,7 +446,7 @@ class _SqliteAdvFuncHelper(_GenericAdvFuncHelper): cursor.execute(sql) return [r[0] for r in cursor.fetchall()] - + class _MyAdvFuncHelper(_GenericAdvFuncHelper): """MySQL helper, taking advantage of postgres SEQUENCE support """ @@ -455,24 +455,24 @@ class _MyAdvFuncHelper(_GenericAdvFuncHelper): ilike_support = False # insensitive search by default # modifiable but should not be shared - FUNCTIONS = _GenericAdvFuncHelper.FUNCTIONS.copy() + FUNCTIONS = _GenericAdvFuncHelper.FUNCTIONS.copy() TYPE_MAPPING = _GenericAdvFuncHelper.TYPE_MAPPING.copy() TYPE_MAPPING['Password'] = 'tinyblob' TYPE_MAPPING['String'] = 'mediumtext' TYPE_MAPPING['Bytes'] = 'longblob' # don't use timestamp which is automatically updated on row update TYPE_MAPPING['Datetime'] = 'datetime' - + def system_database(self): """return the system database for the given driver""" return '' - + def backup_command(self, dbname, dbhost, dbuser, backupfile, keepownership=True): """return a command to backup the given database""" # XXX compress return 'mysqldump -h %s -u %s -p -r %s %s' % (dbhost, dbuser, backupfile, dbname) - + def restore_commands(self, dbname, dbhost, dbuser, backupfile, encoding='utf-8', keepownership=True, drop=True): """return a list of commands to restore a backup the given database""" @@ -487,21 +487,21 @@ class _MyAdvFuncHelper(_GenericAdvFuncHelper): cmd = 'mysql -h %s -u %s -p %s < %s' % (dbhost, dbuser, dbname, backupfile) cmds.append(cmd) return cmds - + def sql_temporary_table(self, table_name, table_schema, drop_on_commit=True): if not drop_on_commit: return "CREATE TEMPORARY TABLE %s (%s);" % (table_name, - table_schema) + table_schema) return "CREATE TEMPORARY TABLE %s (%s) ON COMMIT DROP;" % (table_name, table_schema) - + def sql_create_database(self, dbname, encoding='utf-8'): sql = "CREATE DATABASE %(dbname)s" if encoding: sql += " CHARACTER SET %(encoding)s" return sql % locals() - + def create_database(self, cursor, dbname, owner=None, encoding='utf-8'): """create a new database""" cursor.execute(self.sql_create_database(dbname, encoding)) @@ -510,10 +510,10 @@ class _MyAdvFuncHelper(_GenericAdvFuncHelper): def boolean_value(self, value): if value: - return True + return 1 else: - return False - + return 0 + def list_users(self, cursor): """return the list of existing database users""" # Host, Password @@ -524,7 +524,7 @@ class _MyAdvFuncHelper(_GenericAdvFuncHelper): """return the list of existing databases""" cursor.execute('SHOW DATABASES') return [r[0] for r in cursor.fetchall()] - + def list_tables(self, cursor): """return the list of tables of a database""" cursor.execute("SHOW TABLES") @@ -541,7 +541,7 @@ class _MyAdvFuncHelper(_GenericAdvFuncHelper): return allindices - + ADV_FUNC_HELPER_DIRECTORY = {'postgres': _PGAdvFuncHelper(), 'sqlite': _SqliteAdvFuncHelper(), 'mysql': _MyAdvFuncHelper(), @@ -554,7 +554,7 @@ def get_adv_func_helper(driver): return ADV_FUNC_HELPER_DIRECTORY[driver] def register_function(driver, funcdef): - ADV_FUNC_HELPER_DIRECTORY[driver].register_function(funcdef) + ADV_FUNC_HELPER_DIRECTORY[driver].register_function(funcdef) # this function should be called `register_function` but the other # definition was defined prior to this one |