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-2009 copyright by Peter Cock.  All rights reserved. 
  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 execute(self, cursor, sql, args=None):
38 """Just execute an sql command. 39 """ 40 cursor.execute(sql, args or ())
41
42 - def autocommit(self, conn, y = 1):
43 # Let's hope it was not really needed 44 pass
45
46 -class Sqlite_dbutils(Generic_dbutils):
47 """Custom database utilities for SQLite. 48 """
49 - def execute(self, cursor, sql, args=None):
50 """Replace %s with ? for variable substitution in sqlite3. 51 """ 52 sql = sql.replace("%s", "?") 53 cursor.execute(sql, args or ())
54 55 _dbutils["sqlite3"] = Sqlite_dbutils 56
57 -class Mysql_dbutils(Generic_dbutils):
58 - def last_id(self, cursor, table):
59 try: 60 #This worked on older versions of MySQL 61 return cursor.insert_id() 62 except AttributeError: 63 #See bug 2390 64 #Google suggests this is the new way, 65 #same fix also suggested by Eric Gibert: 66 return cursor.lastrowid
67 68 _dbutils["MySQLdb"] = Mysql_dbutils 69
70 -class Psycopg_dbutils(Generic_dbutils):
71 - def next_id(self, cursor, table):
72 table = self.tname(table) 73 sql = r"select nextval('%s_pk_seq')" % table 74 cursor.execute(sql) 75 rv = cursor.fetchone() 76 return rv[0]
77
78 - def last_id(self, cursor, table):
79 table = self.tname(table) 80 sql = r"select currval('%s_pk_seq')" % table 81 cursor.execute(sql) 82 rv = cursor.fetchone() 83 return rv[0]
84
85 - def autocommit(self, conn, y = True):
86 conn.autocommit(y)
87 88 _dbutils["psycopg"] = Psycopg_dbutils 89
90 -class Psycopg2_dbutils(Psycopg_dbutils):
91 - def autocommit(self, conn, y = True):
92 if y: 93 conn.set_isolation_level(0) 94 else: 95 conn.set_isolation_level(1)
96 97 _dbutils["psycopg2"] = Psycopg2_dbutils 98
99 -class Pgdb_dbutils(Generic_dbutils):
100 """Add support for pgdb in the PyGreSQL database connectivity package. 101 """
102 - def next_id(self, cursor, table):
103 table = self.tname(table) 104 sql = r"select nextval('%s_pk_seq')" % table 105 cursor.execute(sql) 106 rv = cursor.fetchone() 107 return rv[0]
108
109 - def last_id(self, cursor, table):
110 table = self.tname(table) 111 sql = r"select currval('%s_pk_seq')" % table 112 cursor.execute(sql) 113 rv = cursor.fetchone() 114 return rv[0]
115
116 - def autocommit(self, conn, y = True):
117 raise NotImplementedError("pgdb does not support this!")
118 119 _dbutils["pgdb"] = Pgdb_dbutils 120
121 -def get_dbutils(module_name):
122 try: 123 return _dbutils[module_name]() 124 except: 125 return Generic_dbutils()
126