This is an automated email from the git hooks/post-receive script. ebourg-guest pushed a commit to annotated tag REL9_3_1100 in repository libpostgresql-jdbc-java.
commit b8f72a6e06800091b19cb1f55e2152526f3076cb Author: Tom Dunstan <[email protected]> Date: Sun Jul 7 12:53:43 2013 +0930 Make PreparedStatement.setObject(pos, value, Types.VARCHAR) respect stringtype=unspecified --- org/postgresql/jdbc2/AbstractJdbc2Statement.java | 10 +- org/postgresql/test/TestUtil.java | 45 +++++++ org/postgresql/test/jdbc3/Jdbc3TestSuite.java | 1 + .../test/jdbc3/StringTypeParameterTest.java | 133 +++++++++++++++++++++ 4 files changed, 186 insertions(+), 3 deletions(-) diff --git a/org/postgresql/jdbc2/AbstractJdbc2Statement.java b/org/postgresql/jdbc2/AbstractJdbc2Statement.java index 7671cf6..30252dd 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Statement.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Statement.java @@ -1388,7 +1388,11 @@ public abstract class AbstractJdbc2Statement implements BaseStatement public void setString(int parameterIndex, String x) throws SQLException { checkClosed(); - setString(parameterIndex, x, (connection.getStringVarcharFlag() ? Oid.VARCHAR : Oid.UNSPECIFIED)); + setString(parameterIndex, x, getStringType()); + } + + private int getStringType() { + return (connection.getStringVarcharFlag() ? Oid.VARCHAR : Oid.UNSPECIFIED); } protected void setString(int parameterIndex, String x, int oid) throws SQLException @@ -1398,7 +1402,7 @@ public abstract class AbstractJdbc2Statement implements BaseStatement if (x == null) { if ( adjustIndex ) - parameterIndex--; + parameterIndex--; preparedParameters.setNull( parameterIndex, oid); } else @@ -1786,7 +1790,7 @@ public abstract class AbstractJdbc2Statement implements BaseStatement break; case Types.VARCHAR: case Types.LONGVARCHAR: - setString(parameterIndex, pgType.toString(), Oid.VARCHAR); + setString(parameterIndex, pgType.toString(), getStringType()); break; case Types.DATE: if (in instanceof java.sql.Date) diff --git a/org/postgresql/test/TestUtil.java b/org/postgresql/test/TestUtil.java index 2ef7ba6..4c42aee 100644 --- a/org/postgresql/test/TestUtil.java +++ b/org/postgresql/test/TestUtil.java @@ -313,6 +313,33 @@ public class TestUtil } } + /** + * Helper creates an enum type + * @param con Connection + * @param name String + * @param values String + * @throws SQLException + */ + + public static void createEnumType( Connection con, + String name, + String values) throws SQLException + { + Statement st = con.createStatement(); + try + { + dropType(con, name); + + + // Now create the table + st.executeUpdate("create type " + name + " as enum (" + values + ")"); + } + finally + { + st.close(); + } + } + /* * drop a sequence because older versions don't have dependency * information for serials @@ -359,6 +386,24 @@ public class TestUtil } /* + * Helper - drops a type + */ + public static void dropType(Connection con, String type) throws SQLException + { + Statement stmt = con.createStatement(); + try + { + String sql = "DROP TYPE " + type; + stmt.executeUpdate(sql); + } + catch (SQLException ex) + { + if (!con.getAutoCommit()) + throw ex; + } + } + + /* * Helper - generates INSERT SQL - very simple */ public static String insertSQL(String table, String values) diff --git a/org/postgresql/test/jdbc3/Jdbc3TestSuite.java b/org/postgresql/test/jdbc3/Jdbc3TestSuite.java index 6d22d41..a4ab275 100644 --- a/org/postgresql/test/jdbc3/Jdbc3TestSuite.java +++ b/org/postgresql/test/jdbc3/Jdbc3TestSuite.java @@ -50,6 +50,7 @@ public class Jdbc3TestSuite extends TestSuite suite.addTestSuite(Jdbc3BlobTest.class); suite.addTestSuite(DatabaseMetaDataTest.class); suite.addTestSuite(SendRecvBufferSizeTest.class); + suite.addTestSuite(StringTypeParameterTest.class); return suite; } } diff --git a/org/postgresql/test/jdbc3/StringTypeParameterTest.java b/org/postgresql/test/jdbc3/StringTypeParameterTest.java new file mode 100644 index 0000000..1b887d3 --- /dev/null +++ b/org/postgresql/test/jdbc3/StringTypeParameterTest.java @@ -0,0 +1,133 @@ +/*------------------------------------------------------------------------- +* +* Copyright (c) 2005-2011, PostgreSQL Global Development Group +* +* +*------------------------------------------------------------------------- +*/ +package org.postgresql.test.jdbc3; + +import junit.framework.TestCase; +import org.postgresql.test.TestUtil; + +import java.sql.*; +import java.util.Properties; + +public class StringTypeParameterTest extends TestCase { + + private Connection _conn; + + public StringTypeParameterTest(String name) { + super(name); + } + + protected void setUp(String stringType) throws Exception { + Properties props = new Properties(); + if(stringType != null) { + props.put("stringtype", stringType); + } + _conn = TestUtil.openDB(props); + TestUtil.createEnumType(_conn, "mood", "'happy', 'sad'"); + TestUtil.createTable(_conn, "stringtypetest", "m mood"); + } + + protected void tearDown() throws SQLException { + if(_conn != null) { + TestUtil.dropTable(_conn, "stringtypetest"); + TestUtil.dropType(_conn, "mood"); + TestUtil.closeDB(_conn); + } + } + + public void testParameterStringTypeVarchar() throws Exception { + if (!TestUtil.isProtocolVersion(_conn, 3)) + return; + testParameterVarchar("varchar"); + } + + public void testParameterStringTypeNotSet() throws Exception { + if (!TestUtil.isProtocolVersion(_conn, 3)) + return; + testParameterVarchar(null); + } + + private void testParameterVarchar(String param) throws Exception { + setUp(param); + + PreparedStatement update = _conn.prepareStatement("insert into stringtypetest (m) values (?)"); + update.setString(1, "sad"); + try { + update.executeUpdate(); + fail("Expected exception thrown"); + } catch(SQLException e) { + // expected + } + + update.clearParameters(); + update.setObject(1, "sad", Types.VARCHAR); + try { + update.executeUpdate(); + fail("Expected exception thrown"); + } catch(SQLException e) { + // expected + } + + update.clearParameters(); + update.setObject(1, "happy", Types.OTHER); + update.executeUpdate(); + // all good + update.close(); + + PreparedStatement query = _conn.prepareStatement("select * from stringtypetest where m = ? or m = ?"); + query.setString(1, "sad"); + try { + query.executeQuery(); + fail("Expected exception thrown"); + } catch(SQLException e) { + // expected + } + + query.clearParameters(); + query.setObject(2, "sad", Types.VARCHAR); + try { + query.executeQuery(); + fail("Expected exception thrown"); + } catch(SQLException e) { + // expected + } + + query.clearParameters(); + query.setObject(1, "happy", Types.OTHER); + query.executeQuery().close(); + // all good + query.close(); + + } + + public void testParameterUnspecified() throws Exception { + setUp("unspecified"); + + PreparedStatement update = _conn.prepareStatement("insert into stringtypetest (m) values (?)"); + update.setString(1, "happy"); + update.executeUpdate(); + // all good + + update.clearParameters(); + update.setObject(1, "happy", Types.VARCHAR); + update.executeUpdate(); + // all good + update.close(); + + PreparedStatement query = _conn.prepareStatement("select * from stringtypetest where m = ?"); + query.setString(1, "happy"); + query.executeQuery().close(); + + query.clearParameters(); + query.setObject(1, "happy", Types.VARCHAR); + query.executeQuery().close(); + + // all good + query.close(); + + } +} -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git _______________________________________________ pkg-java-commits mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-java-commits

