Back to index

python-biopython  1.60
DBUtils.py
Go to the documentation of this file.
00001 # Copyright 2002 by Andrew Dalke.  All rights reserved.
00002 # Revisions 2007-2010 copyright by Peter Cock.  All rights reserved.
00003 # Revisions 2009 copyright by Brad Chapman.  All rights reserved.
00004 # This code is part of the Biopython distribution and governed by its
00005 # license.  Please see the LICENSE file that should have been included
00006 # as part of this package.
00007 #
00008 # Note that BioSQL (including the database schema and scripts) is
00009 # available and licensed separately.  Please consult www.biosql.org
00010 
00011 _dbutils = {}
00012 
00013 class Generic_dbutils:
00014     """Default database utilities."""
00015     def __init__(self):
00016         pass
00017 
00018     def tname(self, table):
00019         if table != 'biosequence': return table
00020         else: return 'bioentry'
00021 
00022     def last_id(self, cursor, table):
00023         # XXX: Unsafe without transactions isolation
00024         table = self.tname(table)
00025         sql = r"select max(%s_id) from %s" % (table, table)
00026         cursor.execute(sql)
00027         rv = cursor.fetchone()
00028         return rv[0]
00029     
00030     def execute(self, cursor, sql, args=None):
00031         """Just execute an sql command.
00032         """
00033         cursor.execute(sql, args or ())
00034 
00035     def autocommit(self, conn, y = 1):
00036         # Let's hope it was not really needed
00037         pass
00038 
00039 
00040 class Sqlite_dbutils(Generic_dbutils):
00041     """Custom database utilities for SQLite."""
00042     def execute(self, cursor, sql, args=None):
00043         """Execute SQL command, replacing %s with ? for variable substitution in sqlite3.
00044         """
00045         cursor.execute(sql.replace("%s", "?"), args or ())
00046 
00047 _dbutils["sqlite3"] = Sqlite_dbutils
00048 
00049 
00050 class Mysql_dbutils(Generic_dbutils):
00051     """Custom database utilities for MySQL."""
00052     def last_id(self, cursor, table):
00053         try:
00054             #This worked on older versions of MySQL
00055             return cursor.insert_id()
00056         except AttributeError:
00057             #See bug 2390
00058             #Google suggests this is the new way,
00059             #same fix also suggested by Eric Gibert:
00060             return cursor.lastrowid
00061         
00062 _dbutils["MySQLdb"] = Mysql_dbutils
00063 
00064 
00065 class _PostgreSQL_dbutils(Generic_dbutils):
00066     """Base class for any PostgreSQL adaptor."""
00067     def next_id(self, cursor, table):
00068         table = self.tname(table)
00069         sql = r"select nextval('%s_pk_seq')" % table
00070         cursor.execute(sql)
00071         rv = cursor.fetchone()
00072         return rv[0]
00073         
00074     def last_id(self, cursor, table):
00075         table = self.tname(table)
00076         sql = r"select currval('%s_pk_seq')" % table
00077         cursor.execute(sql)
00078         rv = cursor.fetchone()
00079         return rv[0]
00080 
00081 class Psycopg2_dbutils(_PostgreSQL_dbutils):
00082     """Custom database utilities for Psycopg2 (PostgreSQL)."""
00083     def autocommit(self, conn, y = True):
00084         if y:
00085             conn.set_isolation_level(0)
00086         else:
00087             conn.set_isolation_level(1)
00088 
00089 _dbutils["psycopg2"] = Psycopg2_dbutils
00090 
00091 
00092 class Pgdb_dbutils(_PostgreSQL_dbutils):
00093     """Custom database utilities for Pgdb (aka PyGreSQL, for PostgreSQL)."""
00094     def autocommit(self, conn, y = True):
00095         raise NotImplementedError("pgdb does not support this!")
00096 
00097 _dbutils["pgdb"] = Pgdb_dbutils
00098 
00099 
00100 def get_dbutils(module_name):
00101     try:
00102         return _dbutils[module_name]()
00103     except KeyError:
00104         return Generic_dbutils()