pgmodule.c 71.1 KB
Newer Older
Bruce Momjian's avatar
Bruce Momjian committed
1 2 3 4 5
/*
 * PyGres, version 2.2 A Python interface for PostgreSQL database. Written by
 * D'Arcy J.M. Cain, (darcy@druid.net).  Based heavily on code written by
 * Pascal Andre, andre@chimay.via.ecp.fr. Copyright (c) 1995, Pascal Andre
 * (andre@via.ecp.fr).
Bruce Momjian's avatar
Bruce Momjian committed
6
 *
Bruce Momjian's avatar
Bruce Momjian committed
7 8 9 10 11
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without a written
 * agreement is hereby granted, provided that the above copyright notice and
 * this paragraph and the following two paragraphs appear in all copies or in
 * any new file that contains a substantial portion of this file.
Bruce Momjian's avatar
Bruce Momjian committed
12
 *
Bruce Momjian's avatar
Bruce Momjian committed
13 14 15 16
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
 * AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Bruce Momjian's avatar
Bruce Momjian committed
17
 *
Bruce Momjian's avatar
Bruce Momjian committed
18 19 20 21 22
 * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE
 * AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 * ENHANCEMENTS, OR MODIFICATIONS.
Bruce Momjian's avatar
Bruce Momjian committed
23
 *
24 25
 * Further modifications copyright 1997, 1998, 1999 by D'Arcy J.M. Cain
 * (darcy@druid.net) subject to the same terms and conditions as above.
Bruce Momjian's avatar
Bruce Momjian committed
26
 *
Bruce Momjian's avatar
Bruce Momjian committed
27 28
 */

29
#include <Python.h>
30 31 32 33
#include "postgres.h"
#include "libpq-fe.h"
#include "libpq/libpq-fs.h"
#include "catalog/pg_type.h"
Bruce Momjian's avatar
Bruce Momjian committed
34 35 36 37
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

38
static PyObject *PGError;
39
static const char *PyPgVersion = "3.3";
Bruce Momjian's avatar
Bruce Momjian committed
40 41 42 43

/* taken from fileobject.c */
#define BUF(v) PyString_AS_STRING((PyStringObject *)(v))

44
/* default values */
45 46
#define MODULE_NAME			"pgsql"
#define ARRAYSIZE			1
47 48 49 50 51 52 53

/* flags for object validity checks */
#define CHECK_OPEN			1
#define CHECK_CLOSE			2
#define CHECK_CNX			4
#define CHECK_RESULT		8
#define CHECK_DQL			16
Bruce Momjian's avatar
Bruce Momjian committed
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68
/* query result types */
#define RESULT_EMPTY		1
#define RESULT_DML			2
#define RESULT_DDL			3
#define RESULT_DQL			4

/* flags for move methods */
#define QUERY_MOVEFIRST		1
#define QUERY_MOVELAST		2
#define QUERY_MOVENEXT		3
#define QUERY_MOVEPREV		4

/* moves names for errors */
const char *__movename[5] =
69
{"", "movefirst", "movelast", "movenext", "moveprev"};
70

71
#define MAX_BUFFER_SIZE 8192	/* maximum transaction size */
Bruce Momjian's avatar
Bruce Momjian committed
72 73

#ifndef NO_DIRECT
74
#define DIRECT_ACCESS	1		/* enables direct access functions */
75
#endif	 /* NO_DIRECT */
Bruce Momjian's avatar
Bruce Momjian committed
76 77

#ifndef NO_LARGE
78
#define LARGE_OBJECTS	1		/* enables large objects support */
79
#endif	 /* NO_LARGE */
Bruce Momjian's avatar
Bruce Momjian committed
80 81

#ifndef NO_DEF_VAR
82
#define DEFAULT_VARS	1		/* enables default variables use */
83
#endif	 /* NO_DEF_VAR */
Bruce Momjian's avatar
Bruce Momjian committed
84

D'Arcy J.M. Cain's avatar
D'Arcy J.M. Cain committed
85 86 87 88
#ifdef MS_WIN32
#define NO_SNPRINTF 1
#endif

Bruce Momjian's avatar
Bruce Momjian committed
89 90 91 92 93 94
/* --------------------------------------------------------------------- */

/* MODULE GLOBAL VARIABLES */

#ifdef DEFAULT_VARS

95 96 97 98 99 100 101
static PyObject *pg_default_host;		/* default database host */
static PyObject *pg_default_base;		/* default database name */
static PyObject *pg_default_opt;/* default connection options */
static PyObject *pg_default_tty;/* default debug tty */
static PyObject *pg_default_port;		/* default connection port */
static PyObject *pg_default_user;		/* default username */
static PyObject *pg_default_passwd;		/* default password */
Bruce Momjian's avatar
Bruce Momjian committed
102

103
#endif	 /* DEFAULT_VARS */
Bruce Momjian's avatar
Bruce Momjian committed
104

105 106 107
DL_EXPORT(void) init_pg(void);
int *get_type_array(PGresult *result, int nfields);

Bruce Momjian's avatar
Bruce Momjian committed
108 109 110 111 112 113 114 115
/* --------------------------------------------------------------------- */
/* OBJECTS DECLARATION */

/* pg connection object */

typedef struct
{
	PyObject_HEAD
Bruce Momjian's avatar
Bruce Momjian committed
116
	int			valid;			/* validity flag */
117 118 119
	PGconn	   *cnx;			/* PostGres connection handle */
	PGresult   *last_result;	/* last result content */
}			pgobject;
Bruce Momjian's avatar
Bruce Momjian committed
120 121 122 123 124

staticforward PyTypeObject PgType;

#define is_pgobject(v) ((v)->ob_type == &PgType)

125 126 127
static PyObject *
pgobject_New(void)
{
128
	pgobject   *pgobj;
129 130 131 132 133 134 135

	if ((pgobj = PyObject_NEW(pgobject, &PgType)) == NULL)
		return NULL;

	pgobj->valid = 1;
	pgobj->last_result = NULL;
	pgobj->cnx = NULL;
136
	return (PyObject *) pgobj;
137 138
}

Bruce Momjian's avatar
Bruce Momjian committed
139 140 141 142 143
/* pg query object */

typedef struct
{
	PyObject_HEAD
144
	PGresult   *last_result;	/* last result content */
145 146 147
	int			result_type;	/* type of previous result */
	long		current_pos;	/* current position in last result */
	long		num_rows;		/* number of (affected) rows */
148
}			pgqueryobject;
Bruce Momjian's avatar
Bruce Momjian committed
149 150 151 152 153

staticforward PyTypeObject PgQueryType;

#define is_pgqueryobject(v) ((v)->ob_type == &PgQueryType)

154 155 156 157 158
/* pg source object */

typedef struct
{
	PyObject_HEAD
159 160 161 162 163 164 165 166 167
	int			valid;			/* validity flag */
	pgobject   *pgcnx;			/* parent connection object */
	PGresult   *last_result;	/* last result content */
	int			result_type;	/* result type (DDL/DML/DQL) */
	long		arraysize;		/* array size for fetch method */
	int			current_row;	/* current selected row */
	int			max_row;		/* number of rows in the result */
	int			num_fields;		/* number of fields in each row */
}			pgsourceobject;
168 169 170 171 172 173

staticforward PyTypeObject PgSourceType;

#define is_pgsourceobject(v) ((v)->ob_type == &PgSourceType)


Bruce Momjian's avatar
Bruce Momjian committed
174 175 176 177 178 179
#ifdef LARGE_OBJECTS
/* pg large object */

typedef struct
{
	PyObject_HEAD
180 181 182 183
	pgobject * pgcnx;			/* parent connection object */
	Oid			lo_oid;			/* large object oid */
	int			lo_fd;			/* large object fd */
}			pglargeobject;
Bruce Momjian's avatar
Bruce Momjian committed
184 185 186 187

staticforward PyTypeObject PglargeType;

#define is_pglargeobject(v) ((v)->ob_type == &PglargeType)
188
#endif	 /* LARGE_OBJECTS */
Bruce Momjian's avatar
Bruce Momjian committed
189 190 191 192

/* --------------------------------------------------------------------- */
/* INTERNAL FUNCTIONS */

193 194 195 196
/* checks connection validity */
static int
check_cnx_obj(pgobject * self)
{
197 198
	if (!self->valid)
	{
199 200 201 202 203 204
		PyErr_SetString(PGError, "connection has been closed");
		return 0;
	}
	return 1;
}

Bruce Momjian's avatar
Bruce Momjian committed
205
#ifdef LARGE_OBJECTS
206
/* checks large object validity */
Bruce Momjian's avatar
Bruce Momjian committed
207
static int
208
check_lo_obj(pglargeobject * self, int level)
Bruce Momjian's avatar
Bruce Momjian committed
209
{
210 211 212
	if (!check_cnx_obj(self->pgcnx))
		return 0;

Bruce Momjian's avatar
Bruce Momjian committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	if (!self->lo_oid)
	{
		PyErr_SetString(PGError, "object is not valid (null oid).");
		return 0;
	}

	if (level & CHECK_OPEN)
	{
		if (self->lo_fd < 0)
		{
			PyErr_SetString(PyExc_IOError, "object is not opened.");
			return 0;
		}
	}

	if (level & CHECK_CLOSE)
	{
		if (self->lo_fd >= 0)
		{
			PyErr_SetString(PyExc_IOError, "object is already opened.");
			return 0;
		}
	}

	return 1;
}

240
#endif	 /* LARGE_OBJECTS */
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

/* checks source object validity */
static int
check_source_obj(pgsourceobject * self, int level)
{
	if (!self->valid)
	{
		PyErr_SetString(PGError, "object has been closed");
		return 0;
	}

	if ((level & CHECK_RESULT) && self->last_result == NULL)
	{
		PyErr_SetString(PGError, "no result.");
		return 0;
	}

	if ((level & CHECK_DQL) && self->result_type != RESULT_DQL)
	{
		PyErr_SetString(PGError, "last query did not return tuples.");
		return 0;
	}

	if ((level & CHECK_CNX) && !check_cnx_obj(self->pgcnx))
		return 0;

	return 1;
}

/* shared functions for converting PG types to Python types */
int *
get_type_array(PGresult *result, int nfields)
{
274 275
	int		   *typ;
	int			j;
276 277 278 279 280 281 282 283 284 285 286 287 288

	if ((typ = malloc(sizeof(int) * nfields)) == NULL)
	{
		PyErr_SetString(PyExc_SyntaxError, "memory error in getresult().");
		return NULL;
	}

	for (j = 0; j < nfields; j++)
	{
		switch (PQftype(result, j))
		{
			case INT2OID:
			case INT4OID:
289
			case INT8OID:
290 291 292 293 294 295
			case OIDOID:
				typ[j] = 1;
				break;

			case FLOAT4OID:
			case FLOAT8OID:
296
			case NUMERICOID:
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
				typ[j] = 2;
				break;

			case CASHOID:
				typ[j] = 3;
				break;

			default:
				typ[j] = 4;
				break;
		}
	}

	return typ;
}


/* prototypes for constructors */
315
static pgsourceobject *pgsource_new(pgobject * pgcnx);
316 317 318 319 320 321 322 323

/* --------------------------------------------------------------------- */
/* PG SOURCE OBJECT IMPLEMENTATION */

/* constructor (internal use only) */
static pgsourceobject *
pgsource_new(pgobject * pgcnx)
{
324
	pgsourceobject *npgobj;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

	/* allocates new query object */
	if ((npgobj = PyObject_NEW(pgsourceobject, &PgSourceType)) == NULL)
		return NULL;

	/* initializes internal parameters */
	Py_XINCREF(pgcnx);
	npgobj->pgcnx = pgcnx;
	npgobj->last_result = NULL;
	npgobj->valid = 1;
	npgobj->arraysize = ARRAYSIZE;

	return npgobj;
}

/* destructor */
static void
pgsource_dealloc(pgsourceobject * self)
{
	if (self->last_result)
		PQclear(self->last_result);

	Py_XDECREF(self->pgcnx);
	PyMem_DEL(self);
}

/* closes object */
static char pgsource_close__doc__[] =
"close() -- close query object without deleting it. "
"All instances of the query object can no longer be used after this call.";

static PyObject *
pgsource_close(pgsourceobject * self, PyObject * args)
{
	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_TypeError, "method close() takes no parameter.");
		return NULL;
	}

	/* frees result if necessary and invalidates object */
	if (self->last_result)
	{
		PQclear(self->last_result);
		self->result_type = RESULT_EMPTY;
		self->last_result = NULL;
	}

	self->valid = 0;

	/* return None */
	Py_INCREF(Py_None);
	return Py_None;
}

/* database query */
static char pgsource_execute__doc__[] =
"execute(sql) -- execute a SQL statement (string).\n "
"On success, this call returns the number of affected rows, "
"or None for DQL (SELECT, ...) statements.\n"
"The fetch (fetch(), fetchone() and fetchall()) methods can be used "
"to get result rows.";

