Skip to content

Commit 70e455d

Browse files
committed
fix a bug that break an example in doc and add that example to test
1 parent eebb5d3 commit 70e455d

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

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

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2018 the original author or authors.
2+
* Copyright 2009-2019 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,7 +18,6 @@
1818
import java.io.IOException;
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21-
import java.util.Collections;
2221
import java.util.List;
2322

2423
/**
@@ -62,8 +61,8 @@ public T INSERT_INTO(String tableName) {
6261
}
6362

6463
public T VALUES(String columns, String values) {
65-
sql().columns.add(columns);
66-
sql().values.add(Collections.singletonList(values));
64+
INTO_COLUMNS(columns);
65+
INTO_VALUES(values);
6766
return getSelf();
6867
}
6968

@@ -79,15 +78,12 @@ public T INTO_COLUMNS(String... columns) {
7978
* @since 3.4.2
8079
*/
8180
public T INTO_VALUES(String... values) {
82-
List<List<String>> list = sql().values;
83-
if(list.isEmpty()){
84-
list.add(new ArrayList<>());
85-
}
86-
int index = list.size() - 1;
87-
for (String value : values) {
88-
list.get(index).add(value);
89-
}
90-
return getSelf();
81+
List<List<String>> list = sql().values;
82+
int index = list.size() - 1;
83+
for (String value : values) {
84+
list.get(index).add(value);
85+
}
86+
return getSelf();
9187
}
9288

9389
public T SELECT(String columns) {
@@ -270,7 +266,7 @@ public T ORDER_BY(String... columns) {
270266
return getSelf();
271267
}
272268

273-
public T ADD_ROW(){
269+
public T ADD_ROW() {
274270
sql().values.add(new ArrayList<>());
275271
return getSelf();
276272
}
@@ -343,7 +339,8 @@ public enum StatementType {
343339
boolean distinct;
344340

345341
public SQLStatement() {
346-
// Prevent Synthetic Access
342+
// Prevent Synthetic Access
343+
values.add(new ArrayList<>());
347344
}
348345

349346
private void sqlClause(SafeAppendable builder, String keyword, List<String> parts, String open, String close,
@@ -395,17 +392,9 @@ private void joins(SafeAppendable builder) {
395392
private String insertSQL(SafeAppendable builder) {
396393
sqlClause(builder, "INSERT INTO", tables, "", "", "");
397394
sqlClause(builder, "", columns, "(", ")", ", ");
398-
for (int i = 0; i < values.size(); i++) {
399-
String keyword = "";
400-
String close = "),";
401-
if(i == 0){
402-
keyword = "VALUES";
403-
}
404-
if(i == values.size() - 1){
405-
close = ")";
406-
}
407-
sqlClause(builder, keyword, values.get(i), "(", close, ", ");
408-
}
395+
for (int i = 0 ; i < values.size(); i++) {
396+
sqlClause(builder, i > 0 ? "," : "VALUES", values.get(i), "(", ")", ", ");
397+
}
409398
return builder.toString();
410399
}
411400

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-2018 the original author or authors.
2+
* Copyright 2009-2019 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.
@@ -15,6 +15,7 @@
1515
*/
1616
package org.apache.ibatis.jdbc;
1717

18+
import org.assertj.core.api.AssertionsForClassTypes;
1819
import org.junit.Test;
1920

2021
import static org.assertj.core.api.Assertions.assertThat;
@@ -350,4 +351,15 @@ public void batchInsertWithMultipleInsertValues() {
350351
System.out.println(sql);
351352
assertThat(sql).isEqualToIgnoringWhitespace("INSERT INTO TABLE_A (a, b) VALUES (#{a1}, #{b1}), (#{a2}, #{b2})");
352353
}
354+
355+
356+
@Test
357+
public void testValues() {
358+
final SQL sql = new SQL() {{
359+
INSERT_INTO("PERSON");
360+
VALUES("ID, FIRST_NAME", "#{id}, #{firstName}");
361+
VALUES("LAST_NAME", "#{lastName}");
362+
}};
363+
AssertionsForClassTypes.assertThat(sql.toString()).isEqualToIgnoringWhitespace("INSERT INTO PERSON (ID, FIRST_NAME, LAST_NAME) VALUES (#{id}, #{firstName}, #{lastName})");
364+
}
353365
}

0 commit comments

Comments
 (0)