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
.immutables
;
9 import org
.immutables
.value
.Value
;
10 import wtf
.metio
.yosql
.internals
.jdk
.Buckets
;
11 import wtf
.metio
.yosql
.models
.configuration
.ResultRowConverter
;
12 import wtf
.metio
.yosql
.models
.configuration
.ReturningMode
;
13 import wtf
.metio
.yosql
.models
.configuration
.SqlStatementType
;
15 import java
.nio
.file
.Path
;
16 import java
.util
.Comparator
;
17 import java
.util
.List
;
18 import java
.util
.stream
.Stream
;
21 * Encapsulates everything we know about a single SQL statement.
24 public interface SqlStatement
{
28 static ImmutableSqlStatement
.SourcePathBuildStage
builder() {
29 return ImmutableSqlStatement
.builder();
36 static Stream
<ResultRowConverter
> resultConverters(
37 final List
<SqlStatement
> statements
,
38 final ResultRowConverter defaultConverter
) {
39 return statements
.stream()
40 .map(SqlStatement
::getConfiguration
)
41 .filter(SqlStatement
::requiresConverter
)
42 .map(config
-> config
.resultRowConverter().orElse(defaultConverter
))
43 .sorted(Comparator
.comparing((ResultRowConverter converter
) -> converter
.alias().orElse(""))
44 .thenComparing(converter
-> converter
.converterType().orElse(""))
45 .thenComparing(converter
-> converter
.resultType().orElse(""))
46 .thenComparing(converter
-> converter
.methodName().orElse("")))
50 private static boolean requiresConverter(final SqlConfiguration configuration
) {
51 return configuration
.returningMode()
52 .map(mode
-> ReturningMode
.NONE
!= mode
)
53 .orElse(Boolean
.FALSE
);
59 * @return The file system path to the source file of this SQL statement.
64 * @return The parsed configuration of this SQL statement.
66 SqlConfiguration
getConfiguration();
69 * @return The raw SQL statement.
71 String
getRawStatement();
76 default String
getName() {
77 return getConfiguration().name().orElseThrow();
81 default String
getRepositoryClass() {
82 return getConfiguration().repository().orElseThrow();
86 default String
getRepositoryInterface() {
87 return getConfiguration().repositoryInterface().orElse("");
91 default boolean isReading() {
92 return getConfiguration().type()
93 .map(type
-> SqlStatementType
.READING
== type
)
94 .orElse(Boolean
.FALSE
);
98 default boolean isWriting() {
99 return getConfiguration().type()
100 .map(type
-> SqlStatementType
.WRITING
== type
)
101 .orElse(Boolean
.FALSE
);
105 default boolean isCalling() {
106 return getConfiguration().type()
107 .map(type
-> SqlStatementType
.CALLING
== type
)
108 .orElse(Boolean
.FALSE
);
112 default boolean executeWriteBatch() {
113 return isWriting() &&
114 getConfiguration().executeBatch().orElse(Boolean
.FALSE
) &&
115 Buckets
.hasEntries(getConfiguration().parameters());
119 default boolean executeReadOnce() {
120 return isReading() && getConfiguration().executeOnce().orElse(Boolean
.FALSE
);
124 default boolean executeCallOnce() {
125 return isCalling() && getConfiguration().executeOnce().orElse(Boolean
.FALSE
);
129 default boolean executeWriteOnce() {
130 return isWriting() && getConfiguration().executeOnce().orElse(Boolean
.FALSE
);