Skip to content

Commit

Permalink
Merge pull request #2980 from taoyect/xml_parsing_learning_refine
Browse files Browse the repository at this point in the history
Try to keep several private methods and configuration items symmetrical
  • Loading branch information
hazendaz committed Oct 29, 2023
2 parents c2b5bce + 3829cd6 commit a6e47d8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 32 deletions.
39 changes: 20 additions & 19 deletions src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ private void parseConfiguration(XNode root) {
// issue #117 read properties first
propertiesElement(root.evalNode("properties"));
Properties settings = settingsAsProperties(root.evalNode("settings"));
loadCustomVfs(settings);
loadCustomVfsImpl(settings);
loadCustomLogImpl(settings);
typeAliasesElement(root.evalNode("typeAliases"));
pluginElement(root.evalNode("plugins"));
pluginsElement(root.evalNode("plugins"));
objectFactoryElement(root.evalNode("objectFactory"));
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
reflectorFactoryElement(root.evalNode("reflectorFactory"));
settingsElement(settings);
// read it after objectFactory and objectWrapperFactory issue #631
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
mapperElement(root.evalNode("mappers"));
typeHandlersElement(root.evalNode("typeHandlers"));
mappersElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
Expand All @@ -150,7 +150,7 @@ private Properties settingsAsProperties(XNode context) {
return props;
}

private void loadCustomVfs(Properties props) throws ClassNotFoundException {
private void loadCustomVfsImpl(Properties props) throws ClassNotFoundException {
String value = props.getProperty("vfsImpl");
if (value == null) {
return;
Expand Down Expand Up @@ -195,7 +195,7 @@ private void typeAliasesElement(XNode context) {
}
}

private void pluginElement(XNode context) throws Exception {
private void pluginsElement(XNode context) throws Exception {
if (context != null) {
for (XNode child : context.getChildren()) {
String interceptor = child.getStringAttribute("interceptor");
Expand Down Expand Up @@ -317,19 +317,20 @@ private void environmentsElement(XNode context) throws Exception {
}

private void databaseIdProviderElement(XNode context) throws Exception {
DatabaseIdProvider databaseIdProvider = null;
if (context != null) {
String type = context.getStringAttribute("type");
// awful patch to keep backward compatibility
if ("VENDOR".equals(type)) {
type = "DB_VENDOR";
}
Properties properties = context.getChildrenAsProperties();
databaseIdProvider = (DatabaseIdProvider) resolveClass(type).getDeclaredConstructor().newInstance();
databaseIdProvider.setProperties(properties);
if (context == null) {
return;
}
String type = context.getStringAttribute("type");
// awful patch to keep backward compatibility
if ("VENDOR".equals(type)) {
type = "DB_VENDOR";
}
Properties properties = context.getChildrenAsProperties();
DatabaseIdProvider databaseIdProvider = (DatabaseIdProvider) resolveClass(type).getDeclaredConstructor()
.newInstance();
databaseIdProvider.setProperties(properties);
Environment environment = configuration.getEnvironment();
if (environment != null && databaseIdProvider != null) {
if (environment != null) {
String databaseId = databaseIdProvider.getDatabaseId(environment.getDataSource());
configuration.setDatabaseId(databaseId);
}
Expand Down Expand Up @@ -357,7 +358,7 @@ private DataSourceFactory dataSourceElement(XNode context) throws Exception {
throw new BuilderException("Environment declaration requires a DataSourceFactory.");
}

private void typeHandlerElement(XNode context) {
private void typeHandlersElement(XNode context) {
if (context == null) {
return;
}
Expand Down Expand Up @@ -385,7 +386,7 @@ private void typeHandlerElement(XNode context) {
}
}

private void mapperElement(XNode context) throws Exception {
private void mappersElement(XNode context) throws Exception {
if (context == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public void parse() {
configuration.addLoadedResource(resource);
bindMapperForNamespace();
}

parsePendingResultMaps();
parsePendingCacheRefs();
parsePendingStatements();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
package org.apache.ibatis.mapping;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;
Expand Down Expand Up @@ -60,23 +58,16 @@ public void setProperties(Properties p) {
private String getDatabaseName(DataSource dataSource) throws SQLException {
String productName = getDatabaseProductName(dataSource);
if (this.properties != null) {
for (Map.Entry<Object, Object> property : properties.entrySet()) {
if (productName.contains((String) property.getKey())) {
return (String) property.getValue();
}
}
// no match, return null
return null;
return properties.entrySet().stream().filter(entry -> productName.contains((String) entry.getKey()))
.map(entry -> (String) entry.getValue()).findFirst().orElse(null);
}
return productName;
}

private String getDatabaseProductName(DataSource dataSource) throws SQLException {
try (Connection con = dataSource.getConnection()) {
DatabaseMetaData metaData = con.getMetaData();
return metaData.getDatabaseProductName();
return con.getMetaData().getDatabaseProductName();
}

}

private static class LogHolder {
Expand Down

0 comments on commit a6e47d8

Please sign in to comment.