You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by xu...@apache.org on 2014/10/27 19:44:51 UTC
svn commit: r1634639 - in /hive/trunk:
itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java
jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java
Author: xuefu
Date: Mon Oct 27 18:44:50 2014
New Revision: 1634639
URL: http://svn.apache.org/r1634639
Log:
HIVE-6165: Unify HivePreparedStatement from jdbc:hive and jdbc:hive2 (Helmut via Xuefu)
Modified:
hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java
hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java
Modified: hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java
URL: http://svn.apache.org/viewvc/hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java?rev=1634639&r1=1634638&r2=1634639&view=diff
==============================================================================
--- hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java (original)
+++ hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java Mon Oct 27 18:44:50 2014
@@ -39,6 +39,7 @@ import java.sql.SQLException;
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;
@@ -417,50 +418,18 @@ public class TestJdbcDriver2 {
///////////////////////////////////////////////
//////////////////// 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();
@@ -515,6 +484,82 @@ public class TestJdbcDriver2 {
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);
}
/**
Modified: hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java
URL: http://svn.apache.org/viewvc/hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java?rev=1634639&r1=1634638&r2=1634639&view=diff
==============================================================================
--- hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java (original)
+++ hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java Mon Oct 27 18:44:50 2014
@@ -37,6 +37,8 @@ import java.sql.SQLException;
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 class HivePreparedStatement exten
*/
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 class HivePreparedStatement exten
*/
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 class HivePreparedStatement exten
*/
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()));
+ }
}
/*