ZPsycopgDA now uses connection-level type-casters.

This commit is contained in:
Federico Di Gregorio 2007-02-23 15:23:06 +00:00
parent 6598a279e2
commit 2ae0c32031
3 changed files with 19 additions and 17 deletions

View File

@ -125,30 +125,23 @@ class Connection(Shared.DC.ZRDB.Connection.Connection):
raise ImportError("psycopg version mismatch (imported %s)" % raise ImportError("psycopg version mismatch (imported %s)" %
psycopg2.__version__) psycopg2.__version__)
self.set_type_casts()
self._v_connected = '' self._v_connected = ''
dbf = self.factory() dbf = self.factory()
# TODO: let the psycopg exception propagate, or not? # TODO: let the psycopg exception propagate, or not?
self._v_database_connection = dbf( self._v_database_connection = dbf(
self.connection_string, self.tilevel, self.encoding) self.connection_string, self.tilevel, self.get_type_casts(), self.encoding)
self._v_database_connection.open() self._v_database_connection.open()
self._v_connected = DateTime() self._v_connected = DateTime()
return self return self
def set_type_casts(self): def get_type_casts(self):
# note that in both cases order *is* important # note that in both cases order *is* important
if self.zdatetime: if self.zdatetime:
# use zope internal datetime routines return ZDATETIME, ZDATE, ZTIME
register_type(ZDATETIME)
register_type(ZDATE)
register_type(ZTIME)
else: else:
# use the standard return DATETIME, DATE, TIME
register_type(DATETIME)
register_type(DATE)
register_type(TIME)
## browsing and table/column management ## ## browsing and table/column management ##

View File

@ -27,6 +27,7 @@ import pool
import psycopg2 import psycopg2
from psycopg2.extensions import INTEGER, LONGINTEGER, FLOAT, BOOLEAN, DATE, TIME from psycopg2.extensions import INTEGER, LONGINTEGER, FLOAT, BOOLEAN, DATE, TIME
from psycopg2.extensions import register_type
from psycopg2 import NUMBER, STRING, ROWID, DATETIME from psycopg2 import NUMBER, STRING, ROWID, DATETIME
@ -36,17 +37,21 @@ class DB(TM, dbi_db.DB):
_p_oid = _p_changed = _registered = None _p_oid = _p_changed = _registered = None
def __init__(self, dsn, tilevel, enc='utf-8'): def __init__(self, dsn, tilevel, typecasts, enc='utf-8'):
self.dsn = dsn self.dsn = dsn
self.tilevel = tilevel self.tilevel = tilevel
self.typecasts = typecasts
self.encoding = enc self.encoding = enc
self.failures = 0 self.failures = 0
self.calls = 0 self.calls = 0
self.make_mappings() self.make_mappings()
def getconn(self, create=True): def getconn(self, create=True):
conn = pool.getconn(self.dsn) conn = pool.getconn(self.dsn)
conn.set_isolation_level(int(self.tilevel)) conn.set_isolation_level(int(self.tilevel))
conn.set_client_encoding(self.encoding)
for tc in self.typecasts:
register_type(tc, conn)
return conn return conn
def putconn(self, close=False): def putconn(self, close=False):
@ -159,9 +164,6 @@ class DB(TM, dbi_db.DB):
try: try:
for qs in [x for x in query_string.split('\0') if x]: for qs in [x for x in query_string.split('\0') if x]:
if type(qs) == unicode:
if self.encoding:
qs = qs.encode(self.encoding)
try: try:
if query_data: if query_data:
c.execute(qs, query_data) c.execute(qs, query_data)

View File

@ -236,6 +236,9 @@ conn_switch_isolation_level(connectionObject *self, int level)
{ {
int res = 0; int res = 0;
/* if the current isolation level is equal to the requested one don't switch */
if (self->isolation_level == level) return 0;
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&self->lock); pthread_mutex_lock(&self->lock);
@ -263,7 +266,11 @@ conn_set_client_encoding(connectionObject *self, char *enc)
PGresult *pgres; PGresult *pgres;
char query[48]; char query[48];
int res = 0; int res = 0;
/* If the current encoding is equal to the requested one we don't
issue any query to the backend */
if (strcmp(self->encoding, enc) == 0) return 0;
/* TODO: check for async query here and raise error if necessary */ /* TODO: check for async query here and raise error if necessary */
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;