mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-29 20:23:45 +03:00
Making sync and async connection setup somewhat more consistent.
This commit is contained in:
parent
d915cb12a8
commit
df959c20be
|
@ -232,13 +232,25 @@ conn_get_isolation_level(PGresult *pgres)
|
||||||
int
|
int
|
||||||
conn_get_protocol_version(PGconn *pgconn)
|
conn_get_protocol_version(PGconn *pgconn)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
#ifdef HAVE_PQPROTOCOL3
|
#ifdef HAVE_PQPROTOCOL3
|
||||||
return PQprotocolVersion(pgconn);
|
ret = PQprotocolVersion(pgconn);
|
||||||
#else
|
#else
|
||||||
return 2;
|
ret = 2;
|
||||||
#endif
|
#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 */
|
/* conn_setup - setup and read basic information about the connection */
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -250,12 +262,6 @@ conn_setup(connectionObject *self, PGconn *pgconn)
|
||||||
pthread_mutex_lock(&self->lock);
|
pthread_mutex_lock(&self->lock);
|
||||||
Py_BLOCK_THREADS;
|
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()) {
|
if (!psyco_green()) {
|
||||||
Py_UNBLOCK_THREADS;
|
Py_UNBLOCK_THREADS;
|
||||||
pgres = PQexec(pgconn, psyco_datestyle);
|
pgres = PQexec(pgconn, psyco_datestyle);
|
||||||
|
@ -366,14 +372,6 @@ conn_sync_connect(connectionObject *self)
|
||||||
|
|
||||||
PQsetNoticeProcessor(pgconn, conn_notice_callback, (void*)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 the connection is green, wait to finish connection */
|
||||||
if (green) {
|
if (green) {
|
||||||
wait_rv = psyco_wait(self);
|
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,
|
/* From here the connection is considered ready: with the new status,
|
||||||
* poll() will use PQisBusy instead of PQconnectPoll.
|
* 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 */
|
/* since this is the last step, set the other instance variables now */
|
||||||
self->equote = conn_get_standard_conforming_strings(self->pgconn);
|
self->equote = conn_get_standard_conforming_strings(self->pgconn);
|
||||||
self->protocol = conn_get_protocol_version(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
|
* asynchronous connections always use isolation level 0, the user is
|
||||||
* expected to manage the transactions himself, by sending
|
* expected to manage the transactions himself, by sending
|
||||||
|
@ -600,20 +600,12 @@ conn_poll_connect_fetch(connectionObject *self)
|
||||||
*/
|
*/
|
||||||
self->isolation_level = 0;
|
self->isolation_level = 0;
|
||||||
|
|
||||||
Py_BEGIN_ALLOW_THREADS;
|
/* FIXME: this is a bug: the above queries were sent to the server
|
||||||
pthread_mutex_lock(&(self->lock));
|
with a blocking connection */
|
||||||
|
if (pq_set_non_blocking(self, 1, 1) != 0) {
|
||||||
/* 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");
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_unlock(&(self->lock));
|
|
||||||
Py_END_ALLOW_THREADS;
|
|
||||||
|
|
||||||
/* next status is going to READY */
|
/* next status is going to READY */
|
||||||
next_status = CONN_STATUS_READY;
|
next_status = CONN_STATUS_READY;
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,6 +74,11 @@ class AsyncTests(unittest.TestCase):
|
||||||
# the async connection should be in isolevel 0
|
# the async connection should be in isolevel 0
|
||||||
self.assertEquals(self.conn.isolation_level, 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):
|
def test_async_named_cursor(self):
|
||||||
self.assertRaises(psycopg2.ProgrammingError,
|
self.assertRaises(psycopg2.ProgrammingError,
|
||||||
self.conn.cursor, "name")
|
self.conn.cursor, "name")
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
import psycopg2.extensions
|
||||||
import tests
|
import tests
|
||||||
|
|
||||||
class ConnectionTests(unittest.TestCase):
|
class ConnectionTests(unittest.TestCase):
|
||||||
|
@ -49,6 +50,20 @@ class ConnectionTests(unittest.TestCase):
|
||||||
conn = self.connect()
|
conn = self.connect()
|
||||||
self.assert_(conn.server_version)
|
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():
|
def test_suite():
|
||||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user