⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pgmodule.c

📁 关系型数据库 Postgresql 6.5.2
💻 C
📖 第 1 页 / 共 4 页
字号:
			char	   *s = PQgetvalue(self->last_result, i, j);			char		cashbuf[64];			switch (typ[j])			{				case 1:					val = PyInt_FromLong(strtol(s, NULL, 10));					break;				case 2:					val = PyFloat_FromDouble(strtod(s, NULL));					break;				case 3: /* get rid of the '$' and commas */					if (*s == '$')		/* there's talk of getting rid of										 * it */						s++;					if ((s[0] == '-' || s[0] == '(') && s[1] == '$')						*(++s) = '-';					for (k = 0; *s; s++)						if (*s != ',')							cashbuf[k++] = *s;					cashbuf[k] = 0;					val = PyFloat_FromDouble(strtod(cashbuf, NULL));					break;				default:					val = PyString_FromString(s);					break;			}			PyDict_SetItemString(dict, PQfname(self->last_result, j), val);			Py_XDECREF(val);		}		PyList_Append(reslist, dict);		Py_XDECREF(dict);	}	free(typ);	/* returns list */	return reslist;}/* gets asynchronous notify */static char pg_getnotify__doc__[] ="getnotify() -- get database notify for this connection.";static PyObject *pg_getnotify(pgobject * self, PyObject * args){	PGnotify   *notify;	PGresult   *result;	PyObject   *notify_result,			   *temp;	if (!self->cnx)	{		PyErr_SetString(PyExc_TypeError, "Connection is not valid");		return NULL;	}	/* checks args */	if (!PyArg_ParseTuple(args, ""))	{		PyErr_SetString(PyExc_SyntaxError,						"method getnotify() takes no parameters.");		return NULL;	}	/* gets notify and builds result */	/*	 * notifies only come back as result of a query, so I send an empty	 * query	 */	result = PQexec(self->cnx, " ");	if ((notify = PQnotifies(self->cnx)) != NULL)	{		notify_result = PyTuple_New(2);		temp = PyString_FromString(notify->relname);		PyTuple_SetItem(notify_result, 0, temp);		temp = PyInt_FromLong(notify->be_pid);		PyTuple_SetItem(notify_result, 1, temp);		free(notify);	}	else	{		Py_INCREF(Py_None);		notify_result = Py_None;	}	PQclear(result);	/* returns result */	return notify_result;}/* database query */static char pg_query__doc__[] ="query() -- creates a new query object for this connection.";static PyObject *pg_query(pgobject * self, PyObject * args){	char	   *query;	PGresult   *result;	pgqueryobject *npgobj;	int			status;	if (!self->cnx)	{		PyErr_SetString(PyExc_TypeError, "Connection is not valid");		return NULL;	}	/* get query args */	if (!PyArg_ParseTuple(args, "s", &query))	{		PyErr_SetString(PyExc_TypeError, "query(sql), with sql (string).");		return NULL;	}	/* gets result */	result = PQexec(self->cnx, query);	/* checks result validity */	if (!result)	{		PyErr_SetString(PyExc_ValueError, PQerrorMessage(self->cnx));		return NULL;	}	/* checks result status */	if ((status = PQresultStatus(result)) != PGRES_TUPLES_OK)	{		const char *str;		PQclear(result);		switch (status)		{			case PGRES_EMPTY_QUERY:				PyErr_SetString(PyExc_ValueError, "empty query.");				break;			case PGRES_BAD_RESPONSE:			case PGRES_FATAL_ERROR:			case PGRES_NONFATAL_ERROR:				PyErr_SetString(PGError, PQerrorMessage(self->cnx));				break;			case PGRES_COMMAND_OK:		/* could be an INSERT */				if (*(str = PQoidStatus(result)) == 0)	/* nope */				{					Py_INCREF(Py_None);					return Py_None;				}				/* otherwise, return the oid */				return PyInt_FromLong(strtol(str, NULL, 10));			case PGRES_COPY_OUT:		/* no data will be received */			case PGRES_COPY_IN:				Py_INCREF(Py_None);				return Py_None;			default:				PyErr_SetString(PGError, "internal error: "								"unknown result status.");				break;		}		return NULL;			/* error detected on query */	}	if ((npgobj = PyObject_NEW(pgqueryobject, &PgQueryType)) == NULL)		return NULL;	/* stores result and returns object */	npgobj->last_result = result;	return (PyObject *) npgobj;}#ifdef DIRECT_ACCESSstatic char pg_putline__doc__[] ="putline() -- sends a line directly to the backend";/* direct acces function : putline */static PyObject *pg_putline(pgobject * self, PyObject * args){	char	   *line;	if (!self->cnx)	{		PyErr_SetString(PyExc_TypeError, "Connection is not valid");		return NULL;	}	/* reads args */	if (!PyArg_ParseTuple(args, "s", &line))	{		PyErr_SetString(PyExc_TypeError, "putline(line), with line (string).");		return NULL;	}	/* sends line to backend */	PQputline(self->cnx, line);	Py_INCREF(Py_None);	return Py_None;}/* direct access function : getline */static char pg_getline__doc__[] ="getline() -- gets a line directly from the backend.";static PyObject *pg_getline(pgobject * self, PyObject * args){	char		line[MAX_BUFFER_SIZE];	PyObject   *str = NULL;		/* GCC */	int			ret;	if (!self->cnx)	{		PyErr_SetString(PyExc_TypeError, "Connection is not valid");		return NULL;	}	/* checks args */	if (!PyArg_ParseTuple(args, ""))	{		PyErr_SetString(PyExc_SyntaxError,						"method getline() takes no parameters.");		return NULL;	}	/* gets line */	switch (PQgetline(self->cnx, line, MAX_BUFFER_SIZE))	{		case 0:			str = PyString_FromString(line);			break;		case 1:			PyErr_SetString(PyExc_MemoryError, "buffer overflow");			str = NULL;			break;		case EOF:			Py_INCREF(Py_None);			str = Py_None;			break;	}	return str;}/* direct access function : end copy */static char pg_endcopy__doc__[] ="endcopy() -- synchronizes client and server";static PyObject *pg_endcopy(pgobject * self, PyObject * args){	if (!self->cnx)	{		PyErr_SetString(PyExc_TypeError, "Connection is not valid");		return NULL;	}	/* checks args */	if (!PyArg_ParseTuple(args, ""))	{		PyErr_SetString(PyExc_SyntaxError,						"method endcopy() takes no parameters.");		return NULL;	}	/* ends direct copy */	PQendcopy(self->cnx);	Py_INCREF(Py_None);	return Py_None;}#endif	 /* DIRECT_ACCESS */static PyObject *pgquery_print(pgqueryobject * self, FILE *fp, int flags){	PQprintOpt	op;	memset(&op, 0, sizeof(op));	op.align = 1;	op.header = 1;	op.fieldSep = "|";	op.pager = 1;	PQprint(fp, self->last_result, &op);	return 0;}/* insert table */static char pg_inserttable__doc__[] ="inserttable(string, list) -- insert list in table. The fields in the list ""must be in the same order as in the table.";static PyObject *pg_inserttable(pgobject * self, PyObject * args){	PGresult   *result;	char	   *table,			   *buffer,			   *temp;	char		temp_buffer[256];	PyObject   *list,			   *sublist,			   *item;	PyObject   *(*getitem) (PyObject *, int);	PyObject   *(*getsubitem) (PyObject *, int);	int			i,				j;	if (!self->cnx)	{		PyErr_SetString(PyExc_TypeError, "Connection is not valid");		return NULL;	}	/* gets arguments */	if (!PyArg_ParseTuple(args, "sO:filter", &table, &list))	{		PyErr_SetString(PyExc_TypeError,					  "tableinsert(table, content), with table (string) "						"and content (list).");		return NULL;	}	/* checks list type */	if (PyTuple_Check(list))		getitem = PyTuple_GetItem;	else if (PyList_Check(list))		getitem = PyList_GetItem;	else	{		PyErr_SetString(PyExc_TypeError,						"second arg must be some kind of array.");		return NULL;	}	/* checks sublists type */	for (i = 0; (sublist = getitem(list, i)) != NULL; i++)	{		if (!PyTuple_Check(sublist) && !PyList_Check(sublist))		{			PyErr_SetString(PyExc_TypeError,						 "second arg must contain some kind of arrays.");			return NULL;		}	}	/* allocate buffer */	if (!(buffer = malloc(MAX_BUFFER_SIZE)))	{		PyErr_SetString(PyExc_MemoryError, "can't allocate insert buffer.");		return NULL;	}	/* starts query */	sprintf(buffer, "copy %s from stdin", table);	if (!(result = PQexec(self->cnx, buffer)))	{		free(buffer);		PyErr_SetString(PyExc_ValueError, PQerrorMessage(self->cnx));		return NULL;	}	PQclear(result);	/* feeds table */	for (i = 0; (sublist = getitem(list, i)) != NULL; i++)	{		if (PyTuple_Check(sublist))			getsubitem = PyTuple_GetItem;		else			getsubitem = PyList_GetItem;		/* builds insert line */		buffer[0] = 0;		for (j = 0; (item = getsubitem(sublist, j)) != NULL; j++)		{			/* converts item to string */			if (PyString_Check(item))				PyArg_ParseTuple(item, "s", &temp);			else if (PyInt_Check(item))			{				int			k;				PyArg_ParseTuple(item, "i", &k);				sprintf(temp_buffer, "%d", k);				temp = temp_buffer;			}			else if (PyLong_Check(item))			{				long		k;				PyArg_ParseTuple(item, "l", &k);				sprintf(temp_buffer, "%ld", k);				temp = temp_buffer;			}			else if (PyFloat_Check(item))			{				double		k;				PyArg_ParseTuple(item, "d", &k);				sprintf(temp_buffer, "%g", k);				temp = temp_buffer;			}			else			{				free(buffer);				PyErr_SetString(PyExc_ValueError,								"items must be strings, integers, "								"longs or double (real).");				return NULL;			}			/* concats buffer */			if (strlen(buffer))				strncat(buffer, "\t", MAX_BUFFER_SIZE - strlen(buffer));			fprintf(stderr, "Buffer: '%s', Temp: '%s'\n", buffer, temp);			strncat(buffer, temp, MAX_BUFFER_SIZE - strlen(buffer));		}		strncat(buffer, "\n", MAX_BUFFER_SIZE - strlen(buffer));		/* sends data */		PQputline(self->cnx, buffer);	}	/* ends query */	PQputline(self->cnx, ".\n");	PQendcopy(self->cnx);	free(buffer);	/* no error : returns nothing */	Py_INCREF(Py_None);	return Py_None;}/* creates large object */static char pg_locreate__doc__[] ="locreate() -- creates a new large object in the database.";static PyObject *pg_locreate(pgobject * self, PyObject * args){	int			mode;	Oid			lo_oid;	/* checks validity */	if (!check_cnx_obj(self))		return NULL;	/* gets arguments */	if (!PyArg_ParseTuple(args, "i", &mode))	{		PyErr_SetString(PyExc_TypeError,						"locreate(mode), with mode (integer).");		return NULL;	}	/* creates large object */	lo_oid = lo_creat(self->cnx, mode);	if (lo_oid == 0)	{		PyErr_SetString(PGError, "can't create large object.");		return NULL;	}	return (PyObject *) pglarge_new(self, lo_oid);}/* init from already known oid */static char pg_getlo__doc__[] ="getlo(long) -- create a large object instance for the specified oid.";static PyObject *pg_getlo(pgobject * self, PyObject * args){	int			lo_oid;	/* checks validity */	if (!check_cnx_obj(self))		return NULL;	/* gets arguments */	if (!PyArg_ParseTuple(args, "i", &lo_oid))	{		PyErr_SetString(PyExc_TypeError, "loopen(oid), with oid (integer).");		return NULL;	}	if (!lo_oid)	{		PyErr_SetString(PyExc_ValueError, "the object oid can't be null.");		return NULL;	}	/* creates object */	return (PyObject *) pglarge_new(self, lo_oid);}/* import unix file */static char pg_loimport__doc__[] ="loimport(string) -- create a new large object from specified file.";static PyObject *pg_loimport(pgobject * self, PyObject * args){	char	   *name;	Oid			lo_oid;	/* checks validity */	if (!check_cnx_obj(self))		return NULL;	/* gets arguments */	if (!PyArg_ParseTuple(args, "s", &name))	{		PyErr_SetString(PyExc_TypeError, "loimport(name), with name (string).");		return NULL;	}	/* imports file and checks result */	lo_oid = lo_import(self->cnx, name);	if (lo_oid == 0)	{		PyErr_SetString(PGError, "can't create large object.");		return NULL;	}	return (PyObject *) pglarge_new(self, lo_oid);}/* connection object methods */static struct PyMethodDef pgobj_methods[] = {	{"query", (PyCFunction) pg_query, 1, pg_query__doc__},	{"reset", (PyCFunction) pg_reset, 1, pg_reset__doc__},	{"close", (PyCFunction) pg_close, 1, pg_close__doc__},	{"fileno", (PyCFunction) pg_fileno, 1, pg_fileno__doc__},	{"getnotify", (PyCFunction) pg_getnotify, 1, pg_getnotify__doc__},	{"inserttable", (PyCFunction) pg_inserttable, 1, pg_inserttable__doc__},#ifdef DIRECT_ACCESS	{"putline", (PyCFunction) pg_putline, 1, pg_putline__doc__},	{"getline", (PyCFunction) pg_getline, 1, pg_getline__doc__},	{"endcopy", (PyCFunction) pg_endcopy, 1, pg_endcopy__doc__},#endif	 /* DIRECT_ACCESS */#ifdef LARGE_OBJECTS	{"locreate", (PyCFunction) pg_locreate, 1, pg_locreate__doc__},	{"getlo", (PyCFunction) pg_getlo, 1, pg_getlo__doc__},	{"loimport", (PyCFunction) pg_loimport, 1, pg_loimport__doc__},#endif	 /* LARGE_OBJECTS */	{NULL, NULL}				/* sentinel */};/* get attribute */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -