Skip to content

Commit 6a2676a

Browse files
authored
Merge pull request mybatis#3297 from harawata/3296-vendor-db-id-provider-empty-properties
`VendorDatabaseIdProvider#getDatabaseId()` should return product name when properties is empty
2 parents b45f64d + dc249c1 commit 6a2676a

File tree

5 files changed

+131
-5
lines changed

5 files changed

+131
-5
lines changed

src/main/java/org/apache/ibatis/mapping/VendorDatabaseIdProvider.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2024 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.
@@ -55,11 +55,11 @@ public void setProperties(Properties p) {
5555

5656
private String getDatabaseName(DataSource dataSource) throws SQLException {
5757
String productName = getDatabaseProductName(dataSource);
58-
if (this.properties != null) {
59-
return properties.entrySet().stream().filter(entry -> productName.contains((String) entry.getKey()))
60-
.map(entry -> (String) entry.getValue()).findFirst().orElse(null);
58+
if (properties == null || properties.isEmpty()) {
59+
return productName;
6160
}
62-
return productName;
61+
return properties.entrySet().stream().filter(entry -> productName.contains((String) entry.getKey()))
62+
.map(entry -> (String) entry.getValue()).findFirst().orElse(null);
6363
}
6464

6565
private String getDatabaseProductName(DataSource dataSource) throws SQLException {

src/test/java/org/apache/ibatis/mapping/VendorDatabaseIdProviderTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ void shouldProductNameBeReturnedIfPropertiesIsNull() throws Exception {
5050
assertEquals(PRODUCT_NAME, provider.getDatabaseId(mockDataSource()));
5151
}
5252

53+
@Test
54+
void shouldProductNameBeReturnedIfPropertiesIsEmpty() throws Exception {
55+
VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
56+
provider.setProperties(new Properties());
57+
assertEquals(PRODUCT_NAME, provider.getDatabaseId(mockDataSource()));
58+
}
59+
5360
@Test
5461
void shouldProductNameBeTranslated() throws Exception {
5562
VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2009-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.databaseid_productname;
17+
18+
import java.io.Reader;
19+
20+
import org.apache.ibatis.io.Resources;
21+
import org.apache.ibatis.session.SqlSession;
22+
import org.apache.ibatis.session.SqlSessionFactory;
23+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
24+
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.api.BeforeAll;
26+
import org.junit.jupiter.api.Test;
27+
28+
class DatabaseIdProductNameTest {
29+
30+
private static SqlSessionFactory sqlSessionFactory;
31+
32+
@BeforeAll
33+
static void setUp() throws Exception {
34+
try (Reader reader = Resources
35+
.getResourceAsReader("org/apache/ibatis/submitted/databaseid_productname/mybatis-config.xml")) {
36+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
37+
}
38+
}
39+
40+
@Test
41+
void shouldProductNameBeDatabaseIdIfDbVendorHasNoProperties() {
42+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
43+
Mapper mapper = sqlSession.getMapper(Mapper.class);
44+
String result = mapper.select();
45+
Assertions.assertEquals("hsql", result);
46+
}
47+
}
48+
49+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2009-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.databaseid_productname;
17+
18+
import org.apache.ibatis.annotations.Select;
19+
20+
public interface Mapper {
21+
22+
@Select(value = "select 'mysql'", databaseId = "MySQL")
23+
@Select(value = "select 'hsql' from (values(0))", databaseId = "HSQL Database Engine")
24+
String select();
25+
26+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2024 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE configuration
20+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
21+
"https://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
25+
<environments default="development">
26+
<environment id="development">
27+
<transactionManager type="JDBC">
28+
<property name="" value="" />
29+
</transactionManager>
30+
<dataSource type="UNPOOLED">
31+
<property name="driver" value="org.hsqldb.jdbcDriver" />
32+
<property name="url" value="jdbc:hsqldb:mem:dbidprodname" />
33+
<property name="username" value="sa" />
34+
</dataSource>
35+
</environment>
36+
</environments>
37+
38+
<databaseIdProvider type="DB_VENDOR" />
39+
40+
<mappers>
41+
<mapper class="org.apache.ibatis.submitted.databaseid_productname.Mapper" />
42+
</mappers>
43+
44+
</configuration>

0 commit comments

Comments
 (0)