Reverted isolation level values to backward compatible values

This basically removes the READ UNCOMMITED level (that internally
PostgreSQL maps to READ COMMITED anyway) to keep the numeric values
compattible with old psycopg versions. For full details and discussion
see this thread:
http://archives.postgresql.org/psycopg/2011-12/msg00008.php
This commit is contained in:
Federico Di Gregorio 2011-12-15 12:22:52 +01:00
parent 8473209d24
commit d2d94e203f
4 changed files with 21 additions and 23 deletions

View File

@ -69,10 +69,12 @@ except ImportError:
"""Isolation level values."""
ISOLATION_LEVEL_AUTOCOMMIT = 0
ISOLATION_LEVEL_READ_UNCOMMITTED = 1
ISOLATION_LEVEL_READ_COMMITTED = 2
ISOLATION_LEVEL_REPEATABLE_READ = 3
ISOLATION_LEVEL_SERIALIZABLE = 4
ISOLATION_LEVEL_READ_COMMITTED = 1
ISOLATION_LEVEL_REPEATABLE_READ = 2
ISOLATION_LEVEL_SERIALIZABLE = 3
# PostgreSQL internally converts uncommited to commited
ISOLATION_LEVEL_READ_UNCOMMITTED = ISOLATION_LEVEL_READ_COMMITTED
"""psycopg connection status values."""
STATUS_SETUP = 0

View File

@ -39,10 +39,10 @@
const IsolationLevel conn_isolevels[] = {
{"", 0}, /* autocommit */
{"read uncommitted", 1},
{"read committed", 2},
{"repeatable read", 3},
{"serializable", 4},
{"read committed", 1},
{"read uncommitted", 1}, /* comes after to report real level */
{"repeatable read", 2},
{"serializable", 3},
{"default", -1},
{ NULL }
};
@ -1041,9 +1041,8 @@ conn_switch_isolation_level(connectionObject *self, int level)
/* use only supported levels on older PG versions */
if (self->server_version < 80000) {
if (level == 1 || level == 3) {
++level;
}
if (level == 2)
level = 3;
}
if (-1 == (curr_level = conn_get_isolation_level(self))) {

View File

@ -405,13 +405,15 @@ _psyco_conn_parse_isolevel(connectionObject *self, PyObject *pyval)
if (PyInt_Check(pyval)) {
long level = PyInt_AsLong(pyval);
if (level == -1 && PyErr_Occurred()) { goto exit; }
if (level < 1 || level > 4) {
if (level < 1 || level > 3) {
PyErr_SetString(PyExc_ValueError,
"isolation_level must be between 1 and 4");
"isolation_level must be between 1 and 3");
goto exit;
}
isolevel = conn_isolevels + level;
isolevel = conn_isolevels;
while ((++isolevel)->value != level)
; /* continue */
}
/* parse from the string -- this includes "default" */

View File

@ -206,7 +206,6 @@ class IsolationLevelsTestCase(unittest.TestCase):
levels = [
(None, psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT),
('read uncommitted', psycopg2.extensions.ISOLATION_LEVEL_READ_UNCOMMITTED),
('read committed', psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED),
('repeatable read', psycopg2.extensions.ISOLATION_LEVEL_REPEATABLE_READ),
('serializable', psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE),
@ -216,10 +215,8 @@ class IsolationLevelsTestCase(unittest.TestCase):
# the only values available on prehistoric PG versions
if conn.server_version < 80000:
if level in (
psycopg2.extensions.ISOLATION_LEVEL_READ_UNCOMMITTED,
psycopg2.extensions.ISOLATION_LEVEL_REPEATABLE_READ):
name, level = levels[levels.index((name, level)) + 1]
if level == psycopg2.extensions.ISOLATION_LEVEL_REPEATABLE_READ:
name, level = ('serializable', psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE)
self.assertEqual(conn.isolation_level, level)
@ -771,13 +768,11 @@ class TransactionControlTests(unittest.TestCase):
self.assertEqual(cur.fetchone()[0], 'read committed')
self.conn.rollback()
# 'read uncommitted' is internally translated to 'read committed'
self.conn.set_session(
isolation_level=psycopg2.extensions.ISOLATION_LEVEL_READ_UNCOMMITTED)
cur.execute("SHOW default_transaction_isolation;")
if self.conn.server_version > 80000:
self.assertEqual(cur.fetchone()[0], 'read uncommitted')
else:
self.assertEqual(cur.fetchone()[0], 'read committed')
self.assertEqual(cur.fetchone()[0], 'read committed')
self.conn.rollback()
def test_set_isolation_level_str(self):