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