Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / psycopg2 / psycopg / cursor.h
1 /* cursor.h - definition for the psycopg cursor type
2  *
3  * Copyright (C) 2003 Federico Di Gregorio <fog@debian.org>
4  *
5  * This file is part of psycopg.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2,
10  * or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #ifndef PSYCOPG_CURSOR_H
23 #define PSYCOPG_CURSOR_H 1
24
25 #include <Python.h>
26 #include <libpq-fe.h>
27
28 #include "psycopg/connection.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 extern PyTypeObject cursorType;
35
36 typedef struct {
37     PyObject HEAD;
38
39     connectionObject *conn; /* connection owning the cursor */
40
41     int closed:1;            /* 1 if the cursor is closed */
42     int notuples:1;          /* 1 if the command was not a SELECT query */
43     int needsfetch:1;        /* 1 if a call to pq_fetch is pending */
44     
45     long int rowcount;       /* number of rows affected by last execute */
46     long int columns;        /* number of columns fetched from the db */
47     long int arraysize;      /* how many rows should fetchmany() return */
48     long int row;            /* the row counter for fetch*() operations */
49     long int mark;           /* transaction marker, copied from conn */
50
51     PyObject *description;   /* read-only attribute: sequence of 7-item
52                                 sequences.*/
53
54     /* postgres connection stuff */
55     PGresult   *pgres;     /* result of last query */
56     PyObject   *pgstatus;  /* last message from the server after an execute */
57     Oid         lastoid;   /* last oid from an insert or InvalidOid */
58
59     PyObject *casts;      /* an array (tuple) of typecast functions */
60     PyObject *caster;     /* the current typecaster object */
61     
62     PyObject *copyfile;   /* file-like used during COPY TO/FROM ops */
63     long int  copysize;   /* size of the copy buffer during COPY TO/FROM ops */
64 #define DEFAULT_COPYSIZE 16384
65     
66     PyObject *tuple_factory;    /* factory for result tuples */
67     PyObject *tzinfo_factory;   /* factory for tzinfo objects */
68     
69     PyObject *query;      /* last query executed */
70     
71     char *qattr;          /* quoting attr, used when quoting strings */
72     char *notice;         /* a notice from the backend */
73     char *name;           /* this cursor name */
74     
75     PyObject *string_types;   /* a set of typecasters for string types */
76     PyObject *binary_types;   /* a set of typecasters for binary types */
77
78 } cursorObject;
79     
80 /* C-callable functions in cursor_int.c and cursor_ext.c */
81 extern void curs_reset(cursorObject *self);
82     
83 /* exception-raising macros */
84 #define EXC_IF_CURS_CLOSED(self) \
85 if ((self)->closed || ((self)->conn && (self)->conn->closed)) { \
86     PyErr_SetString(InterfaceError, "cursor already closed");   \
87     return NULL; }
88
89 #define EXC_IF_NO_TUPLES(self) \
90 if ((self)->notuples && (self)->name == NULL) {               \
91     PyErr_SetString(ProgrammingError, "no results to fetch"); \
92     return NULL; }
93     
94 #define EXC_IF_NO_MARK(self) \
95 if ((self)->mark != (self)->conn->mark) {                                  \
96     PyErr_SetString(ProgrammingError, "named cursor isn't valid anymore"); \
97     return NULL; }
98
99 #ifdef __cplusplus
100 }
101 #endif
102
103 #endif /* !defined(PSYCOPG_CURSOR_H) */