2 * This file is part of yosql. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of yosql,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
8 package wtf
.metio
.yosql
.tooling
.gradle
;
10 import org
.gradle
.api
.Action
;
11 import org
.gradle
.api
.NamedDomainObjectContainer
;
12 import org
.gradle
.api
.model
.ObjectFactory
;
13 import org
.gradle
.api
.provider
.Property
;
14 import org
.gradle
.api
.tasks
.Input
;
15 import wtf
.metio
.yosql
.internals
.jdk
.Strings
;
16 import wtf
.metio
.yosql
.models
.immutables
.JdbcConfiguration
;
17 import wtf
.metio
.yosql
.models
.sql
.ResultRowConverter
;
19 import java
.util
.List
;
20 import java
.util
.Optional
;
21 import java
.util
.function
.Predicate
;
22 import java
.util
.stream
.Collectors
;
25 * Configures JDBC related options.
27 public abstract class Jdbc
{
30 * @return The name suffix to add for raw SQL statements.
33 public abstract Property
<String
> getRawSuffix();
36 * @return The name suffix to add for index lookup tables.
39 public abstract Property
<String
> getIndexSuffix();
42 * @return The name for a DataSource variable.
45 public abstract Property
<String
> getDataSource();
48 * @return The name for a Connection variable.
51 public abstract Property
<String
> getConnection();
54 * @return The name for a Statement variable.
57 public abstract Property
<String
> getStatement();
60 * @return The name for a MetaData variable.
63 public abstract Property
<String
> getMetaData();
66 * @return The name for a ResultSet variable.
69 public abstract Property
<String
> getResultSet();
72 * @return The name for a ColumnCount variable.
75 public abstract Property
<String
> getColumnCount();
78 * @return The name for a ColumnLabel variable.
81 public abstract Property
<String
> getColumnLabel();
84 * @return The name for a Batch variable.
87 public abstract Property
<String
> getBatch();
90 * @return The name for a List variable.
93 public abstract Property
<String
> getList();
96 * @return The name for a JDBC index variable.
99 public abstract Property
<String
> getJdbcIndex();
102 * @return The name for a index variable.
105 public abstract Property
<String
> getIndex();
108 * @return The name for a row variable.
111 public abstract Property
<String
> getRow();
114 * @return The class name of the result-state class.
117 public abstract Property
<String
> getResultStateClassName();
120 * @return The class name of the result-row class.
123 public abstract Property
<String
> getResultRowClassName();
126 * @return The class name of the flow-state class.
129 public abstract Property
<String
> getFlowStateClassName();
132 * @return The default result row converter in case no other is specified.
135 public abstract Property
<DefaultResultRowConverter
> getDefaultConverter();
138 * @return The list of custom converters provided by the user.
141 public abstract NamedDomainObjectContainer
<UserResultRowConverter
> getUserTypes();
144 * @return The package name for JDBC utility classes.
147 public abstract Property
<String
> getUtilityPackageName();
150 * @param action The files config to apply.
152 public void defaultConverter(Action
<?
super DefaultResultRowConverter
> action
) {
153 action
.execute(getDefaultConverter().get());
156 JdbcConfiguration
asConfiguration() {
157 return JdbcConfiguration
.usingDefaults()
158 .setDefaultConverter(getDefaultConverter().get().asResultRowConverter(getUtilityPackageName().get()))
159 .setUserTypes(createRowConverters())
160 .setUtilityPackageName(getUtilityPackageName().get())
161 .setRawSuffix(getRawSuffix().get())
162 .setIndexSuffix(getIndexSuffix().get())
163 .setDataSource(getDataSource().get())
164 .setConnection(getConnection().get())
165 .setStatement(getStatement().get())
166 .setMetaData(getMetaData().get())
167 .setResultSet(getResultSet().get())
168 .setColumnCount(getColumnCount().get())
169 .setColumnLabel(getColumnLabel().get())
170 .setBatch(getBatch().get())
171 .setList(getList().get())
172 .setJdbcIndex(getJdbcIndex().get())
173 .setIndex(getIndex().get())
174 .setRow(getRow().get())
178 void configureConventions(final ObjectFactory objects
) {
179 final var defaultConverter
= objects
.newInstance(DefaultResultRowConverter
.class);
180 defaultConverter
.getAlias().set("resultRow");
181 defaultConverter
.getConverterType().set("ToResultRowConverter");
182 defaultConverter
.getMethodName().set("asUserType");
183 defaultConverter
.getResultType().set("ResultRow");
184 getDefaultConverter().convention(defaultConverter
);
185 getUtilityPackageName().convention("com.example.persistence.util");
186 getRawSuffix().convention("_RAW");
187 getIndexSuffix().convention("_INDEX");
188 getDataSource().convention("dataSource");
189 getConnection().convention("connection");
190 getStatement().convention("statement");
191 getMetaData().convention("metaData");
192 getResultSet().convention("resultSet");
193 getColumnCount().convention("columnCount");
194 getColumnLabel().convention("columnLabel");
195 getBatch().convention("batch");
196 getList().convention("list");
197 getJdbcIndex().convention("jdbcIndex");
198 getIndex().convention("index");
199 getRow().convention("row");
200 getResultStateClassName().convention("ResultState");
201 getResultRowClassName().convention("ResultRow");
202 getFlowStateClassName().convention("FlowState");
205 private List
<ResultRowConverter
> createRowConverters() {
206 return getUserTypes().stream()
207 .map(UserResultRowConverter
::asResultRowConverter
)
208 .collect(Collectors
.toList());
211 private ResultRowConverter
createRowConverter(final String converterDefinition
) {
212 return Optional
.ofNullable(converterDefinition
)
214 .filter(Predicate
.not(Strings
::isBlank
))
215 .map(value
-> value
.split(":"))
216 .map(values
-> ResultRowConverter
.builder()
218 .setConverterType(values
[1])
219 .setMethodName(values
[2])
220 .setResultType(values
[3])
222 .orElse(ResultRowConverter
.builder()
223 .setAlias("resultRow")
224 .setConverterType("com.example.persistence.util.ToResultRowConverter")
225 .setMethodName("asUserType")
226 .setResultType("com.example.persistence.util.ResultRow")