From 47e9e8c7ee4b83d70bd0552a44f3659d5658272f Mon Sep 17 00:00:00 2001 From: adustman Date: Sat, 25 Feb 2006 00:56:15 +0000 Subject: [PATCH] Add callproc() support. --- MySQLdb/MySQLdb/cursors.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/MySQLdb/MySQLdb/cursors.py b/MySQLdb/MySQLdb/cursors.py index ed58cea..481f201 100644 --- a/MySQLdb/MySQLdb/cursors.py +++ b/MySQLdb/MySQLdb/cursors.py @@ -188,7 +188,43 @@ class BaseCursor(object): self._executed = query self._warning_check() return r + + def callproc(self, procname, *args): + """Execute stored procedure procname with args + + procname -- string, name of procedure to execute on server + + args -- Sequence of parameters to use with procedure + + Returns the original args. + + Compatibility warning: PEP-249 specifies that any modified + parameters must be returned. This is currently impossible + as they are only available by storing them in a server + variable and then retrieved by a query. Since stored + procedures return zero or more result sets, there is no + reliable way to get at OUT or INOUT parameters via callproc. + The server variables are named @_procname_n, where procname + is the parameter above and n is the position of the parameter + (from zero). Once all result sets generated by the procedure + have been fetched, you can issue a SELECT @_procname_0, ... + query using .execute() to get any OUT or INOUT values. + """ + + db = self._get_db() + for index, arg in enumerate(args): + db.query("SET @_%s_%d=%s" % (procname, index, + db.literal(arg))) + q = "CALL %s(%s)" % (procname, + ','.join(['@_%s_%d' % (procname, i) + for i in range(len(args))])) + if type(q) is UnicodeType: + q = q.encode(db.charset) + db.query(q) + self._do_get_result() + return args + def _do_query(self, q): from types import UnicodeType db = self._get_db()