From abfd61640518678687d7c6f1b1a8c243ccd0b2f7 Mon Sep 17 00:00:00 2001 From: adustman Date: Tue, 14 Mar 2000 04:47:52 +0000 Subject: [PATCH] Add some support for MySQL-3.23 features. Fixed a few miscellaneous things. --- mysql/MySQL_doc.py | 1 + mysql/MySQLdb.py | 38 ++++++++++++++++-- mysql/_mysqlmodule.c | 94 ++++++++++++++++++++++++++++++++++---------- 3 files changed, 108 insertions(+), 25 deletions(-) diff --git a/mysql/MySQL_doc.py b/mysql/MySQL_doc.py index 318023d..e553ca0 100644 --- a/mysql/MySQL_doc.py +++ b/mysql/MySQL_doc.py @@ -53,6 +53,7 @@ def main(): href="license.py"), " with it's lack of warranty\n" \ "statement."), P(A("Download", href="."), " the friggin' thing."), + P("Read the ", A("FAQ",href="MySQLdb-FAQ.html")), H2("Overview"), P("MySQLdb is an interface to the popular ", A("MySQL", href="http://www.mysql.net/mirrors.html"), diff --git a/mysql/MySQLdb.py b/mysql/MySQLdb.py index 89072ad..7b36e19 100644 --- a/mysql/MySQLdb.py +++ b/mysql/MySQLdb.py @@ -16,6 +16,7 @@ This module uses the mxDateTime package for handling date/time types. __version__ = """$Revision$"""[11:-2] +import _mysql from _mysql import * from time import localtime import re, types @@ -170,7 +171,9 @@ class _Cursor: else: try: self._query(query % escape_row(args, qc)) - except TypeError: + except TypeError, m: + if m.args[0] == "not enough arguments for format string": raise + if m.args[0] == "not all arguments converted": raise self._query(query % escape_dict(args, qc)) def executemany(self, query, args): @@ -191,7 +194,9 @@ class _Cursor: qc = self.connection.quote_conv try: q = [query % escape(args[0], qc)] - except TypeError: + except TypeError, m: + if m.args[0] == "not enough arguments for format string": raise + if m.args[0] == "not all arguments converted": raise escape = escape_dict q = [query % escape(args[0], qc)] qv = query[p:] @@ -249,7 +254,15 @@ class _Cursor: return self.result.fetch_all_rows_as_dict() def nextset(self): return None + + def seek(self, row, whence=0): + if self.use: raise NotSupportedError, "use must be 0 to use seek" + if whence: raise NotSupportedError, "can't do relative seek" + return self.result.data_seek(row) + ## return whence and self.result.row_seek(row) or self.result.data_seek(row) + ## def tell(self): return self.result.row_tell() + class Connection: @@ -292,8 +305,18 @@ class Connection: """Close the connection. No further activity possible.""" self.db.close() - def commit(self): """Does nothing as there are no transactions.""" + if hasattr(_mysql, 'transactions'): + def commit(self): + """Commit the current transaction.""" + return self.db.commit() + else: + def commit(self): """Does nothing as there are no transactions.""" + if hasattr(_mysql, 'transactions'): + def rollback(self): + """Rollback the current transaction.""" + self.db.rollback() + def cursor(self, *args, **kwargs): """Create a cursor on which queries may be performed.""" return apply(self.CursorClass, (self,)+args, kwargs) @@ -314,7 +337,8 @@ class Connection: def list_fields(self, table): return self.db.list_fields(table).fetch_all_rows() def list_processes(self): return self.db.list_processes().fetch_all_rows() def list_tables(self, db): return self.db.list_tables(db).fetch_all_rows() - def num_fields(self): return self.db.num_fields() + def field_count(self): return self.db.field_count() + num_fields = field_count # used prior to MySQL-3.22.24 def ping(self): return self.db.ping() def row_tell(self): return self.db.row_tell() def select_db(self, db): return self.db.select_db(db) @@ -322,5 +346,11 @@ class Connection: def stat(self): return self.db.stat() def thread_id(self): return self.db.thread_id() + def change_user(self, *args, **kwargs): + try: + return apply(self.db.change_user, args, kwargs) + except AttributeError: + raise NotSupportedError, "not supported by MySQL library" + Connect = connect = Connection diff --git a/mysql/_mysqlmodule.c b/mysql/_mysqlmodule.c index 94d85c1..ff6a184 100644 --- a/mysql/_mysqlmodule.c +++ b/mysql/_mysqlmodule.c @@ -194,6 +194,16 @@ static _mysql_Constant _mysql_Constant_cr[] = { { NULL } /* sentinel */ } ; +static _mysql_Constant _mysql_Constant_option[] = { + { "OPT_CONNECT_TIMEOUT", MYSQL_OPT_CONNECT_TIMEOUT }, + { "OPT_COMPRESS", MYSQL_OPT_COMPRESS }, + { "OPT_NAMED_PIPE", MYSQL_OPT_NAMED_PIPE }, + { "INIT_COMMAND", MYSQL_INIT_COMMAND }, + { "READE_DEFAULT_FILE", MYSQL_READ_DEFAULT_FILE }, + { "READ_DEFAULT_GROUP", MYSQL_READ_DEFAULT_GROUP }, + { NULL } /* sentinel */ +} ; + static _mysql_Constant _mysql_Constant_er[] = { { "HASHCHK", ER_HASHCHK }, { "NISAMCHK", ER_NISAMCHK }, @@ -855,6 +865,28 @@ _mysql_ResultObject_fetch_rows_as_dict(self, args) return NULL; } +#if MYSQL_VERSION_ID >= 32303 +static PyObject * +_mysql_ConnectionObject_change_user(self, args, kwargs) + _mysql_ConnectionObject *self; + PyObject *args, *kwargs; +{ + char *user, *pwd=NULL, *db=NULL; + int r; + static char *kwlist[] = { "user", "passwd", "db", NULL } ; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ss:change_user", + kwlist, &user, &pwd, &db)) + return NULL; + Py_BEGIN_ALLOW_THREADS + r = mysql_change_user(&(self->connection), user, pwd, db); + Py_END_ALLOW_THREADS + if (r) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; +} +#endif + static PyObject * _mysql_get_client_info(self, args) PyObject *self; @@ -1001,12 +1033,16 @@ _mysql_ConnectionObject_list_tables(self, args) } static PyObject * -_mysql_ConnectionObject_num_fields(self, args) +_mysql_ConnectionObject_field_count(self, args) _mysql_ConnectionObject *self; PyObject *args; { if (!PyArg_NoArgs(args)) return NULL; +#if MYSQL_VERSION_ID < 32224 return PyInt_FromLong((long)mysql_num_fields(&(self->connection))); +#else + return PyInt_FromLong((long)mysql_field_count(&(self->connection))); +#endif } static PyObject * @@ -1058,19 +1094,6 @@ _mysql_ConnectionObject_query(self, args) return Py_None; } -static PyObject * -_mysql_ConnectionObject_row_tell(self, args) - _mysql_ConnectionObject *self; - PyObject *args; -{ - unsigned int r; - if (!PyArg_NoArgs(args)) return NULL; - Py_BEGIN_ALLOW_THREADS - r = mysql_insert_id(&(self->connection)); - Py_END_ALLOW_THREADS - return PyInt_FromLong((long)r); -} - static PyObject * _mysql_ConnectionObject_select_db(self, args) _mysql_ConnectionObject *self; @@ -1197,15 +1220,38 @@ _mysql_ResultObject_data_seek(self, args) _mysql_ResultObject *self; PyObject *args; { - unsigned int offset; - if (!PyArg_ParseTuple(args, "i:data_seek", &offset)) return NULL; - Py_BEGIN_ALLOW_THREADS - mysql_data_seek(self->result, offset); - Py_END_ALLOW_THREADS + unsigned int row; + if (!PyArg_ParseTuple(args, "i:data_seek", &row)) return NULL; + mysql_data_seek(self->result, row); Py_INCREF(Py_None); return Py_None; } +static PyObject * +_mysql_ResultObject_row_seek(self, args) + _mysql_ResultObject *self; + PyObject *args; +{ + int offset; + MYSQL_ROW_OFFSET r; + if (!PyArg_ParseTuple(args, "i:row_seek", &offset)) return NULL; + r = mysql_row_tell(self->result); + mysql_row_seek(self->result, r+offset); + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * +_mysql_ResultObject_row_tell(self, args) + _mysql_ResultObject *self; + PyObject *args; +{ + MYSQL_ROW_OFFSET r; + if (!PyArg_NoArgs(args)) return NULL; + r = mysql_row_tell(self->result); + return PyInt_FromLong(r-self->result->data->data); +} + static void _mysql_ResultObject_dealloc(self) _mysql_ResultObject *self; @@ -1228,10 +1274,14 @@ _mysql_ResultObject_repr(self) static PyMethodDef _mysql_ConnectionObject_methods[] = { {"affected_rows", (PyCFunction)_mysql_ConnectionObject_affected_rows, 0}, +#if MYSQL_VERSION_ID >= 32303 + {"change_user", (PyCFunction)_mysql_ConnectionObject_change_user, METH_VARARGS | METH_KEYWORDS}, +#endif {"close", (PyCFunction)_mysql_ConnectionObject_close, 0}, {"dump_debug_info", (PyCFunction)_mysql_ConnectionObject_dump_debug_info, 0}, {"error", (PyCFunction)_mysql_ConnectionObject_error, 0}, {"errno", (PyCFunction)_mysql_ConnectionObject_errno, 0}, + {"field_count", (PyCFunction)_mysql_ConnectionObject_field_count, 0}, {"get_host_info", (PyCFunction)_mysql_ConnectionObject_get_host_info, 0}, {"get_proto_info", (PyCFunction)_mysql_ConnectionObject_get_proto_info, 0}, {"get_server_info", (PyCFunction)_mysql_ConnectionObject_get_server_info, 0}, @@ -1242,10 +1292,8 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = { {"list_fields", (PyCFunction)_mysql_ConnectionObject_list_fields, 1}, {"list_processes", (PyCFunction)_mysql_ConnectionObject_list_processes, 0}, {"list_tables", (PyCFunction)_mysql_ConnectionObject_list_tables, 1}, - {"num_fields", (PyCFunction)_mysql_ConnectionObject_num_fields, 0}, {"ping", (PyCFunction)_mysql_ConnectionObject_ping, 0}, {"query", (PyCFunction)_mysql_ConnectionObject_query, 1}, - {"row_tell", (PyCFunction)_mysql_ConnectionObject_row_tell, 0}, {"select_db", (PyCFunction)_mysql_ConnectionObject_select_db, 1}, {"shutdown", (PyCFunction)_mysql_ConnectionObject_shutdown, 0}, {"stat", (PyCFunction)_mysql_ConnectionObject_stat, 0}, @@ -1264,6 +1312,8 @@ static struct memberlist _mysql_ConnectionObject_memberlist[] = { static PyMethodDef _mysql_ResultObject_methods[] = { {"data_seek", (PyCFunction)_mysql_ResultObject_data_seek, 1}, + {"row_seek", (PyCFunction)_mysql_ResultObject_row_seek, 1}, + {"row_tell", (PyCFunction)_mysql_ResultObject_row_tell, 0}, {"describe", (PyCFunction)_mysql_ResultObject_describe, 0}, {"fetch_row", (PyCFunction)_mysql_ResultObject_fetch_row, 0}, {"fetch_row_as_dict", (PyCFunction)_mysql_ResultObject_fetch_row_as_dict, 0}, @@ -1524,6 +1574,8 @@ init_mysql() goto error; if (_mysql_Constant_class(dict, "ER", _mysql_Constant_er)) goto error; + if (_mysql_Constant_class(dict, "option", _mysql_Constant_option)) + goto error; if (!(_mysql_NULL = PyString_FromString("NULL"))) goto error; if (PyDict_SetItemString(dict, "NULL", _mysql_NULL)) goto error;