close some todos
[yosql.git] / yosql-models / yosql-models-configuration / src / main / java / wtf / metio / yosql / models / configuration / SqlParameter.java
blob47352fd49b3d18454725aa7c58b50f34e6471d63
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 https://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 */
7 package wtf.metio.yosql.models.configuration;
9 import com.fasterxml.jackson.annotation.JsonIgnore;
10 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
11 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
12 import com.squareup.javapoet.TypeName;
13 import de.xn__ho_hia.javapoet.TypeGuesser;
14 import org.immutables.value.Value;
15 import wtf.metio.yosql.internals.jdk.Buckets;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
22 /**
23 * Represents a single input parameter of a SQL statement.
25 @Value.Immutable
26 @JsonSerialize(
27 as = ImmutableSqlParameter.class
29 @JsonDeserialize(
30 as = ImmutableSqlParameter.class
32 public interface SqlParameter {
34 //region builders
36 static ImmutableSqlParameter.Builder builder() {
37 return ImmutableSqlParameter.builder();
40 static ImmutableSqlParameter copyOf(final SqlParameter parameter) {
41 return ImmutableSqlParameter.copyOf(parameter);
44 //endregion
46 //region utils
48 static List<SqlParameter> mergeParameters(
49 final List<SqlParameter> first,
50 final List<SqlParameter> second) {
51 if (first == null || first.isEmpty()) {
52 return second;
54 return Stream.concat(copyAttributes(first, second), copyAttributes(second, first))
55 .filter(Buckets.distinctByKey(SqlParameter::name))
56 .collect(Collectors.toList());
59 private static Stream<ImmutableSqlParameter> copyAttributes(final List<SqlParameter> first, final List<SqlParameter> second) {
60 return first.stream()
61 .map(param -> second.stream()
62 .filter(other -> param.name().equals(other.name()))
63 .findFirst()
64 .map(other -> SqlParameter.copyOf(param)
65 .withType(param.type().or(other::type))
66 .withIndices(param.indices().or(other::indices)))
67 .orElseGet(() -> SqlParameter.copyOf(param)));
70 //endregion
72 /**
73 * @return The name of the parameter.
75 Optional<String> name();
77 /**
78 * @return The fully-qualified type name.
80 Optional<String> type();
82 /**
83 * @return The indices in the SQL statement that match this parameter.
85 @JsonIgnore
86 Optional<int[]> indices();
88 // TODO: add parameter type (in, out, inout)
89 // TODO: add sql type (see java.sql.Types)
90 // TODO: add scale for sql numeric/decimal types
92 //region derived
94 @Value.Lazy
95 default Optional<TypeName> typeName() {
96 return type().map(TypeGuesser::guessTypeName);
99 /**
100 * @return <code>true</code> in case this statement has indices, <code>false</code> otherwise.
102 @Value.Lazy
103 default boolean hasIndices() {
104 return indices().map(values -> values.length > 0).orElse(Boolean.FALSE);
107 //endregion