diff --git itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java index 9388a09..4c42ffd 100644 --- itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java +++ itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java @@ -39,6 +39,7 @@ import java.sql.Statement; import java.sql.Types; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -53,7 +54,6 @@ import org.apache.hadoop.hive.metastore.TableType; import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.hive.ql.processors.DfsProcessor; -import org.apache.hadoop.hive.ql.processors.SetProcessor; import org.apache.hive.common.util.HiveVersionInfo; import org.apache.hive.jdbc.Utils.JdbcConnectionParams; import org.apache.hive.service.cli.operation.ClassicTableTypeMapping; @@ -419,50 +419,18 @@ public void testPrepareStatement() { /////////////////////////////////////////////// //////////////////// correct testcase + //////////////////// executed twice: once with the typed ps setters, once with the generic setObject ////////////////////////////////////////////// try { - PreparedStatement ps = con.prepareStatement(sql); - - ps.setBoolean(1, true); - ps.setBoolean(2, true); - - ps.setShort(3, Short.valueOf("1")); - ps.setInt(4, 2); - ps.setFloat(5, 3f); - ps.setDouble(6, Double.valueOf(4)); - ps.setString(7, "test'string\""); - ps.setLong(8, 5L); - ps.setByte(9, (byte) 1); - ps.setByte(10, (byte) 1); - ps.setString(11, "2012-01-01"); - - ps.setMaxRows(2); - - assertTrue(true); - + PreparedStatement ps = createPreapredStatementUsingSetXXX(sql); ResultSet res = ps.executeQuery(); - assertNotNull(res); - - while (res.next()) { - assertEquals("2011-03-25", res.getString("ddate")); - assertEquals("10", res.getString("num")); - assertEquals((byte) 10, res.getByte("num")); - assertEquals("2011-03-25", res.getDate("ddate").toString()); - assertEquals(Double.valueOf(10).doubleValue(), res.getDouble("num"), 0.1); - assertEquals(10, res.getInt("num")); - assertEquals(Short.valueOf("10").shortValue(), res.getShort("num")); - assertEquals(10L, res.getLong("num")); - assertEquals(true, res.getBoolean("bv")); - Object o = res.getObject("ddate"); - assertNotNull(o); - o = res.getObject("num"); - assertNotNull(o); - } - res.close(); - assertTrue(true); + assertPreparedStatementResultAsExpected(res); + ps.close(); + ps = createPreapredStatementUsingSetObject(sql); + res = ps.executeQuery(); + assertPreparedStatementResultAsExpected(res); ps.close(); - assertTrue(true); } catch (Exception e) { e.printStackTrace(); @@ -517,6 +485,82 @@ public void testPrepareStatement() { assertNotNull( "Execute the invalid setted sql statement should throw exception", expectedException); + + // setObject to the yet unknown type java.util.Date + expectedException = null; + try { + PreparedStatement ps = con.prepareStatement(sql); + ps.setObject(1, new Date()); + ps.executeQuery(); + } catch (Exception e) { + expectedException = e; + } + assertNotNull( + "Setting to an unknown type should throw an exception", + expectedException); + + } + + private PreparedStatement createPreapredStatementUsingSetObject(String sql) throws SQLException { + PreparedStatement ps = con.prepareStatement(sql); + + ps.setObject(1, true); //setBoolean + ps.setObject(2, true); //setBoolean + + ps.setObject(3, Short.valueOf("1")); //setShort + ps.setObject(4, 2); //setInt + ps.setObject(5, 3f); //setFloat + ps.setObject(6, Double.valueOf(4)); //setDouble + ps.setObject(7, "test'string\""); //setString + ps.setObject(8, 5L); //setLong + ps.setObject(9, (byte) 1); //setByte + ps.setObject(10, (byte) 1); //setByte + ps.setString(11, "2012-01-01"); //setString + + ps.setMaxRows(2); + return ps; + } + + private PreparedStatement createPreapredStatementUsingSetXXX(String sql) throws SQLException { + PreparedStatement ps = con.prepareStatement(sql); + + ps.setBoolean(1, true); //setBoolean + ps.setBoolean(2, true); //setBoolean + + ps.setShort(3, Short.valueOf("1")); //setShort + ps.setInt(4, 2); //setInt + ps.setFloat(5, 3f); //setFloat + ps.setDouble(6, Double.valueOf(4)); //setDouble + ps.setString(7, "test'string\""); //setString + ps.setLong(8, 5L); //setLong + ps.setByte(9, (byte) 1); //setByte + ps.setByte(10, (byte) 1); //setByte + ps.setString(11, "2012-01-01"); //setString + + ps.setMaxRows(2); + return ps; + } + + private void assertPreparedStatementResultAsExpected(ResultSet res ) throws SQLException { + assertNotNull(res); + + while (res.next()) { + assertEquals("2011-03-25", res.getString("ddate")); + assertEquals("10", res.getString("num")); + assertEquals((byte) 10, res.getByte("num")); + assertEquals("2011-03-25", res.getDate("ddate").toString()); + assertEquals(Double.valueOf(10).doubleValue(), res.getDouble("num"), 0.1); + assertEquals(10, res.getInt("num")); + assertEquals(Short.valueOf("10").shortValue(), res.getShort("num")); + assertEquals(10L, res.getLong("num")); + assertEquals(true, res.getBoolean("bv")); + Object o = res.getObject("ddate"); + assertNotNull(o); + o = res.getObject("num"); + assertNotNull(o); + } + res.close(); + assertTrue(true); } /** diff --git jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java index 8ea125a..8a0671f 100644 --- jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java +++ jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java @@ -37,6 +37,8 @@ import java.sql.SQLXML; import java.sql.Time; import java.sql.Timestamp; +import java.sql.Types; +import java.text.MessageFormat; import java.util.Calendar; import java.util.HashMap; import java.util.Scanner; @@ -564,8 +566,7 @@ public void setNString(int parameterIndex, String value) throws SQLException { */ public void setNull(int parameterIndex, int sqlType) throws SQLException { - // TODO Auto-generated method stub - throw new SQLException("Method not supported"); + this.parameters.put(parameterIndex, "NULL"); } /* @@ -575,8 +576,7 @@ public void setNull(int parameterIndex, int sqlType) throws SQLException { */ public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException { - // TODO Auto-generated method stub - throw new SQLException("Method not supported"); + this.parameters.put(paramIndex, "NULL"); } /* @@ -586,8 +586,38 @@ public void setNull(int paramIndex, int sqlType, String typeName) throws SQLExce */ public void setObject(int parameterIndex, Object x) throws SQLException { - // TODO Auto-generated method stub - throw new SQLException("Method not supported"); + if (x == null) { + setNull(parameterIndex, Types.NULL); + } else if (x instanceof String) { + setString(parameterIndex, (String) x); + } else if (x instanceof Short) { + setShort(parameterIndex, ((Short) x).shortValue()); + } else if (x instanceof Integer) { + setInt(parameterIndex, ((Integer) x).intValue()); + } else if (x instanceof Long) { + setLong(parameterIndex, ((Long) x).longValue()); + } else if (x instanceof Float) { + setFloat(parameterIndex, ((Float) x).floatValue()); + } else if (x instanceof Double) { + setDouble(parameterIndex, ((Double) x).doubleValue()); + } else if (x instanceof Boolean) { + setBoolean(parameterIndex, ((Boolean) x).booleanValue()); + } else if (x instanceof Byte) { + setByte(parameterIndex, ((Byte) x).byteValue()); + } else if (x instanceof Character) { + setString(parameterIndex, x.toString()); + } else if (x instanceof Timestamp) { + setString(parameterIndex, x.toString()); + } else if (x instanceof BigDecimal) { + setString(parameterIndex, x.toString()); + } else { + // Can't infer a type. + throw new SQLException( + MessageFormat + .format( + "Can''t infer the SQL type to use for an instance of {0}. Use setObject() with an explicit Types value to specify the type to use.", + x.getClass().getName())); + } } /*