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 default result row converter in case no other is specified.
33 public abstract Property
<DefaultResultRowConverter
> getDefaultConverter();
36 * @return The list of custom converters provided by the user.
39 public abstract NamedDomainObjectContainer
<UserResultRowConverter
> getUserTypes();
42 * @return The package name for all utility classes.
45 public abstract Property
<String
> getUtilityPackageName();
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())
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
)
81 .filter(Predicate
.not(Strings
::isBlank
))
82 .map(value
-> value
.split(":"))
83 .map(values
-> ResultRowConverter
.builder()
85 .setConverterType(values
[1])
86 .setMethodName(values
[2])
87 .setResultType(values
[3])
89 .orElse(ResultRowConverter
.builder()
90 .setAlias("resultRow")
91 .setConverterType("com.example.persistence.util.ToResultRowConverter")
92 .setMethodName("asUserType")
93 .setResultType("com.example.persistence.util.ResultRow")