static PyObject *
pgsource_execute(pgsourceobject * self, PyObject * args)
{
392 393 394
	char	   *query;
	const char *temp;
	long		num_rows;
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

	/* checks validity */
	if (!check_source_obj(self, CHECK_CNX))
		return NULL;

	/* get query args */
	if (!PyArg_ParseTuple(args, "s", &query))
	{
		PyErr_SetString(PyExc_TypeError, "execute(sql), with sql (string).");
		return NULL;
	}

	/* frees previous result */
	if (self->last_result)
	{
		PQclear(self->last_result);
		self->last_result = NULL;
	}
	self->max_row = 0;
	self->current_row = 0;
	self->num_fields = 0;

	/* gets result */
	Py_BEGIN_ALLOW_THREADS
419
		self->last_result = PQexec(self->pgcnx->cnx, query);
420 421 422
	Py_END_ALLOW_THREADS

	/* checks result validity */
423
		if (!self->last_result)
424 425 426 427 428 429 430 431 432
	{
		PyErr_SetString(PyExc_ValueError, PQerrorMessage(self->pgcnx->cnx));
		return NULL;
	}

	/* checks result status */
	switch (PQresultStatus(self->last_result))
	{

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
			/* query succeeded */
		case PGRES_TUPLES_OK:	/* DQL: returns None (DB-SIG compliant) */
			self->result_type = RESULT_DQL;
			self->max_row = PQntuples(self->last_result);
			self->num_fields = PQnfields(self->last_result);
			Py_INCREF(Py_None);
			return Py_None;
		case PGRES_COMMAND_OK:	/* other requests */
		case PGRES_COPY_OUT:
		case PGRES_COPY_IN:
			self->result_type = RESULT_DDL;
			temp = PQcmdTuples(self->last_result);
			num_rows = 1;
			if (temp[0] != 0)
			{
				self->result_type = RESULT_DML;
				num_rows = atol(temp);
			}
			return PyInt_FromLong(num_rows);

			/* query failed */
		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->pgcnx->cnx));
			break;
		default:
			PyErr_SetString(PGError, "internal error: "
							"unknown result status.");
			break;
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
	}

	/* frees result and returns error */
	PQclear(self->last_result);
	self->last_result = NULL;
	self->result_type = RESULT_EMPTY;
	return NULL;
}

/* gets oid status for last query (valid for INSERTs, 0 for other) */
static char pgsource_oidstatus__doc__[] =
"oidstatus() -- return oid of last inserted row (if available).";

static PyObject *
pgsource_oidstatus(pgsourceobject * self, PyObject * args)
{
	long		oid;

	/* checks validity */
	if (!check_source_obj(self, CHECK_RESULT))
		return NULL;

	/* checks args */
	if ((args != NULL) && (!PyArg_ParseTuple(args, "")))
	{
		PyErr_SetString(PyExc_SyntaxError,
492
						"method oidstatus() takes no parameters.");
493 494 495 496
		return NULL;
	}

	/* retrieves oid status */
497 498 499
	if ((oid = PQoidValue(self->last_result)) == InvalidOid)
		oid = 0;

500 501 502 503 504 505 506 507 508 509 510 511
	return PyInt_FromLong(oid);
}

/* fetches rows from last result */
static char pgsource_fetch__doc__[] =
"fetch(num) -- return the next num rows from the last result in a list. "
"If num parameter is omitted arraysize attribute value is used. "
"If size equals -1, all rows are fetched.";

static PyObject *
pgsource_fetch(pgsourceobject * self, PyObject * args)
{
512 513 514 515 516 517
	PyObject   *rowtuple,
			   *reslist,
			   *str;
	int			i,
				j;
	long		size;
518 519 520 521 522 523 524 525 526 527

	/* checks validity */
	if (!check_source_obj(self, CHECK_RESULT | CHECK_DQL))
		return NULL;

	/* checks args */
	size = self->arraysize;
	if (!PyArg_ParseTuple(args, "|l", &size))
	{
		PyErr_SetString(PyExc_SyntaxError,
528
						"fetch(num), with num (integer, optional).");
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
		return NULL;
	}

	/* seeks last line */
	/* limit size to be within the amount of data we actually have */
	if (size == -1 || (self->max_row - self->current_row) < size)
		size = self->max_row - self->current_row;

	/* allocate list for result */
	if ((reslist = PyList_New(0)) == NULL)
		return NULL;

	/* builds result */
	for (i = 0; i < size; ++i)
	{
		if ((rowtuple = PyTuple_New(self->num_fields)) == NULL)
		{
			Py_DECREF(reslist);
			return NULL;
		}

		for (j = 0; j < self->num_fields; j++)
		{
552
			if (PQgetisnull(self->last_result, self->current_row, j))
553 554 555 556 557
			{
				Py_INCREF(Py_None);
				str = Py_None;
			}
			else
558
				str = PyString_FromString(PQgetvalue(self->last_result, self->current_row, j));
559 560 561 562 563

			PyTuple_SET_ITEM(rowtuple, j, str);
		}

		PyList_Append(reslist, rowtuple);
564
		Py_DECREF(rowtuple);
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
		self->current_row++;
	}

	return reslist;
}

/* changes current row (internal wrapper for all "move" methods) */
static PyObject *
pgsource_move(pgsourceobject * self, PyObject * args, int move)
{
	/* checks validity */
	if (!check_source_obj(self, CHECK_RESULT | CHECK_DQL))
		return NULL;

	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
582
		char		errbuf[256];
583 584 585 586 587

#ifdef	NO_SNPRINTF
		sprintf(errbuf, "method %s() takes no parameter.", __movename[move]);
#else
		snprintf(errbuf, sizeof(errbuf),
588
				 "method %s() takes no parameter.", __movename[move]);
589 590 591 592 593 594 595
#endif

		PyErr_SetString(PyExc_SyntaxError, errbuf);
		return NULL;
	}

	/* changes the current row */
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	switch (move)
	{
		case QUERY_MOVEFIRST:
			self->current_row = 0;
			break;
		case QUERY_MOVELAST:
			self->current_row = self->max_row - 1;
			break;
		case QUERY_MOVENEXT:
			if (self->current_row != self->max_row)
				self->current_row++;
			break;
		case QUERY_MOVEPREV:
			if (self->current_row > 0)
				self->current_row--;
			break;
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
	}

	Py_INCREF(Py_None);
	return Py_None;
}

/* move to first result row */
static char pgsource_movefirst__doc__[] =
"movefirst() -- move to first result row.";

static PyObject *
pgsource_movefirst(pgsourceobject * self, PyObject * args)
{
	return pgsource_move(self, args, QUERY_MOVEFIRST);
}

/* move to last result row */
static char pgsource_movelast__doc__[] =
"movelast() -- move to last valid result row.";

static PyObject *
pgsource_movelast(pgsourceobject * self, PyObject * args)
{
	return pgsource_move(self, args, QUERY_MOVELAST);
}

/* move to next result row */
static char pgsource_movenext__doc__[] =
"movenext() -- move to next result row.";

static PyObject *
pgsource_movenext(pgsourceobject * self, PyObject * args)
{
	return pgsource_move(self, args, QUERY_MOVENEXT);
}

/* move to previous result row */
static char pgsource_moveprev__doc__[] =
"moveprev() -- move to previous result row.";

static PyObject *
pgsource_moveprev(pgsourceobject * self, PyObject * args)
{
	return pgsource_move(self, args, QUERY_MOVEPREV);
}

/* finds field number from string/integer (internal use only) */
static int
660
pgsource_fieldindex(pgsourceobject * self, PyObject * param, const char *usage)
661
{
662
	int			num;
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679

	/* checks validity */
	if (!check_source_obj(self, CHECK_RESULT | CHECK_DQL))
		return -1;

	/* gets field number */
	if (PyString_Check(param))
		num = PQfnumber(self->last_result, PyString_AsString(param));
	else if (PyInt_Check(param))
		num = PyInt_AsLong(param);
	else
	{
		PyErr_SetString(PyExc_TypeError, usage);
		return -1;
	}

	/* checks field validity */
680
	if (num < 0 || num >= self->num_fields)
681 682 683 684 685 686 687 688 689 690 691 692
	{
		PyErr_SetString(PyExc_ValueError, "Unknown field.");
		return -1;
	}

	return num;
}

/* builds field information from position (internal use only) */
static PyObject *
pgsource_buildinfo(pgsourceobject * self, int num)
{
693
	PyObject   *result;
694 695 696 697 698 699 700 701 702

	/* allocates tuple */
	result = PyTuple_New(3);
	if (!result)
		return NULL;

	/* affects field information */
	PyTuple_SET_ITEM(result, 0, PyInt_FromLong(num));
	PyTuple_SET_ITEM(result, 1,
703
				   PyString_FromString(PQfname(self->last_result, num)));
704
	PyTuple_SET_ITEM(result, 2,
705
					 PyInt_FromLong(PQftype(self->last_result, num)));
706 707 708 709 710 711 712 713 714 715 716 717

	return result;
}

/* lists fields info */
static char pgsource_listinfo__doc__[] =
"listinfo() -- return information for all fields "
"(position, name, type oid).";

static PyObject *
pgsource_listinfo(pgsourceobject * self, PyObject * args)
{
718 719 720
	int			i;
	PyObject   *result,
			   *info;
721 722 723 724 725 726 727 728 729

	/* checks validity */
	if (!check_source_obj(self, CHECK_RESULT | CHECK_DQL))
		return NULL;

	/* gets args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_TypeError,
730
						"method listinfo() takes no parameter.");
731 732 733 734 735 736 737
		return NULL;
	}

	/* builds result */
	if ((result = PyTuple_New(self->num_fields)) == NULL)
		return NULL;

738
	for (i = 0; i < self->num_fields; i++)
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
	{
		info = pgsource_buildinfo(self, i);
		if (!info)
		{
			Py_DECREF(result);
			return NULL;
		}
		PyTuple_SET_ITEM(result, i, info);
	}

	/* returns result */
	return result;
};

/* list fields information for last result */
static char pgsource_fieldinfo__doc__[] =
"fieldinfo(string|integer) -- return specified field information "
"(position, name, type oid).";

static PyObject *
pgsource_fieldinfo(pgsourceobject * self, PyObject * args)
{
	static const char short_usage[] =
762 763 764
	"fieldinfo(desc), with desc (string|integer).";
	int			num;
	PyObject   *param;
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788

	/* gets args */
	if (!PyArg_ParseTuple(args, "O", &param))
	{
		PyErr_SetString(PyExc_TypeError, short_usage);
		return NULL;
	}

	/* checks args and validity */
	if ((num = pgsource_fieldindex(self, param, short_usage)) == -1)
		return NULL;

	/* returns result */
	return pgsource_buildinfo(self, num);
};

/* retrieve field value */
static char pgsource_field__doc__[] =
"field(string|integer) -- return specified field value.";

static PyObject *
pgsource_field(pgsourceobject * self, PyObject * args)
{
	static const char short_usage[] =
789 790 791
	"field(desc), with desc (string|integer).";
	int			num;
	PyObject   *param;
792 793 794 795 796 797 798 799 800 801 802 803 804

	/* gets args */
	if (!PyArg_ParseTuple(args, "O", &param))
	{
		PyErr_SetString(PyExc_TypeError, short_usage);
		return NULL;
	}

	/* checks args and validity */
	if ((num = pgsource_fieldindex(self, param, short_usage)) == -1)
		return NULL;

	return PyString_FromString(PQgetvalue(self->last_result,
805
										  self->current_row, num));
806 807 808 809
}

/* query object methods */
static PyMethodDef pgsource_methods[] = {
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
	{"close", (PyCFunction) pgsource_close, 1,
	pgsource_close__doc__},
	{"execute", (PyCFunction) pgsource_execute, 1,
	pgsource_execute__doc__},
	{"oidstatus", (PyCFunction) pgsource_oidstatus, 1,
	pgsource_oidstatus__doc__},
	{"fetch", (PyCFunction) pgsource_fetch, 1,
	pgsource_fetch__doc__},
	{"movefirst", (PyCFunction) pgsource_movefirst, 1,
	pgsource_movefirst__doc__},
	{"movelast", (PyCFunction) pgsource_movelast, 1,
	pgsource_movelast__doc__},
	{"movenext", (PyCFunction) pgsource_movenext, 1,
	pgsource_movenext__doc__},
	{"moveprev", (PyCFunction) pgsource_moveprev, 1,
	pgsource_moveprev__doc__},
	{"field", (PyCFunction) pgsource_field, 1,
	pgsource_field__doc__},
	{"fieldinfo", (PyCFunction) pgsource_fieldinfo, 1,
	pgsource_fieldinfo__doc__},
	{"listinfo", (PyCFunction) pgsource_listinfo, 1,
	pgsource_listinfo__doc__},
	{NULL, NULL}
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
};

/* gets query object attributes */
static PyObject *
pgsource_getattr(pgsourceobject * self, char *name)
{
	/* pg connection object */
	if (!strcmp(name, "pgcnx"))
	{
		if (check_source_obj(self, 0))
		{
			Py_INCREF(self->pgcnx);
			return (PyObject *) (self->pgcnx);
		}
		Py_INCREF(Py_None);
		return Py_None;
	}

	/* arraysize */
	if (!strcmp(name, "arraysize"))
		return PyInt_FromLong(self->arraysize);

	/* resulttype */
	if (!strcmp(name, "resulttype"))
		return PyInt_FromLong(self->result_type);

	/* ntuples */
	if (!strcmp(name, "ntuples"))
		return PyInt_FromLong(self->max_row);

	/* nfields */
	if (!strcmp(name, "nfields"))
		return PyInt_FromLong(self->num_fields);

	/* attributes list */
868 869 870
	if (!strcmp(name, "__members__"))
	{
		PyObject   *list = PyList_New(5);
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894

		PyList_SET_ITEM(list, 0, PyString_FromString("pgcnx"));
		PyList_SET_ITEM(list, 1, PyString_FromString("arraysize"));
		PyList_SET_ITEM(list, 2, PyString_FromString("resulttype"));
		PyList_SET_ITEM(list, 3, PyString_FromString("ntuples"));
		PyList_SET_ITEM(list, 4, PyString_FromString("nfields"));

		return list;
	}

	/* module name */
	if (!strcmp(name, "__module__"))
		return PyString_FromString(MODULE_NAME);

	/* class name */
	if (!strcmp(name, "__class__"))
		return PyString_FromString("pgsource");

	/* seeks name in methods (fallback) */
	return Py_FindMethod(pgsource_methods, (PyObject *) self, name);
}

