Skip to content

Commit eb972e9

Browse files
committed
Merge branch 'master' into support-fetch-first
2 parents c23c738 + 905133e commit eb972e9

File tree

44 files changed

+196
-116
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+196
-116
lines changed

src/main/java/org/apache/ibatis/datasource/pooled/PooledDataSource.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,19 +585,23 @@ public static Connection unwrapConnection(Connection conn) {
585585
return conn;
586586
}
587587

588+
@Override
588589
protected void finalize() throws Throwable {
589590
forceCloseAll();
590591
super.finalize();
591592
}
592593

594+
@Override
593595
public <T> T unwrap(Class<T> iface) throws SQLException {
594596
throw new SQLException(getClass().getName() + " is not a wrapper.");
595597
}
596598

599+
@Override
597600
public boolean isWrapperFor(Class<?> iface) {
598601
return false;
599602
}
600603

604+
@Override
601605
public Logger getParentLogger() {
602606
return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
603607
}

src/main/java/org/apache/ibatis/datasource/unpooled/UnpooledDataSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public boolean jdbcCompliant() {
292292
return this.driver.jdbcCompliant();
293293
}
294294

295-
// @Override only valid jdk7+
295+
@Override
296296
public Logger getParentLogger() {
297297
return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
298298
}
@@ -308,7 +308,7 @@ public boolean isWrapperFor(Class<?> iface) throws SQLException {
308308
return false;
309309
}
310310

