1
/* connection_type.c - python interface to connection objects
3
* Copyright (C) 2003 Federico Di Gregorio <fog@debian.org>
5
* This file is part of psycopg.
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.
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.
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.
23
#include <structmember.h>
24
#include <stringobject.h>
28
#define PSYCOPG_MODULE
29
#include "psycopg/config.h"
30
#include "psycopg/python.h"
31
#include "psycopg/psycopg.h"
32
#include "psycopg/connection.h"
33
#include "psycopg/cursor.h"
37
/* cursor method - allocate a new cursor */
39
#define psyco_conn_cursor_doc \
40
"cursor(cursor_factory=extensions.cursor) -- new cursor\n\n" \
41
"Return a new cursor.\n\nThe ``cursor_factory`` argument can be used to\n" \
42
"create non-standard cursors by passing a class different from the\n" \
43
"default. Note that the new class *should* be a sub-class of\n" \
44
"`extensions.cursor`.\n\n" \
45
":rtype: `extensions.cursor`"
48
psyco_conn_cursor(connectionObject *self, PyObject *args, PyObject *keywds)
51
PyObject *obj, *factory = NULL;
53
static char *kwlist[] = {"name", "cursor_factory", NULL};
55
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|sO", kwlist,
60
EXC_IF_CONN_CLOSED(self);
62
Dprintf("psyco_conn_cursor: new cursor for connection at %p", self);
63
Dprintf("psyco_conn_cursor: parameters: name = %s", name);
65
if (factory == NULL) factory = (PyObject *)&cursorType;
67
obj = PyObject_CallFunction(factory, "Os", self, name);
69
obj = PyObject_CallFunction(factory, "O", self);
71
if (obj == NULL) return NULL;
72
if (PyObject_IsInstance(obj, (PyObject *)&cursorType) == 0) {
73
PyErr_SetString(PyExc_TypeError,
74
"cursor factory must be subclass of psycopg2._psycopg.cursor");
79
Dprintf("psyco_conn_cursor: new cursor at %p: refcnt = %d",
85
/* close method - close the connection and all related cursors */
87
#define psyco_conn_close_doc "close() -- Close the connection."
90
psyco_conn_close(connectionObject *self, PyObject *args)
92
EXC_IF_CONN_CLOSED(self);
94
if (!PyArg_ParseTuple(args, "")) return NULL;
96
Dprintf("psyco_conn_close: closing connection at %p", self);
98
Dprintf("psyco_conn_close: connection at %p closed", self);
105
/* commit method - commit all changes to the database */
107
#define psyco_conn_commit_doc "commit() -- Commit all changes to database."
110
psyco_conn_commit(connectionObject *self, PyObject *args)
112
EXC_IF_CONN_CLOSED(self);
114
if (!PyArg_ParseTuple(args, "")) return NULL;
116
/* FIXME: check return status? */
124
/* rollback method - roll back all changes done to the database */
126
#define psyco_conn_rollback_doc \
127
"rollback() -- Roll back all changes done to database."
130
psyco_conn_rollback(connectionObject *self, PyObject *args)
132
EXC_IF_CONN_CLOSED(self);
134
if (!PyArg_ParseTuple(args, "")) return NULL;
136
/* FIXME: check return status? */
144
#ifdef PSYCOPG_EXTENSIONS
145
/* set_isolation_level method - switch connection isolation level */
147
#define psyco_conn_set_isolation_level_doc \
148
"set_isolation_level(level) -- Switch isolation level to ``level``."
151
psyco_conn_set_isolation_level(connectionObject *self, PyObject *args)
155
EXC_IF_CONN_CLOSED(self);
157
if (!PyArg_ParseTuple(args, "i", &level)) return NULL;
159
if (level < 0 || level > 2) {
160
PyErr_SetString(PyExc_ValueError,
161
"isolation level out of bounds (0,3)");
165
/* FIXME: check return status? */
166
conn_switch_isolation_level(self, level);
174
/* set_isolation_level method - switch connection isolation level */
176
#define psyco_conn_set_client_encoding_doc \
177
"set_client_encoding(encoding) -- Set client encoding to ``encoding``."
180
psyco_conn_set_client_encoding(connectionObject *self, PyObject *args)
184
EXC_IF_CONN_CLOSED(self);
186
if (!PyArg_ParseTuple(args, "s", &enc)) return NULL;
188
if (conn_set_client_encoding(self, enc) == 0) {
199
/** the connection object **/
202
/* object method list */
204
static struct PyMethodDef connectionObject_methods[] = {
205
{"cursor", (PyCFunction)psyco_conn_cursor,
206
METH_VARARGS|METH_KEYWORDS, psyco_conn_cursor_doc},
207
{"close", (PyCFunction)psyco_conn_close,
208
METH_VARARGS, psyco_conn_close_doc},
209
{"commit", (PyCFunction)psyco_conn_commit,
210
METH_VARARGS, psyco_conn_commit_doc},
211
{"rollback", (PyCFunction)psyco_conn_rollback,
212
METH_VARARGS, psyco_conn_rollback_doc},
213
#ifdef PSYCOPG_EXTENSIONS
214
{"set_isolation_level", (PyCFunction)psyco_conn_set_isolation_level,
215
METH_VARARGS, psyco_conn_set_isolation_level_doc},
216
{"set_client_encoding", (PyCFunction)psyco_conn_set_client_encoding,
217
METH_VARARGS, psyco_conn_set_client_encoding_doc},
222
/* object member list */
224
static struct PyMemberDef connectionObject_members[] = {
225
/* DBAPI-2.0 extensions (exception objects) */
227
offsetof(connectionObject, exc_Error), RO, Error_doc},
229
T_OBJECT, offsetof(connectionObject, exc_Warning), RO, Warning_doc},
230
{"InterfaceError", T_OBJECT,
231
offsetof(connectionObject, exc_InterfaceError), RO,
233
{"DatabaseError", T_OBJECT,
234
offsetof(connectionObject, exc_DatabaseError), RO, DatabaseError_doc},
235
{"InternalError", T_OBJECT,
236
offsetof(connectionObject, exc_InternalError), RO, InternalError_doc},
237
{"OperationalError", T_OBJECT,
238
offsetof(connectionObject, exc_OperationalError), RO,
239
OperationalError_doc},
240
{"ProgrammingError", T_OBJECT,
241
offsetof(connectionObject, exc_ProgrammingError), RO,
242
ProgrammingError_doc},
243
{"IntegrityError", T_OBJECT,
244
offsetof(connectionObject, exc_IntegrityError), RO,
246
{"DataError", T_OBJECT,
247
offsetof(connectionObject, exc_DataError), RO, DataError_doc},
248
{"NotSupportedError", T_OBJECT,
249
offsetof(connectionObject, exc_NotSupportedError), RO,
250
NotSupportedError_doc},
251
#ifdef PSYCOPG_EXTENSIONS
252
{"closed", T_LONG, offsetof(connectionObject, closed), RO,
253
"True if the connection is closed."},
254
{"isolation_level", T_LONG,
255
offsetof(connectionObject, isolation_level), RO,
256
"The current isolation level."},
257
{"encoding", T_STRING, offsetof(connectionObject, encoding), RO,
258
"The current client encoding."},
259
{"notices", T_OBJECT, offsetof(connectionObject, notice_list), RO},
260
{"notifies", T_OBJECT, offsetof(connectionObject, notifies), RO},
261
{"dsn", T_STRING, offsetof(connectionObject, dsn), RO,
262
"The current connection string."},
267
/* initialization and finalization methods */
270
connection_setup(connectionObject *self, char *dsn)
272
Dprintf("connection_setup: init connection object at %p, refcnt = %d",
273
self, ((PyObject *)self)->ob_refcnt);
275
self->dsn = strdup(dsn);
276
self->notice_list = PyList_New(0);
277
self->notifies = PyList_New(0);
279
self->status = CONN_STATUS_READY;
280
self->critical = NULL;
281
self->async_cursor = NULL;
285
pthread_mutex_init(&(self->lock), NULL);
287
if (conn_connect(self) != 0) {
288
pthread_mutex_destroy(&(self->lock));
289
Dprintf("connection_init: FAILED");
293
Dprintf("connection_setup: good connection object at %p, refcnt = %d",
294
self, ((PyObject *)self)->ob_refcnt);
299
connection_dealloc(PyObject* obj)
301
connectionObject *self = (connectionObject *)obj;
303
if (self->closed == 0) conn_close(self);
305
if (self->dsn) free(self->dsn);
306
if (self->encoding) PyMem_Free(self->encoding);
307
if (self->critical) free(self->critical);
309
Py_XDECREF(self->notice_list);
310
Py_XDECREF(self->notifies);
311
Py_XDECREF(self->async_cursor);
313
pthread_mutex_destroy(&(self->lock));
315
Dprintf("connection_dealloc: deleted connection object at %p, refcnt = %d",
316
obj, obj->ob_refcnt);
318
obj->ob_type->tp_free(obj);
322
connection_init(PyObject *obj, PyObject *args, PyObject *kwds)
326
if (!PyArg_ParseTuple(args, "s", &dsn))
329
return connection_setup((connectionObject *)obj, dsn);
333
connection_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
335
return type->tp_alloc(type, 0);
339
connection_del(PyObject* self)
345
connection_repr(connectionObject *self)
347
return PyString_FromFormat(
348
"<connection object at %p; dsn: '%s', closed: %ld>",
349
self, self->dsn, self->closed);
355
#define connectionType_doc \
356
"connection(dsn, ...) -> new connection object\n\n" \
358
" * `DBAPI-2.0 errors`: Error, Warning, InterfaceError,\n" \
359
" DatabaseError, InternalError, OperationalError,\n" \
360
" ProgrammingError, IntegrityError, DataError, NotSupportedError"
362
PyTypeObject connectionType = {
363
PyObject_HEAD_INIT(NULL)
365
"psycopg2._psycopg.connection",
366
sizeof(connectionObject),
368
connection_dealloc, /*tp_dealloc*/
373
(reprfunc)connection_repr, /*tp_repr*/
375
0, /*tp_as_sequence*/
380
(reprfunc)connection_repr, /*tp_str*/
385
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
386
connectionType_doc, /*tp_doc*/
391
0, /*tp_richcompare*/
392
0, /*tp_weaklistoffset*/
397
/* Attribute descriptor and subclassing stuff */
399
connectionObject_methods, /*tp_methods*/
400
connectionObject_members, /*tp_members*/
409
connection_init, /*tp_init*/
410
0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/
411
connection_new, /*tp_new*/
412
(freefunc)connection_del, /*tp_free Low-level free-memory routine */
413
0, /*tp_is_gc For PyObject_IS_GC */
415
0, /*tp_mro method resolution order */