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
.codegen
.dao
;
9 import com
.squareup
.javapoet
.MethodSpec
;
10 import org
.slf4j
.cal10n
.LocLogger
;
11 import wtf
.metio
.yosql
.codegen
.lifecycle
.ApplicationWarnings
;
12 import wtf
.metio
.yosql
.models
.immutables
.SqlConfiguration
;
13 import wtf
.metio
.yosql
.models
.immutables
.SqlStatement
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Collection
;
17 import java
.util
.List
;
18 import java
.util
.function
.BiFunction
;
19 import java
.util
.function
.Predicate
;
20 import java
.util
.stream
.Collectors
;
21 import java
.util
.stream
.Stream
;
24 * Default implementation of a {@link MethodsGenerator} that delegates most of its work to other interfaces/classes.
26 public final class DefaultMethodsGenerator
implements MethodsGenerator
{
28 private final ConstructorGenerator constructor
;
29 private final ReadMethodGenerator readMethods
;
30 private final WriteMethodGenerator writeMethods
;
31 private final CallMethodGenerator callingMethods
;
32 private final LocLogger logger
;
35 * @param constructor The constructor generator to use.
36 * @param readMethods The read methods generator to use.
37 * @param writeMethods The write methods generator to use.
38 * @param callingMethods The call methods generator to use.
39 * @param logger The logger to use.
41 public DefaultMethodsGenerator(
42 final ConstructorGenerator constructor
,
43 final ReadMethodGenerator readMethods
,
44 final WriteMethodGenerator writeMethods
,
45 final CallMethodGenerator callingMethods
,
46 final LocLogger logger
) {
47 this.constructor
= constructor
;
48 this.readMethods
= readMethods
;
49 this.writeMethods
= writeMethods
;
50 this.callingMethods
= callingMethods
;
55 public Iterable
<MethodSpec
> asMethodsDeclarations(final List
<SqlStatement
> statements
) {
56 final var methods
= new ArrayList
<MethodSpec
>(statements
.size());
58 methods
.addAll(asMethods(statements
,
59 callingMethods
::callMethodDeclaration
,
60 readMethods
::readMethodDeclaration
,
61 writeMethods
::writeMethodDeclaration
,
62 writeMethods
::batchWriteMethodDeclaration
));
68 public Iterable
<MethodSpec
> asMethods(final List
<SqlStatement
> statements
) {
69 final var methods
= new ArrayList
<MethodSpec
>(statements
.size());
71 methods
.add(constructor
.repository(statements
));
72 methods
.addAll(asMethods(statements
,
73 callingMethods
::callMethod
,
74 readMethods
::readMethod
,
75 writeMethods
::writeMethod
,
76 writeMethods
::batchWriteMethod
));
77 warnAboutIgnoredStatements(statements
);
82 private static List
<MethodSpec
> asMethods(
83 final List
<SqlStatement
> statements
,
84 final BiFunction
<SqlConfiguration
, List
<SqlStatement
>, MethodSpec
> call
,
85 final BiFunction
<SqlConfiguration
, List
<SqlStatement
>, MethodSpec
> read
,
86 final BiFunction
<SqlConfiguration
, List
<SqlStatement
>, MethodSpec
> write
,
87 final BiFunction
<SqlConfiguration
, List
<SqlStatement
>, MethodSpec
> batchWrite
) {
89 asMethods(statements
, SqlStatement
::executeCallOnce
, call
),
90 asMethods(statements
, SqlStatement
::executeReadOnce
, read
),
91 asMethods(statements
, SqlStatement
::executeWriteOnce
, write
),
92 asMethods(statements
, SqlStatement
::executeWriteBatch
, batchWrite
))
93 .flatMap(Collection
::stream
)
97 private static List
<MethodSpec
> asMethods(
98 final List
<SqlStatement
> statements
,
99 final Predicate
<SqlStatement
> filter
,
100 final BiFunction
<SqlConfiguration
, List
<SqlStatement
>, MethodSpec
> generator
) {
101 return statements
.stream()
103 .collect(Collectors
.groupingBy(SqlStatement
::getName
))
106 .map(statementsWithSameName
-> generator
.apply(
107 SqlConfiguration
.fromStatements(statementsWithSameName
), statementsWithSameName
))
111 private void warnAboutIgnoredStatements(final List
<SqlStatement
> statements
) {
113 .filter(Predicate
.not(SqlStatement
::executeCallOnce
)
114 .and(Predicate
.not(SqlStatement
::executeReadOnce
))
115 .and(Predicate
.not(SqlStatement
::executeWriteOnce
))
116 .and(Predicate
.not(SqlStatement
::executeWriteBatch
)))
117 .forEach(statement
-> logger
.warn(ApplicationWarnings
.SQL_STATEMENT_IGNORED
,
118 statement
.getName(), statement
.getSourcePath()));