From 3bc2e7a9d39c7f897c98a051d063a45319e149a3 Mon Sep 17 00:00:00 2001 From: adustman Date: Tue, 28 Mar 2000 17:09:29 +0000 Subject: [PATCH] A big (I hope) optimization and hooks for transaction support. --- mysql/MySQLdb.py | 8 ++++---- mysql/_mysqlmodule.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/mysql/MySQLdb.py b/mysql/MySQLdb.py index 8e1bd94..2fa2114 100644 --- a/mysql/MySQLdb.py +++ b/mysql/MySQLdb.py @@ -34,13 +34,13 @@ except ImportError: def Long2Int(l): return str(l)[:-1] # drop the trailing L def None2NULL(d): return "NULL" -def String2literal(s): return "'%s'" % escape_string(str(s)) +String2Literal = string_literal quote_conv = { types.IntType: str, types.LongType: Long2Int, types.FloatType: str, types.NoneType: None2NULL, - types.StringType: String2literal } + types.StringType: String2Literal } type_conv = { FIELD_TYPE.TINY: int, FIELD_TYPE.SHORT: int, @@ -260,7 +260,7 @@ class CursorStoreResultMixIn: self.connection._acquire() try: BaseCursor._do_query(self, q) - self.__rows = self.result and self._fetch_all_rows() or ((),) + self.__rows = self.result and self._fetch_all_rows() or () self.__pos = 0 del self.result finally: @@ -423,7 +423,7 @@ class Connection: """Close the connection. No further activity possible.""" self.db.close() - if hasattr(_mysql, 'transactions'): + if hasattr(_mysql, 'rollback'): def commit(self): """Commit the current transaction.""" return self.db.commit() diff --git a/mysql/_mysqlmodule.c b/mysql/_mysqlmodule.c index 0ae305e..cd3b894 100644 --- a/mysql/_mysqlmodule.c +++ b/mysql/_mysqlmodule.c @@ -550,6 +550,24 @@ _mysql_escape_string(self, args) return (str); } +static PyObject * +_mysql_string_literal(self, args) + PyObject *self; + PyObject *args; +{ + PyObject *str; + char *in, *out; + int len, size; + if (!PyArg_ParseTuple(args, "s#:string_literal", &in, &size)) return NULL; + str = PyString_FromStringAndSize((char *) NULL, size*2+3); + if (!str) return PyErr_NoMemory(); + out = PyString_AS_STRING(str); + len = mysql_escape_string(out+1, in, size); + *out = *(out+len+1) = '\''; + if (_PyString_Resize(&str, len+2) < 0) return NULL; + return (str); +} + static PyObject *_mysql_NULL; static PyObject * @@ -907,6 +925,16 @@ _mysql_get_client_info(self, args) return PyString_FromString(mysql_get_client_info()); } +static PyObject * +_mysql_ConnectionObject_commit(self, args) + _mysql_ConnectionObject *self; + PyObject *args; +{ + if (!PyArg_NoArgs(args)) return NULL; + Py_INCREF(Py_None); + return Py_None; +} + static PyObject * _mysql_ConnectionObject_get_host_info(self, args) _mysql_ConnectionObject *self; @@ -1289,6 +1317,7 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = { {"change_user", (PyCFunction)_mysql_ConnectionObject_change_user, METH_VARARGS | METH_KEYWORDS}, #endif {"close", (PyCFunction)_mysql_ConnectionObject_close, 0}, + {"commit", (PyCFunction)_mysql_ConnectionObject_commit, 0}, {"dump_debug_info", (PyCFunction)_mysql_ConnectionObject_dump_debug_info, 0}, {"error", (PyCFunction)_mysql_ConnectionObject_error, 0}, {"errno", (PyCFunction)_mysql_ConnectionObject_errno, 0}, @@ -1445,6 +1474,7 @@ _mysql_methods[] = { { "debug", _mysql_debug, METH_VARARGS }, { "escape_row", _mysql_escape_row, METH_VARARGS }, { "escape_string", _mysql_escape_string, METH_VARARGS }, + { "string_literal", _mysql_string_literal, METH_VARARGS }, { "get_client_info", _mysql_get_client_info }, {NULL, NULL} /* sentinel */ };