allow all configs from gradle
[yosql.git] / yosql-tooling / yosql-tooling-gradle / src / main / java / wtf / metio / yosql / tooling / gradle / Jdbc.java
blobd09bc12654e0c2fd2e58b41660ebf12d720a5205
1 /*
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
5 * in the LICENSE file.
6 */
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;
24 /**
25 * Configures JDBC related options.
27 public abstract class Jdbc {
29 /**
30 * @return The name suffix to add for raw SQL statements.
32 @Input
33 public abstract Property<String> getRawSuffix();
35 /**
36 * @return The name suffix to add for index lookup tables.
38 @Input
39 public abstract Property<String> getIndexSuffix();
41 /**
42 * @return The name for a DataSource variable.
44 @Input
45 public abstract Property<String> getDataSource();
47 /**
48 * @return The name for a Connection variable.
50 @Input
51 public abstract Property<String> getConnection();
53 /**
54 * @return The name for a Statement variable.
56 @Input
57 public abstract Property<String> getStatement();
59 /**
60 * @return The name for a MetaData variable.
62 @Input
63 public abstract Property<String> getMetaData();
65 /**
66 * @return The name for a ResultSet variable.
68 @Input
69 public abstract Property<String> getResultSet();
71 /**
72 * @return The name for a ColumnCount variable.
74 @Input
75 public abstract Property<String> getColumnCount();
77 /**
78 * @return The name for a ColumnLabel variable.
80 @Input
81 public abstract Property<String> getColumnLabel();
83 /**
84 * @return The name for a Batch variable.
86 @Input
87 public abstract Property<String> getBatch();
89 /**
90 * @return The name for a List variable.
92 @Input
93 public abstract Property<String> getList();
95 /**
96 * @return The name for a JDBC index variable.
98 @Input
99 public abstract Property<String> getJdbcIndex();
102 * @return The name for a index variable.
104 @Input
105 public abstract Property<String> getIndex();
108 * @return The name for a row variable.
110 @Input
111 public abstract Property<String> getRow();
114 * @return The class name of the result-state class.
116 @Input
117 public abstract Property<String> getResultStateClassName();
120 * @return The class name of the result-row class.
122 @Input
123 public abstract Property<String> getResultRowClassName();
126 * @return The class name of the flow-state class.
128 @Input
129 public abstract Property<String> getFlowStateClassName();
132 * @return The default result row converter in case no other is specified.
134 @Input
135 public abstract Property<DefaultResultRowConverter> getDefaultConverter();
138 * @return The list of custom converters provided by the user.
140 @Input
141 public abstract NamedDomainObjectContainer<UserResultRowConverter> getUserTypes();
144 * @return The package name for JDBC utility classes.
146 @Input
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())
175 .build();
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)
213 .map(String::strip)
214 .filter(Predicate.not(Strings::isBlank))
215 .map(value -> value.split(":"))
216 .map(values -> ResultRowConverter.builder()
217 .setAlias(values[0])
218 .setConverterType(values[1])
219 .setMethodName(values[2])
220 .setResultType(values[3])
221 .build())
222 .orElse(ResultRowConverter.builder()
223 .setAlias("resultRow")
224 .setConverterType("com.example.persistence.util.ToResultRowConverter")
225 .setMethodName("asUserType")
226 .setResultType("com.example.persistence.util.ResultRow")
227 .build());