[teiid-commits] teiid SVN: r4513 - in trunk: client/src/test/java/org/teiid/jdbc and 6 other directories.

teiid-commits at lists.jboss.org teiid-commits at lists.jboss.org
Thu Oct 4 12:08:47 EDT 2012


Author: shawkins
Date: 2012-10-04 12:08:45 -0400 (Thu, 04 Oct 2012)
New Revision: 4513

Modified:
   trunk/client/src/main/java/org/teiid/jdbc/ConnectionImpl.java
   trunk/client/src/main/java/org/teiid/jdbc/JDBCURL.java
   trunk/client/src/main/java/org/teiid/jdbc/StatementImpl.java
   trunk/client/src/main/java/org/teiid/jdbc/TeiidDriver.java
   trunk/client/src/test/java/org/teiid/jdbc/TestStatement.java
   trunk/client/src/test/java/org/teiid/jdbc/TestTeiidDriver.java
   trunk/runtime/src/main/java/org/teiid/deployers/PgCatalogMetadataStore.java
   trunk/runtime/src/main/java/org/teiid/deployers/VDBRepository.java
   trunk/runtime/src/main/java/org/teiid/odbc/ODBCClientRemote.java
   trunk/runtime/src/main/java/org/teiid/odbc/ODBCServerRemoteImpl.java
   trunk/runtime/src/main/java/org/teiid/runtime/RuntimePlugin.java
   trunk/runtime/src/main/java/org/teiid/transport/LocalServerConnection.java
   trunk/runtime/src/main/java/org/teiid/transport/PGCharsetConverter.java
   trunk/runtime/src/main/java/org/teiid/transport/PgBackendProtocol.java
   trunk/runtime/src/main/java/org/teiid/transport/PgFrontendProtocol.java
   trunk/runtime/src/main/resources/org/teiid/runtime/i18n.properties
   trunk/test-integration/common/src/test/java/org/teiid/transport/TestODBCSocketTransport.java
Log:
TEIID-2228 adding support for pg_client_encoding

Modified: trunk/client/src/main/java/org/teiid/jdbc/ConnectionImpl.java
===================================================================
--- trunk/client/src/main/java/org/teiid/jdbc/ConnectionImpl.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/client/src/main/java/org/teiid/jdbc/ConnectionImpl.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -187,6 +187,14 @@
         return this.propInfo;
     }
     
+    public void setExecutionProperty(String key, String value) {
+    	JDBCURL.addNormalizedProperty(key, value, getExecutionProperties());
+    }
+    
+    public String getExecutionProperty(String key) {
+    	return this.getExecutionProperties().getProperty(JDBCURL.getValidKey(key));
+    }
+    
     DQP getDQP() {
     	return this.dqp;
     }
@@ -420,7 +428,7 @@
      * <p>This method gets the ServerConnection object wrapped by this object.</p>
      * @return ServerConnection object
      */
-    ServerConnection getServerConnection() throws SQLException {
+    public ServerConnection getServerConnection() throws SQLException {
         //Check to see the connection is open
         checkConnection();
         return serverConn;

Modified: trunk/client/src/main/java/org/teiid/jdbc/JDBCURL.java
===================================================================
--- trunk/client/src/main/java/org/teiid/jdbc/JDBCURL.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/client/src/main/java/org/teiid/jdbc/JDBCURL.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -69,9 +69,9 @@
 		return result;
 	}
 	
-    public static final Set<String> KNOWN_PROPERTIES = getKnownProperties();
+    public static final Map<String, String> KNOWN_PROPERTIES = getKnownProperties();
     
-    private static Set<String> getKnownProperties() {
+    private static Map<String, String> getKnownProperties() {
     	Set<String> props = new HashSet<String>(Arrays.asList(
     	        BaseDataSource.APP_NAME,
     	        BaseDataSource.VDB_NAME,
@@ -87,7 +87,11 @@
     	        TeiidURL.CONNECTION.KERBEROS_SERVICE_PRINCIPLE_NAME,
     	        TeiidURL.CONNECTION.ENCRYPT_REQUESTS));
     	props.addAll(EXECUTION_PROPERTIES.keySet());
-    	return Collections.unmodifiableSet(props);
+    	Map<String, String> result = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
+    	for (String string : props) {
+			result.put(string, string);
+		}
+    	return Collections.unmodifiableMap(result);
     }
     
     private String vdbName;
@@ -290,11 +294,10 @@
     }
 
     public static String getValidKey(String key) {
-    	for (String prop : KNOWN_PROPERTIES) {
-    		if (prop.equalsIgnoreCase(key)) {
-    			return prop;
-    		}
-        }
+    	String result = KNOWN_PROPERTIES.get(key);
+    	if (result != null) {
+    		return result;
+    	}
     	return key;
     }
     

Modified: trunk/client/src/main/java/org/teiid/jdbc/StatementImpl.java
===================================================================
--- trunk/client/src/main/java/org/teiid/jdbc/StatementImpl.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/client/src/main/java/org/teiid/jdbc/StatementImpl.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -445,7 +445,7 @@
         				this.getMMConnection().getServerConnection().cleanUp();
         			}
         		} else {
-        			JDBCURL.addNormalizedProperty(key, value, this.driverConnection.getExecutionProperties());
+        			this.driverConnection.setExecutionProperty(key, value);
         		}
         		this.updateCounts = new int[] {0};
         		return booleanFuture(false);
