Skip to content

Commit c60682c

Browse files
committed
Reuse set_session to implement autocommit, set_isolation_level
1 parent 8527144 commit c60682c

File tree

3 files changed

+43
-63
lines changed

3 files changed

+43
-63
lines changed

psycopg/connection.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,8 @@ HIDDEN void conn_close(connectionObject *self);
166166
HIDDEN void conn_close_locked(connectionObject *self);
167167
RAISES_NEG HIDDEN int conn_commit(connectionObject *self);
168168
RAISES_NEG HIDDEN int conn_rollback(connectionObject *self);
169-
HIDDEN int conn_set_autocommit(connectionObject *self, int value);
170169
RAISES_NEG HIDDEN int conn_set_session(connectionObject *self, int autocommit,
171170
int isolevel, int readonly, int deferrable);
172-
RAISES_NEG HIDDEN int conn_switch_isolation_level(connectionObject *self, int level);
173171
RAISES_NEG HIDDEN int conn_set_client_encoding(connectionObject *self, const char *enc);
174172
HIDDEN int conn_poll(connectionObject *self);
175173
RAISES_NEG HIDDEN int conn_tpc_begin(connectionObject *self, xidObject *xid);

psycopg/connection_int.c

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,68 +1180,36 @@ conn_rollback(connectionObject *self)
11801180
return res;
11811181
}
11821182

1183-
int
1184-
conn_set_autocommit(connectionObject *self, int value)
1185-
{
1186-
Py_BEGIN_ALLOW_THREADS;
1187-
pthread_mutex_lock(&self->lock);
1188-
1189-
self->autocommit = value;
1190-
1191-
pthread_mutex_unlock(&self->lock);
1192-
Py_END_ALLOW_THREADS;
1193-
1194-
return 0;
1195-
}
1196-
1197-
1198-
/* Promote an isolation level to one of the levels supported by the server */
1199-
1200-
static int
1201-
_adjust_isolevel(connectionObject *self, int level) {
1202-
if (self->server_version < 80000) {
1203-
if (level == ISOLATION_LEVEL_READ_UNCOMMITTED) {
1204-
level = ISOLATION_LEVEL_READ_COMMITTED;
1205-
}
1206-
else if (level == ISOLATION_LEVEL_REPEATABLE_READ) {
1207-
level = ISOLATION_LEVEL_SERIALIZABLE;
1208-
}
1209-
}
1210-
return level;
1211-
}
1212-
12131183

12141184
/* Change the state of the session */
12151185
RAISES_NEG int
12161186
conn_set_session(connectionObject *self, int autocommit,
12171187
int isolevel, int readonly, int deferrable)
12181188
{
1219-
isolevel = _adjust_isolevel(self, isolevel);
1189+
/* Promote an isolation level to one of the levels supported by the server */
1190+
if (self->server_version < 80000) {
1191+
if (isolevel == ISOLATION_LEVEL_READ_UNCOMMITTED) {
1192+
isolevel = ISOLATION_LEVEL_READ_COMMITTED;
1193+
}
1194+
else if (isolevel == ISOLATION_LEVEL_REPEATABLE_READ) {
1195+
isolevel = ISOLATION_LEVEL_SERIALIZABLE;
1196+
}
1197+
}
1198+
1199+
Py_BEGIN_ALLOW_THREADS;
1200+
pthread_mutex_lock(&self->lock);
12201201

12211202
self->isolevel = isolevel;
12221203
self->readonly = readonly;
12231204
self->deferrable = deferrable;
12241205
self->autocommit = autocommit;
12251206

1226-
return 0;
1227-
}
1228-
1229-
1230-
/* conn_switch_isolation_level - switch isolation level on the connection */
1231-
1232-
RAISES_NEG int
1233-
conn_switch_isolation_level(connectionObject *self, int level)
1234-
{
1235-
if (level == 0) {
1236-
self->autocommit = 1;
1237-
}
1238-
else {
1239-
level = _adjust_isolevel(self, level);
1240-
self->isolevel = level;
1241-
self->autocommit = 0;
1242-
}
1207+
pthread_mutex_unlock(&self->lock);
1208+
Py_END_ALLOW_THREADS;
12431209

1244-
Dprintf("conn_switch_isolation_level: switched to level %d", level);
1210+
Dprintf(
1211+
"conn_set_session: autocommit %d, isolevel %d, readonly %d, deferrable %d",
1212+
autocommit, isolevel, readonly, deferrable);
12451213

12461214
return 0;
12471215
}

