Skip to content

Commit eebb5d3

Browse files
committed
prettify sql format
1 parent fc9e988 commit eebb5d3

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,15 @@ private String insertSQL(SafeAppendable builder) {
396396
sqlClause(builder, "INSERT INTO", tables, "", "", "");
397397
sqlClause(builder, "", columns, "(", ")", ", ");
398398
for (int i = 0; i < values.size(); i++) {
399+
String keyword = "";
400+
String close = "),";
399401
if(i == 0){
400-
sqlClause(builder, "VALUES", values.get(0), "(", ")", ", ");
401-
}else{
402-
sqlClause(builder, ",", values.get(i), "(", ")", ", ");
402+
keyword = "VALUES";
403403
}
404+
if(i == values.size() - 1){
405+
close = ")";
406+
}
407+
sqlClause(builder, keyword, values.get(i), "(", close, ", ");
404408
}
405409
return builder.toString();
406410
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,26 +318,26 @@ public void supportBatchInsert(){
318318
}
319319

320320
@Test
321-
public void singleInsert(){
322-
final SQL sql = new SQL(){{
321+
public void singleInsert() {
322+
final SQL sql = new SQL() {{
323323
INSERT_INTO("table1 a");
324324
INTO_COLUMNS("col1,col2");
325-
INTO_VALUES("val1","val2");
325+
INTO_VALUES("val1", "val2");
326326
}};
327327
assertThat(sql.toString()).isEqualToIgnoringWhitespace("INSERT INTO table1 a (col1,col2) VALUES (val1,val2)");
328328
}
329329

330330
@Test
331-
public void singleInsertWithMultipleInsertValues(){
332-
final String sql = new SQL() {{
333-
INSERT_INTO("TABLE_A").INTO_COLUMNS("a", "b").INTO_VALUES("#{a}").INTO_VALUES("#{b}");
334-
}}.toString();
335-
System.out.println(sql);
336-
assertEquals("INSERT INTO TABLE_A\n (a, b)\nVALUES (#{a}, #{b})", sql);
331+
public void singleInsertWithMultipleInsertValues() {
332+
final String sql = new SQL() {{
333+
INSERT_INTO("TABLE_A").INTO_COLUMNS("a", "b").INTO_VALUES("#{a}").INTO_VALUES("#{b}");
334+
}}.toString();
335+
System.out.println(sql);
336+
assertThat(sql).isEqualToIgnoringWhitespace("INSERT INTO TABLE_A (a, b) VALUES (#{a}, #{b})");
337337
}
338338

339339
@Test
340-
public void batchInsertWithMultipleInsertValues(){
340+
public void batchInsertWithMultipleInsertValues() {
341341
final String sql = new SQL() {{
342342
INSERT_INTO("TABLE_A");
343343
INTO_COLUMNS("a", "b");
@@ -348,6 +348,6 @@ public void batchInsertWithMultipleInsertValues(){
348348
INTO_VALUES("#{b2}");
349349
}}.toString();
350350
System.out.println(sql);
351-
assertEquals("INSERT INTO TABLE_A\n (a, b)\nVALUES \n (#{a1}, #{b1}),\n (#{a2}, #{b2})", sql);
351+
assertThat(sql).isEqualToIgnoringWhitespace("INSERT INTO TABLE_A (a, b) VALUES (#{a1}, #{b1}), (#{a2}, #{b2})");
352352
}
353353
}

0 commit comments

Comments
 (0)