/* Python Postgres Interface Library
(C) by Hans Matzen, 1997, Frankfurt, Germany
*/
/* this is pgpylib.h */
#include <stdlib.h>
#include "Python.h"
#include "libpq-fe.h"
static PyObject *ErrorObject;
#define Py_Try(BOOLEAN) { if (!(BOOLEAN)) return NULL;}
/* -------------------------------------------------------------- */
/* everything for pgresult, the postgres result handle object */
/* -------------------------------------------------------------- */
/* the definitions for the python object */
typedef struct {
PyObject_HEAD
PGresult *res;
} pgres;
staticforward PyTypeObject pgres_obj;
/* all that help stuff */
static char pgres_obj__doc__[]=
"Postgres Result Handle Object";
static char pgpy_pgres__doc__[]=
"pgres(res)=pgres";
static char pgres_get_tuple_count__doc__[]=
"returns the number of tuples.";
static char pgres_get_field_count__doc__[]=
"returns the number of fields.";
static char pgres_get_fieldname_byindex__doc__[]=
"returns the fieldname.";
static char pgres_get_fieldindex_byname__doc__[]=
"returns the fieldname.";
static char pgres_get_fieldtype__doc__[]=
"returns the fieldtype.";
static char pgres_get_fieldsize__doc__[]=
"returns the fieldsize.";
static char pgres_get_fieldvalue__doc__[]=
"returns the fieldvalue.";
static char pgres_fieldlength__doc__[]=
"returns the fieldlength.";
static char pgres_commandstatus__doc__[]=
"returns the commandstatus.";
static char pgres_objectstatus__doc__[]=
"returns the objectstatus.";
static char pgres_get_resultlist__doc__[]=
"prints the query result.";
/* forward declarations for methods */
staticforward pgres * new_pgres(pgres *init);
staticforward void free_pgres(pgres *self);
staticforward
PyObject* pgpy_pgres(PyObject *self,PyObject *args);
staticforward
PyObject* pgres_getattr(pgres *self,char *name);
staticforward PyObject* pgres_get_tuple_count(pgres *self);
staticforward PyObject* pgres_get_field_count(pgres *self);
staticforward
PyObject* pgres_get_fieldname_byindex(pgres *self,PyObject *args);
staticforward
PyObject* pgres_get_fieldindex_byname(pgres *self, PyObject *args);
staticforward
PyObject* pgres_get_fieldtype(pgres *self, PyObject *args);
staticforward
PyObject* pgres_get_fieldsize(pgres *self,PyObject *args);
staticforward
PyObject* pgres_get_fieldvalue(pgres *self,PyObject *args);
staticforward
PyObject* pgres_get_fieldlength(pgres *self, PyObject * args);
staticforward PyObject* pgres_commandstatus(pgres *self);
staticforward PyObject* pgres_objectstatus(pgres *self);
staticforward
PyObject* pgres_get_resultlist(pgres *self,PyObject *args);
/* method table for class pgres */
static struct PyMethodDef pgres_methods[]={
{"get_tuple_count" ,(PyCFunction)pgres_get_tuple_count },
{"get_field_count" ,(PyCFunction)pgres_get_field_count },
{"get_fieldname_byindex",(PyCFunction)pgres_get_fieldname_byindex },
{"get_fieldindex_byname",(PyCFunction)pgres_get_fieldindex_byname },
{"get_fieldtype" ,(PyCFunction)pgres_get_fieldtype },
{"get_fieldsize" ,(PyCFunction)pgres_get_fieldsize },
{"get_fieldvalue" ,(PyCFunction)pgres_get_fieldvalue },
{"get_fieldlength" ,(PyCFunction)pgres_get_fieldlength },
{"commandstatus" ,(PyCFunction)pgres_commandstatus },
{"objectstatus" ,(PyCFunction)pgres_objectstatus },
{"get_resultlist" ,(PyCFunction)pgres_get_resultlist },
{NULL,NULL}
};
/* pgres object table */
static PyTypeObject pgres_obj= {
PyObject_HEAD_INIT(&PyType_Type) 0,
"pgres",
sizeof(pgres),
0,
(destructor)free_pgres,
(printfunc)0,
(getattrfunc)pgres_getattr,
(setattrfunc)0,
(cmpfunc)0,
(reprfunc)0,
0,0,0,
(hashfunc)0,
(ternaryfunc)0,
(reprfunc)0,
0L,0L,0L,0L,
pgres_obj__doc__
};
/* -------------------------------------------------------------- */
/* everything for pgconn, the postgres connection object */
/* -------------------------------------------------------------- */
typedef struct {
PyObject_HEAD
PGconn *conn;
int trace;
} pgconn;
staticforward PyTypeObject pgconn_obj;
/* all that help stuff */
static char pgpy_module_documentation[]=
"Postgres95 Python Interface";
static char pgconn_obj__doc__[]=
"Postgres95 Connection Object";
static char pgpy_pgconn__doc__[]=
"pgconn(host,port,dbname) = pgconn";
static char pgconn_get_dbname__doc__[]=
"returns the name of the database you are connected to.";
static char pgconn_get_host__doc__[]=
"returns the hostname you are connected to.";
static char pgconn_get_port__doc__[]=
"returns the port number you are connected to.";
static char pgconn_get_options__doc__[]=
"returns the options which were passed to the postmaster.";
static char pgconn_get_tty__doc__[]=
"returns the tty where the postmaster logs to.";
static char pgconn_connstatus__doc__[]=
"returns 0 if the connectio is ok., or 1 otherwise.";
static char pgconn_get_error__doc__[]=
"returns the error that occures last.";
static char pgconn_connreset__doc__[]=
"reset the connection to the postmaster.";
static char pgconn_switch_trace__doc__[]=
"switch trace mode on or off.";
static char pgconn_exec_query__doc__[]=
"executes the query passed as parameter.";
/* forward declarations for methodes */
staticforward
pgconn * new_pgconn(char *host, char *port, char* dbname);
staticforward void free_pgconn(pgconn *self);
staticforward
PyObject* pgpy_pgconn(PyObject *self,PyObject *args);
staticforward PyObject* pgconn_getattr(pgconn *self,char *name);
staticforward PyObject* pgconn_get_dbname(pgconn *self);
staticforward PyObject* pgconn_get_host(pgconn *self);
staticforward PyObject* pgconn_get_port(pgconn *self);
staticforward PyObject* pgconn_get_options(pgconn *self);
staticforward PyObject* pgconn_get_tty(pgconn *self);
staticforward PyObject* pgconn_get_error(pgconn *self);
staticforward PyObject* pgconn_connstatus(pgconn *self);
staticforward
PyObject* pgconn_exec_query(pgconn *self,PyObject *args);
staticforward PyObject* pgconn_switch_trace(pgconn *self);
staticforward PyObject* pgconn_connreset(pgconn *self);
/* method table for class pgconn */
static struct PyMethodDef pgconn_methods[]={
{"get_dbname" ,(PyCFunction)pgconn_get_dbname },
{"get_host" ,(PyCFunction)pgconn_get_host },
{"get_port" ,(PyCFunction)pgconn_get_port },
{"get_options" ,(PyCFunction)pgconn_get_options },
{"get_tty" ,(PyCFunction)pgconn_get_tty },
{"connstatus" ,(PyCFunction)pgconn_connstatus },
{"get_error" ,(PyCFunction)pgconn_get_error },
{"connreset" ,(PyCFunction)pgconn_connreset },
{"switch_trace",(PyCFunction)pgconn_switch_trace},
{"exec_query" ,(PyCFunction)pgconn_exec_query },
{NULL,NULL}
};
/* pgconn object table */
static PyTypeObject pgconn_obj= {
PyObject_HEAD_INIT(&PyType_Type) 0,
"pgconn",
sizeof(pgconn),
0,
(destructor)free_pgconn,
(printfunc)0,
(getattrfunc)pgconn_getattr,
(setattrfunc)0,
(cmpfunc)0,
(reprfunc)0,
0,0,0,
(hashfunc)0,
(ternaryfunc)0,
(reprfunc)0,
0L,0L,0L,0L,
pgconn_obj__doc__
};
/* ------------------------------------------------------------------ */
/* everything for pgblob, the postgres binary large object interface */
/* ------------------------------------------------------------------ */
typedef struct {
PyObject_HEAD
pgconn *conn;
int fd;
Oid blobid;
char *buff;
} pgblob;
staticforward PyTypeObject pgblob_obj;
/* all that help stuff */
static char pgblob_obj__doc__[]=
"Postgres95 Binary Large Object";
static char pgpy_pgblob__doc__[]=
"pgblob(pgconn)=pgblob";
static char pgblob_bopen__doc__[]=
"open BLOB.";
static char pgblob_bclose__doc__[]=
"close BLOB.";
static char pgblob_bread__doc__[]=
"read from BLOB.";
static char pgblob_bwrite__doc__[]=
"write to BLOB.";
static char pgblob_bseek__doc__[]=
"seek position in BLOB.";
static char pgblob_btell__doc__[]=
"tells current position in BLOB.";
static char pgblob_bimport__doc__[]=
"import from file to BLOB.";
static char pgblob_bexport__doc__[]=
"export from BLOB to file.";
static char pgblob_bcreate__doc__[]=
"create new BLOB.";
static char pgblob_bunlink__doc__[]=
"unlink from BLOB Oid.";
/* forward declarations for methodes */
staticforward pgblob * new_pgblob(pgconn *init);
staticforward void free_pgblob(pgblob *self);
staticforward
PyObject* pgpy_pgblob(PyObject *self,PyObject *args);
staticforward PyObject* pgblob_getattr(pgblob *self,char *name);
staticforward
PyObject* pgblob_bopen(pgblob *self,PyObject *args);
staticforward PyObject* pgblob_bclose(pgblob *self);
staticforward
PyObject* pgblob_bread(pgblob *self,PyObject *args);
staticforward
PyObject* pgblob_bwrite(pgblob *self, PyObject *args);
staticforward
PyObject* pgblob_bseek(pgblob *self, PyObject *args);
staticforward
PyObject* pgblob_btell(pgblob *self);
staticforward
PyObject* pgblob_bimport(pgblob *self,PyObject *args);
staticforward
PyObject* pgblob_bexport(pgblob *self, PyObject *args);
staticforward
PyObject* pgblob_bcreate(pgblob *self, PyObject *args);
staticforward PyObject* pgblob_bunlink(pgblob *self);
/* method table for class pgblob */
static struct PyMethodDef pgblob_methods[]={
{"bopen" ,(PyCFunction)pgblob_bopen },
{"bclose" ,(PyCFunction)pgblob_bclose },
{"bread" ,(PyCFunction)pgblob_bread },
{"bwrite" ,(PyCFunction)pgblob_bwrite },
{"bseek" ,(PyCFunction)pgblob_bseek },
{"btell" ,(PyCFunction)pgblob_btell },
{"bimport" ,(PyCFunction)pgblob_bimport },
{"bexport" ,(PyCFunction)pgblob_bexport },
{"bcreate" ,(PyCFunction)pgblob_bcreate },
{"bunlink" ,(PyCFunction)pgblob_bunlink },
{NULL,NULL}
};
/* pgblob object table */
static PyTypeObject pgblob_obj= {
PyObject_HEAD_INIT(&PyType_Type) 0,
"pgblob",
sizeof(pgblob),
0,
(destructor)free_pgblob,
(printfunc)0,
(getattrfunc)pgblob_getattr,
(setattrfunc)0,
(cmpfunc)0,
(reprfunc)0,
0,0,0,
(hashfunc)0,
(ternaryfunc)0,
(reprfunc)0,
0L,0L,0L,0L,
pgblob_obj__doc__
};
/* pgpylib init table, lets stick it together */
static struct PyMethodDef pgpy_methods[]={
{"pgconn",(PyCFunction)pgpy_pgconn,1,pgpy_pgconn__doc__},
{"pgres" ,(PyCFunction)pgpy_pgres,1,pgpy_pgres__doc__},
{"pgblob",(PyCFunction)pgpy_pgblob,1,pgpy_pgblob__doc__},
{NULL,NULL}
};
|