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
.List
;
17 import java
.util
.stream
.Stream
;
20 * Encapsulates everything we know about a single SQL statement.
23 public interface SqlStatement
{
27 static ImmutableSqlStatement
.SourcePathBuildStage
builder() {
28 return ImmutableSqlStatement
.builder();
35 static Stream
<ResultRowConverter
> resultConverters(
36 final List
<SqlStatement
> statements
,
37 final ResultRowConverter defaultConverter
) {
38 return statements
.stream()
39 .map(SqlStatement
::getConfiguration
)
40 .filter(SqlStatement
::requiresConverter
)
41 .map(config
-> config
.resultRowConverter().orElse(defaultConverter
))
45 private static boolean requiresConverter(final SqlConfiguration configuration
) {
46 return configuration
.returningMode()
47 .map(mode
-> ReturningMode
.NONE
!= mode
)
48 .orElse(Boolean
.FALSE
);
54 * @return The file system path to the source file of this SQL statement.
59 * @return The parsed configuration of this SQL statement.
61 SqlConfiguration
getConfiguration();
64 * @return The raw SQL statement.
66 String
getRawStatement();
71 default String
getName() {
72 return getConfiguration().name().orElse("");
76 default String
getRepositoryClass() {
77 return getConfiguration().repository().orElse("");
81 default String
getRepositoryInterface() {
82 return getConfiguration().repositoryInterface().orElse("");
86 default boolean isReading() {
87 return getConfiguration().type()
88 .map(type
-> SqlStatementType
.READING
== type
)
89 .orElse(Boolean
.FALSE
);
93 default boolean isWriting() {
94 return getConfiguration().type()
95 .map(type
-> SqlStatementType
.WRITING
== type
)
96 .orElse(Boolean
.FALSE
);
100 default boolean isCalling() {
101 return getConfiguration().type()
102 .map(type
-> SqlStatementType
.CALLING
== type
)
103 .orElse(Boolean
.FALSE
);
107 default boolean generateBatchWriteAPI() {
108 return isWriting() &&
109 getConfiguration().generateBatchApi().orElse(Boolean
.FALSE
) &&
110 Buckets
.hasEntries(getConfiguration().parameters());
114 default boolean generateStandardReadAPI() {
115 return isReading() && getConfiguration().generateStandardApi().orElse(Boolean
.FALSE
);
119 default boolean generateStandardCallAPI() {
120 return isCalling() && getConfiguration().generateStandardApi().orElse(Boolean
.FALSE
);
124 default boolean generateStandardWriteAPI() {
125 return isWriting() && getConfiguration().generateStandardApi().orElse(Boolean
.FALSE
);