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