psycopg/connection_type.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,14 @@ _psyco_conn_parse_onoff(PyObject *pyval)
531531
return rv;
532532
}
533533

534+
#define _set_session_checks(self,what) \
535+
do { \
536+
EXC_IF_CONN_CLOSED(self); \
537+
EXC_IF_CONN_ASYNC(self, what); \
538+
EXC_IF_IN_TRANSACTION(self, what); \
539+
EXC_IF_TPC_PREPARED(self, what); \
540+
} while(0)
541+
534542
/* set_session - set default transaction characteristics */
535543

536544
#define psyco_conn_set_session_doc \
@@ -553,9 +561,7 @@ psyco_conn_set_session(connectionObject *self, PyObject *args, PyObject *kwargs)
553561
static char *kwlist[] =
554562
{"isolation_level", "readonly", "deferrable", "autocommit", NULL};
555563

556-
EXC_IF_CONN_CLOSED(self);
557-
EXC_IF_CONN_ASYNC(self, set_session);
558-
EXC_IF_IN_TRANSACTION(self, set_session);
564+
_set_session_checks(self, set_session);
559565

560566
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOOO", kwlist,
561567
&isolevel, &readonly, &deferrable, &autocommit)) {
@@ -615,9 +621,7 @@ _psyco_conn_autocommit_set_checks(connectionObject *self)
615621
{
616622
/* wrapper to use the EXC_IF macros.
617623
* return NULL in case of error, else whatever */
618-
EXC_IF_CONN_CLOSED(self);
619-
EXC_IF_CONN_ASYNC(self, autocommit);
620-
EXC_IF_IN_TRANSACTION(self, autocommit);
624+
_set_session_checks(self, autocommit);
621625
return Py_None; /* borrowed */
622626
}
623627

@@ -628,7 +632,10 @@ psyco_conn_autocommit_set(connectionObject *self, PyObject *pyvalue)
628632

629633
if (!_psyco_conn_autocommit_set_checks(self)) { return -1; }
630634
if (-1 == (value = PyObject_IsTrue(pyvalue))) { return -1; }
631-
if (0 != conn_set_autocommit(self, value)) { return -1; }
635+
if (0 > conn_set_session(self, value,
636+
self->isolevel, self->readonly, self->deferrable)) {
637+
return -1;
638+
}
632639

633640
return 0;
634641
}
@@ -660,9 +667,7 @@ psyco_conn_set_isolation_level(connectionObject *self, PyObject *args)
660667
{
661668
int level = 1;
662669

663-
EXC_IF_CONN_CLOSED(self);
664-
EXC_IF_CONN_ASYNC(self, set_isolation_level);
665-
EXC_IF_TPC_PREPARED(self, set_isolation_level);
670+
_set_session_checks(self, set_isolation_level);
666671

667672
if (!PyArg_ParseTuple(args, "i", &level)) return NULL;
668673

@@ -672,8 +677,17 @@ psyco_conn_set_isolation_level(connectionObject *self, PyObject *args)
672677
return NULL;
673678
}
674679

675-
if (conn_switch_isolation_level(self, level) < 0) {
676-
return NULL;
680+
if (level == 0) {
681+
if (0 > conn_set_session(self, 1,
682+
ISOLATION_LEVEL_DEFAULT, self->readonly, self->deferrable)) {
683+
return NULL;
684+
}
685+
}
686+
else {
687+
if (0 > conn_set_session(self, 0,
688+
level, self->readonly, self->deferrable)) {
689+
return NULL;
690+
}
677691
}
678692

679693
Py_RETURN_NONE;

0 commit comments

Comments
 (0)