changeset 131:0112d8496636

Added test program for bug 6349. Added case Types.CLOB: at several places in setObject(). This fixes bug 6349 Also replaced "" + x into direct <Class>.toString(x)
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 13 Jul 2017 15:35:09 +0200 (2017-07-13)
parents f9ba2b04db7b
children ed499bf51b17
files src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java tests/Bug_PrepStmtSetObject_CLOB_6349.java
diffstat 2 files changed, 98 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
@@ -1198,7 +1198,7 @@ public class MonetPreparedStatement
 	 */
 	@Override
 	public void setBoolean(int parameterIndex, boolean x) throws SQLException {
-		setValue(parameterIndex, "" + x);
+		setValue(parameterIndex, Boolean.toString(x));
 	}
 
 	/**
@@ -1211,7 +1211,7 @@ public class MonetPreparedStatement
 	 */
 	@Override
 	public void setByte(int parameterIndex, byte x) throws SQLException {
-		setValue(parameterIndex, "" + x);
+		setValue(parameterIndex, Byte.toString(x));
 	}
 
 	static final String HEXES = "0123456789ABCDEF";
@@ -1476,7 +1476,7 @@ public class MonetPreparedStatement
 	 */
 	@Override
 	public void setDouble(int parameterIndex, double x) throws SQLException {
-		setValue(parameterIndex, "" + x);
+		setValue(parameterIndex, Double.toString(x));
 	}
 
 	/**
@@ -1489,7 +1489,7 @@ public class MonetPreparedStatement
 	 */
 	@Override
 	public void setFloat(int parameterIndex, float x) throws SQLException {
-		setValue(parameterIndex, "" + x);
+		setValue(parameterIndex, Float.toString(x));
 	}
 
 	/**
@@ -1502,7 +1502,7 @@ public class MonetPreparedStatement
 	 */
 	@Override
 	public void setInt(int parameterIndex, int x) throws SQLException {
-		setValue(parameterIndex, "" + x);
+		setValue(parameterIndex, Integer.toString(x));
 	}
 
 	/**
@@ -1515,7 +1515,7 @@ public class MonetPreparedStatement
 	 */
 	@Override
 	public void setLong(int parameterIndex, long x) throws SQLException {
-		setValue(parameterIndex, "" + x);
+		setValue(parameterIndex, Long.toString(x));
 	}
 
 	/**
@@ -1769,6 +1769,12 @@ public class MonetPreparedStatement
 		// this is according to table B-5
 		if (x instanceof String) {
 			switch (targetSqlType) {
+				case Types.CHAR:
+				case Types.VARCHAR:
+				case Types.LONGVARCHAR:
+				case Types.CLOB:
+					setString(parameterIndex, (String)x);
+				break;
 				case Types.TINYINT:
 				case Types.SMALLINT:
 				case Types.INTEGER:
@@ -1832,11 +1838,6 @@ public class MonetPreparedStatement
 				case Types.BOOLEAN:
 					setBoolean(parameterIndex, (Boolean.valueOf((String)x)).booleanValue());
 				break;
-				case Types.CHAR:
-				case Types.VARCHAR:
-				case Types.LONGVARCHAR:
-					setString(parameterIndex, (String)x);
-				break;
 				case Types.BINARY:
 				case Types.VARBINARY:
 				case Types.LONGVARBINARY:
@@ -1933,6 +1934,7 @@ public class MonetPreparedStatement
 				case Types.CHAR:
 				case Types.VARCHAR:
 				case Types.LONGVARCHAR:
+				case Types.CLOB:
 					setString(parameterIndex, x.toString());
 				break;
 				default:
@@ -1978,7 +1980,8 @@ public class MonetPreparedStatement
 				case Types.CHAR:
 				case Types.VARCHAR:
 				case Types.LONGVARCHAR:
-					setString(parameterIndex, "" + val);
+				case Types.CLOB:
+					setString(parameterIndex, x.toString());
 				break;
 				default:
 					throw new SQLException("Conversion not allowed", "M1M05");
@@ -1989,9 +1992,21 @@ public class MonetPreparedStatement
 				case Types.BIGINT:
 					setLong(parameterIndex, num.longValue());
 				break;
+				case Types.DECIMAL:
+				case Types.NUMERIC:
+				{
+					BigDecimal dec;
+					try {
+						dec = new BigDecimal(num);
+					} catch (NumberFormatException e) {
+						throw new SQLException("Internal error: unable to create template BigDecimal: " + e.getMessage(), "M0M03");
+					}
+					setBigDecimal(parameterIndex, dec);
+				} break;
 				case Types.CHAR:
 				case Types.VARCHAR:
 				case Types.LONGVARCHAR:
+				case Types.CLOB:
 					setString(parameterIndex, x.toString());
 				break;
 				default:
@@ -2014,15 +2029,8 @@ public class MonetPreparedStatement
 				x instanceof java.util.Date)
 		{
 			switch (targetSqlType) {
-				case Types.CHAR:
-				case Types.VARCHAR:
-				case Types.LONGVARCHAR:
-					setString(parameterIndex, x.toString());
-				break;
 				case Types.DATE:
-					if (x instanceof Time) {
-						throw new SQLException("Conversion not allowed", "M1M05");
-					} else if (x instanceof java.sql.Date) {
+					if (x instanceof java.sql.Date) {
 						setDate(parameterIndex, (java.sql.Date)x);
 					} else if (x instanceof Timestamp) {
 						setDate(parameterIndex, new java.sql.Date(((Timestamp)x).getTime()));
@@ -2032,13 +2040,13 @@ public class MonetPreparedStatement
 					} else if (x instanceof Calendar) {
 						setDate(parameterIndex, new java.sql.Date(
 									((Calendar)x).getTimeInMillis()));
+					} else {
+						throw new SQLException("Conversion not allowed", "M1M05");
 					}
 				break;
 				case Types.TIME:
 					if (x instanceof Time) {
 						setTime(parameterIndex, (Time)x);
-					} else if (x instanceof java.sql.Date) {
-						throw new SQLException("Conversion not allowed", "M1M05");
 					} else if (x instanceof Timestamp) {
 						setTime(parameterIndex, new Time(((Timestamp)x).getTime()));
 					} else if (x instanceof java.util.Date) {
@@ -2047,23 +2055,31 @@ public class MonetPreparedStatement
 					} else if (x instanceof Calendar) {
 						setTime(parameterIndex, new java.sql.Time(
 									((Calendar)x).getTimeInMillis()));
+					} else {
+						throw new SQLException("Conversion not allowed", "M1M05");
 					}
 				break;
 				case Types.TIMESTAMP:
-					if (x instanceof Time) {
-						throw new SQLException("Conversion not allowed", "M1M05");
+					if (x instanceof Timestamp) {
+						setTimestamp(parameterIndex, (Timestamp)x);
 					} else if (x instanceof java.sql.Date) {
 						setTimestamp(parameterIndex, new Timestamp(((java.sql.Date)x).getTime()));
-					} else if (x instanceof Timestamp) {
-						setTimestamp(parameterIndex, (Timestamp)x);
 					} else if (x instanceof java.util.Date) {
 						setTimestamp(parameterIndex, new java.sql.Timestamp(
 									((java.util.Date)x).getTime()));
 					} else if (x instanceof Calendar) {
 						setTimestamp(parameterIndex, new java.sql.Timestamp(
 									((Calendar)x).getTimeInMillis()));
+					} else {
+						throw new SQLException("Conversion not allowed", "M1M05");
 					}
 				break;
+				case Types.CHAR:
+				case Types.VARCHAR:
+				case Types.LONGVARCHAR:
+				case Types.CLOB:
+					setString(parameterIndex, x.toString());
+				break;
 				default:
 					throw new SQLException("Conversion not allowed", "M1M05");
 			}
@@ -2281,7 +2297,7 @@ public class MonetPreparedStatement
 	 */
 	@Override
 	public void setShort(int parameterIndex, short x) throws SQLException {
-		setValue(parameterIndex, "" + x);
+		setValue(parameterIndex, Short.toString(x));
 	}
 
 	/**
new file mode 100644
--- /dev/null
+++ b/tests/Bug_PrepStmtSetObject_CLOB_6349.java
@@ -0,0 +1,55 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2017 MonetDB B.V.
+ */
+
+import java.sql.*;
+
+public class Bug_PrepStmtSetObject_CLOB_6349 {
+	public static void main(String[] args) throws Exception {
+		// Class.forName("nl.cwi.monetdb.jdbc.MonetDriver");	// not needed anymore for self registering JDBC drivers
+		Connection con = DriverManager.getConnection(args[0]);
+		Statement stmt = con.createStatement();
+		PreparedStatement pstmt = null;
+		ParameterMetaData pmd = null;
+		ResultSet rs = null;
+		ResultSetMetaData rsmd = null;
+
+		System.out.println("0. true\t" + con.getAutoCommit());
+
+		try {
+			stmt.executeUpdate("CREATE TABLE PrepStmtSetObject_CLOB (myint INT, myvarchar VARCHAR(15), myclob CLOB)");
+			stmt.executeUpdate("INSERT INTO PrepStmtSetObject_CLOB VALUES (123, 'A string', 'A longer string')");
+			stmt.executeUpdate("INSERT INTO PrepStmtSetObject_CLOB VALUES (NULL, NULL, NULL)");  // all NULLs
+
+			pstmt = con.prepareStatement("SELECT myclob, myvarchar, myint FROM PrepStmtSetObject_CLOB WHERE myclob = ?");
+			pmd = pstmt.getParameterMetaData();
+			System.out.println("Prepared Query has " + pmd.getParameterCount() + " parameters. Type of first is: " + pmd.getParameterTypeName(1));
+			rsmd = pstmt.getMetaData();
+			System.out.println("Prepared Query has " + rsmd.getColumnCount() + " columns. Type of first is: " + rsmd.getColumnTypeName(1));
+
+			pstmt.setObject(1, "A longer string");
+			rs = pstmt.executeQuery();
+			rsmd = rs.getMetaData();
+			System.out.println("Query ResultSet has " + rsmd.getColumnCount() + " columns. Type of first is: " + rsmd.getColumnTypeName(1));
+
+			stmt.executeUpdate("DROP TABLE PrepStmtSetObject_CLOB");
+
+		} catch (SQLException e) {
+			System.out.println("FAILED :( "+ e.getMessage());
+			while ((e = e.getNextException()) != null)
+				System.out.println("FAILED :( " + e.getMessage());
+			System.out.println("ABORTING TEST!!!");
+		} finally {
+			if (rs != null)    rs.close();
+			if (pstmt != null) pstmt.close();
+			stmt.close();
+		}
+
+		con.close();
+	}
+}
+