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

Source Code for Module BioSQL.DBUtils

  1  # Copyright 2002 by Andrew Dalke.  All rights reserved. 
  2  # Revisions 2007-2008 by Peter Cock. 
  3  # This code is part of the Biopython distribution and governed by its 
  4  # license.  Please see the LICENSE file that should have been included 
  5  # as part of this package. 
  6  # 
  7  # Note that BioSQL (including the database schema and scripts) is 
  8  # available and licensed separately.  Please consult www.biosql.org 
  9   
 10  _dbutils = {} 
 11   
12 -class Generic_dbutils:
13 - def __init__(self):
14 pass
15
16 - def tname(self, table):
17 if table != 'biosequence': return table 18 else: return 'bioentry'
19 20 # Disabled: better safe than sorry 21 ## def next_id(self, cursor, table): 22 ## # XXX brain-dead! Hopefully, the database will enforce PK unicity.. 23 ## table = self.tname(table) 24 ## sql = r"select 1+max(%s_id) from %s" % (table, table) 25 ## cursor.execute(sql) 26 ## rv = cursor.fetchone() 27 ## return rv[0] 28
29 - def last_id(self, cursor, table):
30 # XXX: Unsafe without transactions isolation 31 table = self.tname(table) 32 sql = r"select max(%s_id) from %s" % (table, table) 33 cursor.execute(sql) 34 rv = cursor.fetchone() 35 return rv[0]
36
37 - def autocommit(self, conn, y = 1):
38 # Let's hope it was not really needed 39 pass
40
41 -class Mysql_dbutils(Generic_dbutils):
42 - def last_id(self, cursor, table):
43 try : 44 #This worked on older versions of MySQL 45 return cursor.insert_id() 46 except AttributeError: 47 #See bug 2390 48 #Google suggests this is the new way, 49 #same fix also suggested by Eric Gibert: 50 return cursor.lastrowid
51 52 _dbutils["MySQLdb"] = Mysql_dbutils 53
54 -class Psycopg_dbutils(Generic_dbutils):
55 - def next_id(self, cursor, table):
56 table = self.tname(table) 57 sql = r"select nextval('%s_pk_seq')" % table 58 cursor.execute(sql) 59 rv = cursor.fetchone() 60 return rv[0]
61
62 - def last_id(self, cursor, table):
63 table = self.tname(table) 64 sql = r"select currval('%s_pk_seq')" % table 65 cursor.execute(sql) 66 rv = cursor.fetchone() 67 return rv[0]
68
69 - def autocommit(self, conn, y = True):
70 conn.autocommit(y)
71 72 _dbutils["psycopg"] = Psycopg_dbutils 73
74 -class Psycopg2_dbutils(Psycopg_dbutils):
75 - def autocommit(self, conn, y = True):
76 if y: 77 conn.set_isolation_level(0) 78 else: 79 conn.set_isolation_level(1)
80 81 _dbutils["psycopg2"] = Psycopg2_dbutils 82
83 -class Pgdb_dbutils(Generic_dbutils):
84 """Add support for pgdb in the PyGreSQL database connectivity package. 85 """
86 - def next_id(self, cursor, table):
87 table = self.tname(table) 88 sql = r"select nextval('%s_pk_seq')" % table 89 cursor.execute(sql) 90 rv = cursor.fetchone() 91 return rv[0]
92
93 - def last_id(self, cursor, table):
94 table = self.tname(table) 95 sql = r"select currval('%s_pk_seq')" % table 96 cursor.execute(sql) 97 rv = cursor.fetchone() 98 return rv[0]
99
100 - def autocommit(self, conn, y = True):
101 raise NotImplementedError("pgdb does not support this!")
102 103 _dbutils["pgdb"] = Pgdb_dbutils 104
105 -def get_dbutils(module_name):
106 try: 107 return _dbutils[module_name]() 108 except: 109 return Generic_dbutils()
110