| |
- xformSQL(query, params, fmt)
- This function helps to transform an SQL query from a standard format to
the one expected by various python database modules. The database module
being used must be compliant to the Python Database API Version 2
(DB SIG 2). The values supplied in the query must be of the form %(name)s
i.e. it must use pyformat formatting. So a select query supplied to this
function would look similar to the one given below:
"SELECT name, address FROM clients WHERE id = %(client_id)s AND entry_date
= %(entry_date)s".
The "params" in this case will contain values for "client_id" and
"entry_date" i.e. {'client_id':31, 'entry_date':None, ... }. The parameter
"fmt" indicates the "paramstyle" of the underlying database module.
Parameters:
query: A string containing SQL query.
params: A dictionary containing a set of keyword-value pairs to be used
in the query.
fmt: A numeric constant specifying any of the following:
1 => DB_FMT_QMARK
2 => DB_FMT_NUMERIC
3 => DB_FMT_NAMED
4 => DB_FMT_FORMAT
5 => DB_FMT_PYFORMAT
It represents the paramstyle attribute of the underlying database
module.
Returns: A tuple containing two elements (queryStr, paramObj); first one
being a transformed query string and second one a list or dictionary
containing the set of parameters required to execute that query. These
returned values can be used to query the database (with execute method
of respective cursor) as shown below:
apply(cursorObj.execute, (queryStr, paramObj))
Raises:
ParamException: The parameters query, params or fmt is not of correct
type or they do not contain valid values.
NotExistsException: An entity whose value is being used in the query
is not present in the params.
|