Skip to content

Commit 05c657b

Browse files
authored
Merge pull request #325 from databendlabs/fix/insert-with-select
fix: insert with select case
2 parents dcecc82 + 040d05d commit 05c657b

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,8 @@ public void setBigDecimal(int i, BigDecimal bigDecimal)
532532
public void setString(int i, String s)
533533
throws SQLException {
534534
checkOpen();
535-
if (originalSql.toLowerCase().startsWith("insert") ||
536-
originalSql.toLowerCase().startsWith("replace")) {
535+
if ((originalSql.toLowerCase().startsWith("insert") ||
536+
originalSql.toLowerCase().startsWith("replace")) && !originalSql.toLowerCase().contains("select")) {
537537
String finalS1 = s;
538538
batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, finalS1));
539539
} else {

databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,4 +641,47 @@ public void testExecuteUpdate() throws SQLException {
641641
conn.createStatement().execute("delete from test_prepare_statement");
642642
}
643643

644+
@Test
645+
646+
public void testInsertWithSelect() throws SQLException {
647+
Connection conn = Utils.createConnection();
648+
conn.createStatement().execute("delete from test_prepare_statement");
649+
650+
String insertSql = "insert into test_prepare_statement select a, b from test_prepare_statement where b = ?";
651+
try (PreparedStatement statement = conn.prepareStatement(insertSql)) {
652+
statement.setString(1, "a");
653+
int insertedRows = statement.executeUpdate();
654+
Assertions.assertEquals(0, insertedRows, "should not insert any rows as the table is empty");
655+
}
656+
657+
// Insert some data
658+
String insertDataSql = "insert into test_prepare_statement values (?,?)";
659+
try (PreparedStatement statement = conn.prepareStatement(insertDataSql)) {
660+
statement.setInt(1, 1);
661+
statement.setString(2, "a");
662+
statement.executeUpdate();
663+
664+
statement.setInt(1, 2);
665+
statement.setString(2, "b");
666+
statement.executeUpdate();
667+
}
668+
669+
// Now try to insert again with select
670+
try (PreparedStatement statement = conn.prepareStatement(insertSql)) {
671+
statement.setString(1, "a");
672+
int insertedRows = statement.executeUpdate();
673+
Assertions.assertEquals(1, insertedRows, "should insert two rows from the select");
674+
}
675+
676+
ResultSet rs = conn.createStatement().executeQuery("select * from test_prepare_statement order by a");
677+
int count = 0;
678+
while (rs.next()) {
679+
count++;
680+
}
681+
Assertions.assertEquals(3, count, "should have four rows in the table after insert with select");
682+
683+
// Clean up
684+
conn.createStatement().execute("delete from test_prepare_statement");
685+
}
686+
644687
}

0 commit comments

Comments
 (0)