/* sets query object attributes */
static int
895
pgsource_setattr(pgsourceobject * self, char *name, PyObject * v)
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
{
	/* arraysize */
	if (!strcmp(name, "arraysize"))
	{
		if (!PyInt_Check(v))
		{
			PyErr_SetString(PyExc_TypeError, "arraysize must be integer.");
			return -1;
		}

		self->arraysize = PyInt_AsLong(v);
		return 0;
	}

	/* unknown attribute */
	PyErr_SetString(PyExc_TypeError, "not a writable attribute.");
	return -1;
}

/* prints query object in human readable format */
static int
pgsource_print(pgsourceobject * self, FILE *fp, int flags)
{
919
	PQprintOpt	op;
920 921 922

	switch (self->result_type)
	{
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
		case RESULT_DQL:
			memset(&op, 0, sizeof(op));
			op.align = 1;
			op.header = 1;
			op.fieldSep = "|";
			op.pager = 1;
			PQprint(fp, self->last_result, &op);
			break;
		case RESULT_DDL:
		case RESULT_DML:
			fputs(PQcmdStatus(self->last_result), fp);
			break;
		case RESULT_EMPTY:
		default:
			fputs("Empty PostgreSQL source object.", fp);
			break;
939 940 941 942 943 944 945
	}

	return 0;
}

/* query type definition */
staticforward PyTypeObject PgSourceType = {
D'Arcy J.M. Cain's avatar
D'Arcy J.M. Cain committed
946 947
	PyObject_HEAD_INIT(NULL)

948 949 950 951
	0,							/* ob_size */
	"pgsourceobject",			/* tp_name */
	sizeof(pgsourceobject),		/* tp_basicsize */
	0,							/* tp_itemsize */
952
	/* methods */
953 954 955 956 957 958 959 960 961 962
	(destructor) pgsource_dealloc,		/* tp_dealloc */
	(printfunc) pgsource_print, /* tp_print */
	(getattrfunc) pgsource_getattr,		/* tp_getattr */
	(setattrfunc) pgsource_setattr,		/* tp_setattr */
	0,							/* tp_compare */
	0,							/* tp_repr */
	0,							/* tp_as_number */
	0,							/* tp_as_sequence */
	0,							/* tp_as_mapping */
	0,							/* tp_hash */
963
};
Bruce Momjian's avatar
Bruce Momjian committed
964 965

/* --------------------------------------------------------------------- */
966
/* PG "LARGE" OBJECT IMPLEMENTATION */
Bruce Momjian's avatar
Bruce Momjian committed
967

968
#ifdef LARGE_OBJECTS
Bruce Momjian's avatar
Bruce Momjian committed
969

970 971 972
/* constructor (internal use only) */
static pglargeobject *
pglarge_new(pgobject * pgcnx, Oid oid)
Bruce Momjian's avatar
Bruce Momjian committed
973
{
974
	pglargeobject *npglo;
Bruce Momjian's avatar
Bruce Momjian committed
975 976

	if ((npglo = PyObject_NEW(pglargeobject, &PglargeType)) == NULL)
Bruce Momjian's avatar
Bruce Momjian committed
977 978
		return NULL;

979 980
	Py_XINCREF(pgcnx);
	npglo->pgcnx = pgcnx;
Bruce Momjian's avatar
Bruce Momjian committed
981
	npglo->lo_fd = -1;
982
	npglo->lo_oid = oid;
Bruce Momjian's avatar
Bruce Momjian committed
983

Bruce Momjian's avatar
Bruce Momjian committed
984
	return npglo;
Bruce Momjian's avatar
Bruce Momjian committed
985 986 987 988 989 990
}

/* destructor */
static void
pglarge_dealloc(pglargeobject * self)
{
991
	if (self->lo_fd >= 0 && check_cnx_obj(self->pgcnx))
Bruce Momjian's avatar
Bruce Momjian committed
992 993 994 995 996 997 998
		lo_close(self->pgcnx->cnx, self->lo_fd);

	Py_XDECREF(self->pgcnx);
	PyMem_DEL(self);
}

/* opens large object */
999
static char pglarge_open__doc__[] =
1000 1001
"open(mode) -- open access to large object with specified mode "
"(INV_READ, INV_WRITE constants defined by module).";
1002

Bruce Momjian's avatar
Bruce Momjian committed
1003 1004 1005
static PyObject *
pglarge_open(pglargeobject * self, PyObject * args)
{
1006 1007
	int			mode,
				fd;
Bruce Momjian's avatar
Bruce Momjian committed
1008 1009

	/* check validity */
1010
	if (!check_lo_obj(self, CHECK_CLOSE))
Bruce Momjian's avatar
Bruce Momjian committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
		return NULL;

	/* gets arguments */
	if (!PyArg_ParseTuple(args, "i", &mode))
	{
		PyErr_SetString(PyExc_TypeError, "open(mode), with mode(integer).");
		return NULL;
	}

	/* opens large object */
	if ((fd = lo_open(self->pgcnx->cnx, self->lo_oid, mode)) < 0)
	{
		PyErr_SetString(PyExc_IOError, "can't open large object.");
		return NULL;
	}
	self->lo_fd = fd;

	/* no error : returns Py_None */
	Py_INCREF(Py_None);
	return Py_None;
}

/* close large object */
1034
static char pglarge_close__doc__[] =
1035
"close() -- close access to large object data.";
Bruce Momjian's avatar
Bruce Momjian committed
1036

Bruce Momjian's avatar
Bruce Momjian committed
1037 1038 1039 1040 1041 1042 1043
static PyObject *
pglarge_close(pglargeobject * self, PyObject * args)
{
	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
1044
						"method close() takes no parameters.");
Bruce Momjian's avatar
Bruce Momjian committed
1045 1046 1047 1048
		return NULL;
	}

	/* checks validity */
1049
	if (!check_lo_obj(self, CHECK_OPEN))
Bruce Momjian's avatar
Bruce Momjian committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
		return NULL;

	/* closes large object */
	if (lo_close(self->pgcnx->cnx, self->lo_fd))
	{
		PyErr_SetString(PyExc_IOError, "error while closing large object fd.");
		return NULL;
	}
	self->lo_fd = -1;

	/* no error : returns Py_None */
	Py_INCREF(Py_None);
	return Py_None;
}

/* reads from large object */
1066
static char pglarge_read__doc__[] =
1067 1068
"read(integer) -- read from large object to sized string. "
"Object must be opened in read mode before calling this method.";
1069

Bruce Momjian's avatar
Bruce Momjian committed
1070 1071 1072
static PyObject *
pglarge_read(pglargeobject * self, PyObject * args)
{
Bruce Momjian's avatar
Bruce Momjian committed
1073
	int			size;
1074
	PyObject   *buffer;
Bruce Momjian's avatar
Bruce Momjian committed
1075 1076

	/* checks validity */
1077
	if (!check_lo_obj(self, CHECK_OPEN))
Bruce Momjian's avatar
Bruce Momjian committed
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
		return NULL;

	/* gets arguments */
	if (!PyArg_ParseTuple(args, "i", &size))
	{
		PyErr_SetString(PyExc_TypeError, "read(size), wih size (integer).");
		return NULL;
	}

	if (size <= 0)
	{
		PyErr_SetString(PyExc_ValueError, "size must be positive.");
		return NULL;
	}

	/* allocate buffer and runs read */
	buffer = PyString_FromStringAndSize((char *) NULL, size);

	if ((size = lo_read(self->pgcnx->cnx, self->lo_fd, BUF(buffer), size)) < 0)
	{
		PyErr_SetString(PyExc_IOError, "error while reading.");
		Py_XDECREF(buffer);
		return NULL;
	}

	/* resize buffer and returns it */
	_PyString_Resize(&buffer, size);
	return buffer;
}

/* write to large object */
1109
static char pglarge_write__doc__[] =
1110 1111
"write(string) -- write sized string to large object. "
"Object must be opened in read mode before calling this method.";
Bruce Momjian's avatar
Bruce Momjian committed
1112

Bruce Momjian's avatar
Bruce Momjian committed
1113 1114 1115
static PyObject *
pglarge_write(pglargeobject * self, PyObject * args)
{
1116 1117 1118
	char	   *buffer;
	int			size,
				bufsize;
Bruce Momjian's avatar
Bruce Momjian committed
1119 1120

	/* checks validity */
1121
	if (!check_lo_obj(self, CHECK_OPEN))
Bruce Momjian's avatar
Bruce Momjian committed
1122 1123 1124
		return NULL;

	/* gets arguments */
1125
	if (!PyArg_ParseTuple(args, "s#", &buffer, &bufsize))
Bruce Momjian's avatar
Bruce Momjian committed
1126 1127
	{
		PyErr_SetString(PyExc_TypeError,
1128
						"write(buffer), with buffer (sized string).");
Bruce Momjian's avatar
Bruce Momjian committed
1129 1130 1131 1132 1133
		return NULL;
	}

	/* sends query */
	if ((size = lo_write(self->pgcnx->cnx, self->lo_fd, buffer,
1134
						 bufsize)) < bufsize)
Bruce Momjian's avatar
Bruce Momjian committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
	{
		PyErr_SetString(PyExc_IOError, "buffer truncated during write.");
		return NULL;
	}

	/* no error : returns Py_None */
	Py_INCREF(Py_None);
	return Py_None;
}

/* go to position in large object */
1146
static char pglarge_seek__doc__[] =
1147 1148 1149
"seek(off, whence) -- move to specified position. Object must be opened "
"before calling this method. whence can be SEEK_SET, SEEK_CUR or SEEK_END, "
"constants defined by module.";
1150

Bruce Momjian's avatar
Bruce Momjian committed
1151 1152 1153
static PyObject *
pglarge_lseek(pglargeobject * self, PyObject * args)
{
1154
	/* offset and whence are initialized to keep compiler happy */
1155 1156 1157
	int			ret,
				offset = 0,
				whence = 0;
Bruce Momjian's avatar
Bruce Momjian committed
1158 1159

	/* checks validity */
1160
	if (!check_lo_obj(self, CHECK_OPEN))
Bruce Momjian's avatar
Bruce Momjian committed
1161 1162 1163 1164 1165 1166
		return NULL;

	/* gets arguments */
	if (!PyArg_ParseTuple(args, "ii", &offset, &whence))
	{
		PyErr_SetString(PyExc_TypeError,
1167
			"lseek(offset, whence), with offset and whence (integers).");
Bruce Momjian's avatar
Bruce Momjian committed
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
		return NULL;
	}

	/* sends query */
	if ((ret = lo_lseek(self->pgcnx->cnx, self->lo_fd, offset, whence)) == -1)
	{
		PyErr_SetString(PyExc_IOError, "error while moving cursor.");
		return NULL;
	}

	/* returns position */
	return PyInt_FromLong(ret);
}

/* gets large object size */
1183
static char pglarge_size__doc__[] =
1184 1185
"size() -- return large object size. "
"Object must be opened before calling this method.";
1186

Bruce Momjian's avatar
Bruce Momjian committed
1187 1188 1189
static PyObject *
pglarge_size(pglargeobject * self, PyObject * args)
{
1190 1191
	int			start,
				end;
Bruce Momjian's avatar
Bruce Momjian committed
1192 1193 1194 1195 1196

	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
1197
						"method size() takes no parameters.");
Bruce Momjian's avatar
Bruce Momjian committed
1198 1199 1200 1201
		return NULL;
	}

	/* checks validity */
1202
	if (!check_lo_obj(self, CHECK_OPEN))
Bruce Momjian's avatar
Bruce Momjian committed
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
		return NULL;

	/* gets current position */
	if ((start = lo_tell(self->pgcnx->cnx, self->lo_fd)) == -1)
	{
		PyErr_SetString(PyExc_IOError, "error while getting current position.");
		return NULL;
	}

	/* gets end position */
	if ((end = lo_lseek(self->pgcnx->cnx, self->lo_fd, 0, SEEK_END)) == -1)
	{
		PyErr_SetString(PyExc_IOError, "error while getting end position.");
		return NULL;
	}

	/* move back to start position */
1220
	if ((start = lo_lseek(self->pgcnx->cnx, self->lo_fd, start, SEEK_SET)) == -1)
Bruce Momjian's avatar
Bruce Momjian committed
1221 1222
	{
		PyErr_SetString(PyExc_IOError,
1223
						"error while moving back to first position.");
Bruce Momjian's avatar
Bruce Momjian committed
1224 1225 1226 1227 1228 1229 1230 1231
		return NULL;
	}

	/* returns size */
	return PyInt_FromLong(end);
}

