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   
 14  _dbutils = {} 
 15   
 16   
17 -class Generic_dbutils(object):
18 """Default database utilities.""" 19
20 - def __init__(self):
21 pass
22
23 - def tname(self, table):
24 if table != 'biosequence': 25 return table 26 else: 27 return 'bioentry'
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 executemany(self, cursor, sql, seq):
43 """Execute many sql commands 44 """ 45 cursor.executemany(sql, seq)
46
47 - def autocommit(self, conn, y=1):
48 # Let's hope it was not really needed 49 pass
50 51
52 -class Sqlite_dbutils(Generic_dbutils):
53 """Custom database utilities for SQLite.""" 54
55 - def _sub_placeholder(self, sql):
56 """Format the argument placeholders for sqlite 57 """ 58 return sql.replace("%s", "?")
59
60 - def execute(self, cursor, sql, args=None):
61 """Execute SQL command, replacing %s with ? for variable substitution in sqlite3. 62 """ 63 sql = self._sub_placeholder(sql) 64 cursor.execute(sql, args or ())
65
66 - def executemany(self, cursor, sql, seq):
67 sql = self._sub_placeholder(sql) 68 cursor.executemany(sql, seq)
69 70 71 _dbutils["sqlite3"] = Sqlite_dbutils 72 73
74 -class Mysql_dbutils(Generic_dbutils):
75 """Custom database utilities for MySQL.""" 76
77 - def last_id(self, cursor, table):
78 if os.name == "java": 79 return Generic_dbutils.last_id(self, cursor, table) 80 try: 81 # This worked on older versions of MySQL 82 return cursor.insert_id() 83 except AttributeError: 84 # See bug 2390 85 # Google suggests this is the new way, 86 # same fix also suggested by Eric Gibert: 87 return cursor.lastrowid
88 89 _dbutils["MySQLdb"] = Mysql_dbutils 90 91
92 -class _PostgreSQL_dbutils(Generic_dbutils):
93 """Base class for any PostgreSQL adaptor.""" 94
95 - def next_id(self, cursor, table):
96 table = self.tname(table) 97 sql = r"select nextval('%s_pk_seq')" % table 98 cursor.execute(sql) 99 rv = cursor.fetchone() 100 return rv[0]
101
102 - def last_id(self, cursor, table):
103 table = self.tname(table) 104 sql = r"select currval('%s_pk_seq')" % table 105 cursor.execute(sql) 106 rv = cursor.fetchone() 107 return rv[0]
108 109
110 -class Psycopg2_dbutils(_PostgreSQL_dbutils):
111 """Custom database utilities for Psycopg2 (PostgreSQL).""" 112
113 - def autocommit(self, conn, y=True):
114 if y: 115 if os.name == "java": 116 conn.autocommit = 1 117 else: 118 conn.set_isolation_level(0) 119 else: 120 if os.name == "java": 121 conn.autocommit = 0 122 else: 123 conn.set_isolation_level(1)
124 125 _dbutils["psycopg2"] = Psycopg2_dbutils 126 127
128 -class Pgdb_dbutils(_PostgreSQL_dbutils):
129 """Custom database utilities for Pgdb (aka PyGreSQL, for PostgreSQL).""" 130
131 - def autocommit(self, conn, y=True):
132 raise NotImplementedError("pgdb does not support this!")
133 134 _dbutils["pgdb"] = Pgdb_dbutils 135 136
137 -def get_dbutils(module_name):
138 try: 139 return _dbutils[module_name]() 140 except KeyError: 141 return Generic_dbutils()
142