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 -class Generic_dbutils:
14 """Default database utilities."""
15 - def __init__(self):
16 pass
17
18 - def tname(self, table):
19 if table != 'biosequence': return table 20 else: return 'bioentry'
21
22 - def last_id(self, cursor, table):
23 # XXX: Unsafe without transactions isolation 24 table = self.tname(table) 25 sql = r"select max(%s_id) from %s" % (table, table) 26 cursor.execute(sql) 27 rv = cursor.fetchone() 28 return rv[0]
29
30 - def execute(self, cursor, sql, args=None):
31 """Just execute an sql command. 32 """ 33 cursor.execute(sql, args or ())
34
35 - def autocommit(self, conn, y = 1):
36 # Let's hope it was not really needed 37 pass
38 39
40 -class Sqlite_dbutils(Generic_dbutils):
41 """Custom database utilities for SQLite."""
42 - def execute(self, cursor, sql, args=None):
43 """Execute SQL command, replacing %s with ? for variable substitution in sqlite3. 44 """ 45 cursor.execute(sql.replace("%s", "?"), args or ())
46 47 _dbutils["sqlite3"] = Sqlite_dbutils 48 49
50 -class Mysql_dbutils(Generic_dbutils):
51 """Custom database utilities for MySQL."""
52 - def last_id(self, cursor, table):
53 try: 54 #This worked on older versions of MySQL 55 return cursor.insert_id() 56 except AttributeError: 57 #See bug 2390 58 #Google suggests this is the new way, 59 #same fix also suggested by Eric Gibert: 60 return cursor.lastrowid
61 62 _dbutils["MySQLdb"] = Mysql_dbutils 63 64
65 -class _PostgreSQL_dbutils(Generic_dbutils):
66 """Base class for any PostgreSQL adaptor."""
67 - def next_id(self, cursor, table):
68 table = self.tname(table) 69 sql = r"select nextval('%s_pk_seq')" % table 70 cursor.execute(sql) 71 rv = cursor.fetchone() 72 return rv[0]
73
74 - def last_id(self, cursor, table):
75 table = self.tname(table) 76 sql = r"select currval('%s_pk_seq')" % table 77 cursor.execute(sql) 78 rv = cursor.fetchone() 79 return rv[0]
80
81 -class Psycopg_dbutils(_PostgreSQL_dbutils):
82 """Custom database utilities for Psycopg (PostgreSQL)."""
83 - def autocommit(self, conn, y = True):
84 conn.autocommit(y)
85 86 _dbutils["psycopg"] = Psycopg_dbutils 87 88
89 -class Psycopg2_dbutils(_PostgreSQL_dbutils):
90 """Custom database utilities for Psycopg2 (PostgreSQL)."""
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
100 -class Pgdb_dbutils(_PostgreSQL_dbutils):
101 """Custom database utilities for Pgdb (aka PyGreSQL, for PostgreSQL)."""
102 - def autocommit(self, conn, y = True):
103 raise NotImplementedError("pgdb does not support this!")
104 105 _dbutils["pgdb"] = Pgdb_dbutils 106 107
108 -def get_dbutils(module_name):
109 try: 110 return _dbutils[module_name]() 111 except KeyError: 112 return Generic_dbutils()
113