Making sync and async connection setup somewhat more consistent.

This commit is contained in:
Daniele Varrazzo 2010-04-21 18:40:05 +01:00
parent d915cb12a8
commit df959c20be
3 changed files with 41 additions and 29 deletions

View File

@ -232,13 +232,25 @@ conn_get_isolation_level(PGresult *pgres)
int
conn_get_protocol_version(PGconn *pgconn)
{
int ret;
#ifdef HAVE_PQPROTOCOL3
return PQprotocolVersion(pgconn);
ret = PQprotocolVersion(pgconn);
#else
return 2;
ret = 2;
#endif
Dprintf("conn_connect: using protocol %d", ret);
return ret;
}
int
conn_get_server_version(PGconn *pgconn)
{
return (int)PQserverVersion(pgconn);
}
/* conn_setup - setup and read basic information about the connection */
int
@ -250,12 +262,6 @@ conn_setup(connectionObject *self, PGconn *pgconn)
pthread_mutex_lock(&self->lock);
Py_BLOCK_THREADS;
if (self->encoding) free(self->encoding);
self->equote = 0;
self->isolation_level = 0;
self->equote = conn_get_standard_conforming_strings(pgconn);
if (!psyco_green()) {
Py_UNBLOCK_THREADS;
pgres = PQexec(pgconn, psyco_datestyle);
@ -366,14 +372,6 @@ conn_sync_connect(connectionObject *self)
PQsetNoticeProcessor(pgconn, conn_notice_callback, (void*)self);
#ifdef HAVE_PQPROTOCOL3
self->protocol = PQprotocolVersion(pgconn);
#else
self->protocol = 2;
#endif
Dprintf("conn_connect: using protocol %d", self->protocol);
/* if the connection is green, wait to finish connection */
if (green) {
wait_rv = psyco_wait(self);
@ -384,7 +382,9 @@ conn_sync_connect(connectionObject *self)
}
}
self->server_version = (int)PQserverVersion(pgconn);
self->equote = conn_get_standard_conforming_strings(pgconn);
self->server_version = conn_get_server_version(pgconn);
self->protocol = conn_get_protocol_version(self->pgconn);
/* From here the connection is considered ready: with the new status,
* poll() will use PQisBusy instead of PQconnectPoll.
@ -592,7 +592,7 @@ conn_poll_connect_fetch(connectionObject *self)
/* since this is the last step, set the other instance variables now */
self->equote = conn_get_standard_conforming_strings(self->pgconn);
self->protocol = conn_get_protocol_version(self->pgconn);
self->server_version = (int) PQserverVersion(self->pgconn);
self->server_version = conn_get_server_version(self->pgconn);
/*
* asynchronous connections always use isolation level 0, the user is
* expected to manage the transactions himself, by sending
@ -600,20 +600,12 @@ conn_poll_connect_fetch(connectionObject *self)
*/
self->isolation_level = 0;
Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&(self->lock));
/* set the connection to nonblocking */
if (PQsetnonblocking(self->pgconn, 1) != 0) {
Dprintf("conn_async_connect: PQsetnonblocking() FAILED");
Py_BLOCK_THREADS;
PyErr_SetString(OperationalError, "PQsetnonblocking() failed");
/* FIXME: this is a bug: the above queries were sent to the server
with a blocking connection */
if (pq_set_non_blocking(self, 1, 1) != 0) {
return NULL;
}
pthread_mutex_unlock(&(self->lock));
Py_END_ALLOW_THREADS;
/* next status is going to READY */
next_status = CONN_STATUS_READY;
}

View File

@ -74,6 +74,11 @@ class AsyncTests(unittest.TestCase):
# the async connection should be in isolevel 0
self.assertEquals(self.conn.isolation_level, 0)
# check other properties to be found on the connection
self.assert_(self.conn.server_version)
self.assert_(self.conn.protocol_version in (2,3))
self.assert_(self.conn.encoding in psycopg2.extensions.encodings)
def test_async_named_cursor(self):
self.assertRaises(psycopg2.ProgrammingError,
self.conn.cursor, "name")

View File

@ -2,6 +2,7 @@
import unittest
import psycopg2
import psycopg2.extensions
import tests
class ConnectionTests(unittest.TestCase):
@ -49,6 +50,20 @@ class ConnectionTests(unittest.TestCase):
conn = self.connect()
self.assert_(conn.server_version)
def test_protocol_version(self):
conn = self.connect()
self.assert_(conn.protocol_version in (2,3), conn.protocol_version)
def test_isolation_level(self):
conn = self.connect()
self.assertEqual(
conn.isolation_level,
psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED)
def test_encoding(self):
conn = self.connect()
self.assert_(conn.encoding in psycopg2.extensions.encodings)
def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__)