configure plugin & source sets
[yosql.git] / yosql-tooling / yosql-tooling-gradle / src / main / java / wtf / metio / yosql / tooling / gradle / Jdbc.java
blob5f6ceba17705ae347a4ab8bbe76117059ce8f189
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 default result row converter in case no other is specified.
32 @Input
33 public abstract Property<DefaultResultRowConverter> getDefaultConverter();
35 /**
36 * @return The list of custom converters provided by the user.
38 @Input
39 public abstract NamedDomainObjectContainer<UserResultRowConverter> getUserTypes();
41 /**
42 * @return The package name for all utility classes.
44 @Input
45 public abstract Property<String> getUtilityPackageName();
47 /**
48 * @param action The files config to apply.
50 public void defaultConverter(Action<? super DefaultResultRowConverter> action) {
51 action.execute(getDefaultConverter().get());
54 JdbcConfiguration asConfiguration() {
55 return JdbcConfiguration.usingDefaults()
56 .setDefaultConverter(getDefaultConverter().get().asResultRowConverter(getUtilityPackageName().get()))
57 .setUserTypes(createRowConverters())
58 .setUtilityPackageName(getUtilityPackageName().get())
59 .build();
62 void configureConventions(final ObjectFactory objects) {
63 final var defaultConverter = objects.newInstance(DefaultResultRowConverter.class);
64 defaultConverter.getAlias().set("resultRow");
65 defaultConverter.getConverterType().set("ToResultRowConverter");
66 defaultConverter.getMethodName().set("asUserType");
67 defaultConverter.getResultType().set("ResultRow");
68 getDefaultConverter().convention(defaultConverter);
69 getUtilityPackageName().convention("com.example.persistence.util");
72 private List<ResultRowConverter> createRowConverters() {
73 return getUserTypes().stream()
74 .map(UserResultRowConverter::asResultRowConverter)
75 .collect(Collectors.toList());
78 private ResultRowConverter createRowConverter(final String converterDefinition) {
79 return Optional.ofNullable(converterDefinition)
80 .map(String::strip)
81 .filter(Predicate.not(Strings::isBlank))
82 .map(value -> value.split(":"))
83 .map(values -> ResultRowConverter.builder()
84 .setAlias(values[0])
85 .setConverterType(values[1])
86 .setMethodName(values[2])
87 .setResultType(values[3])
88 .build())
89 .orElse(ResultRowConverter.builder()
90 .setAlias("resultRow")
91 .setConverterType("com.example.persistence.util.ToResultRowConverter")
92 .setMethodName("asUserType")
93 .setResultType("com.example.persistence.util.ResultRow")
94 .build());