/* gets large object cursor position */
1232
static char pglarge_tell__doc__[] =
1233 1234
"tell() -- give current position in large object. "
"Object must be opened before calling this method.";
1235

Bruce Momjian's avatar
Bruce Momjian committed
1236 1237 1238
static PyObject *
pglarge_tell(pglargeobject * self, PyObject * args)
{
1239
	int			start;
Bruce Momjian's avatar
Bruce Momjian committed
1240 1241 1242 1243 1244

	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
1245
						"method tell() takes no parameters.");
Bruce Momjian's avatar
Bruce Momjian committed
1246 1247 1248 1249
		return NULL;
	}

	/* checks validity */
1250
	if (!check_lo_obj(self, CHECK_OPEN))
Bruce Momjian's avatar
Bruce Momjian committed
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
		return NULL;

	/* gets current position */
	if ((start = lo_tell(self->pgcnx->cnx, self->lo_fd)) == -1)
	{
		PyErr_SetString(PyExc_IOError, "error while getting position.");
		return NULL;
	}

	/* returns size */
	return PyInt_FromLong(start);
}

/* exports large object as unix file */
1265
static char pglarge_export__doc__[] =
1266 1267
"export(string) -- export large object data to specified file. "
"Object must be closed when calling this method.";
1268

Bruce Momjian's avatar
Bruce Momjian committed
1269 1270 1271
static PyObject *
pglarge_export(pglargeobject * self, PyObject * args)
{
1272
	char	   *name;
Bruce Momjian's avatar
Bruce Momjian committed
1273 1274

	/* checks validity */
1275
	if (!check_lo_obj(self, CHECK_CLOSE))
Bruce Momjian's avatar
Bruce Momjian committed
1276 1277 1278 1279 1280 1281
		return NULL;

	/* gets arguments */
	if (!PyArg_ParseTuple(args, "s", &name))
	{
		PyErr_SetString(PyExc_TypeError,
1282
						"export(filename), with filename (string).");
Bruce Momjian's avatar
Bruce Momjian committed
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
		return NULL;
	}

	/* runs command */
	if (!lo_export(self->pgcnx->cnx, self->lo_oid, name))
	{
		PyErr_SetString(PyExc_IOError, "error while exporting large object.");
		return NULL;
	}

	Py_INCREF(Py_None);
	return Py_None;
}

/* deletes a large object */
1298
static char pglarge_unlink__doc__[] =
1299 1300
"unlink() -- destroy large object. "
"Object must be closed when calling this method.";
1301

Bruce Momjian's avatar
Bruce Momjian committed
1302 1303 1304 1305 1306 1307 1308
static PyObject *
pglarge_unlink(pglargeobject * self, PyObject * args)
{
	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
1309
						"method unlink() takes no parameters.");
Bruce Momjian's avatar
Bruce Momjian committed
1310 1311 1312 1313
		return NULL;
	}

	/* checks validity */
1314
	if (!check_lo_obj(self, CHECK_CLOSE))
Bruce Momjian's avatar
Bruce Momjian committed
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
		return NULL;

	/* deletes the object, invalidate it on success */
	if (!lo_unlink(self->pgcnx->cnx, self->lo_oid))
	{
		PyErr_SetString(PyExc_IOError, "error while unlinking large object");
		return NULL;
	}
	self->lo_oid = 0;

	Py_INCREF(Py_None);
	return Py_None;
}

/* large object methods */
static struct PyMethodDef pglarge_methods[] = {
1331 1332 1333 1334 1335 1336 1337 1338 1339
	{"open", (PyCFunction) pglarge_open, 1, pglarge_open__doc__},
	{"close", (PyCFunction) pglarge_close, 1, pglarge_close__doc__},
	{"read", (PyCFunction) pglarge_read, 1, pglarge_read__doc__},
	{"write", (PyCFunction) pglarge_write, 1, pglarge_write__doc__},
	{"seek", (PyCFunction) pglarge_lseek, 1, pglarge_seek__doc__},
	{"size", (PyCFunction) pglarge_size, 1, pglarge_size__doc__},
	{"tell", (PyCFunction) pglarge_tell, 1, pglarge_tell__doc__},
	{"export", (PyCFunction) pglarge_export, 1, pglarge_export__doc__},
	{"unlink", (PyCFunction) pglarge_unlink, 1, pglarge_unlink__doc__},
1340
	{NULL, NULL}
Bruce Momjian's avatar
Bruce Momjian committed
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
};

/* get attribute */
static PyObject *
pglarge_getattr(pglargeobject * self, char *name)
{
	/* list postgreSQL large object fields */

	/* associated pg connection object */
	if (!strcmp(name, "pgcnx"))
	{
1352
		if (check_lo_obj(self, 0))
Bruce Momjian's avatar
Bruce Momjian committed
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
		{
			Py_INCREF(self->pgcnx);
			return (PyObject *) (self->pgcnx);
		}

		Py_INCREF(Py_None);
		return Py_None;
	}

	/* large object oid */
	if (!strcmp(name, "oid"))
	{
1365
		if (check_lo_obj(self, 0))
Bruce Momjian's avatar
Bruce Momjian committed
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
			return PyInt_FromLong(self->lo_oid);

		Py_INCREF(Py_None);
		return Py_None;
	}

	/* error (status) message */
	if (!strcmp(name, "error"))
		return PyString_FromString(PQerrorMessage(self->pgcnx->cnx));

	/* attributes list */
	if (!strcmp(name, "__members__"))
	{
1379
		PyObject   *list = PyList_New(3);
Bruce Momjian's avatar
Bruce Momjian committed
1380 1381 1382

		if (list)
		{
1383 1384 1385
			PyList_SET_ITEM(list, 0, PyString_FromString("oid"));
			PyList_SET_ITEM(list, 1, PyString_FromString("pgcnx"));
			PyList_SET_ITEM(list, 2, PyString_FromString("error"));
Bruce Momjian's avatar
Bruce Momjian committed
1386 1387 1388 1389 1390
		}

		return list;
	}

1391 1392
	/* module name */
	if (!strcmp(name, "__module__"))
Bruce Momjian's avatar
Bruce Momjian committed
1393
		return PyString_FromString(MODULE_NAME);
1394 1395 1396

	/* class name */
	if (!strcmp(name, "__class__"))
Bruce Momjian's avatar
Bruce Momjian committed
1397
		return PyString_FromString("pglarge");
1398 1399

	/* seeks name in methods (fallback) */
Bruce Momjian's avatar
Bruce Momjian committed
1400 1401 1402
	return Py_FindMethod(pglarge_methods, (PyObject *) self, name);
}

1403 1404 1405 1406
/* prints query object in human readable format */
static int
pglarge_print(pglargeobject * self, FILE *fp, int flags)
{
1407
	char		print_buffer[128];
1408

Bruce Momjian's avatar
Bruce Momjian committed
1409
	if (self->lo_fd >= 0)
1410
	{
1411 1412
#ifdef	NO_SNPRINTF
		sprintf(print_buffer,
1413
				"Opened large object, oid %ld", (long) self->lo_oid);
1414
#else
Bruce Momjian's avatar
Bruce Momjian committed
1415
		snprintf(print_buffer, sizeof(print_buffer),
1416
				 "Opened large object, oid %ld", (long) self->lo_oid);
1417
#endif
1418
		fputs(print_buffer, fp);
Bruce Momjian's avatar
Bruce Momjian committed
1419 1420
	}
	else
1421
	{
1422 1423
#ifdef	NO_SNPRINTF
		sprintf(print_buffer,
1424
				"Closed large object, oid %ld", (long) self->lo_oid);
1425
#else
Bruce Momjian's avatar
Bruce Momjian committed
1426
		snprintf(print_buffer, sizeof(print_buffer),
1427
				 "Closed large object, oid %ld", (long) self->lo_oid);
1428
#endif
1429 1430 1431 1432
		fputs(print_buffer, fp);
	}

	return 0;
1433
}
1434

Bruce Momjian's avatar
Bruce Momjian committed
1435 1436 1437
/* object type definition */
staticforward PyTypeObject PglargeType = {
	PyObject_HEAD_INIT(NULL)
1438 1439 1440 1441
	0,							/* ob_size */
	"pglarge",					/* tp_name */
	sizeof(pglargeobject),		/* tp_basicsize */
	0,							/* tp_itemsize */
Bruce Momjian's avatar
Bruce Momjian committed
1442 1443

	/* methods */
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
	(destructor) pglarge_dealloc,		/* tp_dealloc */
	(printfunc) pglarge_print,	/* tp_print */
	(getattrfunc) pglarge_getattr,		/* tp_getattr */
	0,							/* tp_setattr */
	0,							/* tp_compare */
	0,							/* tp_repr */
	0,							/* tp_as_number */
	0,							/* tp_as_sequence */
	0,							/* tp_as_mapping */
	0,							/* tp_hash */
Bruce Momjian's avatar
Bruce Momjian committed
1454 1455
};

1456
#endif	 /* LARGE_OBJECTS */
Bruce Momjian's avatar
Bruce Momjian committed
1457 1458 1459


/* --------------------------------------------------------------------- */
1460
/* PG QUERY OBJECT IMPLEMENTATION */
Bruce Momjian's avatar
Bruce Momjian committed
1461

1462 1463
/* connects to a database */
static char connect__doc__[] =
1464 1465
"connect(dbname, host, port, opt, tty) -- connect to a PostgreSQL database "
"using specified parameters (optionals, keywords aware).";
Bruce Momjian's avatar
Bruce Momjian committed
1466 1467

static PyObject *
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
pgconnect(pgobject * self, PyObject * args, PyObject * dict)
{
	static const char *kwlist[] = {"dbname", "host", "port", "opt",
	"tty", "user", "passwd", NULL};
	char	   *pghost,
			   *pgopt,
			   *pgtty,
			   *pgdbname,
			   *pguser,
			   *pgpasswd;
	int			pgport;
	char		port_buffer[20];
	pgobject   *npgobj;
Bruce Momjian's avatar
Bruce Momjian committed
1481 1482 1483 1484

	pghost = pgopt = pgtty = pgdbname = pguser = pgpasswd = NULL;
	pgport = -1;

1485 1486 1487 1488 1489 1490
	/*
	 * parses standard arguments With the right compiler warnings, this
	 * will issue a diagnostic. There is really no way around it.  If I
	 * don't declare kwlist as const char *kwlist[] then it complains when
	 * I try to assign all those constant strings to it.
	 */
1491
	if (!PyArg_ParseTupleAndKeywords(args, dict, "|zzizzzz", (char **) kwlist,
1492
		&pgdbname, &pghost, &pgport, &pgopt, &pgtty, &pguser, &pgpasswd))
Bruce Momjian's avatar
Bruce Momjian committed
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
		return NULL;

#ifdef DEFAULT_VARS
	/* handles defaults variables (for unintialised vars) */
	if ((!pghost) && (pg_default_host != Py_None))
		pghost = PyString_AsString(pg_default_host);

	if ((pgport == -1) && (pg_default_port != Py_None))
		pgport = PyInt_AsLong(pg_default_port);

	if ((!pgopt) && (pg_default_opt != Py_None))
		pgopt = PyString_AsString(pg_default_opt);

	if ((!pgtty) && (pg_default_tty != Py_None))
		pgtty = PyString_AsString(pg_default_tty);

	if ((!pgdbname) && (pg_default_base != Py_None))
		pgdbname = PyString_AsString(pg_default_base);

	if ((!pguser) && (pg_default_user != Py_None))
		pguser = PyString_AsString(pg_default_user);

	if ((!pgpasswd) && (pg_default_passwd != Py_None))
		pgpasswd = PyString_AsString(pg_default_passwd);
1517
#endif	 /* DEFAULT_VARS */
Bruce Momjian's avatar
Bruce Momjian committed
1518

1519
	if ((npgobj = (pgobject *) pgobject_New()) == NULL)
Bruce Momjian's avatar
Bruce Momjian committed
1520 1521 1522 1523
		return NULL;

	if (pgport != -1)
	{
Bruce Momjian's avatar
Bruce Momjian committed
1524
		memset(port_buffer, 0, sizeof(port_buffer));
Bruce Momjian's avatar
Bruce Momjian committed
1525 1526
		sprintf(port_buffer, "%d", pgport);
		npgobj->cnx = PQsetdbLogin(pghost, port_buffer, pgopt, pgtty, pgdbname,
1527
								   pguser, pgpasswd);
Bruce Momjian's avatar
Bruce Momjian committed
1528 1529 1530
	}
	else
		npgobj->cnx = PQsetdbLogin(pghost, NULL, pgopt, pgtty, pgdbname,
1531
								   pguser, pgpasswd);
Bruce Momjian's avatar
Bruce Momjian committed
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555

	if (PQstatus(npgobj->cnx) == CONNECTION_BAD)
	{
		PyErr_SetString(PGError, PQerrorMessage(npgobj->cnx));
		Py_XDECREF(npgobj);
		return NULL;
	}

	return (PyObject *) npgobj;
}

/* pgobject methods */

/* destructor */
static void
pg_dealloc(pgobject * self)
{
	if (self->cnx)
		PQfinish(self->cnx);

	PyMem_DEL(self);
}

/* close without deleting */
Bruce Momjian's avatar
Bruce Momjian committed
1556
static char pg_close__doc__[] =
1557 1558 1559
"close() -- close connection. All instances of the connection object and "
"derived objects (queries and large objects) can no longer be used after "
"this call.";
1560

