Skip to content

Commit

Permalink
Merge pull request #215 from datafuselabs/fix/select-preparement-ts
Browse files Browse the repository at this point in the history
fix: select with preparement for timestamp
  • Loading branch information
hantmac authored May 8, 2024
2 parents 9b8f908 + 82bcc55 commit d22e8cc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public void setString(int i, String s)
String finalS1 = s;
batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, finalS1));
} else {
if (s.contains("'")){
if (s.contains("'")) {
s = s.replace("'", "\\\'");
}
String finalS = s;
Expand Down Expand Up @@ -557,7 +557,11 @@ public void setTime(int i, Time time)
if (time == null) {
batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, null));
} else {
batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, toTimeLiteral(time)));
if (originalSql.toLowerCase().startsWith("select")) {
batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, String.format("%s%s%s", "'", time, "'")));
} else {
batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, toTimeLiteral(time)));
}
}
}

Expand All @@ -568,7 +572,11 @@ public void setTimestamp(int i, Timestamp timestamp)
if (timestamp == null) {
batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, null));
} else {
batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, toTimestampLiteral(timestamp)));
if (originalSql.toLowerCase().startsWith("select")) {
batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, String.format("%s%s%s", "'", timestamp, "'")));
} else {
batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, toTimestampLiteral(timestamp)));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.testng.AssertJUnit.assertEquals;

@Test(timeOut = 10000)
public class TestBasicDriver {
private Connection createConnection()
Expand Down Expand Up @@ -79,7 +80,7 @@ public void testExecuteInvalidSql() {
}

@Test
public void testCreateUserFunction() throws SQLException {
public void testCreateUserFunction() throws SQLException {
String s = "create or replace function add_plus(int,int)\n" +
"returns int\n" +
"language javascript\n" +
Expand Down Expand Up @@ -259,4 +260,18 @@ public void testResultException() {
Assert.assertTrue(e.getMessage().contains("Query failed"));
}
}

@Test(groups = {"IT"})
public void testSelectWithPreparement()
throws SQLException {
try (Connection connection = createConnection()) {
connection.createStatement().execute("create or replace table test_basic_driver.table_time(t timestamp)");
connection.createStatement().execute("insert into test_basic_driver.table_time values('2021-01-01 00:00:00')");
PreparedStatement statement = connection.prepareStatement("SELECT * from test_basic_driver.table_time where t < ?");
statement.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
ResultSet r = statement.executeQuery();
r.next();
Assert.assertEquals(r.getString(1), "2021-01-01 00:00:00.000000");
}
}
}

0 comments on commit d22e8cc

Please sign in to comment.