@@ -589,7 +589,7 @@
 					new String[] {DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.STRING});
 			return booleanFuture(true);
 		}
-		List<List<String>> records = Collections.singletonList(Collections.singletonList(driverConnection.getExecutionProperties().getProperty(JDBCURL.getValidKey(show))));
+		List<List<String>> records = Collections.singletonList(Collections.singletonList(driverConnection.getExecutionProperty(show)));
 		createResultSet(records, new String[] {show}, new String[] {DataTypeManager.DefaultDataTypes.STRING});
 		return booleanFuture(true);
 	}

Modified: trunk/client/src/main/java/org/teiid/jdbc/TeiidDriver.java
===================================================================
--- trunk/client/src/main/java/org/teiid/jdbc/TeiidDriver.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/client/src/main/java/org/teiid/jdbc/TeiidDriver.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -26,7 +26,6 @@
 import java.sql.DriverManager;
 import java.sql.DriverPropertyInfo;
 import java.sql.SQLException;
-import java.sql.SQLFeatureNotSupportedException;
 import java.util.Enumeration;
 import java.util.LinkedList;
 import java.util.List;
@@ -164,7 +163,7 @@
 
         parseURL(url, info);
 
-        for (String property: JDBCURL.KNOWN_PROPERTIES) {
+        for (String property: JDBCURL.KNOWN_PROPERTIES.keySet()) {
         	DriverPropertyInfo dpi = new DriverPropertyInfo(property, info.getProperty(property));
         	if (property.equals(BaseDataSource.VDB_NAME)) {
         		dpi.required = true;
@@ -199,7 +198,7 @@
             }
             Properties optionalParams = jdbcURL.getProperties();
             JDBCURL.normalizeProperties(info);
-            Enumeration keys = optionalParams.keys();
+            Enumeration<?> keys = optionalParams.keys();
             while (keys.hasMoreElements()) {
                 String propName = (String)keys.nextElement();
                 // Don't let the URL properties override the passed-in Properties object.
@@ -229,7 +228,7 @@
         return false;
     }
 
-	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
+	public Logger getParentLogger() {
 		return logger;
 	}
 }

Modified: trunk/client/src/test/java/org/teiid/jdbc/TestStatement.java
===================================================================
--- trunk/client/src/test/java/org/teiid/jdbc/TestStatement.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/client/src/test/java/org/teiid/jdbc/TestStatement.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -86,14 +86,12 @@
 	
 	@Test public void testSetStatement() throws Exception {
 		ConnectionImpl conn = Mockito.mock(ConnectionImpl.class);
-		Properties p = new Properties();
-		Mockito.stub(conn.getExecutionProperties()).toReturn(p);
 		StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
 		assertFalse(statement.execute("set foo bar")); //$NON-NLS-1$
-		assertEquals("bar", p.get("foo")); //$NON-NLS-1$ //$NON-NLS-2$
+		Mockito.verify(conn).setExecutionProperty("foo", "bar");
 		
 		assertFalse(statement.execute("set foo 'b''ar'")); //$NON-NLS-1$
-		assertEquals("b'ar", p.get("foo")); //$NON-NLS-1$ //$NON-NLS-2$
+		Mockito.verify(conn).setExecutionProperty("foo", "b'ar");
 	}
 	
 	@Test public void testSetPayloadStatement() throws Exception {

Modified: trunk/client/src/test/java/org/teiid/jdbc/TestTeiidDriver.java
===================================================================
--- trunk/client/src/test/java/org/teiid/jdbc/TestTeiidDriver.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/client/src/test/java/org/teiid/jdbc/TestTeiidDriver.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -139,9 +139,9 @@
         DriverPropertyInfo info[] = drv.getPropertyInfo("jdbc:teiid:vdb at mm://localhost:12345;applicationName=x", null); //$NON-NLS-1$
 
         assertEquals(25, info.length);
-        assertEquals(false, info[0].required);
-        assertEquals("ApplicationName", info[0].name); //$NON-NLS-1$
-        assertEquals("x", info[0].value); //$NON-NLS-1$
+        assertEquals(false, info[1].required);
+        assertEquals("ApplicationName", info[1].name); //$NON-NLS-1$
+        assertEquals("x", info[1].value); //$NON-NLS-1$
     }
 
 }

Modified: trunk/runtime/src/main/java/org/teiid/deployers/PgCatalogMetadataStore.java
===================================================================
--- trunk/runtime/src/main/java/org/teiid/deployers/PgCatalogMetadataStore.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/runtime/src/main/java/org/teiid/deployers/PgCatalogMetadataStore.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -28,6 +28,7 @@
 import java.util.Map;
 import java.util.Properties;
 
+import org.teiid.adminapi.impl.SessionMetadata;
 import org.teiid.adminapi.impl.VDBMetaData;
 import org.teiid.core.types.ArrayImpl;
 import org.teiid.core.types.DataTypeManager;
@@ -36,16 +37,17 @@
 import org.teiid.metadata.FunctionMethod;
 import org.teiid.metadata.MetadataFactory;
 import org.teiid.metadata.Table;
+import org.teiid.metadata.FunctionMethod.Determinism;
 import org.teiid.metadata.Table.Type;
 import org.teiid.odbc.ODBCServerRemoteImpl;
 import org.teiid.query.metadata.TransformationMetadata;
 import org.teiid.query.resolver.util.ResolverVisitor;
-import org.teiid.translator.TranslatorException;
+import org.teiid.transport.PgBackendProtocol;
 
 public class PgCatalogMetadataStore extends MetadataFactory {
 	private static final long serialVersionUID = 2158418324376966987L;
 
-	public PgCatalogMetadataStore(String modelName, Map<String, Datatype> dataTypes) throws TranslatorException {
+	public PgCatalogMetadataStore(String modelName, Map<String, Datatype> dataTypes) {
 		super(modelName, 1, modelName, dataTypes, new Properties(), null); 
 		add_pg_namespace();			
 		add_pg_class();			
@@ -66,9 +68,11 @@
 		FunctionMethod func = addFunction("asPGVector", "asPGVector"); //$NON-NLS-1$ //$NON-NLS-2$
 		func.setProperty(ResolverVisitor.TEIID_PASS_THROUGH_TYPE, Boolean.TRUE.toString());
 		addFunction("getOid", "getOid"); //$NON-NLS-1$ //$NON-NLS-2$
+		func = addFunction("pg_client_encoding", "pg_client_encoding"); //$NON-NLS-1$ //$NON-NLS-2$
+		func.setDeterminism(Determinism.COMMAND_DETERMINISTIC);
 	}
 	
-	private Table createView(String name) throws TranslatorException {
+	private Table createView(String name) {
 		Table t = addTable(name);
 		t.setSystem(true);
 		t.setSupportsUpdate(false);
@@ -78,7 +82,7 @@
 	}
 	
 	//index access methods
-	private Table add_pg_am() throws TranslatorException {
+	private Table add_pg_am() {
 		Table t = createView("pg_am"); //$NON-NLS-1$ 
 		addColumn("oid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$ 
 		addColumn("amname", DataTypeManager.DefaultDataTypes.STRING, t); //$NON-NLS-1$ 
@@ -90,7 +94,7 @@
 	}
 
 	// column default values
-	private Table add_pg_attrdef() throws TranslatorException {
+	private Table add_pg_attrdef() {
 		Table t = createView("pg_attrdef"); //$NON-NLS-1$ 
 
 		addColumn("adrelid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$ 
@@ -110,7 +114,7 @@
 	 * table columns ("attributes")
 	 * see also {@link ODBCServerRemoteImpl} getPGColInfo for the mod calculation
 	 */
-	private Table add_pg_attribute() throws TranslatorException {
+	private Table add_pg_attribute() {
 		Table t = createView("pg_attribute"); //$NON-NLS-1$ 
 
 		addColumn("oid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$ 
@@ -175,7 +179,7 @@
 	}
 	
 	// tables, indexes, sequences ("relations")
-	private Table add_pg_class() throws TranslatorException {
+	private Table add_pg_class() {
 		Table t = createView("pg_class"); //$NON-NLS-1$ 
 		
 		addColumn("oid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$ 
@@ -231,7 +235,7 @@
 	}
 	
 	// additional index information
-	private Table add_pg_index() throws TranslatorException {
+	private Table add_pg_index() {
 		Table t = createView("pg_index"); //$NON-NLS-1$ 
 		
 		addColumn("oid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$ 
@@ -275,7 +279,7 @@
 	}
 
 	// schemas
-	private Table add_pg_namespace() throws TranslatorException {
+	private Table add_pg_namespace() {
 		Table t = createView("pg_namespace"); //$NON-NLS-1$ 
 		addColumn("oid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$ 
 		addColumn("nspname", DataTypeManager.DefaultDataTypes.STRING, t); //$NON-NLS-1$ 
@@ -289,7 +293,7 @@
 	}
 	
 	// functions and procedures
-	private Table add_pg_proc() throws TranslatorException {
+	private Table add_pg_proc() {
 		Table t = createView("pg_proc"); //$NON-NLS-1$ 
 
 		addColumn("oid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$ 
@@ -355,7 +359,7 @@
 	}
 
 	// triggers
-	private Table add_pg_trigger() throws TranslatorException  {
+	private Table add_pg_trigger()  {
 		Table t = createView("pg_trigger"); //$NON-NLS-1$ 
 		addColumn("oid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$ 
 		addColumn("tgconstrrelid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$ 
@@ -381,7 +385,7 @@
 	}
 	
 	//data types
-	private Table add_pg_type() throws TranslatorException {
+	private Table add_pg_type() {
 		Table t = createView("pg_type"); //$NON-NLS-1$ 
 		// Data type name
 		addColumn("oid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$ 
@@ -452,7 +456,7 @@
 		return t;		
 	}
 	
-	private Table add_pg_database() throws TranslatorException  {
+	private Table add_pg_database()  {
 		Table t = createView("pg_database"); //$NON-NLS-1$ 
 		addColumn("oid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$ 
 		addColumn("datname", DataTypeManager.DefaultDataTypes.STRING, t); //$NON-NLS-1$ 
@@ -478,7 +482,7 @@
 		return t;
 	}
 	
-	private Table add_pg_user() throws TranslatorException  {
+	private Table add_pg_user()  {
 		Table t = createView("pg_user"); //$NON-NLS-1$ 
 		addColumn("oid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$ 
 		addColumn("usename", DataTypeManager.DefaultDataTypes.STRING, t); //$NON-NLS-1$ 
@@ -493,7 +497,7 @@
 		return t;
 	}
 	
-	private Table add_matpg_relatt() throws TranslatorException  {
+	private Table add_matpg_relatt()  {
 		Table t = createView("matpg_relatt"); //$NON-NLS-1$ 
 		addColumn("attrelid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$
 		addColumn("attnum", DataTypeManager.DefaultDataTypes.SHORT, t); //$NON-NLS-1$ 
@@ -514,7 +518,7 @@
 		return t;
 	}
 	
-	private Table add_matpg_datatype() throws TranslatorException  {
+	private Table add_matpg_datatype()  {
 		Table t = createView("matpg_datatype"); //$NON-NLS-1$ 
 		addColumn("oid", DataTypeManager.DefaultDataTypes.INTEGER, t); //$NON-NLS-1$
 		addColumn("typname", DataTypeManager.DefaultDataTypes.STRING, t); //$NON-NLS-1$ 
@@ -537,7 +541,7 @@
 		return t;
 	}	
 	
-	private FunctionMethod addFunction(String javaFunction, String name) throws TranslatorException {
+	private FunctionMethod addFunction(String javaFunction, String name) {
 		Method[] methods = FunctionMethods.class.getMethods();
 		for (Method method : methods) {
 			if (!method.getName().equals(javaFunction)) {
@@ -580,5 +584,18 @@
 			TransformationMetadata tm = metadata.getAttachment(TransformationMetadata.class);
 			return tm.getMetadataStore().getOid(uid);
 		}
+		
+		public static String pg_client_encoding(org.teiid.CommandContext cc) {
+			SessionMetadata session = (SessionMetadata)cc.getSession();
+			ODBCServerRemoteImpl server = session.getAttachment(ODBCServerRemoteImpl.class);
+			String encoding = null;
+			if (server != null) {
+				encoding = server.getEncoding();
+			}
+			if (encoding == null) {
+				return PgBackendProtocol.DEFAULT_ENCODING;
+			}
+			return encoding;
+		}
 	}
 }

Modified: trunk/runtime/src/main/java/org/teiid/deployers/VDBRepository.java
===================================================================
--- trunk/runtime/src/main/java/org/teiid/deployers/VDBRepository.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/runtime/src/main/java/org/teiid/deployers/VDBRepository.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -45,6 +45,7 @@
 import org.teiid.logging.LogConstants;
 import org.teiid.logging.LogManager;
 import org.teiid.metadata.Datatype;
+import org.teiid.metadata.MetadataException;
 import org.teiid.metadata.MetadataStore;
 import org.teiid.net.ConnectionException;
 import org.teiid.query.function.SystemFunctionManager;
@@ -53,7 +54,6 @@
 import org.teiid.query.metadata.TransformationMetadata.Resource;
 import org.teiid.query.validator.ValidatorReport;
 import org.teiid.runtime.RuntimePlugin;
-import org.teiid.translator.TranslatorException;
 import org.teiid.vdb.runtime.VDBKey;
 
 
@@ -234,7 +234,7 @@
 		try {
 			PgCatalogMetadataStore pg = new PgCatalogMetadataStore(CoreConstants.ODBC_MODEL, getRuntimeTypeMap());
 			return pg.asMetadataStore();
-		} catch (TranslatorException e) {
+		} catch (MetadataException e) {
 			LogManager.logError(LogConstants.CTX_DQP, e, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40002));
 		}
 		return null;

Modified: trunk/runtime/src/main/java/org/teiid/odbc/ODBCClientRemote.java
===================================================================
--- trunk/runtime/src/main/java/org/teiid/odbc/ODBCClientRemote.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/runtime/src/main/java/org/teiid/odbc/ODBCClientRemote.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -33,7 +33,7 @@
 	
 	void initialized(Properties props);
 	
-	void setEncoding(String value);
+	void setEncoding(String value, boolean init);
 	
 	//	AuthenticationCleartextPassword (B)
 	void useClearTextAuthentication();

Modified: trunk/runtime/src/main/java/org/teiid/odbc/ODBCServerRemoteImpl.java
===================================================================
--- trunk/runtime/src/main/java/org/teiid/odbc/ODBCServerRemoteImpl.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/runtime/src/main/java/org/teiid/odbc/ODBCServerRemoteImpl.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -28,7 +28,6 @@
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
-import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
@@ -59,7 +58,9 @@
 import org.teiid.odbc.PGUtil.PgColInfo;
 import org.teiid.query.parser.SQLParserUtil;
 import org.teiid.runtime.RuntimePlugin;
+import org.teiid.transport.LocalServerConnection;
 import org.teiid.transport.ODBCClientInstance;
+import org.teiid.transport.PgBackendProtocol;
 import org.teiid.transport.PgFrontendProtocol.NullTerminatedStringDataInputStream;
 
 /**
@@ -224,14 +225,14 @@
 				info.put("password", password); //$NON-NLS-1$
 			}
 			
-			this.connection =  (ConnectionImpl)driver.connect(url, info);
+			this.connection =  driver.connect(url, info);
+			//Propagate so that we can use in pg methods
+			((LocalServerConnection)this.connection.getServerConnection()).getWorkContext().getSession().addAttchment(ODBCServerRemoteImpl.class, this);
 			int hash = this.connection.getConnectionId().hashCode();
-			Enumeration keys = this.props.propertyNames();
+			Enumeration<?> keys = this.props.propertyNames();
 			while (keys.hasMoreElements()) {
 				String key = (String)keys.nextElement();
-				Statement stmt = this.connection.createStatement();
-				stmt.execute("SET " + key + " '" + this.props.getProperty(key) + "'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
-				stmt.close();
+				this.connection.setExecutionProperty(key, this.props.getProperty(key));
 			}
 			this.client.authenticationSucess(hash, hash);
 			ready();
@@ -851,30 +852,12 @@
 		String encoding = getEncoding();
 		if (encoding != null) {
 			//this may be unnecessary
-			this.client.setEncoding(encoding);
+			this.client.setEncoding(encoding, false);
 		}
 	}
 	
-	private String getEncoding() {
-		StatementImpl t = null;
-		try {
-			t = connection.createStatement();
-			ResultSet rs = t.executeQuery("show client_encoding"); //$NON-NLS-1$
-			if (rs.next()) {
-				return rs.getString(1);
-			}
-		} catch (Exception e) {
-			//don't care
-		} finally {
-			try {
-				if (t != null) {
-					t.close();
-				}
-			} catch (SQLException e) {
-				
-			}
-		}
-		return null;
+	public String getEncoding() {
+		return this.connection.getExecutionProperty(PgBackendProtocol.CLIENT_ENCODING);
 	}
 	
     private final class QueryWorkItem implements Runnable {

Modified: trunk/runtime/src/main/java/org/teiid/runtime/RuntimePlugin.java
===================================================================
--- trunk/runtime/src/main/java/org/teiid/runtime/RuntimePlugin.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/runtime/src/main/java/org/teiid/runtime/RuntimePlugin.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -115,6 +115,7 @@
     	TEIID40101,
     	TEIID40102,
     	TEIID40103,
-    	TEIID40104
+    	TEIID40104, 
+    	TEIID40105
     }
 }

Modified: trunk/runtime/src/main/java/org/teiid/transport/LocalServerConnection.java
===================================================================
--- trunk/runtime/src/main/java/org/teiid/transport/LocalServerConnection.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/runtime/src/main/java/org/teiid/transport/LocalServerConnection.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -193,4 +193,8 @@
 	public boolean supportsContinuous() {
 		return true;
 	}
+	
+	public DQPWorkContext getWorkContext() {
+		return workContext;
+	}
 }

Modified: trunk/runtime/src/main/java/org/teiid/transport/PGCharsetConverter.java
===================================================================
--- trunk/runtime/src/main/java/org/teiid/transport/PGCharsetConverter.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/runtime/src/main/java/org/teiid/transport/PGCharsetConverter.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -72,7 +72,15 @@
 	}
 	
 	public static Charset getCharset(String name) {
-		return charSetMap.get(name);
+		Charset cs = charSetMap.get(name);
+		if (cs == null) {
+			try {
+				return Charset.forName(name);
+			} catch (IllegalArgumentException e) {
+				return null;
+			}
+		}
+		return cs;
 	}
 	
 }

Modified: trunk/runtime/src/main/java/org/teiid/transport/PgBackendProtocol.java
===================================================================
--- trunk/runtime/src/main/java/org/teiid/transport/PgBackendProtocol.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/runtime/src/main/java/org/teiid/transport/PgBackendProtocol.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -177,13 +177,16 @@
 			}			
 		}
 	}
-    
+
+	public static final String DEFAULT_ENCODING = "UTF8";
+	public static final String CLIENT_ENCODING = "client_encoding";
+
     private ChannelBuffer dataOut;
 	private OutputStreamWriter writer;
 
     private Properties props;    
     private Charset encoding = Charset.forName("UTF-8");
-    private String clientEncoding = "UTF8";
+    private String clientEncoding = DEFAULT_ENCODING;
     private ReflectionHelper clientProxy = new ReflectionHelper(ODBCClientRemote.class);
     private ChannelHandlerContext ctx;
     private MessageEvent message;
@@ -231,7 +234,7 @@
 	@Override
 	public void initialized(Properties props) {
 		this.props = props;
-		setEncoding(props.getProperty("client_encoding", "UTF8"));
+		setEncoding(props.getProperty("client_encoding", this.clientEncoding), true);
 	}
 	
 	@Override
@@ -301,15 +304,26 @@
 		sendReadyForQuery(inTransaction, failedTransaction);
 	}
 	
-	public void setEncoding(String value) {
+	public void setEncoding(String value, boolean init) {
+		if (value == null || value.equals(this.clientEncoding)) {
+			return;
+		}
+		this.clientEncoding = value;
 		Charset cs = PGCharsetConverter.getCharset(value);
 		if (cs != null) {
 			this.encoding = cs;
-			this.clientEncoding = value;
-			//TODO: for non-init this should send a parameter status
+			if (!init) {
+				sendParameterStatus(CLIENT_ENCODING, value);
+			}
+		} else {
+			LogManager.logWarning(LogConstants.CTX_ODBC, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40105, value));
 		}
 	}
 	
+	public String getClientEncoding() {
+		return clientEncoding;
+	}
+	
 	public Charset getEncoding() {
 		return encoding;
 	}

Modified: trunk/runtime/src/main/java/org/teiid/transport/PgFrontendProtocol.java
===================================================================
--- trunk/runtime/src/main/java/org/teiid/transport/PgFrontendProtocol.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/runtime/src/main/java/org/teiid/transport/PgFrontendProtocol.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -209,7 +209,7 @@
         }        
         this.user = props.getProperty("user");
         this.databaseName = props.getProperty("database");
-        String clientEncoding = props.getProperty("client_encoding", "UTF8");
+        String clientEncoding = props.getProperty("client_encoding", PgBackendProtocol.DEFAULT_ENCODING);
         props.setProperty("client_encoding", clientEncoding);
         props.setProperty("default_transaction_isolation", "read committed");
         props.setProperty("DateStyle", "ISO");

Modified: trunk/runtime/src/main/resources/org/teiid/runtime/i18n.properties
===================================================================
--- trunk/runtime/src/main/resources/org/teiid/runtime/i18n.properties	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/runtime/src/main/resources/org/teiid/runtime/i18n.properties	2012-10-04 16:08:45 UTC (rev 4513)
@@ -104,4 +104,5 @@
 
 TEIID40101=error setting state {0}
 TEIID40102= {0} Failed to Pull {1}
-TEIID40103={0} timeout pulling {1}
\ No newline at end of file
+TEIID40103={0} timeout pulling {1}
+TEIID40105=Unsupported ODBC client encoding {0}
\ No newline at end of file

Modified: trunk/test-integration/common/src/test/java/org/teiid/transport/TestODBCSocketTransport.java
===================================================================
--- trunk/test-integration/common/src/test/java/org/teiid/transport/TestODBCSocketTransport.java	2012-10-04 12:26:57 UTC (rev 4512)
+++ trunk/test-integration/common/src/test/java/org/teiid/transport/TestODBCSocketTransport.java	2012-10-04 16:08:45 UTC (rev 4513)
@@ -391,4 +391,27 @@
 		ResultSet rs = s.executeQuery("select indkey FROM pg_index");
 		TestMMDatabaseMetaData.compareResultSet(rs);
 	}
+	
+	@Test public void test_pg_client_encoding() throws Exception {
+		Statement s = conn.createStatement();
+		ResultSet rs = s.executeQuery("select pg_client_encoding()");
+		rs.next();
+		assertEquals("UTF8", rs.getString(1));
+
+		s.execute("set client_encoding UTF8");
+		rs = s.executeQuery("select pg_client_encoding()");
+		rs.next();
+		assertEquals("UTF8", rs.getString(1));
+	}
+	
+	/**
+	 * Ensures that the client is notified about the change.  However the driver will
+	 * throw an exception as it requires UTF8
+	 * @throws Exception
+	 */
+	@Test(expected=SQLException.class) public void test_pg_client_encoding1() throws Exception {
+		Statement s = conn.createStatement();
+		s.execute("set client_encoding LATIN1");
+	}
+
 }



More information about the teiid-commits mailing list