Bruce Momjian's avatar
Bruce Momjian committed
1561
static PyObject *
1562
pg_close(pgobject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
{
	/* gets args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_TypeError, "close().");
		return NULL;
	}

	if (self->cnx)
		PQfinish(self->cnx);

	self->cnx = NULL;
	Py_INCREF(Py_None);
	return Py_None;
Bruce Momjian's avatar
Bruce Momjian committed
1577
}
Bruce Momjian's avatar
Bruce Momjian committed
1578 1579

static void
1580
pgquery_dealloc(pgqueryobject * self)
Bruce Momjian's avatar
Bruce Momjian committed
1581 1582 1583 1584 1585 1586 1587 1588
{
	if (self->last_result)
		PQclear(self->last_result);

	PyMem_DEL(self);
}

/* resets connection */
Bruce Momjian's avatar
Bruce Momjian committed
1589
static char pg_reset__doc__[] =
1590 1591 1592
"reset() -- reset connection with current parameters. All derived queries "
"and large objects derived from this connection will not be usable after "
"this call.";
1593

Bruce Momjian's avatar
Bruce Momjian committed
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
static PyObject *
pg_reset(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,
1607
						"method reset() takes no parameters.");
Bruce Momjian's avatar
Bruce Momjian committed
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
		return NULL;
	}

	/* resets the connection */
	PQreset(self->cnx);
	Py_INCREF(Py_None);
	return Py_None;
}

/* get connection socket */
Bruce Momjian's avatar
Bruce Momjian committed
1618
static char pg_fileno__doc__[] =
1619
"fileno() -- return database connection socket file handle.";
1620

Bruce Momjian's avatar
Bruce Momjian committed
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
static PyObject *
pg_fileno(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,
1634
						"method fileno() takes no parameters.");
Bruce Momjian's avatar
Bruce Momjian committed
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
		return NULL;
	}

#ifdef		NO_PQSOCKET
	return PyInt_FromLong((long) self->cnx->sock);
#else
	return PyInt_FromLong((long) PQsocket(self->cnx));
#endif
}

1645 1646
/* get number of rows */
static char pgquery_ntuples__doc__[] =
1647
"ntuples() -- returns number of tuples returned by query.";
1648 1649 1650 1651 1652 1653 1654 1655

static PyObject *
pgquery_ntuples(pgqueryobject * self, PyObject * args)
{
	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
1656
						"method ntuples() takes no parameters.");
1657 1658 1659 1660
		return NULL;
	}

	return PyInt_FromLong((long) PQntuples(self->last_result));
Bruce Momjian's avatar
Bruce Momjian committed
1661
}
1662

Bruce Momjian's avatar
Bruce Momjian committed
1663
/* list fields names from query result */
1664
static char pgquery_listfields__doc__[] =
1665
"listfields() -- Lists field names from result.";
1666

Bruce Momjian's avatar
Bruce Momjian committed
1667
static PyObject *
1668
pgquery_listfields(pgqueryobject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
1669
{
1670 1671 1672 1673 1674
	int			i,
				n;
	char	   *name;
	PyObject   *fieldstuple,
			   *str;
Bruce Momjian's avatar
Bruce Momjian committed
1675 1676 1677 1678 1679

	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
1680
						"method listfields() takes no parameters.");
Bruce Momjian's avatar
Bruce Momjian committed
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
		return NULL;
	}

	/* builds tuple */
	n = PQnfields(self->last_result);
	fieldstuple = PyTuple_New(n);

	for (i = 0; i < n; i++)
	{
		name = PQfname(self->last_result, i);
		str = PyString_FromString(name);
1692
		PyTuple_SET_ITEM(fieldstuple, i, str);
Bruce Momjian's avatar
Bruce Momjian committed
1693 1694 1695 1696 1697 1698
	}

	return fieldstuple;
}

/* get field name from last result */
1699
static char pgquery_fieldname__doc__[] =
1700
"fieldname() -- returns name of field from result from its position.";
1701

Bruce Momjian's avatar
Bruce Momjian committed
1702
static PyObject *
1703
pgquery_fieldname(pgqueryobject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
1704
{
1705 1706
	int			i;
	char	   *name;
Bruce Momjian's avatar
Bruce Momjian committed
1707 1708 1709 1710 1711

	/* gets args */
	if (!PyArg_ParseTuple(args, "i", &i))
	{
		PyErr_SetString(PyExc_TypeError,
1712
						"fieldname(number), with number(integer).");
Bruce Momjian's avatar
Bruce Momjian committed
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
		return NULL;
	}

	/* checks number validity */
	if (i >= PQnfields(self->last_result))
	{
		PyErr_SetString(PyExc_ValueError, "invalid field number.");
		return NULL;
	}

	/* gets fields name and builds object */
	name = PQfname(self->last_result, i);
	return PyString_FromString(name);
}

/* gets fields number from name in last result */
1729
static char pgquery_fieldnum__doc__[] =
1730
"fieldnum() -- returns position in query for field from its name.";
1731

Bruce Momjian's avatar
Bruce Momjian committed
1732
static PyObject *
1733
pgquery_fieldnum(pgqueryobject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
1734
{
1735 1736
	char	   *name;
	int			num;
Bruce Momjian's avatar
Bruce Momjian committed
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755

	/* gets args */
	if (!PyArg_ParseTuple(args, "s", &name))
	{
		PyErr_SetString(PyExc_TypeError, "fieldnum(name), with name (string).");
		return NULL;
	}

	/* gets field number */
	if ((num = PQfnumber(self->last_result, name)) == -1)
	{
		PyErr_SetString(PyExc_ValueError, "Unknown field.");
		return NULL;
	}

	return PyInt_FromLong(num);
}

/* retrieves last result */
1756
static char pgquery_getresult__doc__[] =
1757 1758 1759
"getresult() -- Gets the result of a query.  The result is returned "
"as a list of rows, each one a list of fields in the order returned "
"by the server.";
1760

Bruce Momjian's avatar
Bruce Momjian committed
1761
static PyObject *
1762
pgquery_getresult(pgqueryobject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
1763
{
1764 1765 1766 1767 1768 1769 1770 1771
	PyObject   *rowtuple,
			   *reslist,
			   *val;
	int			i,
				j,
				m,
				n,
			   *typ;
Bruce Momjian's avatar
Bruce Momjian committed
1772 1773 1774 1775 1776

	/* checks args (args == NULL for an internal call) */
	if ((args != NULL) && (!PyArg_ParseTuple(args, "")))
	{
		PyErr_SetString(PyExc_SyntaxError,
1777
						"method getresult() takes no parameters.");
Bruce Momjian's avatar
Bruce Momjian committed
1778 1779 1780 1781 1782 1783
		return NULL;
	}

	/* stores result in tuple */
	m = PQntuples(self->last_result);
	n = PQnfields(self->last_result);
1784
	reslist = PyList_New(m);
Bruce Momjian's avatar
Bruce Momjian committed
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797

	if ((typ = malloc(sizeof(int) * n)) == NULL)
	{
		PyErr_SetString(PyExc_SyntaxError, "memory error in getresult().");
		return NULL;
	}

	for (j = 0; j < n; j++)
	{
		switch (PQftype(self->last_result, j))
		{
			case INT2OID:
			case INT4OID:
1798
			case INT8OID:
Bruce Momjian's avatar
Bruce Momjian committed
1799 1800 1801 1802 1803 1804
			case OIDOID:
				typ[j] = 1;
				break;

			case FLOAT4OID:
			case FLOAT8OID:
1805
			case NUMERICOID:
Bruce Momjian's avatar
Bruce Momjian committed
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
				typ[j] = 2;
				break;

			case CASHOID:
				typ[j] = 3;
				break;

			default:
				typ[j] = 4;
				break;
		}
	}

	for (i = 0; i < m; i++)
	{
1821 1822 1823 1824 1825 1826 1827
		if ((rowtuple = PyTuple_New(n)) == NULL)
		{
			Py_DECREF(reslist);
			reslist = NULL;
			goto exit;
		}

Bruce Momjian's avatar
Bruce Momjian committed
1828 1829
		for (j = 0; j < n; j++)
		{
1830 1831 1832
			int			k;
			char	   *s = PQgetvalue(self->last_result, i, j);
			char		cashbuf[64];
Bruce Momjian's avatar
Bruce Momjian committed
1833

1834 1835 1836 1837 1838
			if (PQgetisnull(self->last_result, i, j))
			{
				Py_INCREF(Py_None);
				val = Py_None;
			}
1839 1840 1841 1842 1843 1844
			else
				switch (typ[j])
				{
					case 1:
						val = PyInt_FromLong(strtol(s, NULL, 10));
						break;
Bruce Momjian's avatar
Bruce Momjian committed
1845

1846 1847 1848
					case 2:
						val = PyFloat_FromDouble(strtod(s, NULL));
						break;
Bruce Momjian's avatar
Bruce Momjian committed
1849

1850 1851 1852 1853
					case 3:
					{
						int mult = 1;

1854 1855 1856
						if (*s == '$')	/* there's talk of getting rid of
										 * it */
							s++;
Bruce Momjian's avatar
Bruce Momjian committed
1857

1858 1859 1860 1861 1862 1863 1864 1865 1866
						if (*s == '-' || *s == '(')
						{
							s++;
							mult = -1;
						}

						/* get rid of the '$' and commas */
						if (*s == '$')	/* Just in case we exposed one */
							s++;
1867

1868 1869 1870
						for (k = 0; *s; s++)
							if (*s != ',')
								cashbuf[k++] = *s;
Bruce Momjian's avatar
Bruce Momjian committed
1871

1872
						cashbuf[k] = 0;
1873
						val = PyFloat_FromDouble(strtod(cashbuf, NULL) * mult);
1874
						break;
1875
					}
Bruce Momjian's avatar
Bruce Momjian committed
1876

1877 1878 1879 1880
					default:
						val = PyString_FromString(s);
						break;
				}
Bruce Momjian's avatar
Bruce Momjian committed
1881

1882 1883 1884 1885 1886 1887 1888 1889 1890
			if (val == NULL)
			{
				Py_DECREF(reslist);
				Py_DECREF(rowtuple);
				reslist = NULL;
				goto exit;
			}

			PyTuple_SET_ITEM(rowtuple, j, val);
Bruce Momjian's avatar
Bruce Momjian committed
1891 1892
		}

1893
		PyList_SET_ITEM(reslist, i, rowtuple);
Bruce Momjian's avatar
Bruce Momjian committed
1894 1895
	}

1896
exit:
Bruce Momjian's avatar
Bruce Momjian committed
1897 1898 1899 1900 1901 1902 1903
	free(typ);

	/* returns list */
	return reslist;
}

/* retrieves last result as a list of dictionaries*/
1904
static char pgquery_dictresult__doc__[] =
1905 1906 1907
"dictresult() -- Gets the result of a query.  The result is returned "
"as a list of rows, each one a dictionary with the field names used "
"as the labels.";
1908

Bruce Momjian's avatar
Bruce Momjian committed
1909
static PyObject *
1910
pgquery_dictresult(pgqueryobject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
1911
{
1912 1913 1914 1915 1916 1917 1918 1919
	PyObject   *dict,
			   *reslist,
			   *val;
	int			i,
				j,
				m,
				n,
			   *typ;
Bruce Momjian's avatar
Bruce Momjian committed
1920 1921 1922 1923 1924

	/* checks args (args == NULL for an internal call) */
	if ((args != NULL) && (!PyArg_ParseTuple(args, "")))
	{
		PyErr_SetString(PyExc_SyntaxError,
1925
						"method getresult() takes no parameters.");
Bruce Momjian's avatar
Bruce Momjian committed
1926 1927 1928 1929 1930 1931
		return NULL;
	}

	/* stores result in list */
	m = PQntuples(self->last_result);
	n = PQnfields(self->last_result);
1932
	reslist = PyList_New(m);
Bruce Momjian's avatar
Bruce Momjian committed
1933 1934 1935

	if ((typ = malloc(sizeof(int) * n)) == NULL)
	{
1936
		PyErr_SetString(PyExc_SyntaxError, "memory error in dictresult().");
Bruce Momjian's avatar
Bruce Momjian committed
1937 1938 1939 1940 1941 1942 1943 1944 1945
		return NULL;
	}

	for (j = 0; j < n; j++)
	{
		switch (PQftype(self->last_result, j))
		{
			case INT2OID:
			case INT4OID:
1946
			case INT8OID:
Bruce Momjian's avatar
Bruce Momjian committed
1947 1948 1949 1950 1951 1952
			case OIDOID:
				typ[j] = 1;
				break;

			case FLOAT4OID:
			case FLOAT8OID:
1953
			case NUMERICOID:
Bruce Momjian's avatar
Bruce Momjian committed
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
				typ[j] = 2;
				break;

			case CASHOID:
				typ[j] = 3;
				break;

			default:
				typ[j] = 4;
				break;
		}
	}

	for (i = 0; i < m; i++)
	{
1969 1970 1971 1972 1973 1974 1975
		if ((dict = PyDict_New()) == NULL)
		{
			Py_DECREF(reslist);
			reslist = NULL;
			goto exit;
		}

Bruce Momjian's avatar
Bruce Momjian committed
1976 1977
		for (j = 0; j < n; j++)
		{
1978 1979 1980
			int			k;
			char	   *s = PQgetvalue(self->last_result, i, j);
			char		cashbuf[64];
Bruce Momjian's avatar
Bruce Momjian committed
1981

1982 1983 1984 1985 1986
			if (PQgetisnull(self->last_result, i, j))
			{
				Py_INCREF(Py_None);
				val = Py_None;
			}
1987 1988 1989 1990 1991 1992
			else
				switch (typ[j])
				{
					case 1:
						val = PyInt_FromLong(strtol(s, NULL, 10));
						break;
Bruce Momjian's avatar
Bruce Momjian committed
1993

1994 1995 1996
					case 2:
						val = PyFloat_FromDouble(strtod(s, NULL));
						break;
Bruce Momjian's avatar
Bruce Momjian committed
1997

1998 1999 2000 2001
					case 3:
					{
						int mult = 1;

2002 2003 2004
						if (*s == '$')	/* there's talk of getting rid of
										 * it */
							s++;
Bruce Momjian's avatar
Bruce Momjian committed
2005

2006 2007 2008 2009 2010 2011 2012 2013 2014
						if (*s == '-' || *s == '(')
						{
							s++;
							mult = -1;
						}

						/* get rid of the '$' and commas */
						if (*s == '$')	/* Just in case we exposed one */
							s++;
2015

2016 2017 2018
						for (k = 0; *s; s++)
							if (*s != ',')
								cashbuf[k++] = *s;
Bruce Momjian's avatar
Bruce Momjian committed
2019

2020
						cashbuf[k] = 0;
2021
						val = PyFloat_FromDouble(strtod(cashbuf, NULL) * mult);
2022
						break;
2023
					}
Bruce Momjian's avatar
Bruce Momjian committed
2024

2025 2026 2027 2028
					default:
						val = PyString_FromString(s);
						break;
				}
Bruce Momjian's avatar
Bruce Momjian committed
2029

2030 2031 2032 2033 2034 2035 2036 2037
			if (val == NULL)
			{
				Py_DECREF(dict);
				Py_DECREF(reslist);
				reslist = NULL;
				goto exit;
			}

Bruce Momjian's avatar
Bruce Momjian committed
2038
			PyDict_SetItemString(dict, PQfname(self->last_result, j), val);
2039
			Py_DECREF(val);
Bruce Momjian's avatar
Bruce Momjian committed
2040 2041
		}

2042
		PyList_SET_ITEM(reslist, i, dict);
Bruce Momjian's avatar
Bruce Momjian committed
2043 2044
	}

2045
exit:
Bruce Momjian's avatar
Bruce Momjian committed
2046 2047 2048 2049 2050 2051
	free(typ);

	/* returns list */
	return reslist;
}

2052
/* gets asynchronous notify */
Bruce Momjian's avatar
Bruce Momjian committed
2053
static char pg_getnotify__doc__[] =
2054
"getnotify() -- get database notify for this connection.";
2055

Bruce Momjian's avatar
Bruce Momjian committed
2056 2057 2058
static PyObject *
pg_getnotify(pgobject * self, PyObject * args)
{
2059 2060 2061 2062
	PGnotify   *notify;
	PGresult   *result;
	PyObject   *notify_result,
			   *temp;
Bruce Momjian's avatar
Bruce Momjian committed
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073

	if (!self->cnx)
	{
		PyErr_SetString(PyExc_TypeError, "Connection is not valid");
		return NULL;
	}

	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
2074
						"method getnotify() takes no parameters.");
Bruce Momjian's avatar
Bruce Momjian committed
2075 2076 2077 2078
		return NULL;
	}

	/* gets notify and builds result */
2079 2080 2081 2082 2083

	/*
	 * notifies only come back as result of a query, so I send an empty
	 * query
	 */
2084
	Py_BEGIN_ALLOW_THREADS
2085
		result = PQexec(self->cnx, " ");
2086
	Py_END_ALLOW_THREADS
Bruce Momjian's avatar
Bruce Momjian committed
2087

2088
		if ((notify = PQnotifies(self->cnx)) != NULL)
Bruce Momjian's avatar
Bruce Momjian committed
2089
	{
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
		if ((notify_result = PyTuple_New(2)) == NULL)
		{
			PQclear(result);
			return NULL;
		}

		if ((temp = PyString_FromString(notify->relname)) == NULL)
		{
			PQclear(result);
			return NULL;
		}

		PyTuple_SET_ITEM(notify_result, 0, temp);

		if ((temp = PyInt_FromLong(notify->be_pid)) == NULL)
		{
			PQclear(result);
			Py_DECREF(notify_result);
			return NULL;
		}

		PyTuple_SET_ITEM(notify_result, 1, temp);
Bruce Momjian's avatar
Bruce Momjian committed
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125
		free(notify);
	}
	else
	{
		Py_INCREF(Py_None);
		notify_result = Py_None;
	}

	PQclear(result);

	/* returns result */
	return notify_result;
}

2126 2127
/* source creation */
static char pg_source__doc__[] =
2128
"source() -- creates a new source object for this connection";
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144

static PyObject *
pg_source(pgobject * self, PyObject * args)
{
	/* checks validity */
	if (!check_cnx_obj(self))
		return NULL;

	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_TypeError, "method source() takes no parameter.");
		return NULL;
	}

	/* allocate new pg query object */
2145
	return (PyObject *) pgsource_new(self);
2146 2147
}

