diff --git a/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaAutoConfiguration.java b/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaAutoConfiguration.java index c800691..4fae2c6 100644 --- a/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaAutoConfiguration.java +++ b/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaAutoConfiguration.java @@ -15,6 +15,8 @@ */ package org.seasar.doma.boot.autoconfigure; +import javax.sql.DataSource; + import org.seasar.doma.jdbc.Config; import org.seasar.doma.jdbc.EntityListenerProvider; import org.seasar.doma.jdbc.Naming; @@ -26,17 +28,12 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; -import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator; -import javax.sql.DataSource; - -import static org.seasar.doma.boot.autoconfigure.DomaProperties.DOMA_PREFIX; - /** * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration * Auto-configuration} for Doma. @@ -91,11 +88,10 @@ public DomaConfigBuilder domaConfigBuilder() { @Bean @ConditionalOnMissingBean(Config.class) - @ConfigurationProperties(prefix = DOMA_PREFIX) public DomaConfig config(DataSource dataSource, Dialect dialect, SqlFileRepository sqlFileRepository, Naming naming, EntityListenerProvider entityListenerProvider, - DomaConfigBuilder domaConfigBuilder) { + DomaConfigBuilder domaConfigBuilder, DomaProperties domaProperties) { if (domaConfigBuilder.dataSource() == null) { domaConfigBuilder.dataSource(dataSource); } @@ -111,7 +107,7 @@ public DomaConfig config(DataSource dataSource, Dialect dialect, if (domaConfigBuilder.entityListenerProvider() == null) { domaConfigBuilder.entityListenerProvider(entityListenerProvider); } - return new DomaConfig(domaConfigBuilder); + return domaConfigBuilder.build(domaProperties); } } diff --git a/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaConfig.java b/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaConfig.java index 31ceb00..eb27386 100644 --- a/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaConfig.java +++ b/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaConfig.java @@ -15,46 +15,17 @@ */ package org.seasar.doma.boot.autoconfigure; +import javax.sql.DataSource; + import org.seasar.doma.jdbc.*; import org.seasar.doma.jdbc.dialect.Dialect; -import javax.sql.DataSource; - /** * {@link Config} implementation used in doma-spring-boot. * * @author Toshiaki Maki */ public class DomaConfig implements Config { - /** - * Datasource name. - */ - private String dataSourceName = Config.class.getName(); - - /** - * Type of SQL log in the exception. - */ - private SqlLogType exceptionSqlLogType = SqlLogType.NONE; - - /** - * Limit for the maximum number of rows. Ignored unless this value is greater than 0. - */ - private int maxRows = 0; - /** - * Hint to the number of rows that should be fetched. Ignored unless this value is - * greater than 0. - */ - private int fetchSize = 0; - /** - * Number of seconds the driver will wait for a Statement object to - * execute. Ignored unless this value is greater than 0. - */ - private int queryTimeout = 0; - /** - * Size in executing PreparedStatement#addBatch(). Regarded as 1 unless - * this value is greater than 1. - */ - private int batchSize = 0; private final DataSource dataSource; private final Dialect dialect; @@ -69,8 +40,9 @@ public class DomaConfig implements Config { private final MapKeyNaming mapKeyNaming; private final Commenter commenter; private final EntityListenerProvider entityListenerProvider; + private final DomaProperties domaProperties; - public DomaConfig(DomaConfigBuilder builder) { + public DomaConfig(DomaConfigBuilder builder, DomaProperties domaProperties) { this.dataSource = builder.dataSource(); this.dialect = builder.dialect(); this.jdbcLogger = builder.jdbcLogger(); @@ -84,6 +56,7 @@ public DomaConfig(DomaConfigBuilder builder) { this.mapKeyNaming = builder.mapKeyNaming(); this.commenter = builder.commenter(); this.entityListenerProvider = builder.entityListenerProvider(); + this.domaProperties = domaProperties; } @Override @@ -98,11 +71,7 @@ public Dialect getDialect() { @Override public String getDataSourceName() { - return this.dataSourceName; - } - - public void setDataSourceName(String dataSourceName) { - this.dataSourceName = dataSourceName; + return this.domaProperties.getDataSourceName(); } @Override @@ -135,13 +104,9 @@ public QueryImplementors getQueryImplementors() { return this.queryImplementors; } - public void setExceptionSqlLogType(SqlLogType exceptionSqlLogType) { - this.exceptionSqlLogType = exceptionSqlLogType; - } - @Override public SqlLogType getExceptionSqlLogType() { - return this.exceptionSqlLogType; + return this.domaProperties.getExceptionSqlLogType(); } @Override @@ -166,42 +131,39 @@ public Commenter getCommenter() { @Override public int getMaxRows() { - return this.maxRows; - } - - public void setMaxRows(int maxRows) { - this.maxRows = maxRows; + return this.domaProperties.getMaxRows(); } @Override public int getFetchSize() { - return this.fetchSize; - } - - public void setFetchSize(int fetchSize) { - this.fetchSize = fetchSize; + return this.domaProperties.getFetchSize(); } @Override public int getQueryTimeout() { - return this.queryTimeout; - } - - public void setQueryTimeout(int queryTimeout) { - this.queryTimeout = queryTimeout; + return this.domaProperties.getQueryTimeout(); } @Override public int getBatchSize() { - return this.batchSize; - } - - public void setBatchSize(int batchSize) { - this.batchSize = batchSize; + return this.domaProperties.getBatchSize(); } @Override public EntityListenerProvider getEntityListenerProvider() { return this.entityListenerProvider; } + + @Override + public String toString() { + return "DomaConfig{" + "dataSource=" + dataSource + ", dialect=" + dialect + + ", jdbcLogger=" + jdbcLogger + ", sqlFileRepository=" + + sqlFileRepository + ", requiresNewController=" + requiresNewController + + ", classHelper=" + classHelper + ", commandImplementors=" + + commandImplementors + ", queryImplementors=" + queryImplementors + + ", unknownColumnHandler=" + unknownColumnHandler + ", naming=" + naming + + ", mapKeyNaming=" + mapKeyNaming + ", commenter=" + commenter + + ", entityListenerProvider=" + entityListenerProvider + + ", domaProperties=" + domaProperties + '}'; + } } diff --git a/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaConfigBuilder.java b/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaConfigBuilder.java index c88ec68..26aa333 100644 --- a/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaConfigBuilder.java +++ b/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaConfigBuilder.java @@ -15,12 +15,12 @@ */ package org.seasar.doma.boot.autoconfigure; +import javax.sql.DataSource; + import org.seasar.doma.jdbc.*; import org.seasar.doma.jdbc.dialect.Dialect; import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; -import javax.sql.DataSource; - /** * Builder to create {@link DomaConfig}. * @@ -179,7 +179,7 @@ public DomaConfigBuilder entityListenerProvider( return this; } - public DomaConfig build() { - return new DomaConfig(this); + public DomaConfig build(DomaProperties domaProperties) { + return new DomaConfig(this, domaProperties); } } diff --git a/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaProperties.java b/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaProperties.java index c0a9c73..d452112 100644 --- a/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaProperties.java +++ b/doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaProperties.java @@ -15,16 +15,13 @@ */ package org.seasar.doma.boot.autoconfigure; -import org.seasar.doma.jdbc.GreedyCacheSqlFileRepository; -import org.seasar.doma.jdbc.Naming; -import org.seasar.doma.jdbc.NoCacheSqlFileRepository; -import org.seasar.doma.jdbc.SqlFileRepository; -import org.seasar.doma.jdbc.dialect.*; -import org.springframework.boot.context.properties.ConfigurationProperties; +import static org.seasar.doma.boot.autoconfigure.DomaProperties.DOMA_PREFIX; import java.util.function.Supplier; -import static org.seasar.doma.boot.autoconfigure.DomaProperties.DOMA_PREFIX; +import org.seasar.doma.jdbc.*; +import org.seasar.doma.jdbc.dialect.*; +import org.springframework.boot.context.properties.ConfigurationProperties; /** * {@link ConfigurationProperties} for configuring Doma. @@ -56,6 +53,36 @@ public class DomaProperties { */ private boolean exceptionTranslationEnabled = true; + /** + * Datasource name. + */ + private String dataSourceName = Config.class.getName(); + + /** + * Type of SQL log in the exception. + */ + private SqlLogType exceptionSqlLogType = SqlLogType.NONE; + + /** + * Limit for the maximum number of rows. Ignored unless this value is greater than 0. + */ + private int maxRows = 0; + /** + * Hint to the number of rows that should be fetched. Ignored unless this value is + * greater than 0. + */ + private int fetchSize = 0; + /** + * Number of seconds the driver will wait for a Statement object to + * execute. Ignored unless this value is greater than 0. + */ + private int queryTimeout = 0; + /** + * Size in executing PreparedStatement#addBatch(). Regarded as 1 unless + * this value is greater than 1. + */ + private int batchSize = 0; + public DialectType getDialect() { return dialect; } @@ -88,12 +115,58 @@ public void setExceptionTranslationEnabled(boolean exceptionTranslationEnabled) this.exceptionTranslationEnabled = exceptionTranslationEnabled; } - public DomaConfigBuilder initializeDomaConfigBuilder() { - return new DomaConfigBuilder() - .dialect(dialect.create()) - .sqlFileRepository(sqlFileRepository.create()) - .naming(naming.naming()); - } + public String getDataSourceName() { + return dataSourceName; + } + + public void setDataSourceName(String dataSourceName) { + this.dataSourceName = dataSourceName; + } + + public SqlLogType getExceptionSqlLogType() { + return exceptionSqlLogType; + } + + public void setExceptionSqlLogType(SqlLogType exceptionSqlLogType) { + this.exceptionSqlLogType = exceptionSqlLogType; + } + + public int getMaxRows() { + return maxRows; + } + + public void setMaxRows(int maxRows) { + this.maxRows = maxRows; + } + + public int getFetchSize() { + return fetchSize; + } + + public void setFetchSize(int fetchSize) { + this.fetchSize = fetchSize; + } + + public int getQueryTimeout() { + return queryTimeout; + } + + public void setQueryTimeout(int queryTimeout) { + this.queryTimeout = queryTimeout; + } + + public int getBatchSize() { + return batchSize; + } + + public void setBatchSize(int batchSize) { + this.batchSize = batchSize; + } + + public DomaConfigBuilder initializeDomaConfigBuilder() { + return new DomaConfigBuilder().dialect(dialect.create()) + .sqlFileRepository(sqlFileRepository.create()).naming(naming.naming()); + } public static enum DialectType { STANDARD(StandardDialect::new), SQLITE(SqliteDialect::new), DB2(Db2Dialect::new), MSSQL( @@ -148,6 +221,10 @@ public Naming naming() { public String toString() { return "DomaProperties{" + "dialect=" + dialect + ", sqlFileRepository=" + sqlFileRepository + ", naming=" + naming - + ", exceptionTranslationEnabled=" + exceptionTranslationEnabled + '}'; + + ", exceptionTranslationEnabled=" + exceptionTranslationEnabled + + ", dataSourceName='" + dataSourceName + '\'' + ", exceptionSqlLogType=" + + exceptionSqlLogType + ", maxRows=" + maxRows + ", fetchSize=" + + fetchSize + ", queryTimeout=" + queryTimeout + ", batchSize=" + + batchSize + '}'; } }