From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 92D1D2BACF for ; Wed, 10 Oct 2018 07:48:29 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PpwUGRll_l9Q for ; Wed, 10 Oct 2018 07:48:29 -0400 (EDT) Received: from mail-lf1-f65.google.com (mail-lf1-f65.google.com [209.85.167.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 3DC732BA80 for ; Wed, 10 Oct 2018 07:48:29 -0400 (EDT) Received: by mail-lf1-f65.google.com with SMTP id m80-v6so3715150lfi.12 for ; Wed, 10 Oct 2018 04:48:29 -0700 (PDT) From: Sergei Kalashnikov Subject: [tarantool-patches] [PATCH] jdbc: fix date/time parameters binding Date: Wed, 10 Oct 2018 14:48:20 +0300 Message-Id: <1539172100-10669-1-git-send-email-ztarvos@gmail.com> Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org Cc: Sergei Kalashnikov Fixed exception when packing date/time parameters of prepared statement. Added test for parameter binding methods. Closes #43 --- https://github.com/tarantool/tarantool-java/issues/43 https://github.com/ztarvos/tarantool-java/commits/gh-43-fix-jdbc-datetime-param .../java/org/tarantool/jdbc/SQLMsgPackLite.java | 6 +- .../tarantool/jdbc/JdbcPreparedStatementIT.java | 66 ++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/tarantool/jdbc/SQLMsgPackLite.java b/src/main/java/org/tarantool/jdbc/SQLMsgPackLite.java index 0f73efb..1506877 100644 --- a/src/main/java/org/tarantool/jdbc/SQLMsgPackLite.java +++ b/src/main/java/org/tarantool/jdbc/SQLMsgPackLite.java @@ -17,11 +17,11 @@ public class SQLMsgPackLite extends MsgPackLite { public void pack(Object item, OutputStream os) throws IOException { if(item instanceof Date) { super.pack(((Date)item).getTime(), os); - } if(item instanceof Time) { + } else if(item instanceof Time) { super.pack(((Time)item).getTime(), os); - } if(item instanceof Timestamp) { + } else if(item instanceof Timestamp) { super.pack(((Timestamp)item).getTime(), os); - } if(item instanceof BigDecimal) { + } else if(item instanceof BigDecimal) { super.pack(((BigDecimal)item).toPlainString(), os); } else { super.pack(item, os); diff --git a/src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java b/src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java index f356f6b..68628ef 100644 --- a/src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java +++ b/src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java @@ -3,9 +3,13 @@ package org.tarantool.jdbc; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.AfterEach; +import java.math.BigDecimal; +import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -64,4 +68,66 @@ public class JdbcPreparedStatementIT extends AbstractJdbcIT { assertEquals("thousand", getRow("test", 1000).get(1)); } + + @Test + public void testSetParameter() throws SQLException { + prep = conn.prepareStatement("INSERT INTO test_types VALUES (" + + "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + assertNotNull(prep); + + prep.setInt(1, 1000);//INT + prep.setString(2, (String)testRow[1]);//CHAR + prep.setString(3, (String)testRow[2]);//VARCHAR + prep.setString(4, (String)testRow[3]);//LONGVARCHAR + prep.setBigDecimal(5, (BigDecimal)testRow[4]);//NUMERIC + prep.setBigDecimal(6, (BigDecimal)testRow[5]);//DECIMAL + prep.setBoolean(7, (Boolean)testRow[6]);//BIT + prep.setByte(8, (Byte)testRow[7]);//TINYINT + prep.setShort(9, (Short)testRow[8]);//SMALLINT + prep.setInt(10, (Integer)testRow[9]);//INTEGER + prep.setLong(11, (Long)testRow[10]);//BIGINT + prep.setFloat(12, (Float)testRow[11]);//REAL + prep.setDouble(13, (Double)testRow[12]);//FLOAT + prep.setBytes(14, (byte[])testRow[13]);//BINARY + prep.setBytes(15, (byte[])testRow[14]);//VARBINARY + prep.setBytes(16, (byte[])testRow[15]);//LONGVARBINARY + prep.setDate(17, (Date)testRow[16]);//DATE + prep.setTime(18, (Time)testRow[17]);//TIME + prep.setTimestamp(19, (Timestamp)testRow[18]);//TIMESTAMP + + int count = prep.executeUpdate(); + assertEquals(1, count); + + prep.close(); + + prep = conn.prepareStatement("SELECT * FROM test_types WHERE f1 = ?"); + prep.setInt(1, 1000); + + ResultSet rs = prep.executeQuery(); + assertNotNull(rs); + + assertTrue(rs.next()); + assertEquals(1000, rs.getInt(1));//INT + assertEquals(testRow[1], rs.getString(2));//CHAR + assertEquals(testRow[2], rs.getString(3));//VARCHAR + assertEquals(testRow[3], rs.getString(4)); //LONGVARCHAR + assertEquals(testRow[4], rs.getBigDecimal(5));//NUMERIC + assertEquals(testRow[5], rs.getBigDecimal(6));//DECIMAL + assertEquals(testRow[6], rs.getBoolean(7));//BIT + assertEquals(testRow[7], rs.getByte(8));//TINYINT + assertEquals(testRow[8], rs.getShort(9));//SMALLINT + assertEquals(testRow[9], rs.getInt(10));//INTEGER + assertEquals(testRow[10], rs.getLong(11));//BIGINT + assertEquals((Float)testRow[11], rs.getFloat(12), 1e-10f);//REAL + assertEquals((Double)testRow[12], rs.getDouble(13), 1e-10d);//FLOAT + //Issue#45 + //assertTrue(Arrays.equals((byte[])testRow[13], rs.getBytes(14)));//BINARY + //assertTrue(Arrays.equals((byte[])testRow[14], rs.getBytes(15)));//VARBINARY + //assertTrue(Arrays.equals((byte[])testRow[15], rs.getBytes(16)));//LONGVARBINARY + assertEquals(testRow[16], rs.getDate(17));//DATE + assertEquals(testRow[17], rs.getTime(18));//TIME + assertEquals(testRow[18], rs.getTimestamp(19));//TIMESTAMP + + rs.close(); + } } -- 1.8.3.1