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
8 package wtf
.metio
.yosql
.codegen
.files
;
10 import org
.slf4j
.cal10n
.LocLogger
;
11 import wtf
.metio
.yosql
.codegen
.lifecycle
.ApplicationErrors
;
12 import wtf
.metio
.yosql
.codegen
.lifecycle
.FileLifecycle
;
13 import wtf
.metio
.yosql
.codegen
.orchestration
.ExecutionErrors
;
14 import wtf
.metio
.yosql
.models
.immutables
.FilesConfiguration
;
15 import wtf
.metio
.yosql
.models
.immutables
.SqlStatement
;
17 import java
.io
.IOException
;
18 import java
.nio
.file
.FileVisitOption
;
19 import java
.nio
.file
.Files
;
20 import java
.util
.List
;
23 * Default SQL file parser that starts at {@link FilesConfiguration#inputBaseDirectory()
24 * the given source}, walks into every subdirectory and returns all statements found in
25 * {@link FilesConfiguration#sqlFilesSuffix() .sql files}.
27 public final class DefaultFileParser
implements FileParser
{
29 private final LocLogger logger
;
30 private final ParserPreconditions preconditions
;
31 private final FilesConfiguration fileConfiguration
;
32 private final ExecutionErrors errors
;
33 private final SqlStatementParser fileParser
;
35 public DefaultFileParser(
36 final LocLogger logger
,
37 final ParserPreconditions preconditions
,
38 final FilesConfiguration fileConfiguration
,
39 final ExecutionErrors errors
,
40 final SqlStatementParser fileParser
) {
42 this.preconditions
= preconditions
;
43 this.fileConfiguration
= fileConfiguration
;
45 this.fileParser
= fileParser
;
49 public List
<SqlStatement
> parseFiles() {
50 final var source
= fileConfiguration
.inputBaseDirectory();
51 logger
.trace(FileLifecycle
.READ_FILES
, source
);
52 preconditions
.directoryIsReadable(source
);
54 if (!errors
.hasErrors()) {
55 try (final var files
= Files
.walk(source
, FileVisitOption
.FOLLOW_LINKS
).parallel()) {
56 return files
.peek(path
-> logger
.trace(FileLifecycle
.ENCOUNTER_FILE
, path
))
57 .filter(Files
::isRegularFile
)
58 .filter(path
-> path
.toString().endsWith(fileConfiguration
.sqlFilesSuffix()))
59 .peek(path
-> logger
.trace(FileLifecycle
.CONSIDER_FILE
, path
))
60 .flatMap(fileParser
::parse
)
62 } catch (final IOException
| SecurityException exception
) {
63 logger
.error(ApplicationErrors
.READ_FILES_FAILED
, exception
.getLocalizedMessage());
64 errors
.add(exception
);