Bruce Momjian's avatar
Bruce Momjian committed
2148
/* database query */
2149
static char pg_query__doc__[] =
2150 2151
"query(sql) -- creates a new query object for this connection,"
" using sql (string) request.";
2152

Bruce Momjian's avatar
Bruce Momjian committed
2153 2154 2155
static PyObject *
pg_query(pgobject * self, PyObject * args)
{
2156 2157 2158 2159
	char	   *query;
	PGresult   *result;
	pgqueryobject *npgobj;
	int			status;
Bruce Momjian's avatar
Bruce Momjian committed
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173

	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;
	}

2174 2175 2176 2177 2178 2179 2180
	/* frees previous result */
	if (self->last_result)
	{
		PQclear(self->last_result);
		self->last_result = NULL;
	}

Bruce Momjian's avatar
Bruce Momjian committed
2181
	/* gets result */
2182
	Py_BEGIN_ALLOW_THREADS
2183
		result = PQexec(self->cnx, query);
2184
	Py_END_ALLOW_THREADS
Bruce Momjian's avatar
Bruce Momjian committed
2185 2186

	/* checks result validity */
2187
		if (!result)
Bruce Momjian's avatar
Bruce Momjian committed
2188 2189 2190 2191 2192 2193 2194 2195
	{
		PyErr_SetString(PyExc_ValueError, PQerrorMessage(self->cnx));
		return NULL;
	}

	/* checks result status */
	if ((status = PQresultStatus(result)) != PGRES_TUPLES_OK)
	{
2196
		Oid			oid;
Bruce Momjian's avatar
Bruce Momjian committed
2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209

		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;
2210
			case PGRES_COMMAND_OK:		/* could be an INSERT */
2211
				if ((oid = PQoidValue(result)) == InvalidOid)	/* nope */
Bruce Momjian's avatar
Bruce Momjian committed
2212 2213 2214 2215 2216 2217
				{
					Py_INCREF(Py_None);
					return Py_None;
				}

				/* otherwise, return the oid */
2218
				return PyInt_FromLong(oid);
Bruce Momjian's avatar
Bruce Momjian committed
2219

2220
			case PGRES_COPY_OUT:		/* no data will be received */
Bruce Momjian's avatar
Bruce Momjian committed
2221 2222 2223 2224 2225
			case PGRES_COPY_IN:
				Py_INCREF(Py_None);
				return Py_None;
			default:
				PyErr_SetString(PGError, "internal error: "
2226
								"unknown result status.");
Bruce Momjian's avatar
Bruce Momjian committed
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
				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_ACCESS
2242
static char pg_putline__doc__[] =
2243
"putline() -- sends a line directly to the backend";
2244

Bruce Momjian's avatar
Bruce Momjian committed
2245 2246 2247 2248
/* direct acces function : putline */
static PyObject *
pg_putline(pgobject * self, PyObject * args)
{
2249
	char	   *line;
Bruce Momjian's avatar
Bruce Momjian committed
2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270

	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 */
2271
static char pg_getline__doc__[] =
2272
"getline() -- gets a line directly from the backend.";
2273

Bruce Momjian's avatar
Bruce Momjian committed
2274 2275 2276
static PyObject *
pg_getline(pgobject * self, PyObject * args)
{
Bruce Momjian's avatar
Bruce Momjian committed
2277
	char		line[MAX_BUFFER_SIZE];
2278
	PyObject   *str = NULL;		/* GCC */
Bruce Momjian's avatar
Bruce Momjian committed
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289

	if (!self->cnx)
	{
		PyErr_SetString(PyExc_TypeError, "Connection is not valid");
		return NULL;
	}

	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
2290
						"method getline() takes no parameters.");
Bruce Momjian's avatar
Bruce Momjian committed
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
		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 */
2314
static char pg_endcopy__doc__[] =
2315
"endcopy() -- synchronizes client and server";
2316

Bruce Momjian's avatar
Bruce Momjian committed
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
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,
2330
						"method endcopy() takes no parameters.");
Bruce Momjian's avatar
Bruce Momjian committed
2331 2332 2333 2334 2335 2336 2337 2338
		return NULL;
	}

	/* ends direct copy */
	PQendcopy(self->cnx);
	Py_INCREF(Py_None);
	return Py_None;
}
2339 2340

#endif	 /* DIRECT_ACCESS */
Bruce Momjian's avatar
Bruce Momjian committed
2341 2342 2343


static PyObject *
2344
pgquery_print(pgqueryobject * self, FILE *fp, int flags)
Bruce Momjian's avatar
Bruce Momjian committed
2345
{
2346
	PQprintOpt	op;
Bruce Momjian's avatar
Bruce Momjian committed
2347 2348

	memset(&op, 0, sizeof(op));
Bruce Momjian's avatar
Bruce Momjian committed
2349
	op.align = 1;
Bruce Momjian's avatar
Bruce Momjian committed
2350 2351 2352 2353 2354 2355 2356
	op.header = 1;
	op.fieldSep = "|";
	op.pager = 1;
	PQprint(fp, self->last_result, &op);
	return 0;
}

2357
static PyObject *
2358
pgquery_repr(pgqueryobject * self)
2359 2360 2361 2362
{
	return PyString_FromString("<pg query result>");
}

Bruce Momjian's avatar
Bruce Momjian committed
2363
/* insert table */
Bruce Momjian's avatar
Bruce Momjian committed
2364
static char pg_inserttable__doc__[] =
2365 2366
"inserttable(string, list) -- insert list in table. The fields in the "
"list must be in the same order as in the table.";
2367

Bruce Momjian's avatar
Bruce Momjian committed
2368 2369 2370
static PyObject *
pg_inserttable(pgobject * self, PyObject * args)
{
2371 2372 2373 2374
	PGresult   *result;
	char	   *table,
			   *buffer,
			   *temp;
Bruce Momjian's avatar
Bruce Momjian committed
2375
	char		temp_buffer[256];
2376 2377 2378 2379 2380 2381 2382
	PyObject   *list,
			   *sublist,
			   *item;
	PyObject   *(*getitem) (PyObject *, int);
	PyObject   *(*getsubitem) (PyObject *, int);
	int			i,
				j;
Bruce Momjian's avatar
Bruce Momjian committed
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393

	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,
2394 2395
					  "tableinsert(table, content), with table (string) "
						"and content (list).");
Bruce Momjian's avatar
Bruce Momjian committed
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
		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,
2407
						"second arg must be some kind of array.");
Bruce Momjian's avatar
Bruce Momjian committed
2408 2409 2410 2411 2412 2413 2414 2415 2416
		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,
2417
						 "second arg must contain some kind of arrays.");
Bruce Momjian's avatar
Bruce Momjian committed
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
			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);

2432
	Py_BEGIN_ALLOW_THREADS
2433
		result = PQexec(self->cnx, buffer);
2434 2435
	Py_END_ALLOW_THREADS

2436
		if (!result)
Bruce Momjian's avatar
Bruce Momjian committed
2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
	{
		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))
2460
				temp = PyString_AS_STRING(item);
Bruce Momjian's avatar
Bruce Momjian committed
2461 2462
			else if (PyInt_Check(item))
			{
2463
				long		k;
Bruce Momjian's avatar
Bruce Momjian committed
2464

2465 2466
				k = PyInt_AsLong(item);
				sprintf(temp_buffer, "%ld", k);
Bruce Momjian's avatar
Bruce Momjian committed
2467 2468 2469 2470
				temp = temp_buffer;
			}
			else if (PyLong_Check(item))
			{
2471
				long		k;
Bruce Momjian's avatar
Bruce Momjian committed
2472

2473
				k = PyLong_AsLong(item);
Bruce Momjian's avatar
Bruce Momjian committed
2474 2475 2476 2477 2478
				sprintf(temp_buffer, "%ld", k);
				temp = temp_buffer;
			}
			else if (PyFloat_Check(item))
			{
2479
				double		k;
Bruce Momjian's avatar
Bruce Momjian committed
2480

2481
				k = PyFloat_AS_DOUBLE(item);
Bruce Momjian's avatar
Bruce Momjian committed
2482 2483 2484 2485 2486 2487 2488
				sprintf(temp_buffer, "%g", k);
				temp = temp_buffer;
			}
			else
			{
				free(buffer);
				PyErr_SetString(PyExc_ValueError,
2489 2490
								"items must be strings, integers, "
								"longs or double (real).");
Bruce Momjian's avatar
Bruce Momjian committed
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
				return NULL;
			}

			/* concats buffer */
			if (strlen(buffer))
				strncat(buffer, "\t", MAX_BUFFER_SIZE - strlen(buffer));

			strncat(buffer, temp, MAX_BUFFER_SIZE - strlen(buffer));
		}

		strncat(buffer, "\n", MAX_BUFFER_SIZE - strlen(buffer));

		/* sends data */
		PQputline(self->cnx, buffer);
	}

	/* ends query */
