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
.logging
;
9 import com
.squareup
.javapoet
.CodeBlock
;
10 import com
.squareup
.javapoet
.FieldSpec
;
11 import com
.squareup
.javapoet
.TypeName
;
12 import wtf
.metio
.yosql
.codegen
.exceptions
.CodeGenerationException
;
13 import wtf
.metio
.yosql
.models
.configuration
.LoggingApis
;
14 import wtf
.metio
.yosql
.models
.immutables
.LoggingConfiguration
;
16 import java
.util
.Optional
;
20 * Delegates logging statement generation to a set of {@link LoggingGenerator}s.
22 public final class DelegatingLoggingGenerator
implements LoggingGenerator
{
24 private final LoggingConfiguration loggingConfiguration
;
25 private final Set
<LoggingGenerator
> generators
;
27 public DelegatingLoggingGenerator(
28 final LoggingConfiguration loggingConfiguration
,
29 final Set
<LoggingGenerator
> generators
) {
30 this.loggingConfiguration
= loggingConfiguration
;
31 this.generators
= generators
;
35 public boolean supports(final LoggingApis api
) {
36 return generators
.stream().anyMatch(generator
-> generator
.supports(api
));
40 public CodeBlock
queryPicked(final String fieldName
) {
41 return log().queryPicked(fieldName
);
45 public CodeBlock
indexPicked(final String fieldName
) {
46 return log().indexPicked(fieldName
);
50 public CodeBlock
vendorQueryPicked(final String fieldName
) {
51 return log().vendorQueryPicked(fieldName
);
55 public CodeBlock
vendorIndexPicked(final String fieldName
) {
56 return log().vendorIndexPicked(fieldName
);
60 public CodeBlock
vendorDetected() {
61 return log().vendorDetected();
65 public CodeBlock
executingQuery() {
66 return log().executingQuery();
70 public CodeBlock
shouldLog() {
71 return log().shouldLog();
75 public boolean isEnabled() {
76 return log().isEnabled();
80 public Optional
<FieldSpec
> logger(final TypeName repoClass
) {
81 return log().logger(repoClass
);
85 public CodeBlock
entering(final String repository
, final String method
) {
86 return log().entering(repository
, method
);
89 private LoggingGenerator
log() {
90 return generators
.stream()
91 .filter(generator
-> generator
.supports(loggingConfiguration
.api()))
93 .orElseThrow(() -> new CodeGenerationException("TODO: add error message for missing support for " + loggingConfiguration
.api()));