2004-10-19 07:17:12 +04:00
|
|
|
/* connection.h - definition for the psycopg connection type
|
|
|
|
*
|
2010-02-13 01:34:53 +03:00
|
|
|
* Copyright (C) 2003-2010 Federico Di Gregorio <fog@debian.org>
|
2004-10-19 07:17:12 +04:00
|
|
|
*
|
|
|
|
* This file is part of psycopg.
|
|
|
|
*
|
2010-02-13 01:34:53 +03:00
|
|
|
* psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2004-10-19 07:17:12 +04:00
|
|
|
*
|
2010-02-13 01:34:53 +03:00
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link this program with the OpenSSL library (or with
|
|
|
|
* modified versions of OpenSSL that use the same license as OpenSSL),
|
|
|
|
* and distribute linked combinations including the two.
|
2004-10-19 07:17:12 +04:00
|
|
|
*
|
2010-02-13 01:34:53 +03:00
|
|
|
* You must obey the GNU Lesser General Public License in all respects for
|
|
|
|
* all of the code used other than OpenSSL.
|
|
|
|
*
|
|
|
|
* psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
* License for more details.
|
2004-10-19 07:17:12 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PSYCOPG_CONNECTION_H
|
|
|
|
#define PSYCOPG_CONNECTION_H 1
|
|
|
|
|
2010-10-08 13:15:50 +04:00
|
|
|
#include "psycopg/xid.h"
|
2008-01-21 08:54:01 +03:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* connection status */
|
2010-10-09 01:03:06 +04:00
|
|
|
#define CONN_STATUS_SETUP 0
|
|
|
|
#define CONN_STATUS_READY 1
|
|
|
|
#define CONN_STATUS_BEGIN 2
|
|
|
|
#define CONN_STATUS_PREPARED 5
|
2010-04-22 17:46:19 +04:00
|
|
|
/* async connection building statuses */
|
|
|
|
#define CONN_STATUS_CONNECTING 20
|
|
|
|
#define CONN_STATUS_DATESTYLE 21
|
|
|
|
|
2010-03-26 06:01:33 +03:00
|
|
|
/* async query execution status */
|
2010-04-11 05:48:33 +04:00
|
|
|
#define ASYNC_DONE 0
|
2010-03-26 06:01:33 +03:00
|
|
|
#define ASYNC_READ 1
|
|
|
|
#define ASYNC_WRITE 2
|
|
|
|
|
2010-04-05 18:52:25 +04:00
|
|
|
/* polling result */
|
|
|
|
#define PSYCO_POLL_OK 0
|
2010-04-05 13:30:03 +04:00
|
|
|
#define PSYCO_POLL_READ 1
|
|
|
|
#define PSYCO_POLL_WRITE 2
|
2010-04-02 04:59:31 +04:00
|
|
|
#define PSYCO_POLL_ERROR 3
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2007-11-09 05:28:47 +03:00
|
|
|
/* Hard limit on the notices stored by the Python connection */
|
|
|
|
#define CONN_NOTICES_LIMIT 50
|
|
|
|
|
2010-04-05 13:30:03 +04:00
|
|
|
/* we need the initial date style to be ISO, for typecasters; if the user
|
|
|
|
later change it, she must know what she's doing... these are the queries we
|
|
|
|
need to issue */
|
|
|
|
#define psyco_datestyle "SET DATESTYLE TO 'ISO'"
|
|
|
|
#define psyco_transaction_isolation "SHOW default_transaction_isolation"
|
|
|
|
|
2010-11-18 03:20:39 +03:00
|
|
|
/* possible values for isolation_level */
|
|
|
|
typedef enum {
|
|
|
|
ISOLATION_LEVEL_AUTOCOMMIT = 0,
|
|
|
|
ISOLATION_LEVEL_READ_COMMITTED = 1,
|
|
|
|
ISOLATION_LEVEL_SERIALIZABLE = 2,
|
|
|
|
} conn_isolation_level_t;
|
|
|
|
|
2008-01-21 08:54:01 +03:00
|
|
|
extern HIDDEN PyTypeObject connectionType;
|
2004-10-19 07:17:12 +04:00
|
|
|
|
2009-04-04 21:17:40 +04:00
|
|
|
struct connectionObject_notice {
|
|
|
|
struct connectionObject_notice *next;
|
|
|
|
const char *message;
|
|
|
|
};
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
typedef struct {
|
2006-10-29 06:12:51 +03:00
|
|
|
PyObject_HEAD
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
pthread_mutex_t lock; /* the global connection lock */
|
|
|
|
|
|
|
|
char *dsn; /* data source name */
|
|
|
|
char *critical; /* critical error on this connection */
|
|
|
|
char *encoding; /* current backend encoding */
|
2010-12-18 08:02:11 +03:00
|
|
|
char *codec; /* python codec name for encoding */
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2008-05-27 19:40:19 +04:00
|
|
|
long int closed; /* 1 means connection has been closed;
|
|
|
|
2 that something horrible happened */
|
2004-10-19 07:17:12 +04:00
|
|
|
long int isolation_level; /* isolation level for this connection */
|
2007-04-10 10:36:18 +04:00
|
|
|
long int mark; /* number of commits/rollbacks done so far */
|
2004-10-19 07:17:12 +04:00
|
|
|
int status; /* status of the connection */
|
2010-10-08 13:15:50 +04:00
|
|
|
XidObject *tpc_xid; /* Transaction ID in two-phase commit */
|
2010-04-05 13:30:03 +04:00
|
|
|
|
|
|
|
long int async; /* 1 means the connection is async */
|
2004-10-19 07:17:12 +04:00
|
|
|
int protocol; /* protocol version */
|
2009-04-19 18:36:10 +04:00
|
|
|
int server_version; /* server version */
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2010-04-05 13:30:03 +04:00
|
|
|
PGconn *pgconn; /* the postgresql connection */
|
2010-07-25 01:01:27 +04:00
|
|
|
PGcancel *cancel; /* the cancellation structure */
|
2004-10-19 07:17:12 +04:00
|
|
|
|
2010-04-05 13:30:03 +04:00
|
|
|
PyObject *async_cursor; /* a cursor executing an asynchronous query */
|
2010-03-26 06:01:33 +03:00
|
|
|
int async_status; /* asynchronous execution status */
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
/* notice processing */
|
|
|
|
PyObject *notice_list;
|
|
|
|
PyObject *notice_filter;
|
2009-04-04 21:17:40 +04:00
|
|
|
struct connectionObject_notice *notice_pending;
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
/* notifies */
|
|
|
|
PyObject *notifies;
|
|
|
|
|
2007-02-22 18:16:54 +03:00
|
|
|
/* per-connection typecasters */
|
|
|
|
PyObject *string_types; /* a set of typecasters for string types */
|
|
|
|
PyObject *binary_types; /* a set of typecasters for binary types */
|
|
|
|
|
2007-11-11 11:53:44 +03:00
|
|
|
int equote; /* use E''-style quotes for escaped strings */
|
2010-10-08 13:15:50 +04:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
} connectionObject;
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
/* C-callable functions in connection_int.c and connection_ext.c */
|
2010-04-05 13:30:03 +04:00
|
|
|
HIDDEN int conn_get_standard_conforming_strings(PGconn *pgconn);
|
|
|
|
HIDDEN int conn_get_isolation_level(PGresult *pgres);
|
|
|
|
HIDDEN int conn_get_protocol_version(PGconn *pgconn);
|
2009-04-04 21:17:40 +04:00
|
|
|
HIDDEN void conn_notice_process(connectionObject *self);
|
|
|
|
HIDDEN void conn_notice_clean(connectionObject *self);
|
2010-04-20 21:17:27 +04:00
|
|
|
HIDDEN void conn_notifies_process(connectionObject *self);
|
2009-08-09 18:19:08 +04:00
|
|
|
HIDDEN int conn_setup(connectionObject *self, PGconn *pgconn);
|
2010-04-05 13:30:03 +04:00
|
|
|
HIDDEN int conn_connect(connectionObject *self, long int async);
|
2008-01-21 08:54:01 +03:00
|
|
|
HIDDEN void conn_close(connectionObject *self);
|
|
|
|
HIDDEN int conn_commit(connectionObject *self);
|
|
|
|
HIDDEN int conn_rollback(connectionObject *self);
|
|
|
|
HIDDEN int conn_switch_isolation_level(connectionObject *self, int level);
|
2008-01-21 20:34:08 +03:00
|
|
|
HIDDEN int conn_set_client_encoding(connectionObject *self, const char *enc);
|
2010-04-22 22:59:00 +04:00
|
|
|
HIDDEN int conn_poll(connectionObject *self);
|
2010-10-08 13:15:50 +04:00
|
|
|
HIDDEN int conn_tpc_begin(connectionObject *self, XidObject *xid);
|
2010-10-09 01:03:06 +04:00
|
|
|
HIDDEN int conn_tpc_command(connectionObject *self,
|
|
|
|
const char *cmd, XidObject *xid);
|
2010-10-11 16:03:37 +04:00
|
|
|
HIDDEN PyObject *conn_tpc_recover(connectionObject *self);
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
/* exception-raising macros */
|
|
|
|
#define EXC_IF_CONN_CLOSED(self) if ((self)->closed > 0) { \
|
|
|
|
PyErr_SetString(InterfaceError, "connection already closed"); \
|
|
|
|
return NULL; }
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2010-03-31 03:50:21 +04:00
|
|
|
#define EXC_IF_CONN_ASYNC(self, cmd) if ((self)->async == 1) { \
|
|
|
|
PyErr_SetString(ProgrammingError, #cmd " cannot be used " \
|
|
|
|
"in asynchronous mode"); \
|
|
|
|
return NULL; }
|
|
|
|
|
2010-10-13 03:28:42 +04:00
|
|
|
#define EXC_IF_TPC_NOT_SUPPORTED(self) \
|
|
|
|
if ((self)->server_version < 80100) { \
|
|
|
|
PyErr_Format(NotSupportedError, \
|
|
|
|
"server version %d: " \
|
|
|
|
"two-phase transactions not supported", \
|
|
|
|
(self)->server_version); \
|
|
|
|
return NULL; \
|
|
|
|
}
|
|
|
|
|
2010-10-08 18:16:17 +04:00
|
|
|
#define EXC_IF_TPC_BEGIN(self, cmd) if ((self)->tpc_xid) { \
|
|
|
|
PyErr_Format(ProgrammingError, "%s cannot be used " \
|
|
|
|
"during a two-phase transaction", #cmd); \
|
|
|
|
return NULL; }
|
|
|
|
|
2010-10-09 01:03:06 +04:00
|
|
|
#define EXC_IF_TPC_PREPARED(self, cmd) \
|
|
|
|
if ((self)->status == CONN_STATUS_PREPARED) { \
|
|
|
|
PyErr_Format(ProgrammingError, "%s cannot be used " \
|
|
|
|
"with a prepared two-phase transaction", #cmd); \
|
|
|
|
return NULL; }
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* !defined(PSYCOPG_CONNECTION_H) */
|