You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by ha...@apache.org on 2013/06/05 06:55:21 UTC
svn commit: r1489704 - in /hive/trunk/jdbc/src:
java/org/apache/hadoop/hive/jdbc/HivePreparedStatement.java
test/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java
Author: hashutosh
Date: Wed Jun 5 04:55:21 2013
New Revision: 1489704
URL: http://svn.apache.org/r1489704
Log:
HIVE-2304 : Support PreparedStatement.setObject (Ido Hadanny via Ashutosh Chauhan)
Modified:
hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HivePreparedStatement.java
hive/trunk/jdbc/src/test/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java
Modified: hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HivePreparedStatement.java
URL: http://svn.apache.org/viewvc/hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HivePreparedStatement.java?rev=1489704&r1=1489703&r2=1489704&view=diff
==============================================================================
--- hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HivePreparedStatement.java (original)
+++ hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HivePreparedStatement.java Wed Jun 5 04:55:21 2013
@@ -39,6 +39,7 @@ import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
+import java.text.MessageFormat;
import java.util.Calendar;
import java.util.HashMap;
@@ -648,8 +649,32 @@ public class HivePreparedStatement imple
*/
public void setObject(int parameterIndex, Object x) throws SQLException {
- // TODO Auto-generated method stub
- throw new SQLException("Method not supported");
+ 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, ((Character) 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()));
+ }
}
/*
Modified: hive/trunk/jdbc/src/test/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java
URL: http://svn.apache.org/viewvc/hive/trunk/jdbc/src/test/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java?rev=1489704&r1=1489703&r2=1489704&view=diff
==============================================================================
--- hive/trunk/jdbc/src/test/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java (original)
+++ hive/trunk/jdbc/src/test/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java Wed Jun 5 04:55:21 2013
@@ -32,6 +32,7 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Arrays;
+import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -229,49 +230,18 @@ public class TestJdbcDriver extends Test
///////////////////////////////////////////////
//////////////////// 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.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();
@@ -326,6 +296,80 @@ public class TestJdbcDriver extends Test
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.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.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);
}
public final void testSelectAll() throws Exception {