fix #123 allow to specify extra annotations on repositories/methods
[yosql.git] / yosql-models / yosql-models-immutables / src / main / java / wtf / metio / yosql / models / immutables / SqlStatement.java
blob364a69b290173284465559c1bb547214cd4213c0
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.List;
17 import java.util.stream.Stream;
19 /**
20 * Encapsulates everything we know about a single SQL statement.
22 @Value.Immutable
23 public interface SqlStatement {
25 //region builders
27 static ImmutableSqlStatement.SourcePathBuildStage builder() {
28 return ImmutableSqlStatement.builder();
31 //endregion
33 //region utils
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))
42 .distinct();
45 private static boolean requiresConverter(final SqlConfiguration configuration) {
46 return configuration.returningMode()
47 .map(mode -> ReturningMode.NONE != mode)
48 .orElse(Boolean.FALSE);
51 //endregion
53 /**
54 * @return The file system path to the source file of this SQL statement.
56 Path getSourcePath();
58 /**
59 * @return The parsed configuration of this SQL statement.
61 SqlConfiguration getConfiguration();
63 /**
64 * @return The raw SQL statement.
66 String getRawStatement();
68 //region derived
70 @Value.Lazy
71 default String getName() {
72 return getConfiguration().name().orElse("");
75 @Value.Lazy
76 default String getRepositoryClass() {
77 return getConfiguration().repository().orElse("");
80 @Value.Lazy
81 default String getRepositoryInterface() {
82 return getConfiguration().repositoryInterface().orElse("");
85 @Value.Lazy
86 default boolean isReading() {
87 return getConfiguration().type()
88 .map(type -> SqlStatementType.READING == type)
89 .orElse(Boolean.FALSE);
92 @Value.Lazy
93 default boolean isWriting() {
94 return getConfiguration().type()
95 .map(type -> SqlStatementType.WRITING == type)
96 .orElse(Boolean.FALSE);
99 @Value.Lazy
100 default boolean isCalling() {
101 return getConfiguration().type()
102 .map(type -> SqlStatementType.CALLING == type)
103 .orElse(Boolean.FALSE);
106 @Value.Lazy
107 default boolean generateBatchWriteAPI() {
108 return isWriting() &&
109 getConfiguration().generateBatchApi().orElse(Boolean.FALSE) &&
110 Buckets.hasEntries(getConfiguration().parameters());
113 @Value.Lazy
114 default boolean generateStandardReadAPI() {
115 return isReading() && getConfiguration().generateStandardApi().orElse(Boolean.FALSE);
118 @Value.Lazy
119 default boolean generateStandardCallAPI() {
120 return isCalling() && getConfiguration().generateStandardApi().orElse(Boolean.FALSE);
123 @Value.Lazy
124 default boolean generateStandardWriteAPI() {
125 return isWriting() && getConfiguration().generateStandardApi().orElse(Boolean.FALSE);
128 //endregion