Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: select with preparement for timestamp #215

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
}
Loading