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
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
;
23 * Represents a single input parameter of a SQL statement.
27 as
= ImmutableSqlParameter
.class
30 as
= ImmutableSqlParameter
.class
32 public interface SqlParameter
{
36 static ImmutableSqlParameter
.Builder
builder() {
37 return ImmutableSqlParameter
.builder();
40 static ImmutableSqlParameter
copyOf(final SqlParameter parameter
) {
41 return ImmutableSqlParameter
.copyOf(parameter
);
48 static List
<SqlParameter
> mergeParameters(
49 final List
<SqlParameter
> first
,
50 final List
<SqlParameter
> second
) {
51 if (first
== null || first
.isEmpty()) {
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
) {
61 .map(param
-> second
.stream()
62 .filter(other
-> param
.name().equals(other
.name()))
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
)));
73 * @return The name of the parameter.
75 Optional
<String
> name();
78 * @return The fully-qualified type name.
80 Optional
<String
> type();
83 * @return The indices in the SQL statement that match this parameter.
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
95 default Optional
<TypeName
> typeName() {
96 return type().map(TypeGuesser
::guessTypeName
);
100 * @return <code>true</code> in case this statement has indices, <code>false</code> otherwise.
103 default boolean hasIndices() {
104 return indices().map(values
-> values
.length
> 0).orElse(Boolean
.FALSE
);