Skip to content

Commit cbe8bb3

Browse files
committed
support batch insert in AbstractSQL
1 parent 8fb6440 commit cbe8bb3

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/main/java/org/apache/ibatis/jdbc/AbstractSQL.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
import java.io.IOException;
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21+
import java.util.Collections;
2122
import java.util.List;
2223

2324
/**
@@ -62,7 +63,7 @@ public T INSERT_INTO(String tableName) {
6263

6364
public T VALUES(String columns, String values) {
6465
sql().columns.add(columns);
65-
sql().values.add(values);
66+
sql().values.add(Collections.singletonList(values));
6667
return getSelf();
6768
}
6869

@@ -78,7 +79,7 @@ public T INTO_COLUMNS(String... columns) {
7879
* @since 3.4.2
7980
*/
8081
public T INTO_VALUES(String... values) {
81-
sql().values.addAll(Arrays.asList(values));
82+
sql().values.add(Arrays.asList(values));
8283
return getSelf();
8384
}
8485

@@ -326,7 +327,7 @@ public enum StatementType {
326327
List<String> orderBy = new ArrayList<String>();
327328
List<String> lastList = new ArrayList<String>();
328329
List<String> columns = new ArrayList<String>();
329-
List<String> values = new ArrayList<String>();
330+
List<List<String>> values = new ArrayList<>();
330331
boolean distinct;
331332

332333
public SQLStatement() {
@@ -382,7 +383,13 @@ private void joins(SafeAppendable builder) {
382383
private String insertSQL(SafeAppendable builder) {
383384
sqlClause(builder, "INSERT INTO", tables, "", "", "");
384385
sqlClause(builder, "", columns, "(", ")", ", ");
385-
sqlClause(builder, "VALUES", values, "(", ")", ", ");
386+
for (int i = 0; i < values.size(); i++) {
387+
if(i == 0){
388+
sqlClause(builder, "VALUES", values.get(0), "(", ")", ", ");
389+
}else{
390+
sqlClause(builder, ",", values.get(i), "(", ")", ", ");
391+
}
392+
}
386393
return builder.toString();
387394
}
388395

src/test/java/org/apache/ibatis/jdbc/SQLTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -303,4 +303,16 @@ public void fixFor903UpdateJoins() {
303303
final SQL sql = new SQL().UPDATE("table1 a").INNER_JOIN("table2 b USING (ID)").SET("a.value = b.value");
304304
assertThat(sql.toString()).isEqualTo("UPDATE table1 a\nINNER JOIN table2 b USING (ID)\nSET a.value = b.value");
305305
}
306+
307+
@Test
308+
public void supportBatchInsert(){
309+
310+
final SQL sql = new SQL(){{
311+
INSERT_INTO("table1 a");
312+
INTO_COLUMNS("col1,col2");
313+
INTO_VALUES("val1","val2");
314+
INTO_VALUES("val1","val2");
315+
}};
316+
assertThat(sql.toString()).isEqualToIgnoringWhitespace("INSERT INTO table1 a (col1,col2) VALUES (val1,val2), (val1,val2)");
317+
}
306318
}

0 commit comments

Comments
 (0)