From: Sergei Kalashnikov <ztarvos@gmail.com> To: tarantool-patches@freelists.org Cc: Sergei Kalashnikov <ztarvos@gmail.com> Subject: [tarantool-patches] [PATCH] jdbc: fix date/time parameters binding Date: Wed, 10 Oct 2018 14:48:20 +0300 [thread overview] Message-ID: <1539172100-10669-1-git-send-email-ztarvos@gmail.com> (raw) 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
next reply other threads:[~2018-10-10 11:48 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2018-10-10 11:48 Sergei Kalashnikov [this message] 2018-10-29 17:19 ` [tarantool-patches] " Alexander Turenko
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=1539172100-10669-1-git-send-email-ztarvos@gmail.com \ --to=ztarvos@gmail.com \ --cc=tarantool-patches@freelists.org \ --subject='Re: [tarantool-patches] [PATCH] jdbc: fix date/time parameters binding' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox