Package qm :: Module db
[hide private]
[frames] | no frames]

Source Code for Module qm.db

  1  ######################################################################## 
  2  # 
  3  # File:   db.py 
  4  # Author: Nathaniel Smith <njs@codesourcery.com 
  5  # Date:   2003-06-13 
  6  # 
  7  # Contents: 
  8  #   A few simple functions to help with connecting to SQL databases 
  9  #   and using the DB 2.0 API. 
 10  # 
 11  # Copyright (c) 2003 by CodeSourcery, LLC.  All rights reserved.  
 12  # 
 13  # For license terms see the file COPYING. 
 14  # 
 15  ######################################################################## 
 16   
 17  ######################################################################## 
 18  # Imports 
 19  ######################################################################## 
 20   
 21  import os 
 22   
 23  ######################################################################## 
 24  # Classes 
 25  ######################################################################## 
 26   
27 -class Connection:
28 """A wrapper around a DB 2.0 connection. 29 30 Provides a minimal but consistent interface to an underlying 31 database connection. In particular, a 'Connection' quotes SQL 32 queries as necessary for the underlying DB 2.0 connection.""" 33
34 - def __init__(self, module_name, *args, **more_args):
35 """Uses the given DB 2.0 module to connect to a database. 36 37 'module_name' -- The DB 2.0-compliant module to use to connect, 38 for example "pgdb". 39 40 'args' -- Positional arguments to pass to the module's 'connect' 41 method. 42 43 'more_args' -- Keyword arguments to pass to the module's 44 'connect' method.""" 45 46 # Last argument must be a non-empty list or __import__ will 47 # return the wrong module. 48 self._module = __import__(module_name, 49 globals(), 50 locals(), 51 [""]) 52 self._connection = self._module.connect(*args, **more_args)
53 54
55 - def close(self):
56 57 self._connection.close()
58 59
60 - def commit(self):
61 62 self._connection.commit()
63 64
65 - def rollback(self):
66 67 self._connection.rollback()
68 69
70 - def execute(self, sql):
71 """Execute a SQL statement in this database. 72 73 If this database requires any overall quoting of the given SQL 74 (for instance, doubling of %'s), it will be performed by this 75 method. 76 77 returns -- A database cursor. 78 79 """ 80 81 # According to the DB 2.0 spec, the following lines should be 82 # necessary. But in fact it seems that the database modules we 83 # use do not strip doubled %'s inside SQL string constants, and 84 # do not try to expand parameters inside SQL string constants. 85 # Since string constants are the main place where %'s may occur, 86 # we do not quote %'s at all. 87 #if self._module.paramstyle in ["format", "pyformat"]: 88 # sql = sql.replace("%", "%%") 89 cursor = self._connection.cursor() 90 91 cursor.execute(sql) 92 return cursor
93 94 95 ######################################################################## 96 # Functions 97 ######################################################################## 98
99 -def quote_string(string):
100 """Quotes a string for SQL. 101 102 'string' -- A string whose contents are to be used in an SQL literal 103 string. 104 105 returns -- A SQL literal string whose contents match that of 106 'string'.""" 107 108 # Replace each ' with '', then surround with more 's. Also double 109 # backslashes. It'd be nice to handle things like quoting non-ASCII 110 # characters (by replacing them with octal escapes), but we don't. 111 return "'" + string.replace("'", "''").replace("\\", "\\\\") + "'"
112 113 ######################################################################## 114 # Local Variables: 115 # mode: python 116 # indent-tabs-mode: nil 117 # fill-column: 72 118 # End: 119