Package BioSQL :: Module DBUtils
[hide private]
[frames] | no frames]

Source Code for Module BioSQL.DBUtils

 1  _dbutils = {} 
 2   
3 -class Generic_dbutils:
4 - def __init__(self):
5 pass
6
7 - def tname(self, table):
8 if table != 'biosequence': return table 9 else: return 'bioentry'
10 11 # Disabled: better safe than sorry 12 ## def next_id(self, cursor, table): 13 ## # XXX brain-dead! Hopefully, the database will enforce PK unicity.. 14 ## table = self.tname(table) 15 ## sql = r"select 1+max(%s_id) from %s" % (table, table) 16 ## cursor.execute(sql) 17 ## rv = cursor.fetchone() 18 ## return rv[0] 19
20 - def last_id(self, cursor, table):
21 # XXX: Unsafe without transactions isolation 22 table = self.tname(table) 23 sql = r"select max(%s_id) from %s" % table 24 cursor.execute(sql) 25 rv = cursor.fetchone() 26 return rv[0]
27
28 - def autocommit(self, conn, y = 1):
29 # Let's hope it was not really needed 30 pass
31
32 -class Mysql_dbutils(Generic_dbutils):
33 - def last_id(self, cursor, table):
34 return cursor.insert_id()
35 _dbutils["MySQLdb"] = Mysql_dbutils 36
37 -class Psycopg_dbutils(Generic_dbutils):
38 - def next_id(self, cursor, table):
39 table = self.tname(table) 40 sql = r"select nextval('%s_pk_seq')" % table 41 cursor.execute(sql) 42 rv = cursor.fetchone() 43 return rv[0]
44
45 - def last_id(self, cursor, table):
46 table = self.tname(table) 47 sql = r"select currval('%s_pk_seq')" % table 48 cursor.execute(sql) 49 rv = cursor.fetchone() 50 return rv[0]
51
52 - def autocommit(self, conn, y = True):
53 conn.autocommit(y)
54 _dbutils["psycopg"] = Psycopg_dbutils 55
56 -class Pgdb_dbutils(Generic_dbutils):
57 """Add support for pgdb in the PyGreSQL database connectivity package. 58 """
59 - def next_id(self, cursor, table):
60 table = self.tname(table) 61 sql = r"select nextval('%s_pk_seq')" % table 62 cursor.execute(sql) 63 rv = cursor.fetchone() 64 return rv[0]
65
66 - def last_id(self, cursor, table):
67 table = self.tname(table) 68 sql = r"select currval('%s_pk_seq')" % table 69 cursor.execute(sql) 70 rv = cursor.fetchone() 71 return rv[0]
72
73 - def autocommit(self, conn, y = True):
74 raise NotImplementedError("pgdb does not support this!")
75 76 _dbutils["pgdb"] = Pgdb_dbutils 77
78 -def get_dbutils(module_name):
79 try: 80 return _dbutils[module_name]() 81 except: 82 return Generic_dbutils()
83