311-
// @Override only valid jdk7+
311+
@Override
312312
public Logger getParentLogger() {
313313
// requires JDK version 1.6
314314
return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public T INSERT_INTO(String tableName) {
6161
}
6262

6363
public T VALUES(String columns, String values) {
64-
sql().columns.add(columns);
65-
sql().values.add(values);
64+
INTO_COLUMNS(columns);
65+
INTO_VALUES(values);
6666
return getSelf();
6767
}
6868

@@ -78,7 +78,10 @@ public T INTO_COLUMNS(String... columns) {
7878
* @since 3.4.2
7979
*/
8080
public T INTO_VALUES(String... values) {
81-
sql().values.addAll(Arrays.asList(values));
81+
List<String> list = sql().valuesList.get(sql().valuesList.size() - 1);
82+
for (String value : values) {
83+
list.add(value);
84+
}
8285
return getSelf();
8386
}
8487

@@ -366,6 +369,16 @@ public T OFFSET_ROWS(long value) {
366369
return OFFSET_ROWS(String.valueOf(value));
367370
}
368371

372+
/*
373+
* used to add a new inserted row while do multi-row insert.
374+
*
375+
* @since 3.5.2
376+
*/
377+
public T ADD_ROW() {
378+
sql().valuesList.add(new ArrayList<>());
379+
return getSelf();
380+
}
381+
369382
private SQLStatement sql() {
370383
return sql;
371384
}
@@ -464,14 +477,15 @@ protected void appendClause(SafeAppendable builder, String offset, String limit)
464477
List<String> orderBy = new ArrayList<>();
465478
List<String> lastList = new ArrayList<>();
466479
List<String> columns = new ArrayList<>();
467-
List<String> values = new ArrayList<>();
480+
List<List<String>> valuesList = new ArrayList<>();
468481
boolean distinct;
469482
String offset;
470483
String limit;
471484
LimitingRowsStrategy limitingRowsStrategy = LimitingRowsStrategy.NOP;
472485

473486
public SQLStatement() {
474-
// Prevent Synthetic Access
487+
// Prevent Synthetic Access
488+
valuesList.add(new ArrayList<>());
475489
}
476490

477491
private void sqlClause(SafeAppendable builder, String keyword, List<String> parts, String open, String close,
@@ -524,7 +538,9 @@ private void joins(SafeAppendable builder) {
524538
private String insertSQL(SafeAppendable builder) {
525539
sqlClause(builder, "INSERT INTO", tables, "", "", "");
526540
sqlClause(builder, "", columns, "(", ")", ", ");
527-
sqlClause(builder, "VALUES", values, "(", ")", ", ");
541+
for (int i = 0; i < valuesList.size(); i++) {
542+
sqlClause(builder, i > 0 ? "," : "VALUES", valuesList.get(i), "(", ")", ", ");
543+
}
528544
return builder.toString();
529545
}
530546

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ public StrictMap<V> conflictMessageProducer(BiFunction<V, V, String> conflictMes
923923
return this;
924924
}
925925

926+
@Override
926927
@SuppressWarnings("unchecked")
927928
public V put(String key, V value) {
928929
if (containsKey(key)) {
@@ -940,6 +941,7 @@ public V put(String key, V value) {
940941
return super.put(key, value);
941942
}
942943

944+
@Override
943945
public V get(Object key) {
944946
V value = super.get(key);
945947
if (value == null) {

src/test/java/org/apache/ibatis/autoconstructor/ExtensiveSubject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@ public ExtensiveSubject(final byte aByte,
6464
public enum TestEnum {
6565
AVALUE, BVALUE, CVALUE;
6666
}
67-
}
67+
}

src/test/java/org/apache/ibatis/binding/BindingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void shouldInsertAuthorWithSelectKeyAndDynamicParams() {
180180
Author author = new Author(-1, "cbegin", "******", "[email protected]", "N/A", Section.NEWS);
181181
int rows = mapper.insertAuthorDynamic(author);
182182
assertEquals(1, rows);
183-
assertFalse(-1 == author.getId()); // id must be autogenerated
183+
assertNotEquals(-1, author.getId()); // id must be autogenerated
184184
Author author2 = mapper.selectAuthor(author.getId());
185185
assertNotNull(author2);
186186
assertEquals(author.getEmail(), author2.getEmail());

src/test/java/org/apache/ibatis/cache/BaseCacheTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class BaseCacheTest {
3232
@Test
3333
void shouldDemonstrateEqualsAndHashCodeForVariousCacheTypes() {
3434
PerpetualCache cache = new PerpetualCache("test_cache");
35-
assertTrue(cache.equals(cache));
36-
assertTrue(cache.equals(new SynchronizedCache(cache)));
37-
assertTrue(cache.equals(new SerializedCache(cache)));
38-
assertTrue(cache.equals(new LoggingCache(cache)));
39-
assertTrue(cache.equals(new ScheduledCache(cache)));
35+
assertEquals(cache, cache);
36+
assertEquals(cache, new SynchronizedCache(cache));
37+
assertEquals(cache, new SerializedCache(cache));
38+
assertEquals(cache, new LoggingCache(cache));
39+
assertEquals(cache, new ScheduledCache(cache));
4040

4141
assertEquals(cache.hashCode(), new SynchronizedCache(cache).hashCode());
4242
assertEquals(cache.hashCode(), new SerializedCache(cache).hashCode());

src/test/java/org/apache/ibatis/cache/CacheKeyTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,32 @@ void shouldTestCacheKeysEqual() {
3434
Date date = new Date();
3535
CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null, new Date(date.getTime()) });
3636
CacheKey key2 = new CacheKey(new Object[] { 1, "hello", null, new Date(date.getTime()) });
37-
assertTrue(key1.equals(key2));
38-
assertTrue(key2.equals(key1));
39-
assertTrue(key1.hashCode() == key2.hashCode());
40-
assertTrue(key1.toString().equals(key2.toString()));
37+
assertEquals(key1, key2);
38+
assertEquals(key2, key1);
39+
assertEquals(key1.hashCode(), key2.hashCode());
40+
assertEquals(key1.toString(), key2.toString());
4141
}
4242

4343
@Test
4444
void shouldTestCacheKeysNotEqualDueToDateDifference() throws Exception {
4545
CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null, new Date() });
4646
Thread.sleep(1000);
4747
CacheKey key2 = new CacheKey(new Object[] { 1, "hello", null, new Date() });
48-
assertFalse(key1.equals(key2));
49-
assertFalse(key2.equals(key1));
50-
assertFalse(key1.hashCode() == key2.hashCode());
51-
assertFalse(key1.toString().equals(key2.toString()));
48+
assertNotEquals(key1, key2);
49+
assertNotEquals(key2, key1);
50+
assertNotEquals(key1.hashCode(), key2.hashCode());
51+
assertNotEquals(key1.toString(), key2.toString());
5252
}
5353

5454
@Test
5555
void shouldTestCacheKeysNotEqualDueToOrder() throws Exception {
5656
CacheKey key1 = new CacheKey(new Object[] { 1, "hello", null });
5757
Thread.sleep(1000);
5858
CacheKey key2 = new CacheKey(new Object[] { 1, null, "hello" });
59-
assertFalse(key1.equals(key2));
60-
assertFalse(key2.equals(key1));
61-
assertFalse(key1.hashCode() == key2.hashCode());
62-
assertFalse(key1.toString().equals(key2.toString()));
59+
assertNotEquals(key1, key2);
60+
assertNotEquals(key2, key1);
61+
assertNotEquals(key1.hashCode(), key2.hashCode());
62+
assertNotEquals(key1.toString(), key2.toString());
6363
}
6464

6565
@Test
@@ -84,7 +84,7 @@ void shouldTestCacheKeysWithBinaryArrays() {
8484
byte[] array2 = new byte[] { 1 };
8585
CacheKey key1 = new CacheKey(new Object[] { array1 });
8686
CacheKey key2 = new CacheKey(new Object[] { array2 });
87-
assertTrue(key1.equals(key2));
87+
assertEquals(key1, key2);
8888
}
8989

9090
@Test

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,62 @@ void selectUsingOffsetRowsAndFetchFirstRowsOnly() {
374374
assertEquals("SELECT *\nFROM test\nORDER BY id OFFSET 100 ROWS FETCH FIRST 20 ROWS ONLY", sql);
375375
}
376376

377+
@Test
378+
void supportBatchInsert(){
379+
final String sql = new SQL(){{
380+
INSERT_INTO("table1 a");
381+
INTO_COLUMNS("col1,col2");
382+
INTO_VALUES("val1","val2");
383+
ADD_ROW();
384+
INTO_VALUES("val1","val2");
385+
}}.toString();
386+
387+
assertThat(sql).isEqualToIgnoringWhitespace("INSERT INTO table1 a (col1,col2) VALUES (val1,val2), (val1,val2)");
388+
}
389+
390+
@Test
391+
void singleInsert() {
392+
final String sql = new SQL() {{
393+
INSERT_INTO("table1 a");
394+
INTO_COLUMNS("col1,col2");
395+
INTO_VALUES("val1", "val2");
396+
}}.toString();
397+
398+
assertThat(sql).isEqualToIgnoringWhitespace("INSERT INTO table1 a (col1,col2) VALUES (val1,val2)");
399+
}
400+
401+
@Test
402+
void singleInsertWithMultipleInsertValues() {
403+
final String sql = new SQL() {{
404+
INSERT_INTO("TABLE_A").INTO_COLUMNS("a", "b").INTO_VALUES("#{a}").INTO_VALUES("#{b}");
405+
}}.toString();
406+
407+
assertThat(sql).isEqualToIgnoringWhitespace("INSERT INTO TABLE_A (a, b) VALUES (#{a}, #{b})");
408+
}
409+
410+
@Test
411+
void batchInsertWithMultipleInsertValues() {
412+
final String sql = new SQL() {{
413+
INSERT_INTO("TABLE_A");
414+
INTO_COLUMNS("a", "b");
415+
INTO_VALUES("#{a1}");
416+
INTO_VALUES("#{b1}");
417+
ADD_ROW();
418+
INTO_VALUES("#{a2}");
419+
INTO_VALUES("#{b2}");
420+
}}.toString();
421+
422+
assertThat(sql).isEqualToIgnoringWhitespace("INSERT INTO TABLE_A (a, b) VALUES (#{a1}, #{b1}), (#{a2}, #{b2})");
423+
}
424+
425+
@Test
426+
void testValues() {
427+
final String sql = new SQL() {{
428+
INSERT_INTO("PERSON");
429+
VALUES("ID, FIRST_NAME", "#{id}, #{firstName}");
430+
VALUES("LAST_NAME", "#{lastName}");
431+
}}.toString();
432+
433+
assertThat(sql).isEqualToIgnoringWhitespace("INSERT INTO PERSON (ID, FIRST_NAME, LAST_NAME) VALUES (#{id}, #{firstName}, #{lastName})");
434+
}
377435
}

src/test/java/org/apache/ibatis/submitted/blocking_cache/PersonMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
public interface PersonMapper {
2525

2626
@Select("select id, firstname, lastname from person")
27-
public List<Person> findAll();
27+
List<Person> findAll();
2828
}

0 commit comments

Comments
 (0)