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 _dbutils["psycopg"] = Psycopg_dbutils 72
73 -class Pgdb_dbutils(Generic_dbutils):
74 """Add support for pgdb in the PyGreSQL database connectivity package. 75 """
76 - def next_id(self, cursor, table):
77 table = self.tname(table) 78 sql = r"select nextval('%s_pk_seq')" % table 79 cursor.execute(sql) 80 rv = cursor.fetchone() 81 return rv[0]
82
83 - def last_id(self, cursor, table):
84 table = self.tname(table) 85 sql = r"select currval('%s_pk_seq')" % table 86 cursor.execute(sql) 87 rv = cursor.fetchone() 88 return rv[0]
89
90 - def autocommit(self, conn, y = True):
91 raise NotImplementedError("pgdb does not support this!")
92 93 _dbutils["pgdb"] = Pgdb_dbutils 94
95 -def get_dbutils(module_name):
96 try: 97 return _dbutils[module_name]() 98 except: 99 return Generic_dbutils()
100