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-2010 copyright by Peter Cock.  All rights reserved. 
  3  # Revisions 2009 copyright by Brad Chapman.  All rights reserved. 
  4  # This code is part of the Biopython distribution and governed by its 
  5  # license.  Please see the LICENSE file that should have been included 
  6  # as part of this package. 
  7  # 
  8  # Note that BioSQL (including the database schema and scripts) is 
  9  # available and licensed separately.  Please consult www.biosql.org 
 10   
 11  _dbutils = {} 
 12   
 13   
14 -class Generic_dbutils:
15 """Default database utilities."""
16 - def __init__(self):
17 pass
18
19 - def tname(self, table):
20 if table != 'biosequence': 21 return table 22 else: 23 return 'bioentry'
24
25 - def last_id(self, cursor, table):
26 # XXX: Unsafe without transactions isolation 27 table = self.tname(table) 28 sql = r"select max(%s_id) from %s" % (table, table) 29 cursor.execute(sql) 30 rv = cursor.fetchone() 31 return rv[0]
32
33 - def execute(self, cursor, sql, args=None):
34 """Just execute an sql command. 35 """ 36 cursor.execute(sql, args or ())
37
38 - def autocommit(self, conn, y=1):
39 # Let's hope it was not really needed 40 pass
41 42
43 -class Sqlite_dbutils(Generic_dbutils):
44 """Custom database utilities for SQLite."""
45 - def execute(self, cursor, sql, args=None):
46 """Execute SQL command, replacing %s with ? for variable substitution in sqlite3. 47 """ 48 cursor.execute(sql.replace("%s", "?"), args or ())
49 50 _dbutils["sqlite3"] = Sqlite_dbutils 51 52
53 -class Mysql_dbutils(Generic_dbutils):
54 """Custom database utilities for MySQL."""
55 - def last_id(self, cursor, table):
56 try: 57 #This worked on older versions of MySQL 58 return cursor.insert_id() 59 except AttributeError: 60 #See bug 2390 61 #Google suggests this is the new way, 62 #same fix also suggested by Eric Gibert: 63 return cursor.lastrowid
64 65 _dbutils["MySQLdb"] = Mysql_dbutils 66 67
68 -class _PostgreSQL_dbutils(Generic_dbutils):
69 """Base class for any PostgreSQL adaptor."""
70 - def next_id(self, cursor, table):
71 table = self.tname(table) 72 sql = r"select nextval('%s_pk_seq')" % table 73 cursor.execute(sql) 74 rv = cursor.fetchone() 75 return rv[0]
76
77 - def last_id(self, cursor, table):
78 table = self.tname(table) 79 sql = r"select currval('%s_pk_seq')" % table 80 cursor.execute(sql) 81 rv = cursor.fetchone() 82 return rv[0]
83 84
85 -class Psycopg2_dbutils(_PostgreSQL_dbutils):
86 """Custom database utilities for Psycopg2 (PostgreSQL)."""
87 - def autocommit(self, conn, y=True):
88 if y: 89 conn.set_isolation_level(0) 90 else: 91 conn.set_isolation_level(1)
92 93 _dbutils["psycopg2"] = Psycopg2_dbutils 94 95
96 -class Pgdb_dbutils(_PostgreSQL_dbutils):
97 """Custom database utilities for Pgdb (aka PyGreSQL, for PostgreSQL)."""
98 - def autocommit(self, conn, y=True):
99 raise NotImplementedError("pgdb does not support this!")
100 101 _dbutils["pgdb"] = Pgdb_dbutils 102 103
104 -def get_dbutils(module_name):
105 try: 106 return _dbutils[module_name]() 107 except KeyError: 108 return Generic_dbutils()
109