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 http://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
8 package wtf
.metio
.yosql
.codegen
.blocks
;
10 import com
.squareup
.javapoet
.CodeBlock
;
11 import de
.xn__ho_hia
.javapoet
.TypeGuesser
;
12 import wtf
.metio
.yosql
.codegen
.api
.Javadoc
;
13 import wtf
.metio
.yosql
.internals
.jdk
.Strings
;
14 import wtf
.metio
.yosql
.models
.immutables
.FilesConfiguration
;
15 import wtf
.metio
.yosql
.models
.immutables
.SqlConfiguration
;
16 import wtf
.metio
.yosql
.models
.immutables
.SqlStatement
;
17 import wtf
.metio
.yosql
.models
.sql
.ResultRowConverter
;
19 import java
.util
.List
;
20 import java
.util
.Objects
;
22 import static java
.util
.function
.Predicate
.not
;
24 public final class DefaultJavadoc
implements Javadoc
{
26 private final FilesConfiguration files
;
28 public DefaultJavadoc(final FilesConfiguration files
) {
33 public CodeBlock
repositoryJavadoc(final List
<SqlStatement
> statements
) {
34 final var builder
= CodeBlock
.builder()
35 .add("Generated based on the following file(s):\n")
37 final var input
= files
.inputBaseDirectory();
39 .map(SqlStatement
::getSourcePath
)
41 .map(input
::relativize
)
42 .forEach(path
-> builder
.add("<li>$L</li>\n", path
));
43 builder
.add("</ul>\n");
44 return builder
.build();
48 public CodeBlock
fieldJavaDoc(final SqlStatement statement
) {
49 final var input
= files
.inputBaseDirectory();
50 final var builder
= CodeBlock
.builder()
51 .add("Generated based on the following file:\n")
53 .add("<li>$L</li>\n", input
.relativize(statement
.getSourcePath()));
54 builder
.add("</ul>\n");
55 return builder
.build();
59 public CodeBlock
methodJavadoc(final List
<SqlStatement
> statements
) {
60 final var builder
= CodeBlock
.builder();
62 .map(SqlStatement
::getConfiguration
)
63 .map(SqlConfiguration
::description
)
64 .filter(not(Strings
::isBlank
))
65 .forEach(description
-> builder
.add("<p>$L<p>\n", description
));
66 if (statements
.size() > 1) {
67 builder
.add("<p>Executes one of the following statements:</p>");
69 builder
.add("<p>Executes the following statement:</p>");
71 for (final var statement
: statements
) {
72 if (Strings
.isBlank(statement
.getConfiguration().vendor())) {
73 if (statements
.size() > 1) {
74 builder
.add("\n\n<p>Fallback for other databases:</p>");
77 builder
.add("\n\n<p>Vendor: $N</p>", statement
.getConfiguration().vendor());
79 builder
.add("\n<pre>\n$L</pre>", statement
.getRawStatement());
81 builder
.add("\n\n<p>Generated based on the following file(s):</p>\n")
83 final var input
= files
.inputBaseDirectory();
85 .map(SqlStatement
::getSourcePath
)
87 .map(input
::relativize
)
88 .forEach(path
-> builder
.add("<li>$L</li>\n", path
));
89 builder
.add("</ul>\n");
91 .map(SqlStatement
::getConfiguration
)
92 .flatMap(config
-> config
.resultRowConverter().stream())
93 .filter(Objects
::nonNull
)
94 .map(ResultRowConverter
::resultType
)
95 .filter(Objects
::nonNull
)
96 .filter(type
-> !type
.startsWith("java"))
97 .map(type
-> type
.substring(0, type
.contains("<") ? type
.indexOf("<") : type
.length()))
99 .map(TypeGuesser
::guessTypeName
)
100 .filter(type
-> !type
.isPrimitive())
101 .filter(type
-> !type
.isBoxedPrimitive())
102 .forEach(type
-> builder
.add("\n@see $S", type
));
103 return builder
.build();