diff options
| -rw-r--r-- | ChangeLog | 5 | ||||
| -rw-r--r-- | NEWS-2.3 | 4 | ||||
| -rw-r--r-- | psycopg/connection_int.c | 74 |
3 files changed, 58 insertions, 25 deletions
@@ -6,9 +6,12 @@ * psycopg/connection_int.c: dropped support for protocol 2 at compile time and protocol 2-specific code. - * psycopg/connection_int.c: don't run a query at connection to detect + * psycopg/connection_int.c: don't run a query at every connection to detect client encoding: use PQparameterStatus() instead. + * psycopg/connection_int.c: don't run a query at every connection to set + the datestyle to ISO if PQparameterStatus() reports it already is. + 2010-11-11 Daniele Varrazzo <daniele.varrazzo@gmail.com> * lib/extras.py: build the namedtuple only once per execution, not once @@ -15,7 +15,9 @@ psycopg 2.3 aims to expose some new features introduced in PostgreSQL 9.0. - Dropped support for protocol 2: Psycopg 2.3 can only connect to PostgreSQL servers with version at least 7.4. - - don't issue a query at every connection to detect the client encoding. + - don't issue a query at every connection to detect the client encoding + and to set the datestyle to ISO if it is already compatible with what + expected. - `mogrify()` now supports unicode queries. - subclasses of a type that can be adapted are adapted as the superclass. - `errorcodes` knows a couple of new codes introduced in PostgreSQL 9.0. diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index ba923e0..8516b2b 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -292,6 +292,22 @@ conn_get_server_version(PGconn *pgconn) } +/* Return 1 if the server datestyle allows us to work without problems, + 0 if it needs to be set to something better, e.g. ISO. */ +static int +conn_is_datestyle_ok(PGconn *pgconn) +{ + const char *ds; + + ds = PQparameterStatus(pgconn, "DateStyle"); + Dprintf("conn_connect: DateStyle %s", ds); + + /* Return true if ds starts with "ISO" + * e.g. "ISO, DMY" is fine, "German" not. */ + return (ds[0] == 'I' && ds[1] == 'S' && ds[2] == 'O'); +} + + /* conn_setup - setup and read basic information about the connection */ int @@ -323,23 +339,26 @@ conn_setup(connectionObject *self, PGconn *pgconn) return -1; } - if (!green) { - Py_UNBLOCK_THREADS; - pgres = PQexec(pgconn, psyco_datestyle); - Py_BLOCK_THREADS; - } else { - pgres = psyco_exec_green(self, psyco_datestyle); - } + if (!conn_is_datestyle_ok(self->pgconn)) { + if (!green) { + Py_UNBLOCK_THREADS; + Dprintf("conn_connect: exec query \"%s\";", psyco_datestyle); + pgres = PQexec(pgconn, psyco_datestyle); + Py_BLOCK_THREADS; + } else { + pgres = psyco_exec_green(self, psyco_datestyle); + } - if (pgres == NULL || PQresultStatus(pgres) != PGRES_COMMAND_OK ) { - PyErr_SetString(OperationalError, "can't set datestyle to ISO"); - IFCLEARPGRES(pgres); - Py_UNBLOCK_THREADS; - pthread_mutex_unlock(&self->lock); - Py_BLOCK_THREADS; - return -1; + if (pgres == NULL || PQresultStatus(pgres) != PGRES_COMMAND_OK ) { + PyErr_SetString(OperationalError, "can't set datestyle to ISO"); + IFCLEARPGRES(pgres); + Py_UNBLOCK_THREADS; + pthread_mutex_unlock(&self->lock); + Py_BLOCK_THREADS; + return -1; + } + CLEARPGRES(pgres); } - CLEARPGRES(pgres); if (!green) { Py_UNBLOCK_THREADS; @@ -642,15 +661,24 @@ _conn_poll_setup_async(connectionObject *self) */ self->isolation_level = 0; - Dprintf("conn_poll: status -> CONN_STATUS_DATESTYLE"); - self->status = CONN_STATUS_DATESTYLE; - if (0 == pq_send_query(self, psyco_datestyle)) { - PyErr_SetString(OperationalError, PQerrorMessage(self->pgconn)); - break; + /* If the datestyle is ISO or anything else good, + * we can skip the CONN_STATUS_DATESTYLE step. */ + if (!conn_is_datestyle_ok(self->pgconn)) { + Dprintf("conn_poll: status -> CONN_STATUS_DATESTYLE"); + self->status = CONN_STATUS_DATESTYLE; + if (0 == pq_send_query(self, psyco_datestyle)) { + PyErr_SetString(OperationalError, PQerrorMessage(self->pgconn)); + break; + } + Dprintf("conn_poll: async_status -> ASYNC_WRITE"); + self->async_status = ASYNC_WRITE; + res = PSYCO_POLL_WRITE; + } + else { + Dprintf("conn_poll: status -> CONN_STATUS_READY"); + self->status = CONN_STATUS_READY; + res = PSYCO_POLL_OK; } - Dprintf("conn_poll: async_status -> ASYNC_WRITE"); - self->async_status = ASYNC_WRITE; - res = PSYCO_POLL_WRITE; break; case CONN_STATUS_DATESTYLE: |
