Bump dawidd6/action-send-mail from 3 to 4
[yosql.git] / yosql-models / yosql-models-immutables / src / main / java / wtf / metio / yosql / models / immutables / SqlStatement.java
blob786b7e9703508475b6ef891c3af3866cea5871bb
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.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;
20 /**
21 * Encapsulates everything we know about a single SQL statement.
23 @Value.Immutable
24 public interface SqlStatement {
26 //region builders
28 static ImmutableSqlStatement.SourcePathBuildStage builder() {
29 return ImmutableSqlStatement.builder();
32 //endregion
34 //region utils
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("")))
47 .distinct();
50 private static boolean requiresConverter(final SqlConfiguration configuration) {
51 return configuration.returningMode()
52 .map(mode -> ReturningMode.NONE != mode)
53 .orElse(Boolean.FALSE);
56 //endregion
58 /**
59 * @return The file system path to the source file of this SQL statement.
61 Path getSourcePath();
63 /**
64 * @return The parsed configuration of this SQL statement.
66 SqlConfiguration getConfiguration();
68 /**
69 * @return The raw SQL statement.
71 String getRawStatement();
73 //region derived
75 @Value.Lazy
76 default String getName() {
77 return getConfiguration().name().orElseThrow();
80 @Value.Lazy
81 default String getRepositoryClass() {
82 return getConfiguration().repository().orElseThrow();
85 @Value.Lazy
86 default String getRepositoryInterface() {
87 return getConfiguration().repositoryInterface().orElse("");
90 @Value.Lazy
91 default boolean isReading() {
92 return getConfiguration().type()
93 .map(type -> SqlStatementType.READING == type)
94 .orElse(Boolean.FALSE);
97 @Value.Lazy
98 default boolean isWriting() {
99 return getConfiguration().type()
100 .map(type -> SqlStatementType.WRITING == type)
101 .orElse(Boolean.FALSE);
104 @Value.Lazy
105 default boolean isCalling() {
106 return getConfiguration().type()
107 .map(type -> SqlStatementType.CALLING == type)
108 .orElse(Boolean.FALSE);
111 @Value.Lazy
112 default boolean executeWriteBatch() {
113 return isWriting() &&
114 getConfiguration().executeBatch().orElse(Boolean.FALSE) &&
115 Buckets.hasEntries(getConfiguration().parameters());
118 @Value.Lazy
119 default boolean executeReadOnce() {
120 return isReading() && getConfiguration().executeOnce().orElse(Boolean.FALSE);
123 @Value.Lazy
124 default boolean executeCallOnce() {
125 return isCalling() && getConfiguration().executeOnce().orElse(Boolean.FALSE);
128 @Value.Lazy
129 default boolean executeWriteOnce() {
130 return isWriting() && getConfiguration().executeOnce().orElse(Boolean.FALSE);
133 //endregion