Home | Trees | Indices | Help |
---|
|
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 ######################################################################## 2628 """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.""" 3393 94 95 ######################################################################## 96 # Functions 97 ######################################################################## 9835 """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 58 59 63 64 68 6971 """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 cursor100 """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
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Fri Dec 23 12:30:46 2011 | http://epydoc.sourceforge.net |