Skip to content

Commit 7e5fa9d

Browse files
authored
Add SimpleConfig to simplify the construction of Config (#1290)
2 parents d6985f2 + 031eb48 commit 7e5fa9d

File tree

6 files changed

+954
-4
lines changed

6 files changed

+954
-4
lines changed
Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
/*
2+
* Copyright Doma 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.seasar.doma.jdbc;
17+
18+
import java.util.Objects;
19+
import javax.sql.DataSource;
20+
import org.seasar.doma.jdbc.dialect.Dialect;
21+
import org.seasar.doma.jdbc.statistic.StatisticManager;
22+
import org.seasar.doma.jdbc.tx.LocalTransactionDataSource;
23+
import org.seasar.doma.jdbc.tx.LocalTransactionManager;
24+
import org.seasar.doma.jdbc.tx.TransactionManager;
25+
26+
/**
27+
* Represents a simplified configuration interface that extends the base {@code Config} interface.
28+
* It provides methods for accessing specific configuration components and constructing builder
29+
* instances for creating configuration objects.
30+
*
31+
* <pre>
32+
* SimpleConfig config = SimpleConfig.builder("jdbc:h2:mem:test")
33+
* .naming(Naming.SNAKE_UPPER_CASE)
34+
* .queryTimeout(10)
35+
* .build();
36+
* </pre>
37+
*
38+
* This configuration uses Doma's local transaction manager. If you're using Doma with Spring
39+
* Framework or Quarkus, avoid using this class.
40+
*/
41+
public interface SimpleConfig extends Config {
42+
43+
/**
44+
* Retrieves the {@code LocalTransactionDataSource} associated with this configuration.
45+
*
46+
* @return an instance of {@code LocalTransactionDataSource} for managing local database
47+
* connections and transactions
48+
*/
49+
LocalTransactionDataSource getLocalTransactionDataSource();
50+
51+
/**
52+
* Retrieves the {@code LocalTransactionManager} associated with this configuration.
53+
*
54+
* @return an instance of {@code LocalTransactionManager} for managing local transactions
55+
*/
56+
LocalTransactionManager getLocalTransactionManager();
57+
58+
/**
59+
* Creates a new instance of {@code SimpleConfigBuilder} with the specified URL.
60+
*
61+
* <p>You can use a specially modified JDBC URL for Testcontainers as follows:
62+
*
63+
* <pre>
64+
* jdbc:tc:postgresql:12.20:///test?TC_DAEMON=true
65+
* </pre>
66+
*
67+
* The {@link Dialect} is inferred from the URL information.
68+
*
69+
* @param url the database URL to be used for the configuration; must not be {@code null}
70+
* @return an instance of {@code SimpleConfigBuilder} initialized with the given URL
71+
* @throws NullPointerException if the URL is {@code null}
72+
*/
73+
static SimpleConfigBuilder builder(String url) {
74+
Objects.requireNonNull(url);
75+
return SimpleConfigBuilderImpl.of(url);
76+
}
77+
78+
/**
79+
* Creates a new instance of {@code SimpleConfigBuilder} with the specified database URL,
80+
* username, and password.
81+
*
82+
* <p>You can use a specially modified JDBC URL for Testcontainers as follows:
83+
*
84+
* <pre>
85+
* jdbc:tc:postgresql:12.20:///test?TC_DAEMON=true
86+
* </pre>
87+
*
88+
* The {@link Dialect} is inferred from the URL information.
89+
*
90+
* @param url the database URL to be used for the configuration; must not be {@code null}
91+
* @param user the username to be used for the database connection; can be {@code null}
92+
* @param password the password to be used for the database connection; can be {@code null}
93+
* @return an instance of {@code SimpleConfigBuilder} initialized with the specified parameters
94+
* @throws NullPointerException if the URL is {@code null}
95+
*/
96+
static SimpleConfigBuilder builder(String url, String user, String password) {
97+
Objects.requireNonNull(url);
98+
return SimpleConfigBuilderImpl.of(url, user, password);
99+
}
100+
101+
/**
102+
* Creates a new instance of {@code SimpleConfigBuilder} with the specified {@code DataSource} and
103+
* {@code Dialect}.
104+
*
105+
* @param dataSource the data source to be used for the configuration; must not be {@code null}
106+
* @param dialect the dialect to be used for the configuration; must not be {@code null}
107+
* @return an instance of {@code SimpleConfigBuilder} initialized with the provided {@code
108+
* DataSource} and {@code Dialect}
109+
* @throws NullPointerException if the {@code dataSource} or {@code dialect} is {@code null}
110+
*/
111+
static SimpleConfigBuilder builder(DataSource dataSource, Dialect dialect) {
112+
Objects.requireNonNull(dataSource);
113+
Objects.requireNonNull(dialect);
114+
return SimpleConfigBuilderImpl.of(dataSource, dialect);
115+
}
116+
}
117+
118+
class SimpleConfigImpl implements Config, SimpleConfig {
119+
120+
private final LocalTransactionDataSource dataSource;
121+
private final Dialect dialect;
122+
private final SqlFileRepository sqlFileRepository;
123+
private final ScriptFileLoader scriptFileLoader;
124+
private final JdbcLogger jdbcLogger;
125+
private final RequiresNewController requiresNewController;
126+
private final ClassHelper classHelper;
127+
private final CommandImplementors commandImplementors;
128+
private final QueryImplementors queryImplementors;
129+
private final SqlLogType exceptionSqlLogType;
130+
private final UnknownColumnHandler unknownColumnHandler;
131+
private final DuplicateColumnHandler duplicateColumnHandler;
132+
private final Naming naming;
133+
private final MapKeyNaming mapKeyNaming;
134+
private final LocalTransactionManager transactionManager;
135+
private final Commenter commenter;
136+
private final EntityListenerProvider entityListenerProvider;
137+
private final SqlBuilderSettings sqlBuilderSettings;
138+
private final StatisticManager statisticManager;
139+
private final int maxRows;
140+
private final int fetchSize;
141+
private final int queryTimeout;
142+
private final int batchSize;
143+
144+
SimpleConfigImpl(
145+
LocalTransactionDataSource dataSource,
146+
Dialect dialect,
147+
SqlFileRepository sqlFileRepository,
148+
ScriptFileLoader scriptFileLoader,
149+
JdbcLogger jdbcLogger,
150+
RequiresNewController requiresNewController,
151+
ClassHelper classHelper,
152+
CommandImplementors commandImplementors,
153+
QueryImplementors queryImplementors,
154+
SqlLogType exceptionSqlLogType,
155+
UnknownColumnHandler unknownColumnHandler,
156+
DuplicateColumnHandler duplicateColumnHandler,
157+
Naming naming,
158+
MapKeyNaming mapKeyNaming,
159+
LocalTransactionManager transactionManager,
160+
Commenter commenter,
161+
EntityListenerProvider entityListenerProvider,
162+
SqlBuilderSettings sqlBuilderSettings,
163+
StatisticManager statisticManager,
164+
int maxRows,
165+
int fetchSize,
166+
int queryTimeout,
167+
int batchSize) {
168+
this.dataSource = Objects.requireNonNull(dataSource);
169+
this.dialect = Objects.requireNonNull(dialect);
170+
this.sqlFileRepository = Objects.requireNonNull(sqlFileRepository);
171+
this.scriptFileLoader = Objects.requireNonNull(scriptFileLoader);
172+
this.jdbcLogger = Objects.requireNonNull(jdbcLogger);
173+
this.requiresNewController = Objects.requireNonNull(requiresNewController);
174+
this.classHelper = Objects.requireNonNull(classHelper);
175+
this.commandImplementors = Objects.requireNonNull(commandImplementors);
176+
this.queryImplementors = Objects.requireNonNull(queryImplementors);
177+
this.exceptionSqlLogType = Objects.requireNonNull(exceptionSqlLogType);
178+
this.unknownColumnHandler = Objects.requireNonNull(unknownColumnHandler);
179+
this.duplicateColumnHandler = Objects.requireNonNull(duplicateColumnHandler);
180+
this.naming = Objects.requireNonNull(naming);
181+
this.mapKeyNaming = Objects.requireNonNull(mapKeyNaming);
182+
this.transactionManager = Objects.requireNonNull(transactionManager);
183+
this.commenter = Objects.requireNonNull(commenter);
184+
this.entityListenerProvider = Objects.requireNonNull(entityListenerProvider);
185+
this.sqlBuilderSettings = Objects.requireNonNull(sqlBuilderSettings);
186+
this.statisticManager = Objects.requireNonNull(statisticManager);
187+
this.maxRows = maxRows;
188+
this.fetchSize = fetchSize;
189+
this.queryTimeout = queryTimeout;
190+
this.batchSize = batchSize;
191+
}
192+
193+
@Override
194+
public DataSource getDataSource() {
195+
return dataSource;
196+
}
197+
198+
@Override
199+
public Dialect getDialect() {
200+
return dialect;
201+
}
202+
203+
@Override
204+
public SqlFileRepository getSqlFileRepository() {
205+
return sqlFileRepository;
206+
}
207+
208+
@Override
209+
public ScriptFileLoader getScriptFileLoader() {
210+
return scriptFileLoader;
211+
}
212+
213+
@Override
214+
public JdbcLogger getJdbcLogger() {
215+
return jdbcLogger;
216+
}
217+
218+
@Override
219+
public RequiresNewController getRequiresNewController() {
220+
return requiresNewController;
221+
}
222+
223+
@Override
224+
public ClassHelper getClassHelper() {
225+
return classHelper;
226+
}
227+
228+
@Override
229+
public CommandImplementors getCommandImplementors() {
230+
return commandImplementors;
231+
}
232+
233+
@Override
234+
public QueryImplementors getQueryImplementors() {
235+
return queryImplementors;
236+
}
237+
238+
@Override
239+
public SqlLogType getExceptionSqlLogType() {
240+
return exceptionSqlLogType;
241+
}
242+
243+
@Override
244+
public UnknownColumnHandler getUnknownColumnHandler() {
245+
return unknownColumnHandler;
246+
}
247+
248+
@Override
249+
public DuplicateColumnHandler getDuplicateColumnHandler() {
250+
return duplicateColumnHandler;
251+
}
252+
253+
@Override
254+
public Naming getNaming() {
255+
return naming;
256+
}
257+
258+
@Override
259+
public MapKeyNaming getMapKeyNaming() {
260+
return mapKeyNaming;
261+
}
262+
263+
@Override
264+
public TransactionManager getTransactionManager() {
265+
return transactionManager;
266+
}
267+
268+
@Override
269+
public Commenter getCommenter() {
270+
return commenter;
271+
}
272+
273+
@Override
274+
public EntityListenerProvider getEntityListenerProvider() {
275+
return entityListenerProvider;
276+
}
277+
278+
@Override
279+
public SqlBuilderSettings getSqlBuilderSettings() {
280+
return sqlBuilderSettings;
281+
}
282+
283+
@Override
284+
public StatisticManager getStatisticManager() {
285+
return statisticManager;
286+
}
287+
288+
@Override
289+
public int getMaxRows() {
290+
return maxRows;
291+
}
292+
293+
@Override
294+
public int getFetchSize() {
295+
return fetchSize;
296+
}
297+
298+
@Override
299+
public int getQueryTimeout() {
300+
return queryTimeout;
301+
}
302+
303+
@Override
304+
public int getBatchSize() {
305+
return batchSize;
306+
}
307+
308+
@Override
309+
public LocalTransactionDataSource getLocalTransactionDataSource() {
310+
return dataSource;
311+
}
312+
313+
@Override
314+
public LocalTransactionManager getLocalTransactionManager() {
315+
return transactionManager;
316+
}
317+
}

0 commit comments

Comments
 (0)