The idea here (I believe) is that someone is creating a placeholder object that keeps reference to a cursor c so that it may be used to execute queries later. Proposed usage is probably something like this:
c = DBConnectionObject.cursor()
myExecutor = get_sql_object(c)
# some amount of code later
rows = myExecutor(Queries.GetAllUsersByFName, ['Larry', 'Bob'])
-- To address some of the comments --
I find that attempting to keep cursors around can cause problems in volatile environments where it's not always guaranteed that the database connection remains connected. I opt for this approach instead:
class DBConnection(object):
def __init__(self, dbpath):
self.dbPath = dbpath
# Any connection object here, really.
self._conn = kinterbasdb.connect()
def cursor(self, query, params = None):
try:
cursor = self._conn.cursor()
if params:
cursor.execute(query, params)
else:
cursor.execute(query)
return cursor
except (kdb.ProgrammingError, AttributeError), e:
print e
def qry(self, query, params = None):
cursor = self.cursor(query, params)
return [[x[0].title() for x in cursor.description]] + [r for r in cursor.fetchall()]
Then, you'd create a global database connection like so:
dbcon = DBConnection('/path/to/mydb.fdb')
and run queries with:
rows = dbcon.qry(Queries.GetSomething)
or:
filtered = dbcon.qry(Queries.FilteredQuery, ['my', 'parameters'])
I've omitted some of the code that does error handling / reconnects to the database if the connection has dropped, but the general idea is there. This way, cursors are created when they're needed, used, and allowed to go out of scope, letting me centralize error handling.
run_sql_command.c = c. Perhaps the author of the code didn't understand that c would still be accessible by the inner function.