From: Billy G. A. <bal...@us...> - 2004-05-10 03:34:13
|
Update of /cvsroot/pypgsql/pypgsql In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8739 Modified Files: pgconnection.c pgconnection.h setup.py Log Message: 09MAY2003 bga Added a debug feature to the libpq.PgConnection object and to the PgSQL.Connection object to allow the query to be executed to be printed to STDOUT in either text or HTML format. Index: pgconnection.c =================================================================== RCS file: /cvsroot/pypgsql/pypgsql/pgconnection.c,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** pgconnection.c 27 Jun 2003 03:01:20 -0000 1.19 --- pgconnection.c 10 May 2004 03:34:03 -0000 1.20 *************** *** 29,32 **** --- 29,33 ---- | Date Ini Description | | --------- --- ------------------------------------------------------- | + | 09MAY2004 bga Added a 'debug' feature to the PgConnection object. | | 26JUN2003 bga Fixed a bug I introduced into lo_import. | | 15JUN2003 bga Applied patch by Laurent Pinchart to correct a problem | *************** *** 148,152 **** self->bePID = Py_BuildValue("i", PQbackendPID(conn)); self->socket = Py_BuildValue("i", PQsocket(conn)); ! self->showQuery = 0; if (PyErr_Occurred()) --- 149,154 ---- self->bePID = Py_BuildValue("i", PQbackendPID(conn)); self->socket = Py_BuildValue("i", PQsocket(conn)); ! self->debug = Py_None; ! Py_INCREF(Py_None); if (PyErr_Occurred()) *************** *** 200,203 **** --- 202,206 ---- Py_XDECREF(self->version); Py_XDECREF(self->notices); + Py_XDECREF(self->debug); PyObject_Del(self); *************** *** 273,276 **** --- 276,339 ---- /*--------------------------------------------------------------------------*/ + static char *debugQuery(char *debug, char *query) + { + if ((strcasecmp(debug, "div") == 0) || + (strcasecmp(debug, "pre") == 0) || + (strcasecmp(debug, "html") == 0)) + { + char *_tag = NULL; + PyObject *_query = NULL; + PyObject *_fmt = NULL; + PyObject *_tmp = NULL; + PyObject *_res = NULL; + + if (strcasecmp(debug, "div") == 0) + _tag = "div"; + else + _tag = "pre"; + + _fmt = PyString_FromString("<%s style='background: #aaaaaa; " + "border: thin dashed #333333'>" + "%s</%s>"); + if (_fmt == (PyObject *)NULL) goto ErrorExit10; + + _query = PyString_FromString(query); + if (_query == (PyObject *)NULL) goto ErrorExit10; + + /** + * The 'replace' method call will either return a copy of the original + * string (with the reference count incremented) if no replacements + * were made, or a copy of the string with a replacements made. + */ + _tmp = PyObject_CallMethod(_query, "replace", "ss", "&", "&"); + if (_tmp == (PyObject *)NULL) goto ErrorExit10; + Py_XDECREF(_query); + _query = _tmp; + _tmp = PyObject_CallMethod(_query, "replace", "ss", "<", "<"); + if (_tmp == (PyObject *)NULL) goto ErrorExit10; + Py_XDECREF(_query); + _query = _tmp; + _tmp = PyObject_CallMethod(_query, "replace", "ss", ">", ">"); + if (_tmp == (PyObject *)NULL) goto ErrorExit10; + Py_XDECREF(_query); + _query = _tmp; + _tmp = Py_BuildValue("(sOs)", _tag, _query, _tag); + _res = PyString_Format(_fmt, _tmp); + puts(PyString_AsString(_res)); + ErrorExit10: + Py_XDECREF(_fmt); + Py_XDECREF(_query); + Py_XDECREF(_tmp); + Py_XDECREF(_res); + if (PyErr_Occurred() != NULL) + return NULL; + } else { + printf("QUERY: %s\n", query); + } + return ""; + } + + /*--------------------------------------------------------------------------*/ + static char libPQexec_Doc[] = "query() -- Submit a query and wait for the result(s)."; *************** *** 288,293 **** return NULL; ! if (self->showQuery) ! fprintf(stderr, "QUERY: %s\n", query); Py_BEGIN_ALLOW_THREADS --- 351,358 ---- return NULL; ! if (self->debug != Py_None) { ! if (debugQuery(PyString_AsString(self->debug), query) == NULL) ! return NULL; ! } Py_BEGIN_ALLOW_THREADS *************** *** 371,376 **** return NULL; ! if (self->showQuery) ! fprintf(stderr, "QUERY: %s\n", query); if (!PQsendQuery(PgConnection_Get(self), query)) --- 436,443 ---- return NULL; ! if (self->debug != Py_None) { ! if (debugQuery(PyString_AsString(self->debug), query) == NULL) ! return NULL; ! } if (!PQsendQuery(PgConnection_Get(self), query)) *************** *** 581,584 **** --- 648,652 ---- Py_XDECREF(self->version); self->version = Py_None; Py_XINCREF(Py_None); Py_XDECREF(self->notices); self->notices = Py_None; Py_XINCREF(Py_None); + Py_XDECREF(self->debug); self->debug = Py_None; Py_XINCREF(Py_None); Py_INCREF(Py_None); *************** *** 1072,1075 **** --- 1140,1144 ---- { "isBusy", T_INT, 0, RO }, { "isnonblocking", T_INT, 0, RO }, + { "debug", T_OBJECT, CoOFF(debug), 0 }, { NULL } }; *************** *** 1165,1170 **** if (!strcmp(attr, "toggleShowQuery")) { ! self->showQuery = (self->showQuery ? 0 : 1); ! return Py_BuildValue("s", (self->showQuery ? "On" : "Off")); } --- 1234,1247 ---- if (!strcmp(attr, "toggleShowQuery")) { ! if (self->debug != Py_None) ! { ! self->debug = Py_None; ! Py_INCREF(Py_None); ! } ! else ! { ! self->debug = PyString_FromString("text"); ! } ! return self->debug; } *************** *** 1172,1175 **** --- 1249,1258 ---- } + static int PgConnection_setattr(PgConnection *self, char* attr, + PyObject *val) + { + return PyMember_Set((char *)self, PgConnection_members, attr, val); + } + /*--------------------------------------------------------------------------*/ *************** *** 1185,1189 **** (printfunc)NULL, /* tp_print */ (getattrfunc)PgConnection_getattr, /* tp_getattr */ ! (setattrfunc)NULL, /* tp_setattr */ NULL, /* tp_compare */ (reprfunc)PgConnection_repr, /* tp_repr */ --- 1268,1272 ---- (printfunc)NULL, /* tp_print */ (getattrfunc)PgConnection_getattr, /* tp_getattr */ ! (setattrfunc)PgConnection_setattr, /* tp_setattr */ NULL, /* tp_compare */ (reprfunc)PgConnection_repr, /* tp_repr */ Index: pgconnection.h =================================================================== RCS file: /cvsroot/pypgsql/pypgsql/pgconnection.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** pgconnection.h 13 Oct 2001 20:40:35 -0000 1.2 --- pgconnection.h 10 May 2004 03:34:03 -0000 1.3 *************** *** 52,56 **** PyObject *notices; PyObject *cinfo; ! int showQuery; } PgConnection; --- 52,57 ---- PyObject *notices; PyObject *cinfo; ! PyObject *debug; ! } PgConnection; Index: setup.py =================================================================== RCS file: /cvsroot/pypgsql/pypgsql/setup.py,v retrieving revision 1.22 retrieving revision 1.23 diff -C2 -d -r1.22 -r1.23 *** setup.py 14 Jul 2003 21:02:02 -0000 1.22 --- setup.py 10 May 2004 03:34:03 -0000 1.23 *************** *** 102,107 **** library_dirs = YOUR_LIST_HERE elif sys.platform == "linux2": ! include_dirs = ["/usr/include", "/usr/include/postgresql", ! "/usr/include/pgsql"] library_dirs = ["/usr/lib"] --- 102,106 ---- library_dirs = YOUR_LIST_HERE elif sys.platform == "linux2": ! include_dirs = ["/usr/include", "/usr/local/pgsql/include"] library_dirs = ["/usr/lib"] *************** *** 155,160 **** else: # Assume a Unixish system ! include_dirs = ["/usr/local/include"] ! library_dirs = ["/usr/local/lib"] # patch distutils if it can't cope with the "classifiers" keyword --- 154,159 ---- else: # Assume a Unixish system ! include_dirs = ["/usr/local/include", "/usr/local/pgsql/include"] ! library_dirs = ["/usr/local/lib", "/usr/local/pgsql/lib"] # patch distutils if it can't cope with the "classifiers" keyword *************** *** 184,188 **** maintainer_email = "pyp...@li...", url = "http://pypgsql.sourceforge.net/", ! licence = "Python", packages = ["pyPgSQL", "pyPgSQL.libpq"], ext_modules = [Extension( --- 183,187 ---- maintainer_email = "pyp...@li...", url = "http://pypgsql.sourceforge.net/", ! license = "Python", packages = ["pyPgSQL", "pyPgSQL.libpq"], ext_modules = [Extension( |