2508
	PQputline(self->cnx, "\\.\n");
Bruce Momjian's avatar
Bruce Momjian committed
2509 2510 2511 2512 2513 2514 2515 2516
	PQendcopy(self->cnx);
	free(buffer);

	/* no error : returns nothing */
	Py_INCREF(Py_None);
	return Py_None;
}

2517

2518
/* creates large object */
Bruce Momjian's avatar
Bruce Momjian committed
2519
static char pg_locreate__doc__[] =
2520
"locreate() -- creates a new large object in the database.";
2521

Bruce Momjian's avatar
Bruce Momjian committed
2522
static PyObject *
2523 2524
pg_locreate(pgobject * self, PyObject * args)
{
2525 2526
	int			mode;
	Oid			lo_oid;
2527 2528 2529

	/* checks validity */
	if (!check_cnx_obj(self))
Bruce Momjian's avatar
Bruce Momjian committed
2530
		return NULL;
2531 2532 2533 2534 2535

	/* gets arguments */
	if (!PyArg_ParseTuple(args, "i", &mode))
	{
		PyErr_SetString(PyExc_TypeError,
2536
						"locreate(mode), with mode (integer).");
2537 2538 2539 2540 2541
		return NULL;
	}

	/* creates large object */
	lo_oid = lo_creat(self->cnx, mode);
Bruce Momjian's avatar
Bruce Momjian committed
2542 2543
	if (lo_oid == 0)
	{
2544 2545
		PyErr_SetString(PGError, "can't create large object.");
		return NULL;
Bruce Momjian's avatar
Bruce Momjian committed
2546
	}
2547 2548 2549 2550 2551

	return (PyObject *) pglarge_new(self, lo_oid);
}

/* init from already known oid */
Bruce Momjian's avatar
Bruce Momjian committed
2552
static char pg_getlo__doc__[] =
2553
"getlo(long) -- create a large object instance for the specified oid.";
2554 2555 2556 2557

