[svn:DBD-Pg] r14659 - DBD-Pg/trunk

[email protected]
Newsgroups gmane.comp.db.postgresql.dbdpg.cvs
Message-ID <[email protected]>
Author: turnstep
Date: Thu Jan 27 09:01:53 2011
New Revision: 14659

Modified:
   DBD-Pg/trunk/dbdimp.c
   DBD-Pg/trunk/dbdimp.h

Log:
Commit some utf8 work in progress for David to hack at


Modified: DBD-Pg/trunk/dbdimp.c
==============================================================================
--- DBD-Pg/trunk/dbdimp.c	(original)
+++ DBD-Pg/trunk/dbdimp.c	Thu Jan 27 09:01:53 2011
@@ -229,11 +229,36 @@
 		}
 	}
 
-	/* Figure out the client_encoding for UTF work */
-	imp_dbh->is_utf8 = strncmp("UTF8", PQparameterStatus(imp_dbh->conn, "client_encoding"), 4)
-		? DBDPG_FALSE : DBDPG_TRUE;
+	/* Store the old client_encoding in case we need to restore to it later */
+	/* Longest name is currently 'SHIFT_JIS_2004', so allow for that plus a lot */
+	Renew(imp_dbh->client_encoding, 42, char); /* freed in dbd_db_destroy */
+	strncpy(imp_dbh->client_encoding, PQparameterStatus(imp_dbh->conn, "client_encoding"), 42);
+
+	/* We need to know if the server is SQL_ASCII. We assume not */
+	imp_dbh->pg_byte_soup = DBDPG_FALSE;
+
+	if (0 == strncmp("UTF8", PQparameterStatus(imp_dbh->conn, "client_encoding"), 4)) {
+		imp_dbh->is_utf8 = DBDPG_TRUE;
+	}
+	else {
+		imp_dbh->is_utf8 = DBDPG_FALSE;
+		/* Can we set pg_enable_utf8 this early? */
+		/* We need to find the server_encoding to make sure it is possible to change 
+		 the client_encoding to UTF-8. This is safe for all but SQL_ASCII */
+		/* If we are not SQL_ASCII, let's change the encoding to UTF-8 right now */
+		if (0 == strncmp("SQL_ASCII", PQparameterStatus(imp_dbh->conn, "server_encoding"), 9)) {
+			imp_dbh->pg_byte_soup = DBDPG_TRUE;
+		}
+		else {
+			PQexec(imp_dbh->conn, "SET client_encoding = 'UTF-8'");
+			imp_dbh->is_utf8 = DBDPG_TRUE;
+		}
+	}
+
+	/* At this point, the only way that is_utf8 is false is for SQL_ASCII databases */
+
 	imp_dbh->pg_bool_tf      = DBDPG_FALSE;
-	imp_dbh->pg_enable_utf8  = DBDPG_FALSE;
+	imp_dbh->pg_enable_utf8  = -1;
  	imp_dbh->prepare_now     = DBDPG_FALSE;
 	imp_dbh->done_begin      = DBDPG_FALSE;
 	imp_dbh->dollaronly      = DBDPG_FALSE;
@@ -284,8 +309,10 @@
 	sv_setpv(DBIc_STATE(imp_xxh), (char*)imp_dbh->sqlstate);
 
 	/* Set as utf-8 */
+#ifdef is_utf8_string
 	if (imp_dbh->is_utf8)
 		SvUTF8_on(DBIc_ERRSTR(imp_xxh));
+#endif
 
 	if (TEND) TRC(DBILOGFP, "%sEnd pg_error\n", THEADER);
 
@@ -859,12 +886,14 @@
 
 #ifdef is_utf8_string
 		else if (strEQ("pg_enable_utf8", key)) {
-			/* Turning on has no effect now, but an explicit off will force utf8 off */
 			imp_dbh->pg_enable_utf8 = (SvOK(valuesv) && 0 == (unsigned)SvIV(valuesv))
-				? DBDPG_FALSE : DBDPG_TRUE;
-			if (imp_dbh->pg_enable_utf8 == DBDPG_FALSE) {
+				? 0 : 1;
+			if (0 == imp_dbh->pg_enable_utf8) {
 				imp_dbh->is_utf8 = DBDPG_FALSE;
 			}
+			else {
+				imp_dbh->is_utf8 = DBDPG_TRUE;
+			}
 			retval = 1;
 		}
 #endif

Modified: DBD-Pg/trunk/dbdimp.h
==============================================================================
--- DBD-Pg/trunk/dbdimp.h	(original)
+++ DBD-Pg/trunk/dbdimp.h	Thu Jan 27 09:01:53 2011
@@ -26,15 +26,17 @@
 	int     pg_errorlevel;     /* PQsetErrorVerbosity. Set by user, defaults to 1 */
 	int     server_prepare;    /* do we want to use PQexecPrepared? 0=no 1=yes 2=smart. Can be changed by user */
 	int     async_status;      /* 0=no async 1=async started -1=async has been cancelled */
+	int     pg_enable_utf8;    /* -1=not set  0=force off  1=force on */
 
     imp_sth_t *async_sth;      /* current async statement handle */
 	AV      *savepoints;       /* list of savepoints */
 	PGconn  *conn;             /* connection structure */
 	char    *sqlstate;         /* from the last result */
+	char    *client_encoding;  /* the initial (default) client_encoding */
 
 	bool    is_utf8;           /* does the client_encoding return UTF8? */
+	bool    pg_byte_soup;      /* is the server_encoding SQL_ASCII? */
 	bool    pg_bool_tf;        /* do bools return 't'/'f'? Set by user, default is 0 */
-	bool    pg_enable_utf8;    /* should we attempt to make utf8 strings? Set by user, default is 0 */
 	bool    prepare_now;       /* force immediate prepares, even with placeholders. Set by user, default is 0 */
 	bool    done_begin;        /* have we done a begin? (e.g. are we in a transaction?) */
 	bool    dollaronly;        /* only consider $1, $2 ... as valid placeholders */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.