Skip to content

Commit 31addb7

Browse files
committed
Use standard generator architecture for AspectModelSqlGenerator
1 parent 24ca03b commit 31addb7

File tree

6 files changed

+42
-25
lines changed

6 files changed

+42
-25
lines changed

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/sql/AspectModelSqlGenerator.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
package org.eclipse.esmf.aspectmodel.generator.sql;
1515

16-
import org.eclipse.esmf.aspectmodel.generator.ArtifactGenerator;
16+
import java.util.stream.Stream;
17+
18+
import org.eclipse.esmf.aspectmodel.generator.AspectGenerator;
1719
import org.eclipse.esmf.aspectmodel.generator.sql.databricks.AspectModelDatabricksDenormalizedSqlVisitor;
1820
import org.eclipse.esmf.aspectmodel.generator.sql.databricks.AspectModelDatabricksDenormalizedSqlVisitorContextBuilder;
1921
import org.eclipse.esmf.aspectmodel.generator.sql.databricks.DatabricksSqlGenerationConfig;
@@ -22,27 +24,42 @@
2224
/**
2325
* Generates SQL scripts from an Aspect Model that set up tables to contain the data of the Aspect.
2426
*/
25-
public class AspectModelSqlGenerator implements ArtifactGenerator<String, String, Aspect, SqlGenerationConfig, SqlArtifact> {
26-
public static final AspectModelSqlGenerator INSTANCE = new AspectModelSqlGenerator();
27+
public class AspectModelSqlGenerator extends AspectGenerator<String, String, SqlGenerationConfig, SqlArtifact> {
28+
@Deprecated( forRemoval = true )
29+
public static final AspectModelSqlGenerator INSTANCE = new AspectModelSqlGenerator( null );
30+
public static final SqlGenerationConfig DEFAULT_CONFIG = SqlGenerationConfigBuilder.builder().build();
2731

28-
private AspectModelSqlGenerator() {
32+
public AspectModelSqlGenerator( final Aspect aspect ) {
33+
this( aspect, DEFAULT_CONFIG );
2934
}
3035

31-
@Override
36+
public AspectModelSqlGenerator( final Aspect aspect, final SqlGenerationConfig config ) {
37+
super( aspect, config );
38+
}
39+
40+
/**
41+
* @deprecated Use {@link #AspectModelSqlGenerator(Aspect, SqlGenerationConfig)} and {@link #singleResult()} instead
42+
*/
43+
@Deprecated( forRemoval = true )
3244
public SqlArtifact apply( final Aspect aspect, final SqlGenerationConfig sqlGenerationConfig ) {
33-
final String content = switch ( sqlGenerationConfig.dialect() ) {
34-
case DATABRICKS -> switch ( sqlGenerationConfig.mappingStrategy() ) {
45+
return new AspectModelSqlGenerator( aspect, sqlGenerationConfig ).singleResult();
46+
}
47+
48+
@Override
49+
public Stream<SqlArtifact> generate() {
50+
final String content = switch ( config.dialect() ) {
51+
case DATABRICKS -> switch ( config.mappingStrategy() ) {
3552
case DENORMALIZED -> {
36-
final DatabricksSqlGenerationConfig config =
37-
sqlGenerationConfig.dialectSpecificConfig() instanceof final DatabricksSqlGenerationConfig databricksConfig
53+
final DatabricksSqlGenerationConfig dataBricksConfig =
54+
config.dialectSpecificConfig() instanceof final DatabricksSqlGenerationConfig databricksConfig
3855
? databricksConfig
3956
: new DatabricksSqlGenerationConfig();
40-
yield aspect.accept( new AspectModelDatabricksDenormalizedSqlVisitor( config ),
57+
yield aspect().accept( new AspectModelDatabricksDenormalizedSqlVisitor( dataBricksConfig ),
4158
AspectModelDatabricksDenormalizedSqlVisitorContextBuilder.builder().build() );
4259
}
4360
};
4461
};
4562

46-
return new SqlArtifact( aspect.getName() + ".sql", content );
63+
return Stream.of( new SqlArtifact( aspect().getName() + ".sql", content ) );
4764
}
4865
}

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/sql/databricks/DatabricksType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public String toString() {
7979
record DatabricksStructEntry( String name, DatabricksType type, boolean nullable, Optional<String> comment ) {
8080
@Override
8181
public String toString() {
82-
return name + ": " + type + (nullable ? "" : " NOT NULL")
82+
return name + ": " + type + ( nullable ? "" : " NOT NULL" )
8383
+ comment.map( c -> " COMMENT '%s'".formatted( c.replace( "'", "\\'" ) ) ).orElse( "" );
8484
}
8585
}

core/esmf-aspect-model-document-generators/src/test/java/org/eclipse/esmf/aspectmodel/generator/sql/AspectModelSqlGeneratorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ void testDatabricksGeneration( final TestAspect testAspect ) {
3838
.includeColumnComments( true )
3939
.commentLanguage( Locale.ENGLISH )
4040
.build();
41-
final SqlArtifact sqlArtifact = AspectModelSqlGenerator.INSTANCE.apply( aspect, SqlGenerationConfigBuilder.builder()
41+
final SqlArtifact sqlArtifact = new AspectModelSqlGenerator( aspect, SqlGenerationConfigBuilder.builder()
4242
.dialect( SqlGenerationConfig.Dialect.DATABRICKS )
4343
.dialectSpecificConfig( dialectSpecificConfig )
44-
.build() );
44+
.build() ).singleResult();
4545
final String result = sqlArtifact.getContent();
4646

4747
assertThat( result ).contains( "TBLPROPERTIES ('x-samm-aspect-model-urn'='" );

documentation/developer-guide/modules/tooling-guide/examples/GenerateSql.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void generate() {
6363
.mappingStrategy( SqlGenerationConfig.MappingStrategy.DENORMALIZED )
6464
.dialectSpecificConfig( databricksSqlGenerationConfig )
6565
.build();
66-
final String result = AspectModelSqlGenerator.INSTANCE.apply( aspectModel.aspect(), sqlGenerationConfig ).getContent();
66+
final String result = new AspectModelSqlGenerator( aspectModel.aspect(), sqlGenerationConfig ).getContent();
6767
// end::generate[]
6868
}
6969
}

tools/esmf-aspect-model-maven-plugin/src/main/java/org/eclipse/esmf/aspectmodel/GenerateSql.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,22 @@ public class GenerateSql extends AspectModelMojo {
4646
private boolean includeColumnComments;
4747

4848
@Parameter( defaultValue = DatabricksSqlGenerationConfig.DEFAULT_TABLE_COMMAND_PREFIX )
49-
private String tableCommandPrefix = DatabricksSqlGenerationConfig.DEFAULT_TABLE_COMMAND_PREFIX;
49+
private final String tableCommandPrefix = DatabricksSqlGenerationConfig.DEFAULT_TABLE_COMMAND_PREFIX;
5050

5151
@Parameter( defaultValue = "" + DatabricksSqlGenerationConfig.DECIMAL_DEFAULT_PRECISION )
52-
private int decimalPrecision = DatabricksSqlGenerationConfig.DECIMAL_DEFAULT_PRECISION;
52+
private final int decimalPrecision = DatabricksSqlGenerationConfig.DECIMAL_DEFAULT_PRECISION;
5353

5454
@Parameter( defaultValue = "en" )
55-
private String language = DatabricksSqlGenerationConfig.DEFAULT_COMMENT_LANGUAGE.getLanguage();
55+
private final String language = DatabricksSqlGenerationConfig.DEFAULT_COMMENT_LANGUAGE.getLanguage();
5656

5757
@Parameter( defaultValue = "databricks" )
58-
private String dialect = SqlGenerationConfig.Dialect.DATABRICKS.toString().toLowerCase();
58+
private final String dialect = SqlGenerationConfig.Dialect.DATABRICKS.toString().toLowerCase();
5959

6060
@Parameter( defaultValue = "denormalized" )
61-
private String strategy = SqlGenerationConfig.MappingStrategy.DENORMALIZED.toString().toLowerCase();
61+
private final String strategy = SqlGenerationConfig.MappingStrategy.DENORMALIZED.toString().toLowerCase();
6262

6363
@Parameter( property = "column" )
64-
private List<String> customColumns = List.of();
64+
private final List<String> customColumns = List.of();
6565

6666
@Override
6767
public void executeGeneration() throws MojoExecutionException {
@@ -84,7 +84,7 @@ public void executeGeneration() throws MojoExecutionException {
8484
.build();
8585
final SqlGenerationConfig sqlConfig = new SqlGenerationConfig( SqlGenerationConfig.Dialect.valueOf( dialect.toUpperCase() ),
8686
SqlGenerationConfig.MappingStrategy.valueOf( strategy.toUpperCase() ), generatorConfig );
87-
final SqlArtifact result = AspectModelSqlGenerator.INSTANCE.apply( aspect, sqlConfig );
87+
final SqlArtifact result = new AspectModelSqlGenerator( aspect, sqlConfig ).singleResult();
8888

8989
try ( final OutputStream out = getOutputStreamForFile( aspect.getName() + ".sql", outputDirectory ) ) {
9090
out.write( result.getContent().getBytes() );

tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/to/AspectToSqlCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ public void run() {
132132
.customColumns( customColumns )
133133
.build();
134134
final SqlGenerationConfig sqlConfig = new SqlGenerationConfig( dialect, strategy, generatorConfig );
135-
final SqlArtifact result = AspectModelSqlGenerator.INSTANCE.apply( aspect, sqlConfig );
135+
final SqlArtifact result = new AspectModelSqlGenerator( aspect, sqlConfig ).singleResult();
136136

137137
try ( final OutputStream out = getStreamForFile( outputFilePath ) ) {
138138
out.write( result.getContent().getBytes() );
139-
} catch ( final IOException e ) {
140-
throw new CommandException( e );
139+
} catch ( final IOException exception ) {
140+
throw new CommandException( exception );
141141
}
142142
}
143143
}

0 commit comments

Comments
 (0)