static PyObject *
pg_getlo(pgobject * self, PyObject * args)
{
2558
	int			lo_oid;
2559 2560 2561

	/* checks validity */
	if (!check_cnx_obj(self))
Bruce Momjian's avatar
Bruce Momjian committed
2562
		return NULL;
2563 2564 2565 2566

	/* gets arguments */
	if (!PyArg_ParseTuple(args, "i", &lo_oid))
	{
2567
		PyErr_SetString(PyExc_TypeError, "getlo(oid), with oid (integer).");
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
		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 */
Bruce Momjian's avatar
Bruce Momjian committed
2582
static char pg_loimport__doc__[] =
2583
"loimport(string) -- create a new large object from specified file.";
2584 2585 2586 2587

static PyObject *
pg_loimport(pgobject * self, PyObject * args)
{
2588 2589
	char	   *name;
	Oid			lo_oid;
2590 2591 2592

	/* checks validity */
	if (!check_cnx_obj(self))
Bruce Momjian's avatar
Bruce Momjian committed
2593
		return NULL;
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613

	/* 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);
}


Bruce Momjian's avatar
Bruce Momjian committed
2614 2615
/* connection object methods */
static struct PyMethodDef pgobj_methods[] = {
2616 2617 2618 2619 2620 2621 2622
	{"source", (PyCFunction) pg_source, 1, pg_source__doc__},
	{"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__},
Bruce Momjian's avatar
Bruce Momjian committed
2623 2624

#ifdef DIRECT_ACCESS
2625 2626 2627 2628
	{"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 */
Bruce Momjian's avatar
Bruce Momjian committed
2629 2630

#ifdef LARGE_OBJECTS
2631 2632 2633 2634
	{"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 */
Bruce Momjian's avatar
Bruce Momjian committed
2635 2636 2637 2638 2639 2640 2641 2642

	{NULL, NULL}				/* sentinel */
};

/* get attribute */
static PyObject *
pg_getattr(pgobject * self, char *name)
{
2643 2644 2645 2646 2647

	/*
	 * Although we could check individually, there are only a few
	 * attributes that don't require a live connection and unless someone
	 * has an urgent need, this will have to do
Bruce Momjian's avatar
Bruce Momjian committed
2648
	 */
Bruce Momjian's avatar
Bruce Momjian committed
2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
	if (!self->cnx)
	{
		PyErr_SetString(PyExc_TypeError, "Connection is not valid");
		return NULL;
	}

	/* list postgreSQL connection fields */

	/* postmaster host */
	if (!strcmp(name, "host"))
	{
2660 2661
		char	   *r = PQhost(self->cnx);

Bruce Momjian's avatar
Bruce Momjian committed
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
		return r ? PyString_FromString(r) : PyString_FromString("localhost");
	}

	/* postmaster port */
	if (!strcmp(name, "port"))
		return PyInt_FromLong(atol(PQport(self->cnx)));

	/* selected database */
	if (!strcmp(name, "db"))
		return PyString_FromString(PQdb(self->cnx));

	/* selected options */
	if (!strcmp(name, "options"))
		return PyString_FromString(PQoptions(self->cnx));

	/* selected postgres tty */
	if (!strcmp(name, "tty"))
		return PyString_FromString(PQtty(self->cnx));

	/* error (status) message */
	if (!strcmp(name, "error"))
		return PyString_FromString(PQerrorMessage(self->cnx));

	/* connection status : 1 - OK, 0 - BAD */
	if (!strcmp(name, "status"))
		return PyInt_FromLong(PQstatus(self->cnx) == CONNECTION_OK ? 1 : 0);

	/* provided user name */
	if (!strcmp(name, "user"))
		return PyString_FromString("Deprecated facility");
2692
	/* return PyString_FromString(fe_getauthname("<unknown user>")); */
Bruce Momjian's avatar
Bruce Momjian committed
2693 2694 2695 2696

	/* attributes list */
	if (!strcmp(name, "__members__"))
	{
2697
		PyObject   *list = PyList_New(8);
Bruce Momjian's avatar
Bruce Momjian committed
2698 2699 2700

		if (list)
		{
2701 2702 2703 2704 2705 2706 2707 2708
			PyList_SET_ITEM(list, 0, PyString_FromString("host"));
			PyList_SET_ITEM(list, 1, PyString_FromString("port"));
			PyList_SET_ITEM(list, 2, PyString_FromString("db"));
			PyList_SET_ITEM(list, 3, PyString_FromString("options"));
			PyList_SET_ITEM(list, 4, PyString_FromString("tty"));
			PyList_SET_ITEM(list, 5, PyString_FromString("error"));
			PyList_SET_ITEM(list, 6, PyString_FromString("status"));
			PyList_SET_ITEM(list, 7, PyString_FromString("user"));
Bruce Momjian's avatar
Bruce Momjian committed
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739
		}

		return list;
	}

	return Py_FindMethod(pgobj_methods, (PyObject *) self, name);
}

/* object type definition */
staticforward PyTypeObject PgType = {
	PyObject_HEAD_INIT(NULL)
	0,							/* ob_size */
	"pgobject",					/* tp_name */
	sizeof(pgobject),			/* tp_basicsize */
	0,							/* tp_itemsize */
	/* methods */
	(destructor) pg_dealloc,	/* tp_dealloc */
	0,							/* tp_print */
	(getattrfunc) pg_getattr,	/* tp_getattr */
	0,							/* tp_setattr */
	0,							/* tp_compare */
	0,							/* tp_repr */
	0,							/* tp_as_number */
	0,							/* tp_as_sequence */
	0,							/* tp_as_mapping */
	0,							/* tp_hash */
};


/* query object methods */
static struct PyMethodDef pgquery_methods[] = {
2740 2741 2742 2743 2744 2745
	{"getresult", (PyCFunction) pgquery_getresult, 1, pgquery_getresult__doc__},
	{"dictresult", (PyCFunction) pgquery_dictresult, 1, pgquery_dictresult__doc__},
	{"fieldname", (PyCFunction) pgquery_fieldname, 1, pgquery_fieldname__doc__},
	{"fieldnum", (PyCFunction) pgquery_fieldnum, 1, pgquery_fieldnum__doc__},
	{"listfields", (PyCFunction) pgquery_listfields, 1, pgquery_listfields__doc__},
	{"ntuples", (PyCFunction) pgquery_ntuples, 1, pgquery_ntuples__doc__},
2746
	{NULL, NULL}
Bruce Momjian's avatar
Bruce Momjian committed
2747 2748
};

Bruce Momjian's avatar
Bruce Momjian committed
2749
/* gets query object attributes */
Bruce Momjian's avatar
Bruce Momjian committed
2750
static PyObject *
2751
pgquery_getattr(pgqueryobject * self, char *name)
Bruce Momjian's avatar
Bruce Momjian committed
2752 2753 2754 2755 2756 2757 2758 2759
{
	/* list postgreSQL connection fields */
	return Py_FindMethod(pgquery_methods, (PyObject *) self, name);
}

/* query type definition */
staticforward PyTypeObject PgQueryType = {
	PyObject_HEAD_INIT(NULL)
2760 2761 2762 2763
	0,							/* ob_size */
	"pgqueryobject",			/* tp_name */
	sizeof(pgqueryobject),		/* tp_basicsize */
	0,							/* tp_itemsize */
Bruce Momjian's avatar
Bruce Momjian committed
2764
	/* methods */
2765 2766 2767 2768 2769 2770 2771 2772 2773 2774
	(destructor) pgquery_dealloc,		/* tp_dealloc */
	(printfunc) pgquery_print,	/* tp_print */
	(getattrfunc) pgquery_getattr,		/* tp_getattr */
	0,							/* tp_setattr */
	0,							/* tp_compare */
	(reprfunc) pgquery_repr,	/* tp_repr */
	0,							/* tp_as_number */
	0,							/* tp_as_sequence */
	0,							/* tp_as_mapping */
	0,							/* tp_hash */
Bruce Momjian's avatar
Bruce Momjian committed
2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785
};



/* --------------------------------------------------------------------- */

/* MODULE FUNCTIONS */

#ifdef DEFAULT_VARS

/* gets default host */
2786
static char getdefhost__doc__[] =
2787
"get_defhost() -- return default database host.";
2788

Bruce Momjian's avatar
Bruce Momjian committed
2789
static PyObject *
2790
pggetdefhost(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
2791 2792 2793 2794 2795
{
	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
2796
						"method get_defhost() takes no parameter.");
Bruce Momjian's avatar
Bruce Momjian committed
2797 2798 2799 2800 2801 2802 2803 2804
		return NULL;
	}

	Py_XINCREF(pg_default_host);
	return pg_default_host;
}

/* sets default host */
2805
static char setdefhost__doc__[] =
2806
"set_defhost(string) -- set default database host. Return previous value.";
2807

Bruce Momjian's avatar
Bruce Momjian committed
2808
static PyObject *
2809
pgsetdefhost(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
2810
{
2811 2812
	char	   *temp = NULL;
	PyObject   *old;
Bruce Momjian's avatar
Bruce Momjian committed
2813 2814 2815 2816 2817

	/* gets arguments */
	if (!PyArg_ParseTuple(args, "z", &temp))
	{
		PyErr_SetString(PyExc_TypeError,
2818
						"set_defhost(name), with name (string/None).");
Bruce Momjian's avatar
Bruce Momjian committed
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836
		return NULL;
	}

	/* adjusts value */
	old = pg_default_host;

	if (temp)
		pg_default_host = PyString_FromString(temp);
	else
	{
		Py_INCREF(Py_None);
		pg_default_host = Py_None;
	}

	return old;
}

/* gets default base */
2837
static char getdefbase__doc__[] =
2838
"get_defbase() -- return default database name.";
2839

Bruce Momjian's avatar
Bruce Momjian committed
2840
static PyObject *
2841
pggetdefbase(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
2842 2843 2844 2845 2846
{
	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
2847
						"method get_defbase() takes no parameter.");
Bruce Momjian's avatar
Bruce Momjian committed
2848 2849 2850 2851 2852 2853 2854 2855
		return NULL;
	}

	Py_XINCREF(pg_default_base);
	return pg_default_base;
}

/* sets default base */
2856
static char setdefbase__doc__[] =
2857
"set_defbase(string) -- set default database name. Return previous value";
2858

Bruce Momjian's avatar
Bruce Momjian committed
2859
static PyObject *
2860
pgsetdefbase(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
2861
{
2862 2863
	char	   *temp = NULL;
	PyObject   *old;
Bruce Momjian's avatar
Bruce Momjian committed
2864 2865 2866 2867 2868

	/* gets arguments */
	if (!PyArg_ParseTuple(args, "z", &temp))
	{
		PyErr_SetString(PyExc_TypeError,
2869
						"set_defbase(name), with name (string/None).");
Bruce Momjian's avatar
Bruce Momjian committed
2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887
		return NULL;
	}

	/* adjusts value */
	old = pg_default_base;

	if (temp)
		pg_default_base = PyString_FromString(temp);
	else
	{
		Py_INCREF(Py_None);
		pg_default_base = Py_None;
	}

	return old;
}

/* gets default options */
2888
static char getdefopt__doc__[] =
2889
"get_defopt() -- return default database options.";
2890

Bruce Momjian's avatar
Bruce Momjian committed
2891
static PyObject *
2892
pggetdefopt(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
2893 2894 2895 2896 2897
{
	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
2898
						"method get_defopt() takes no parameter.");
Bruce Momjian's avatar
Bruce Momjian committed
2899 2900 2901 2902 2903 2904 2905 2906
		return NULL;
	}

	Py_XINCREF(pg_default_opt);
	return pg_default_opt;
}

/* sets default opt */
2907
static char setdefopt__doc__[] =
2908
"set_defopt(string) -- set default database options. Return previous value.";
2909

Bruce Momjian's avatar
Bruce Momjian committed
2910
static PyObject *
2911
pgsetdefopt(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
2912
{
2913 2914
	char	   *temp = NULL;
	PyObject   *old;
Bruce Momjian's avatar
Bruce Momjian committed
2915 2916 2917 2918 2919

	/* gets arguments */
	if (!PyArg_ParseTuple(args, "z", &temp))
	{
		PyErr_SetString(PyExc_TypeError,
2920
						"set_defopt(name), with name (string/None).");
Bruce Momjian's avatar
Bruce Momjian committed
2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938
		return NULL;
	}

	/* adjusts value */
	old = pg_default_opt;

	if (temp)
		pg_default_opt = PyString_FromString(temp);
	else
	{
		Py_INCREF(Py_None);
		pg_default_opt = Py_None;
	}

	return old;
}

/* gets default tty */
2939
static char getdeftty__doc__[] =
2940
"get_deftty() -- return default database debug terminal.";
2941

Bruce Momjian's avatar
Bruce Momjian committed
2942
static PyObject *
2943
pggetdeftty(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
2944 2945 2946 2947 2948
{
	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
2949
						"method get_deftty() takes no parameter.");
Bruce Momjian's avatar
Bruce Momjian committed
2950 2951 2952 2953 2954 2955 2956 2957
		return NULL;
	}

	Py_XINCREF(pg_default_tty);
	return pg_default_tty;
}

/* sets default tty */
2958
static char setdeftty__doc__[] =
2959 2960
"set_deftty(string) -- set default database debug terminal. "
"Return previous value.";
2961

Bruce Momjian's avatar
Bruce Momjian committed
2962
static PyObject *
2963
pgsetdeftty(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
2964
{
2965 2966
	char	   *temp = NULL;
	PyObject   *old;
Bruce Momjian's avatar
Bruce Momjian committed
2967 2968 2969 2970 2971

	/* gets arguments */
	if (!PyArg_ParseTuple(args, "z", &temp))
	{
		PyErr_SetString(PyExc_TypeError,
2972
						"set_deftty(name), with name (string/None).");
Bruce Momjian's avatar
Bruce Momjian committed
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990
		return NULL;
	}

	/* adjusts value */
	old = pg_default_tty;

	if (temp)
		pg_default_tty = PyString_FromString(temp);
	else
	{
		Py_INCREF(Py_None);
		pg_default_tty = Py_None;
	}

	return old;
}

/* gets default username */
2991
static char getdefuser__doc__[] =
2992
"get_defuser() -- return default database username.";
2993

Bruce Momjian's avatar
Bruce Momjian committed
2994
static PyObject *
2995
pggetdefuser(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
2996 2997 2998 2999 3000
{
	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
3001
						"method get_defuser() takes no parameter.");
Bruce Momjian's avatar
Bruce Momjian committed
3002 3003 3004 3005 3006 3007 3008 3009 3010

		return NULL;
	}

	Py_XINCREF(pg_default_user);
	return pg_default_user;
}

/* sets default username */
3011
static char setdefuser__doc__[] =
3012
"set_defuser() -- set default database username. Return previous value.";
3013

Bruce Momjian's avatar
Bruce Momjian committed
3014
static PyObject *
3015
pgsetdefuser(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
3016
{
3017 3018
	char	   *temp = NULL;
	PyObject   *old;
Bruce Momjian's avatar
Bruce Momjian committed
3019 3020 3021 3022 3023

	/* gets arguments */
	if (!PyArg_ParseTuple(args, "z", &temp))
	{
		PyErr_SetString(PyExc_TypeError,
3024
						"set_defuser(name), with name (string/None).");
Bruce Momjian's avatar
Bruce Momjian committed
3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
		return NULL;
	}

	/* adjusts value */
	old = pg_default_user;

	if (temp)
		pg_default_user = PyString_FromString(temp);
	else
	{
		Py_INCREF(Py_None);
		pg_default_user = Py_None;
	}

	return old;
}

/* sets default password */
3043
static char setdefpasswd__doc__[] =
3044
"set_defpasswd() -- set default database password.";
3045

Bruce Momjian's avatar
Bruce Momjian committed
3046
static PyObject *
3047
pgsetdefpasswd(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
3048
{
3049 3050
	char	   *temp = NULL;
	PyObject   *old;
Bruce Momjian's avatar
Bruce Momjian committed
3051 3052 3053 3054 3055

	/* gets arguments */
	if (!PyArg_ParseTuple(args, "z", &temp))
	{
		PyErr_SetString(PyExc_TypeError,
Bruce Momjian's avatar
Bruce Momjian committed
3056
				"set_defpasswd(password), with password (string/None).");
Bruce Momjian's avatar
Bruce Momjian committed
3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070
		return NULL;
	}

	/* adjusts value */
	old = pg_default_passwd;

	if (temp)
		pg_default_passwd = PyString_FromString(temp);
	else
	{
		Py_INCREF(Py_None);
		pg_default_passwd = Py_None;
	}

Bruce Momjian's avatar
Bruce Momjian committed
3071
	Py_INCREF(Py_None);
3072
	return Py_None;
Bruce Momjian's avatar
Bruce Momjian committed
3073 3074 3075
}

/* gets default port */
3076
static char getdefport__doc__[] =
3077
"get_defport() -- return default database port.";
3078

Bruce Momjian's avatar
Bruce Momjian committed
3079
static PyObject *
3080
pggetdefport(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
3081 3082 3083 3084 3085
{
	/* checks args */
	if (!PyArg_ParseTuple(args, ""))
	{
		PyErr_SetString(PyExc_SyntaxError,
3086
						"method get_defport() takes no parameter.");
Bruce Momjian's avatar
Bruce Momjian committed
3087 3088 3089 3090 3091 3092 3093 3094
		return NULL;
	}

	Py_XINCREF(pg_default_port);
	return pg_default_port;
}

/* sets default port */
3095
static char setdefport__doc__[] =
3096
"set_defport(integer) -- set default database port. Return previous value.";
3097

Bruce Momjian's avatar
Bruce Momjian committed
3098
static PyObject *
3099
pgsetdefport(PyObject * self, PyObject * args)
Bruce Momjian's avatar
Bruce Momjian committed
3100
{
Bruce Momjian's avatar
Bruce Momjian committed
3101
	long int	port = -2;
3102
	PyObject   *old;
Bruce Momjian's avatar
Bruce Momjian committed
3103 3104 3105 3106 3107

	/* gets arguments */
	if ((!PyArg_ParseTuple(args, "l", &port)) || (port < -1))
	{
		PyErr_SetString(PyExc_TypeError, "set_defport(port), with port "
3108
						"(positive integer/-1).");
Bruce Momjian's avatar
Bruce Momjian committed
3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
		return NULL;
	}

	/* adjusts value */
	old = pg_default_port;

	if (port != -1)
		pg_default_port = PyLong_FromLong(port);
	else
	{
		Py_INCREF(Py_None);
		pg_default_port = Py_None;
	}

	return old;
}

3126
#endif	 /* DEFAULT_VARS */
Bruce Momjian's avatar
Bruce Momjian committed
3127 3128 3129 3130

/* List of functions defined in the module */

static struct PyMethodDef pg_methods[] = {
3131
	{"connect", (PyCFunction) pgconnect, 3, connect__doc__},
Bruce Momjian's avatar
Bruce Momjian committed
3132 3133

#ifdef DEFAULT_VARS
3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147
	{"get_defhost", pggetdefhost, 1, getdefhost__doc__},
	{"set_defhost", pgsetdefhost, 1, setdefhost__doc__},
	{"get_defbase", pggetdefbase, 1, getdefbase__doc__},
	{"set_defbase", pgsetdefbase, 1, setdefbase__doc__},
	{"get_defopt", pggetdefopt, 1, getdefopt__doc__},
	{"set_defopt", pgsetdefopt, 1, setdefopt__doc__},
	{"get_deftty", pggetdeftty, 1, getdeftty__doc__},
	{"set_deftty", pgsetdeftty, 1, setdeftty__doc__},
	{"get_defport", pggetdefport, 1, getdefport__doc__},
	{"set_defport", pgsetdefport, 1, setdefport__doc__},
	{"get_defuser", pggetdefuser, 1, getdefuser__doc__},
	{"set_defuser", pgsetdefuser, 1, setdefuser__doc__},
	{"set_defpasswd", pgsetdefpasswd, 1, setdefpasswd__doc__},
#endif	 /* DEFAULT_VARS */
Bruce Momjian's avatar
Bruce Momjian committed
3148 3149 3150
	{NULL, NULL}				/* sentinel */
};

Bruce Momjian's avatar
Bruce Momjian committed
3151
static char pg__doc__[] = "Python interface to PostgreSQL DB";
Bruce Momjian's avatar
Bruce Momjian committed
3152 3153

/* Initialization function for the module */
D'Arcy J.M. Cain's avatar
D'Arcy J.M. Cain committed
3154
DL_EXPORT(void)
Bruce Momjian's avatar
Bruce Momjian committed
3155 3156
init_pg(void)
{
3157 3158 3159
	PyObject   *mod,
			   *dict,
			   *v;
Bruce Momjian's avatar
Bruce Momjian committed
3160 3161

	/* Initialize here because some WIN platforms get confused otherwise */
3162 3163
	PglargeType.ob_type = PgType.ob_type = PgQueryType.ob_type =
		PgSourceType.ob_type = &PyType_Type;
Bruce Momjian's avatar
Bruce Momjian committed
3164 3165 3166 3167 3168 3169

	/* Create the module and add the functions */
	mod = Py_InitModule4("_pg", pg_methods, pg__doc__, NULL, PYTHON_API_VERSION);
	dict = PyModule_GetDict(mod);

	/* Add some symbolic constants to the module */
3170
	PGError = PyString_FromString("_pg.error");
Bruce Momjian's avatar
Bruce Momjian committed
3171 3172 3173 3174 3175 3176 3177 3178
	PyDict_SetItemString(dict, "error", PGError);

	/* Make the version available */
	v = PyString_FromString(PyPgVersion);
	PyDict_SetItemString(dict, "version", v);
	PyDict_SetItemString(dict, "__version__", v);
	Py_DECREF(v);

3179 3180 3181 3182 3183 3184
	/* results type for queries */
	PyDict_SetItemString(dict, "RESULT_EMPTY", PyInt_FromLong(RESULT_EMPTY));
	PyDict_SetItemString(dict, "RESULT_DML", PyInt_FromLong(RESULT_DML));
	PyDict_SetItemString(dict, "RESULT_DDL", PyInt_FromLong(RESULT_DDL));
	PyDict_SetItemString(dict, "RESULT_DQL", PyInt_FromLong(RESULT_DQL));

Bruce Momjian's avatar
Bruce Momjian committed
3185 3186 3187 3188 3189 3190 3191 3192 3193
#ifdef LARGE_OBJECTS
	/* create mode for large objects */
	PyDict_SetItemString(dict, "INV_READ", PyInt_FromLong(INV_READ));
	PyDict_SetItemString(dict, "INV_WRITE", PyInt_FromLong(INV_WRITE));

	/* position flags for lo_lseek */
	PyDict_SetItemString(dict, "SEEK_SET", PyInt_FromLong(SEEK_SET));
	PyDict_SetItemString(dict, "SEEK_CUR", PyInt_FromLong(SEEK_CUR));
	PyDict_SetItemString(dict, "SEEK_END", PyInt_FromLong(SEEK_END));
3194
#endif	 /* LARGE_OBJECTS */
Bruce Momjian's avatar
Bruce Momjian committed
3195 3196 3197

#ifdef DEFAULT_VARS
	/* prepares default values */
3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212
	Py_INCREF(Py_None);
	pg_default_host = Py_None;
	Py_INCREF(Py_None);
	pg_default_base = Py_None;
	Py_INCREF(Py_None);
	pg_default_opt = Py_None;
	Py_INCREF(Py_None);
	pg_default_port = Py_None;
	Py_INCREF(Py_None);
	pg_default_tty = Py_None;
	Py_INCREF(Py_None);
	pg_default_user = Py_None;
	Py_INCREF(Py_None);
	pg_default_passwd = Py_None;
#endif	 /* DEFAULT_VARS */
Bruce Momjian's avatar
Bruce Momjian committed
3213 3214 3215 3216 3217

	/* Check for errors */
	if (PyErr_Occurred())
		Py_FatalError("can't initialize module _pg");
}