From 898873397ffa9d5a0d0c5ccd08e33e0c4ffbaf5c Mon Sep 17 00:00:00 2001 From: Marvin Froeder Date: Thu, 28 Dec 2023 07:56:03 +1300 Subject: [PATCH] Change sql-codegen exporter settings from a push model to pull model --- .../maven/AbstractMetaDataExportMojo.java | 394 ++++++++---- .../sql/codegen/MetaDataExporter.java | 568 +++++------------- .../sql/codegen/MetadataExporterConfig.java | 273 +++++++++ .../codegen/MetadataExporterConfigImpl.java | 419 +++++++++++++ .../sql/codegen/ant/AntMetaDataExporter.java | 234 +++----- .../sql/codegen/support/CustomType.java | 4 + .../sql/codegen/support/TypeMapping.java | 11 + .../querydsl/sql/codegen/CustomTypesTest.java | 21 +- .../java/com/querydsl/sql/codegen/Export.java | 10 +- .../querydsl/sql/codegen/ExportBaseTest.java | 15 +- .../sql/codegen/ExportH2TwoSchemasTest.java | 12 +- .../sql/codegen/MetaDataExporterAllTest.java | 46 +- .../sql/codegen/MetaDataExporterTest.java | 268 +++++---- .../sql/codegen/MetaDataSerializerTest.java | 32 +- 14 files changed, 1447 insertions(+), 860 deletions(-) create mode 100644 querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetadataExporterConfig.java create mode 100644 querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetadataExporterConfigImpl.java diff --git a/querydsl-maven-plugin/src/main/java/com/querydsl/maven/AbstractMetaDataExportMojo.java b/querydsl-maven-plugin/src/main/java/com/querydsl/maven/AbstractMetaDataExportMojo.java index 6c85f013f3..cc328dfc9a 100644 --- a/querydsl-maven-plugin/src/main/java/com/querydsl/maven/AbstractMetaDataExportMojo.java +++ b/querydsl-maven-plugin/src/main/java/com/querydsl/maven/AbstractMetaDataExportMojo.java @@ -14,23 +14,27 @@ package com.querydsl.maven; import com.querydsl.codegen.BeanSerializer; -import com.querydsl.codegen.utils.model.SimpleType; +import com.querydsl.codegen.Property; +import com.querydsl.codegen.Serializer; import com.querydsl.core.util.StringUtils; -import com.querydsl.sql.Configuration; -import com.querydsl.sql.SQLTemplates; -import com.querydsl.sql.codegen.DefaultNamingStrategy; import com.querydsl.sql.codegen.MetaDataExporter; +import com.querydsl.sql.codegen.MetadataExporterConfig; import com.querydsl.sql.codegen.NamingStrategy; +import com.querydsl.sql.codegen.support.CustomType; import com.querydsl.sql.codegen.support.NumericMapping; import com.querydsl.sql.codegen.support.RenameMapping; import com.querydsl.sql.codegen.support.TypeMapping; -import com.querydsl.sql.types.Type; import java.io.File; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import java.util.Arrays; import java.util.Comparator; +import java.util.List; import java.util.regex.Pattern; +import java.util.stream.Collectors; import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -45,7 +49,8 @@ * * @author tiwe */ -public abstract class AbstractMetaDataExportMojo extends AbstractMojo { +public abstract class AbstractMetaDataExportMojo extends AbstractMojo + implements MetadataExporterConfig { /** maven project */ @Parameter(defaultValue = "${project}", required = true, readonly = true) @@ -219,10 +224,6 @@ public abstract class AbstractMetaDataExportMojo extends AbstractMojo { /** override default column order (default: alphabetical) */ @Parameter private String columnComparatorClass; - /** switch to enable spatial type support */ - @Parameter(defaultValue = "false") - private boolean spatial; - /** * Comma-separated list of table types to export (allowable values will depend on JDBC driver). * Allows for arbitrary set of types to be exported, e.g.: "TABLE, MATERIALIZED VIEW". The @@ -267,13 +268,6 @@ public void execute() throws MojoExecutionException, MojoFailureException { } try { - Configuration configuration = new Configuration(SQLTemplates.DEFAULT); - NamingStrategy namingStrategy; - if (namingStrategyClass != null) { - namingStrategy = (NamingStrategy) Class.forName(namingStrategyClass).newInstance(); - } else { - namingStrategy = new DefaultNamingStrategy(); - } // defaults for Scala if (createScalaSources) { @@ -285,110 +279,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { } } - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setNamePrefix(emptyIfSetToBlank(namePrefix)); - exporter.setNameSuffix(StringUtils.nullToEmpty(nameSuffix)); - exporter.setBeanPrefix(StringUtils.nullToEmpty(beanPrefix)); - exporter.setBeanSuffix(StringUtils.nullToEmpty(beanSuffix)); - if (beansTargetFolder != null) { - exporter.setBeansTargetFolder(new File(beansTargetFolder)); - } - exporter.setCreateScalaSources(createScalaSources); - exporter.setPackageName(packageName); - exporter.setBeanPackageName(beanPackageName); - exporter.setInnerClassesForKeys(innerClassesForKeys); - exporter.setTargetFolder(new File(targetFolder)); - exporter.setNamingStrategy(namingStrategy); - exporter.setCatalogPattern(catalogPattern); - exporter.setSchemaPattern(processBlankValues(schemaPattern)); - exporter.setTableNamePattern(tableNamePattern); - exporter.setColumnAnnotations(columnAnnotations); - exporter.setValidationAnnotations(validationAnnotations); - exporter.setSchemaToPackage(schemaToPackage); - exporter.setLowerCase(lowerCase); - exporter.setExportTables(exportTables); - exporter.setExportViews(exportViews); - exporter.setExportAll(exportAll); - exporter.setTableTypesToExport(tableTypesToExport); - exporter.setExportPrimaryKeys(exportPrimaryKeys); - exporter.setExportForeignKeys(exportForeignKeys); - exporter.setExportDirectForeignKeys(exportDirectForeignKeys); - exporter.setExportInverseForeignKeys(exportInverseForeignKeys); - exporter.setSpatial(spatial); - exporter.setGeneratedAnnotationClass(generatedAnnotationClass); - - if (imports != null && imports.length > 0) { - exporter.setImports(imports); - } - - if (serializerClass != null) { - try { - exporter.setSerializerClass((Class) Class.forName(serializerClass)); - } catch (ClassNotFoundException e) { - getLog().error(e); - throw new MojoExecutionException(e.getMessage(), e); - } - } - if (exportBeans) { - if (beanSerializerClass != null) { - exporter.setBeanSerializerClass((Class) Class.forName(beanSerializerClass)); - } else { - BeanSerializer serializer = new BeanSerializer(); - if (beanInterfaces != null) { - for (String iface : beanInterfaces) { - int sepIndex = iface.lastIndexOf('.'); - if (sepIndex < 0) { - serializer.addInterface(new SimpleType(iface)); - } else { - String packageName = iface.substring(0, sepIndex); - String simpleName = iface.substring(sepIndex + 1); - serializer.addInterface(new SimpleType(iface, packageName, simpleName)); - } - } - } - serializer.setAddFullConstructor(beanAddFullConstructor); - serializer.setAddToString(beanAddToString); - serializer.setPrintSupertype(beanPrintSupertype); - exporter.setBeanSerializer(serializer); - } - } - String sourceEncoding = (String) project.getProperties().get("project.build.sourceEncoding"); - if (sourceEncoding != null) { - exporter.setSourceEncoding(sourceEncoding); - } - - if (customTypes != null) { - for (String cl : customTypes) { - configuration.register((Type) Class.forName(cl).newInstance()); - } - } - if (typeMappings != null) { - for (TypeMapping mapping : typeMappings) { - mapping.apply(configuration); - } - } - if (numericMappings != null) { - for (NumericMapping mapping : numericMappings) { - mapping.apply(configuration); - } - } - if (renameMappings != null) { - for (RenameMapping mapping : renameMappings) { - mapping.apply(configuration); - } - } - - if (columnComparatorClass != null) { - try { - exporter.setColumnComparatorClass( - (Class) Class.forName(this.columnComparatorClass).asSubclass(Comparator.class)); - } catch (ClassNotFoundException e) { - getLog().error(e); - throw new MojoExecutionException(e.getMessage(), e); - } - } - - exporter.setConfiguration(configuration); + MetaDataExporter exporter = new MetaDataExporter(this); Class.forName(jdbcDriver); String user; @@ -415,10 +306,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { try (Connection conn = DriverManager.getConnection(jdbcUrl, user, password)) { exporter.export(conn.getMetaData()); } - } catch (ClassNotFoundException - | IllegalAccessException - | InstantiationException - | SQLException e) { + } catch (ClassNotFoundException | SQLException e) { throw new MojoExecutionException(e.getMessage(), e); } } @@ -584,4 +472,260 @@ private static String processBlankValues(String value) { private static final Pattern BLANK_VALUE_PATTERN = Pattern.compile("(^|,)BLANK(,|$)", Pattern.CASE_INSENSITIVE); private static final String BLANK_VALUE_REPLACEMENT = "$1$2"; + + @Override + public String getNamePrefix() { + return emptyIfSetToBlank(namePrefix); + } + + @Override + public String getNameSuffix() { + return StringUtils.nullToEmpty(nameSuffix); + } + + @Override + public String getBeanPrefix() { + return StringUtils.nullToEmpty(beanPrefix); + } + + @Override + public String getBeanSuffix() { + return StringUtils.nullToEmpty(beanSuffix); + } + + @Override + public File getBeansTargetFolder() { + if (beansTargetFolder != null) { + return new File(beansTargetFolder); + } + return null; + } + + @Override + public File getTargetFolder() { + return new File(targetFolder); + } + + @Override + public boolean isCreateScalaSources() { + return createScalaSources; + } + + @Override + public String getPackageName() { + return packageName; + } + + @Override + public String getBeanPackageName() { + return beanPackageName; + } + + @Override + public boolean isInnerClassesForKeys() { + return innerClassesForKeys; + } + + @Override + public Class getNamingStrategyClass() { + if (namingStrategyClass == null) { + return null; + } + try { + return (Class) Class.forName(namingStrategyClass); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + public String getSchemaPattern() { + return processBlankValues(schemaPattern); + } + + @Override + public String getCatalogPattern() { + return catalogPattern; + } + + @Override + public String getTableNamePattern() { + return tableNamePattern; + } + + @Override + public boolean isColumnAnnotations() { + return columnAnnotations; + } + + @Override + public boolean isValidationAnnotations() { + return validationAnnotations; + } + + @Override + public boolean isSchemaToPackage() { + return schemaToPackage; + } + + @Override + public boolean isLowerCase() { + return lowerCase; + } + + @Override + public boolean isExportTables() { + return exportTables; + } + + @Override + public boolean isExportViews() { + return exportViews; + } + + @Override + public boolean isExportAll() { + return exportAll; + } + + @Override + public boolean isExportPrimaryKeys() { + return exportPrimaryKeys; + } + + @Override + public boolean isExportForeignKeys() { + return exportForeignKeys; + } + + @Override + public boolean isExportDirectForeignKeys() { + return exportDirectForeignKeys; + } + + @Override + public boolean isExportInverseForeignKeys() { + return exportInverseForeignKeys; + } + + @Override + public Charset getSourceEncoding() { + String sourceEncoding = (String) project.getProperties().get("project.build.sourceEncoding"); + if (sourceEncoding != null) { + return Charset.forName(sourceEncoding); + } + + return StandardCharsets.UTF_8; + } + + @Override + public String getTableTypesToExport() { + return tableTypesToExport; + } + + @Override + public List getImports() { + if (imports == null) { + return null; + } + return Arrays.asList(imports); + } + + @Override + public String getGeneratedAnnotationClass() { + return generatedAnnotationClass; + } + + @Override + public Class getBeanSerializerClass() { + if (exportBeans && beanSerializerClass != null) { + try { + return (Class) Class.forName(beanSerializerClass); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + return null; + } + + @Override + public boolean isExportBeans() { + return exportBeans; + } + + @Override + public String[] getBeanInterfaces() { + return beanInterfaces; + } + + @Override + public boolean isBeanAddToString() { + return beanAddToString; + } + + @Override + public boolean isBeanAddFullConstructor() { + return beanAddFullConstructor; + } + + @Override + public boolean isBeanPrintSupertype() { + return beanPrintSupertype; + } + + @Override + public List getCustomTypes() { + if (customTypes == null) { + return null; + } + return Arrays.stream(customTypes).map(CustomType::new).collect(Collectors.toList()); + } + + @Override + public List getTypeMappings() { + if (typeMappings == null) { + return null; + } + return Arrays.stream(typeMappings).collect(Collectors.toList()); + } + + @Override + public List getNumericMappings() { + if (numericMappings == null) { + return null; + } + return Arrays.stream(numericMappings).collect(Collectors.toList()); + } + + @Override + public List getRenameMappings() { + if (renameMappings == null) { + return null; + } + return Arrays.stream(renameMappings).collect(Collectors.toList()); + } + + @Override + public Class> getColumnComparatorClass() { + if (columnComparatorClass == null) { + return null; + } + try { + return (Class>) Class.forName(columnComparatorClass); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + public Class getSerializerClass() { + if (serializerClass == null) { + return null; + } + try { + return (Class) Class.forName(serializerClass); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } } diff --git a/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetaDataExporter.java b/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetaDataExporter.java index cde3423897..61ae876216 100644 --- a/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetaDataExporter.java +++ b/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetaDataExporter.java @@ -13,6 +13,7 @@ */ package com.querydsl.sql.codegen; +import com.querydsl.codegen.BeanSerializer; import com.querydsl.codegen.CodegenModule; import com.querydsl.codegen.EntityType; import com.querydsl.codegen.GeneratedAnnotationResolver; @@ -34,15 +35,18 @@ import com.querydsl.sql.SQLTemplates; import com.querydsl.sql.SQLTemplatesRegistry; import com.querydsl.sql.SchemaAndTable; +import com.querydsl.sql.codegen.support.CustomType; import com.querydsl.sql.codegen.support.ForeignKeyData; import com.querydsl.sql.codegen.support.InverseForeignKeyData; import com.querydsl.sql.codegen.support.NotNullImpl; +import com.querydsl.sql.codegen.support.NumericMapping; import com.querydsl.sql.codegen.support.PrimaryKeyData; +import com.querydsl.sql.codegen.support.RenameMapping; import com.querydsl.sql.codegen.support.SizeImpl; +import com.querydsl.sql.codegen.support.TypeMapping; import java.io.File; import java.io.IOException; import java.io.StringWriter; -import java.nio.charset.Charset; import java.nio.file.Files; import java.sql.DatabaseMetaData; import java.sql.ResultSet; @@ -86,24 +90,12 @@ public class MetaDataExporter { private final Set classes = new HashSet(); - private File targetFolder; - - private File beansTargetFolder; - @Nullable private String beanPackageName; - @Nullable private String schemaPattern, tableNamePattern, catalogPattern; - - @Nullable private Serializer beanSerializer; - - private boolean createScalaSources = false; - private final Map entityToWrapped = new HashMap(); private Serializer serializer; - private TypeMappings typeMappings; - private QueryTypeFactory queryTypeFactory; private NamingStrategy namingStrategy; @@ -112,35 +104,15 @@ public class MetaDataExporter { private KeyDataFactory keyDataFactory; - private boolean columnAnnotations = false; - - private boolean validationAnnotations = false; - - private boolean schemaToPackage = false; - - private String sourceEncoding = "UTF-8"; - - private boolean lowerCase = false; - - private boolean exportTables = true; - - private boolean exportViews = true; - - private boolean exportAll = false; - - private boolean exportPrimaryKeys = true; - - private boolean exportForeignKeys = true; + private final MetadataExporterConfig config; - private boolean exportDirectForeignKeys = true; + private Serializer beanSerializer; - private boolean exportInverseForeignKeys = true; - - private boolean spatial = false; - - @Nullable private String tableTypesToExport; + private TypeMappings typeMappings; - public MetaDataExporter() {} + public MetaDataExporter(MetadataExporterConfig config) { + this.config = config; + } protected EntityType createEntityType(SchemaAndTable schemaAndTable, final String className) { EntityType classModel; @@ -190,7 +162,7 @@ protected EntityType createEntityType(SchemaAndTable schemaAndTable, final Strin private String normalizePackage(String packageName, SchemaAndTable schemaAndTable) { String rval = packageName; - if (schemaToPackage) { + if (config.isSchemaToPackage()) { rval = namingStrategy.getPackage(rval, schemaAndTable); } return rval; @@ -209,12 +181,26 @@ protected Property createProperty( * @throws SQLException */ public void export(DatabaseMetaData md) throws SQLException { - if (beanPackageName == null) { - beanPackageName = module.getPackageName(); + configuration = module.get(Configuration.class); + configureModule(); + + if (config.getNamingStrategyClass() != null) { + try { + namingStrategy = config.getNamingStrategyClass().newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } else { + namingStrategy = new DefaultNamingStrategy(); } - if (beansTargetFolder == null) { - beansTargetFolder = targetFolder; + module.bind(NamingStrategy.class, namingStrategy); + + if (config.getBeanPackageName() == null) { + beanPackageName = module.getPackageName(); + } else { + beanPackageName = config.getBeanPackageName(); } + module.bind(SQLCodegenModule.BEAN_PACKAGE_NAME, beanPackageName); module.loadExtensions(); @@ -224,7 +210,6 @@ public void export(DatabaseMetaData md) throws SQLException { serializer = module.get(Serializer.class); beanSerializer = module.get(Serializer.class, SQLCodegenModule.BEAN_SERIALIZER); namingStrategy = module.get(NamingStrategy.class); - configuration = module.get(Configuration.class); SQLTemplates templates = sqlTemplatesRegistry.getTemplates(md); if (templates != null) { @@ -240,7 +225,7 @@ public void export(DatabaseMetaData md) throws SQLException { module.getPackageName(), module.getPrefix(), module.getSuffix(), - schemaToPackage); + config.isSchemaToPackage()); } else { keyDataFactory = new KeyDataFactory( @@ -248,31 +233,31 @@ public void export(DatabaseMetaData md) throws SQLException { beanPackageName, module.getBeanPrefix(), module.getBeanSuffix(), - schemaToPackage); + config.isSchemaToPackage()); } String[] typesArray = null; - if (tableTypesToExport != null && !tableTypesToExport.isEmpty()) { + if (config.getTableTypesToExport() != null && !config.getTableTypesToExport().isEmpty()) { List types = new ArrayList(); - for (String tableType : tableTypesToExport.split(",")) { + for (String tableType : config.getTableTypesToExport().split(",")) { types.add(tableType.trim()); } typesArray = types.toArray(new String[0]); - } else if (!exportAll) { + } else if (!config.isExportAll()) { List types = new ArrayList(2); - if (exportTables) { + if (config.isExportTables()) { types.add("TABLE"); } - if (exportViews) { + if (config.isExportViews()) { types.add("VIEW"); } typesArray = types.toArray(new String[0]); } - List catalogs = patternAsList(catalogPattern); - List schemas = patternAsList(schemaPattern); - List tables = patternAsList(tableNamePattern); + List catalogs = patternAsList(config.getCatalogPattern()); + List schemas = patternAsList(config.getSchemaPattern()); + List tables = patternAsList(config.getTableNamePattern()); for (String catalog : catalogs) { catalog = trimIfNonNull(catalog); @@ -286,6 +271,101 @@ public void export(DatabaseMetaData md) throws SQLException { } } + private void configureModule() { + if (config.getNamePrefix() != null) module.bind(CodegenModule.PREFIX, config.getNamePrefix()); + + if (config.getNameSuffix() != null) { + module.bind(CodegenModule.SUFFIX, config.getNameSuffix()); + } + + if (config.getBeanPrefix() != null) { + module.bind(SQLCodegenModule.BEAN_PREFIX, config.getBeanPrefix()); + } + + if (config.getBeanSuffix() != null) { + module.bind(SQLCodegenModule.BEAN_SUFFIX, config.getBeanSuffix()); + } + + module.bind(SQLCodegenModule.PACKAGE_NAME, config.getPackageName()); + + module.bind(SQLCodegenModule.INNER_CLASSES_FOR_KEYS, config.isInnerClassesForKeys()); + + module.bind(NamingStrategy.class, namingStrategy); + + module.bind(SQLCodegenModule.SCHEMA_TO_PACKAGE, config.isSchemaToPackage()); + + if (config.getImports() != null && !config.getImports().isEmpty()) + module.bind(CodegenModule.IMPORTS, new HashSet(config.getImports())); + + module.bindInstance( + CodegenModule.GENERATED_ANNOTATION_CLASS, + GeneratedAnnotationResolver.resolve(config.getGeneratedAnnotationClass())); + + if (config.isExportBeans()) { + BeanSerializer serializer; + if (config.getBeanSerializerClass() == null) { + serializer = new BeanSerializer(); + } else { + try { + serializer = (BeanSerializer) config.getBeanSerializerClass().newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + if (config.getBeanInterfaces() != null) { + for (String iface : config.getBeanInterfaces()) { + int sepIndex = iface.lastIndexOf('.'); + if (sepIndex < 0) { + serializer.addInterface(new SimpleType(iface)); + } else { + String packageName = iface.substring(0, sepIndex); + String simpleName = iface.substring(sepIndex + 1); + serializer.addInterface(new SimpleType(iface, packageName, simpleName)); + } + } + } + serializer.setAddFullConstructor(config.isBeanAddFullConstructor()); + serializer.setAddToString(config.isBeanAddToString()); + serializer.setPrintSupertype(config.isBeanPrintSupertype()); + module.bind(SQLCodegenModule.BEAN_SERIALIZER, serializer); + } + + if (config.getCustomTypes() != null) { + for (CustomType cl : config.getCustomTypes()) { + try { + configuration.register( + (com.querydsl.sql.types.Type) Class.forName(cl.getClassName()).newInstance()); + } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + } + if (config.getTypeMappings() != null) { + for (TypeMapping mapping : config.getTypeMappings()) { + mapping.apply(configuration); + } + } + if (config.getNumericMappings() != null) { + for (NumericMapping mapping : config.getNumericMappings()) { + mapping.apply(configuration); + } + } + if (config.getRenameMappings() != null) { + for (RenameMapping mapping : config.getRenameMappings()) { + mapping.apply(configuration); + } + } + + if (config.getColumnComparatorClass() != null) + module.bind( + SQLCodegenModule.COLUMN_COMPARATOR, + config.getColumnComparatorClass().asSubclass(Comparator.class)); + + if (config.getSerializerClass() != null) { + module.bind(Serializer.class, config.getSerializerClass()); + } + } + private String trimIfNonNull(String input) { return input != null ? input.trim() : null; } @@ -365,10 +445,10 @@ private void handleColumn(EntityType classModel, String tableName, ResultSet col } property.getData().put("COLUMN", column); - if (columnAnnotations) { + if (config.isColumnAnnotations()) { property.addAnnotation(new ColumnImpl(normalizedColumnName)); } - if (validationAnnotations) { + if (config.isValidationAnnotations()) { if (nullable == DatabaseMetaData.columnNoNulls && columnDefaultValue == null) { property.addAnnotation(new NotNullImpl()); } @@ -398,7 +478,7 @@ private void handleTable(DatabaseMetaData md, ResultSet tables) throws SQLExcept String className = namingStrategy.getClassName(schemaAndTable); EntityType classModel = createEntityType(schemaAndTable, className); - if (exportPrimaryKeys) { + if (config.isExportPrimaryKeys()) { // collect primary keys Map primaryKeyData = keyDataFactory.getPrimaryKeys(md, catalog, schema, tableName); @@ -407,8 +487,8 @@ private void handleTable(DatabaseMetaData md, ResultSet tables) throws SQLExcept } } - if (exportForeignKeys) { - if (exportDirectForeignKeys) { + if (config.isExportForeignKeys()) { + if (config.isExportDirectForeignKeys()) { // collect foreign keys Map foreignKeyData = keyDataFactory.getImportedKeys(md, catalog, schema, tableName); @@ -426,7 +506,7 @@ private void handleTable(DatabaseMetaData md, ResultSet tables) throws SQLExcept } } - if (exportInverseForeignKeys) { + if (config.isExportInverseForeignKeys()) { // collect inverse foreign keys Map inverseForeignKeyData = keyDataFactory.getExportedKeys(md, catalog, schema, tableName); @@ -450,7 +530,7 @@ private void handleTable(DatabaseMetaData md, ResultSet tables) throws SQLExcept } private String normalize(String str) { - if (lowerCase && str != null) { + if (config.isLowerCase() && str != null) { return str.toLowerCase(); } else { return str; @@ -459,19 +539,26 @@ private String normalize(String str) { private void serialize(EntityType type, SchemaAndTable schemaAndTable) { try { - String fileSuffix = createScalaSources ? ".scala" : ".java"; + String fileSuffix = config.isCreateScalaSources() ? ".scala" : ".java"; if (beanSerializer != null) { String packageName = normalizePackage(beanPackageName, schemaAndTable); String path = packageName.replace('.', '/') + "/" + type.getSimpleName() + fileSuffix; - write(beanSerializer, new File(beansTargetFolder, path), type); + write( + beanSerializer, + new File( + config.getBeansTargetFolder() != null + ? config.getBeansTargetFolder() + : config.getTargetFolder(), + path), + type); String otherPath = entityToWrapped.get(type).getFullName().replace('.', '/') + fileSuffix; - write(serializer, new File(targetFolder, otherPath), type); + write(serializer, new File(config.getTargetFolder(), otherPath), type); } else { String packageName = normalizePackage(module.getPackageName(), schemaAndTable); String path = packageName.replace('.', '/') + "/" + type.getSimpleName() + fileSuffix; - write(serializer, new File(targetFolder, path), type); + write(serializer, new File(config.getTargetFolder(), path), type); } } catch (IOException e) { @@ -487,15 +574,14 @@ private void write(Serializer serializer, File targetFile, EntityType type) thro + ", please check your configuration"); } StringWriter w = new StringWriter(); - CodeWriter writer = createScalaSources ? new ScalaWriter(w) : new JavaWriter(w); + CodeWriter writer = config.isCreateScalaSources() ? new ScalaWriter(w) : new JavaWriter(w); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, writer); // conditional creation boolean generate = true; - byte[] bytes = w.toString().getBytes(sourceEncoding); + byte[] bytes = w.toString().getBytes(config.getSourceEncoding()); if (targetFile.exists() && targetFile.length() == bytes.length) { - String str = - new String(Files.readAllBytes(targetFile.toPath()), Charset.forName(sourceEncoding)); + String str = new String(Files.readAllBytes(targetFile.toPath()), config.getSourceEncoding()); if (str.equals(w.toString())) { generate = false; } @@ -508,349 +594,7 @@ private void write(Serializer serializer, File targetFile, EntityType type) thro } } - /** - * Set the schema pattern filter to be used - * - * @param schemaPattern a schema name pattern; must match the schema name as it is stored in the - * database; "" retrieves those without a schema; {@code null} means that the schema name - * should not be used to narrow the search (default: null) - */ - public void setSchemaPattern(@Nullable String schemaPattern) { - this.schemaPattern = schemaPattern; - } - - /** - * a catalog name; must match the catalog name as it is stored in the database; "" retrieves those - * without a catalog; null means that the catalog name should not be used to narrow - * the search - */ - public void setCatalogPattern(@Nullable String catalogPattern) { - this.catalogPattern = catalogPattern; - } - - /** - * Set the table name pattern filter to be used - * - * @param tableNamePattern a table name pattern; must match the table name as it is stored in the - * database (default: null) - */ - public void setTableNamePattern(@Nullable String tableNamePattern) { - this.tableNamePattern = tableNamePattern; - } - - /** - * Override the configuration - * - * @param configuration override configuration for custom type mappings etc - */ public void setConfiguration(Configuration configuration) { module.bind(Configuration.class, configuration); } - - /** - * Set true to create Scala sources instead of Java sources - * - * @param createScalaSources whether to create Scala sources (default: false) - */ - public void setCreateScalaSources(boolean createScalaSources) { - this.createScalaSources = createScalaSources; - } - - /** - * Set the target folder - * - * @param targetFolder target source folder to create the sources into (e.g. - * target/generated-sources/java) - */ - public void setTargetFolder(File targetFolder) { - this.targetFolder = targetFolder; - } - - /** - * Set the target folder for beans - * - *

defaults to the targetFolder value - * - * @param targetFolder target source folder to create the bean sources into - */ - public void setBeansTargetFolder(File targetFolder) { - this.beansTargetFolder = targetFolder; - } - - /** - * Set the package name - * - * @param packageName package name for sources - */ - public void setPackageName(String packageName) { - module.bind(SQLCodegenModule.PACKAGE_NAME, packageName); - } - - /** - * Override the bean package name (default: packageName) - * - * @param beanPackageName package name for bean sources - */ - public void setBeanPackageName(@Nullable String beanPackageName) { - this.beanPackageName = beanPackageName; - } - - /** - * Override the name prefix for the classes (default: Q) - * - * @param namePrefix name prefix for querydsl-types (default: Q) - */ - public void setNamePrefix(String namePrefix) { - module.bind(CodegenModule.PREFIX, namePrefix); - } - - /** - * Override the name suffix for the classes (default: "") - * - * @param nameSuffix name suffix for querydsl-types (default: "") - */ - public void setNameSuffix(String nameSuffix) { - module.bind(CodegenModule.SUFFIX, nameSuffix); - } - - /** - * Override the bean prefix for the classes (default: "") - * - * @param beanPrefix bean prefix for bean-types (default: "") - */ - public void setBeanPrefix(String beanPrefix) { - module.bind(SQLCodegenModule.BEAN_PREFIX, beanPrefix); - } - - /** - * Override the bean suffix for the classes (default: "") - * - * @param beanSuffix bean suffix for bean-types (default: "") - */ - public void setBeanSuffix(String beanSuffix) { - module.bind(SQLCodegenModule.BEAN_SUFFIX, beanSuffix); - } - - /** - * Override the NamingStrategy (default: new DefaultNamingStrategy()) - * - * @param namingStrategy naming strategy to override (default: new DefaultNamingStrategy()) - */ - public void setNamingStrategy(NamingStrategy namingStrategy) { - module.bind(NamingStrategy.class, namingStrategy); - } - - /** - * Set the Bean serializer to create bean types as well - * - * @param beanSerializer serializer for JavaBeans (default: null) - */ - public void setBeanSerializer(@Nullable Serializer beanSerializer) { - module.bind(SQLCodegenModule.BEAN_SERIALIZER, beanSerializer); - } - - /** - * Set the Bean serializer class to create bean types as well - * - * @param beanSerializerClass serializer for JavaBeans (default: null) - */ - public void setBeanSerializerClass(Class beanSerializerClass) { - module.bind(SQLCodegenModule.BEAN_SERIALIZER, beanSerializerClass); - } - - /** - * Set whether inner classes should be created for keys - * - * @param innerClassesForKeys - */ - public void setInnerClassesForKeys(boolean innerClassesForKeys) { - module.bind(SQLCodegenModule.INNER_CLASSES_FOR_KEYS, innerClassesForKeys); - } - - /** - * Set the column comparator class - * - * @param columnComparatorClass - */ - public void setColumnComparatorClass( - Class> columnComparatorClass) { - module.bind(SQLCodegenModule.COLUMN_COMPARATOR, columnComparatorClass); - } - - /** - * Set the serializer class - * - * @param serializerClass - */ - public void setSerializerClass(Class serializerClass) { - module.bind(Serializer.class, serializerClass); - } - - /** - * Set the type mappings to use - * - * @param typeMappings - */ - public void setTypeMappings(TypeMappings typeMappings) { - module.bind(TypeMappings.class, typeMappings); - } - - /** - * Set whether column annotations should be created - * - * @param columnAnnotations - */ - public void setColumnAnnotations(boolean columnAnnotations) { - this.columnAnnotations = columnAnnotations; - } - - /** - * Set whether validation annotations should be created - * - * @param validationAnnotations - */ - public void setValidationAnnotations(boolean validationAnnotations) { - this.validationAnnotations = validationAnnotations; - } - - /** - * Set the source encoding - * - * @param sourceEncoding - */ - public void setSourceEncoding(String sourceEncoding) { - this.sourceEncoding = sourceEncoding; - } - - /** - * Set whether schema names should be appended to the package name. - * - *

!!! Important !!! {@link NamingStrategy#getPackage(String, SchemaAndTable)} will - * be invoked only if schemaToPackage is set to true. - * - * @deprecated This flag will not be necessary in the future because the generated package name - * can be controlled in method {@link NamingStrategy#getPackage(String, SchemaAndTable)}. - * @param schemaToPackage - */ - @Deprecated - public void setSchemaToPackage(boolean schemaToPackage) { - this.schemaToPackage = schemaToPackage; - module.bind(SQLCodegenModule.SCHEMA_TO_PACKAGE, schemaToPackage); - } - - /** - * Set whether names should be normalized to lowercase - * - * @param lowerCase - */ - public void setLowerCase(boolean lowerCase) { - this.lowerCase = lowerCase; - } - - /** - * Set whether tables should be exported - * - * @param exportTables - */ - public void setExportTables(boolean exportTables) { - this.exportTables = exportTables; - } - - /** - * Set whether views should be exported - * - * @param exportViews - */ - public void setExportViews(boolean exportViews) { - this.exportViews = exportViews; - } - - /** - * Set whether all table types should be exported - * - * @param exportAll - */ - public void setExportAll(boolean exportAll) { - this.exportAll = exportAll; - } - - /** - * Set whether primary keys should be exported - * - * @param exportPrimaryKeys - */ - public void setExportPrimaryKeys(boolean exportPrimaryKeys) { - this.exportPrimaryKeys = exportPrimaryKeys; - } - - /** - * Set whether foreign keys should be exported - * - * @param exportForeignKeys - */ - public void setExportForeignKeys(boolean exportForeignKeys) { - this.exportForeignKeys = exportForeignKeys; - } - - /** - * Set whether direct foreign keys should be exported - * - * @param exportDirectForeignKeys - */ - public void setExportDirectForeignKeys(boolean exportDirectForeignKeys) { - this.exportDirectForeignKeys = exportDirectForeignKeys; - } - - /** - * Set whether inverse foreign keys should be exported - * - * @param exportInverseForeignKeys - */ - public void setExportInverseForeignKeys(boolean exportInverseForeignKeys) { - this.exportInverseForeignKeys = exportInverseForeignKeys; - } - - /** - * Set the java imports - * - * @param imports java imports array - */ - public void setImports(String[] imports) { - module.bind(CodegenModule.IMPORTS, new HashSet(Arrays.asList(imports))); - } - - /** - * Set whether spatial type support should be used - * - * @param spatial - */ - public void setSpatial(boolean spatial) { - this.spatial = spatial; - } - - /** - * Set the table types to export as a comma separated string - * - * @param tableTypesToExport - */ - public void setTableTypesToExport(String tableTypesToExport) { - this.tableTypesToExport = tableTypesToExport; - } - - /** - * Set the fully qualified class name of the "generated" annotation added ot the generated sources - * - * @param generatedAnnotationClass the fully qualified class name of the Single-Element - * Annotation (with {@code String} element) to be used on the generated sources, or - * {@code null} (defaulting to {@code javax.annotation.Generated} or {@code - * javax.annotation.processing.Generated} depending on the java version). - * @see Single-Element - * Annotation - */ - public void setGeneratedAnnotationClass(@Nullable String generatedAnnotationClass) { - module.bindInstance( - CodegenModule.GENERATED_ANNOTATION_CLASS, - GeneratedAnnotationResolver.resolve(generatedAnnotationClass)); - } } diff --git a/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetadataExporterConfig.java b/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetadataExporterConfig.java new file mode 100644 index 0000000000..1739048263 --- /dev/null +++ b/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetadataExporterConfig.java @@ -0,0 +1,273 @@ +package com.querydsl.sql.codegen; + +import com.querydsl.codegen.BeanSerializer; +import com.querydsl.codegen.Property; +import com.querydsl.codegen.Serializer; +import com.querydsl.sql.SchemaAndTable; +import com.querydsl.sql.codegen.support.CustomType; +import com.querydsl.sql.codegen.support.NumericMapping; +import com.querydsl.sql.codegen.support.RenameMapping; +import com.querydsl.sql.codegen.support.TypeMapping; +import java.io.File; +import java.nio.charset.Charset; +import java.util.Comparator; +import java.util.List; + +public interface MetadataExporterConfig { + + /** + * Override the name prefix for the classes (default: Q) + * + * @return name prefix for querydsl-types (default: Q) + */ + String getNamePrefix(); + + /** + * Override the name suffix for the classes (default: "") + * + * @return name suffix for querydsl-types (default: "") + */ + String getNameSuffix(); + + /** + * Override the bean prefix for the classes (default: "") + * + * @return bean prefix for bean-types (default: "") + */ + String getBeanPrefix(); + + /** + * Override the bean suffix for the classes (default: "") + * + * @return bean suffix for bean-types (default: "") + */ + String getBeanSuffix(); + + /** + * Set the target folder for beans + * + *

defaults to the targetFolder value + * + * @return target source folder to create the bean sources into + */ + File getBeansTargetFolder(); + + /** + * Set the target folder + * + * @return target source folder to create the sources into (e.g. target/generated-sources/java) + */ + File getTargetFolder(); + + /** + * Set true to create Scala sources instead of Java sources + * + * @return whether to create Scala sources (default: false) + */ + boolean isCreateScalaSources(); + + /** + * Set the package name + * + * @return package name for sources + */ + String getPackageName(); + + /** + * Override the bean package name (default: packageName) + * + * @return package name for bean sources + */ + String getBeanPackageName(); + + /** + * Set whether inner classes should be created for keys + * + * @return whether inner classes should be created for keys + */ + boolean isInnerClassesForKeys(); + + /** + * Override the NamingStrategy (default: new DefaultNamingStrategy()) + * + * @return naming strategy to override (default: new DefaultNamingStrategy()) + */ + Class getNamingStrategyClass(); + + /** + * Set the schema pattern filter to be used + * + * @return a schema name pattern; must match the schema name as it is stored in the database; "" + * retrieves those without a schema; {@code null} means that the schema name should not be + * used to narrow the search (default: null) + */ + String getSchemaPattern(); + + /** + * @return a catalog name; must match the catalog name as it is stored in the database; "" + * retrieves those without a catalog; null means that the catalog name should not + * be used to narrow the search + */ + String getCatalogPattern(); + + /** + * Set the table name pattern filter to be used + * + * @return a table name pattern; must match the table name as it is stored in the database + * (default: null) + */ + String getTableNamePattern(); + + /** + * @return whether column annotations should be created + */ + boolean isColumnAnnotations(); + + /** + * @return whether validation annotations should be created + */ + boolean isValidationAnnotations(); + + /** + * !!! Important !!! {@link NamingStrategy#getPackage(String, SchemaAndTable)} will be + * invoked only if schemaToPackage is set to true. + * + * @deprecated This flag will not be necessary in the future because the generated package name + * can be controlled in method {@link NamingStrategy#getPackage(String, SchemaAndTable)}. + * @return whether schema names should be appended to the package name. + */ + @Deprecated + boolean isSchemaToPackage(); + + /** + * @return whether names should be normalized to lowercase + */ + boolean isLowerCase(); + + /** + * @return whether tables should be exported + */ + boolean isExportTables(); + + /** + * @return whether views should be exported + */ + boolean isExportViews(); + + /** + * @return whether all table types should be exported + */ + boolean isExportAll(); + + /** + * @return whether serialize beans as well + */ + boolean isExportBeans(); + + /** + * @return whether primary keys should be exported + */ + boolean isExportPrimaryKeys(); + + /** + * @return whether foreign keys should be exported + */ + boolean isExportForeignKeys(); + + /** + * @return whether direct foreign keys should be exported + */ + boolean isExportDirectForeignKeys(); + + /** + * @return whether inverse foreign keys should be exported + */ + boolean isExportInverseForeignKeys(); + + /** + * @return the source encoding + */ + Charset getSourceEncoding(); + + /** + * @return the table types to export as a comma separated string + */ + String getTableTypesToExport(); + + /** + * Set the java imports + * + * @return java imports array + */ + List getImports(); + + /** + * Set the fully qualified class name of the "generated" annotation added ot the generated sources + * + * @return the fully qualified class name of the Single-Element Annotation (with {@code + * String} element) to be used on the generated sources, or {@code null} (defaulting to {@code + * javax.annotation.Generated} or {@code javax.annotation.processing.Generated} depending on + * the java version). + * @see Single-Element + * Annotation + */ + String getGeneratedAnnotationClass(); + + /** + * Set the Bean serializer class to create bean types as well + * + * @return serializer for JavaBeans (default: null) + */ + Class getBeanSerializerClass(); + + /** + * @return additional interfaces to be implemented by beans + */ + String[] getBeanInterfaces(); + + /** + * @return switch for {@code toString} addition + */ + boolean isBeanAddToString(); + + /** + * @return switch for full constructor addition + */ + boolean isBeanAddFullConstructor(); + + /** + * @return switch to print supertype content + */ + boolean isBeanPrintSupertype(); + + /** + * @return custom type classnames to use + */ + List getCustomTypes(); + + /** + * @return custom type mappings to use + */ + List getTypeMappings(); + + /** + * @return custom numeric mappings + */ + List getNumericMappings(); + + /** + * @return custom rename mappings + */ + List getRenameMappings(); + + /** + * @return the column comparator class + */ + Class> getColumnComparatorClass(); + + /** + * @return the serializer class + */ + Class getSerializerClass(); +} diff --git a/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetadataExporterConfigImpl.java b/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetadataExporterConfigImpl.java new file mode 100644 index 0000000000..32f546ab1a --- /dev/null +++ b/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/MetadataExporterConfigImpl.java @@ -0,0 +1,419 @@ +package com.querydsl.sql.codegen; + +import com.querydsl.codegen.BeanSerializer; +import com.querydsl.codegen.Property; +import com.querydsl.codegen.Serializer; +import com.querydsl.sql.codegen.support.CustomType; +import com.querydsl.sql.codegen.support.NumericMapping; +import com.querydsl.sql.codegen.support.RenameMapping; +import com.querydsl.sql.codegen.support.TypeMapping; +import java.io.File; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Comparator; +import java.util.List; + +public class MetadataExporterConfigImpl implements MetadataExporterConfig { + + private String namePrefix; + + private String nameSuffix; + + private String beanPrefix; + private String beanSuffix; + + private File beansTargetFolder; + private File targetFolder; + private boolean createScalaSources; + private String packageName; + private String beanPackageName; + private boolean innerClassesForKeys; + private Class namingStrategyClass = DefaultNamingStrategy.class; + + private String schemaPattern; + private String catalogPattern; + private String tableNamePattern; + + private boolean columnAnnotations; + + private boolean validationAnnotations; + + private boolean schemaToPackage; + + private boolean lowerCase; + + private boolean exportTables = true; + + private boolean exportViews = true; + + private boolean exportAll; + + private boolean exportBeans; + + private boolean exportPrimaryKeys = true; + + private boolean exportForeignKeys = true; + + private boolean exportDirectForeignKeys = true; + + private boolean exportInverseForeignKeys = true; + + private Charset sourceEncoding = StandardCharsets.UTF_8; + + private String tableTypesToExport; + + private List imports; + + private String generatedAnnotationClass; + + private Class beanSerializerClass; + + private String[] beanInterfaces; + + private boolean beanAddToString; + + private boolean beanAddFullConstructor; + + private boolean beanPrintSupertype; + + private List customTypes; + + private List typeMappings; + + private List numericMappings; + + private List renameMappings; + + private Class> columnComparatorClass; + + private Class serializerClass; + + public String getNamePrefix() { + return namePrefix; + } + + public void setNamePrefix(String namePrefix) { + this.namePrefix = namePrefix; + } + + public String getNameSuffix() { + return nameSuffix; + } + + public void setNameSuffix(String nameSuffix) { + this.nameSuffix = nameSuffix; + } + + public String getBeanPrefix() { + return beanPrefix; + } + + public void setBeanPrefix(String beanPrefix) { + this.beanPrefix = beanPrefix; + } + + public String getBeanSuffix() { + return beanSuffix; + } + + public void setBeanSuffix(String beanSuffix) { + this.beanSuffix = beanSuffix; + } + + public File getBeansTargetFolder() { + return beansTargetFolder; + } + + public void setBeansTargetFolder(File beansTarFolder) { + this.beansTargetFolder = beansTarFolder; + } + + public File getTargetFolder() { + return targetFolder; + } + + public void setTargetFolder(File tarFolder) { + this.targetFolder = tarFolder; + } + + public boolean isCreateScalaSources() { + return createScalaSources; + } + + public void setCreateScalaSources(boolean createScalaSources) { + this.createScalaSources = createScalaSources; + } + + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + public String getBeanPackageName() { + return beanPackageName; + } + + public void setBeanPackageName(String beanPackageName) { + this.beanPackageName = beanPackageName; + } + + public boolean isInnerClassesForKeys() { + return innerClassesForKeys; + } + + public void setInnerClassesForKeys(boolean innerClassesForKeys) { + this.innerClassesForKeys = innerClassesForKeys; + } + + public Class getNamingStrategyClass() { + return namingStrategyClass; + } + + public void setNamingStrategyClass(Class namingStrategyClass) { + this.namingStrategyClass = namingStrategyClass; + } + + public String getSchemaPattern() { + return schemaPattern; + } + + public void setSchemaPattern(String schemaPattern) { + this.schemaPattern = schemaPattern; + } + + public String getCatalogPattern() { + return catalogPattern; + } + + public void setCatalogPattern(String catalogPattern) { + this.catalogPattern = catalogPattern; + } + + public String getTableNamePattern() { + return tableNamePattern; + } + + public void setTableNamePattern(String tableNamePattern) { + this.tableNamePattern = tableNamePattern; + } + + public boolean isColumnAnnotations() { + return columnAnnotations; + } + + public void setColumnAnnotations(boolean columnAnnotations) { + this.columnAnnotations = columnAnnotations; + } + + public boolean isValidationAnnotations() { + return validationAnnotations; + } + + public void setValidationAnnotations(boolean validationAnnotations) { + this.validationAnnotations = validationAnnotations; + } + + public boolean isSchemaToPackage() { + return schemaToPackage; + } + + public void setSchemaToPackage(boolean schemaToPackage) { + this.schemaToPackage = schemaToPackage; + } + + public boolean isLowerCase() { + return lowerCase; + } + + public void setLowerCase(boolean lowerCase) { + this.lowerCase = lowerCase; + } + + public boolean isExportTables() { + return exportTables; + } + + public void setExportTables(boolean exportTables) { + this.exportTables = exportTables; + } + + public boolean isExportViews() { + return exportViews; + } + + public void setExportViews(boolean exportViews) { + this.exportViews = exportViews; + } + + public boolean isExportAll() { + return exportAll; + } + + public void setExportAll(boolean exportAll) { + this.exportAll = exportAll; + } + + public boolean isExportBeans() { + return exportBeans; + } + + public void setExportBeans(boolean exportBeans) { + this.exportBeans = exportBeans; + } + + public boolean isExportPrimaryKeys() { + return exportPrimaryKeys; + } + + public void setExportPrimaryKeys(boolean exportPrimaryKeys) { + this.exportPrimaryKeys = exportPrimaryKeys; + } + + public boolean isExportForeignKeys() { + return exportForeignKeys; + } + + public void setExportForeignKeys(boolean exportForeignKeys) { + this.exportForeignKeys = exportForeignKeys; + } + + public boolean isExportDirectForeignKeys() { + return exportDirectForeignKeys; + } + + public void setExportDirectForeignKeys(boolean exportDirectForeignKeys) { + this.exportDirectForeignKeys = exportDirectForeignKeys; + } + + public boolean isExportInverseForeignKeys() { + return exportInverseForeignKeys; + } + + public void setExportInverseForeignKeys(boolean exportInverseForeignKeys) { + this.exportInverseForeignKeys = exportInverseForeignKeys; + } + + public Charset getSourceEncoding() { + return sourceEncoding; + } + + public void setSourceEncoding(Charset sourceEncoding) { + this.sourceEncoding = sourceEncoding; + } + + public String getTableTypesToExport() { + return tableTypesToExport; + } + + public void setTableTypesToExport(String tableTypesToExport) { + this.tableTypesToExport = tableTypesToExport; + } + + public List getImports() { + return imports; + } + + public void setImports(List imports) { + this.imports = imports; + } + + public String getGeneratedAnnotationClass() { + return generatedAnnotationClass; + } + + public void setGeneratedAnnotationClass(String generatedAnnotationClass) { + this.generatedAnnotationClass = generatedAnnotationClass; + } + + public Class getBeanSerializerClass() { + return beanSerializerClass; + } + + public void setBeanSerializerClass(Class beanSerializerClass) { + this.beanSerializerClass = beanSerializerClass; + } + + public String[] getBeanInterfaces() { + return beanInterfaces; + } + + public void setBeanInterfaces(String[] beanInterfaces) { + this.beanInterfaces = beanInterfaces; + } + + public boolean isBeanAddToString() { + return beanAddToString; + } + + public void setBeanAddToString(boolean beanAddToString) { + this.beanAddToString = beanAddToString; + } + + public boolean isBeanAddFullConstructor() { + return beanAddFullConstructor; + } + + public void setBeanAddFullConstructor(boolean beanAddFullConstructor) { + this.beanAddFullConstructor = beanAddFullConstructor; + } + + public boolean isBeanPrintSupertype() { + return beanPrintSupertype; + } + + public void setBeanPrintSupertype(boolean beanPrintSupertype) { + this.beanPrintSupertype = beanPrintSupertype; + } + + public List getCustomTypes() { + return customTypes; + } + + public void setCustomTypes(List customTypes) { + this.customTypes = customTypes; + } + + public List getTypeMappings() { + return typeMappings; + } + + public void setTypeMappings(List typeMappings) { + this.typeMappings = typeMappings; + } + + public List getNumericMappings() { + return numericMappings; + } + + public void setNumericMappings(List numericMappings) { + this.numericMappings = numericMappings; + } + + public List getRenameMappings() { + return renameMappings; + } + + public void setRenameMappings(List renameMappings) { + this.renameMappings = renameMappings; + } + + public Class> getColumnComparatorClass() { + return columnComparatorClass; + } + + public void setColumnComparatorClass( + Class> columnComparatorClass) { + this.columnComparatorClass = columnComparatorClass; + } + + public Class getSerializerClass() { + return serializerClass; + } + + public void setSerializerClass(Class serializerClass) { + this.serializerClass = serializerClass; + } +} diff --git a/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/ant/AntMetaDataExporter.java b/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/ant/AntMetaDataExporter.java index e01a010279..0dbd04bfcd 100644 --- a/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/ant/AntMetaDataExporter.java +++ b/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/ant/AntMetaDataExporter.java @@ -14,22 +14,24 @@ package com.querydsl.sql.codegen.ant; import com.querydsl.codegen.BeanSerializer; -import com.querydsl.codegen.utils.model.SimpleType; -import com.querydsl.sql.Configuration; -import com.querydsl.sql.SQLTemplates; +import com.querydsl.codegen.Property; +import com.querydsl.codegen.Serializer; import com.querydsl.sql.codegen.DefaultNamingStrategy; import com.querydsl.sql.codegen.MetaDataExporter; +import com.querydsl.sql.codegen.MetadataExporterConfig; import com.querydsl.sql.codegen.NamingStrategy; import com.querydsl.sql.codegen.support.CustomType; import com.querydsl.sql.codegen.support.NumericMapping; import com.querydsl.sql.codegen.support.RenameMapping; import com.querydsl.sql.codegen.support.TypeMapping; -import com.querydsl.sql.types.Type; import java.io.File; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Comparator; import java.util.List; import org.apache.tools.ant.BuildException; @@ -40,7 +42,7 @@ * * @author tiwe */ -public class AntMetaDataExporter extends Task { +public class AntMetaDataExporter extends Task implements MetadataExporterConfig { /** JDBC driver class name */ private String jdbcDriver; @@ -79,6 +81,8 @@ public class AntMetaDataExporter extends Task { */ private String schemaPattern; + private String catalogPattern; + /** * tableNamePattern a table name pattern; must match the table name as it is stored in the * database (default: null) @@ -160,9 +164,6 @@ public class AntMetaDataExporter extends Task { /** override default column order (default: alphabetical) */ private String columnComparatorClass; - /** spatial type support */ - private boolean spatial; - /** * Comma-separated list of table types to export (allowable values will depend on JDBC driver). * Allows for arbitrary set of types to be exported, e.g.: "TABLE, MATERIALIZED VIEW". The @@ -201,99 +202,8 @@ public void execute() { Class.forName(jdbcDriver).newInstance(); dbConn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword); - Configuration configuration = new Configuration(SQLTemplates.DEFAULT); - MetaDataExporter exporter = new MetaDataExporter(); - if (namePrefix != null) { - exporter.setNamePrefix(namePrefix); - } - if (nameSuffix != null) { - exporter.setNameSuffix(nameSuffix); - } - if (beanPrefix != null) { - exporter.setBeanPrefix(beanPrefix); - } - if (beanSuffix != null) { - exporter.setBeanSuffix(beanSuffix); - } - if (beansTargetFolder != null) { - exporter.setBeansTargetFolder(new File(beansTargetFolder)); - } - exporter.setPackageName(packageName); - exporter.setBeanPackageName(beanPackageName); - exporter.setTargetFolder(new File(targetFolder)); - exporter.setNamingStrategy((NamingStrategy) Class.forName(namingStrategyClass).newInstance()); - exporter.setInnerClassesForKeys(innerClassesForKeys); - exporter.setSchemaPattern(schemaPattern); - exporter.setTableNamePattern(tableNamePattern); - exporter.setColumnAnnotations(columnAnnotations); - exporter.setValidationAnnotations(validationAnnotations); - exporter.setSchemaToPackage(schemaToPackage); - exporter.setLowerCase(lowerCase); - exporter.setExportTables(exportTables); - exporter.setExportViews(exportViews); - exporter.setExportAll(exportAll); - exporter.setTableTypesToExport(tableTypesToExport); - exporter.setExportPrimaryKeys(exportPrimaryKeys); - exporter.setExportForeignKeys(exportForeignKeys); - exporter.setExportDirectForeignKeys(exportDirectForeignKeys); - exporter.setExportInverseForeignKeys(exportInverseForeignKeys); - exporter.setSpatial(spatial); - - if (imports != null && imports.length > 0) { - exporter.setImports(imports); - } - - if (exportBeans) { - BeanSerializer serializer = - (BeanSerializer) Class.forName(beanSerializerClass).newInstance(); - if (beanInterfaces != null) { - for (String iface : beanInterfaces) { - int sepIndex = iface.lastIndexOf('.'); - if (sepIndex < 0) { - serializer.addInterface(new SimpleType(iface)); - } else { - String packageName = iface.substring(0, sepIndex); - String simpleName = iface.substring(sepIndex + 1); - serializer.addInterface(new SimpleType(iface, packageName, simpleName)); - } - } - } - serializer.setAddFullConstructor(beanAddFullConstructor); - serializer.setAddToString(beanAddToString); - serializer.setPrintSupertype(beanPrintSupertype); - exporter.setBeanSerializer(serializer); - } - if (sourceEncoding != null) { - exporter.setSourceEncoding(sourceEncoding); - } - if (customTypes != null) { - for (CustomType customType : customTypes) { - configuration.register((Type) Class.forName(customType.getClassName()).newInstance()); - } - } - if (typeMappings != null) { - for (TypeMapping mapping : typeMappings) { - mapping.apply(configuration); - } - } - if (numericMappings != null) { - for (NumericMapping mapping : numericMappings) { - mapping.apply(configuration); - } - } - if (renameMappings != null) { - for (RenameMapping mapping : renameMappings) { - mapping.apply(configuration); - } - } - - if (columnComparatorClass != null) { - exporter.setColumnComparatorClass( - (Class) Class.forName(this.columnComparatorClass).asSubclass(Comparator.class)); - } - - exporter.setConfiguration(configuration); + MetaDataExporter exporter = new MetaDataExporter(this); exporter.export(dbConn.getMetaData()); @@ -410,32 +320,53 @@ public void setTableNamePattern(String tableNamePattern) { this.tableNamePattern = tableNamePattern; } - public String getTargetFolder() { - return targetFolder; + public File getTargetFolder() { + return new File(targetFolder); } public void setTargetFolder(String targetFolder) { this.targetFolder = targetFolder; } - public String getNamingStrategyClass() { - return namingStrategyClass; + public Class getNamingStrategyClass() { + if (namingStrategyClass == null) { + return null; + } + try { + return (Class) Class.forName(namingStrategyClass); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } } public void setNamingStrategyClass(String namingStrategyClass) { this.namingStrategyClass = namingStrategyClass; } - public String getBeanSerializerClass() { - return beanSerializerClass; + public Class getBeanSerializerClass() { + if (exportBeans && beanSerializerClass != null) { + try { + return (Class) Class.forName(beanSerializerClass); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + return null; } public void setBeanSerializerClass(String beanSerializerClass) { this.beanSerializerClass = beanSerializerClass; } - public String getSerializerClass() { - return serializerClass; + public Class getSerializerClass() { + if (serializerClass == null) { + return null; + } + try { + return (Class) Class.forName(serializerClass); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } } public void setSerializerClass(String serializerClass) { @@ -517,13 +448,8 @@ public void addCustomType(CustomType customType) { * @return a list of custom types * @deprecated Use addCustomType instead */ - public String[] getCustomTypes() { - String[] customTypes = new String[this.customTypes.size()]; - for (int i = 0; i < this.customTypes.size(); i++) { - CustomType customType = this.customTypes.get(i); - customTypes[i] = customType.getClassName(); - } - return customTypes; + public List getCustomTypes() { + return this.customTypes; } /** @@ -541,10 +467,6 @@ public void setCustomTypes(String[] strings) { } } - public boolean isCreateScalaSources() { - return createScalaSources; - } - public void setCreateScalaSources(boolean createScalaSources) { this.createScalaSources = createScalaSources; } @@ -621,22 +543,22 @@ public void setExportForeignKeys(boolean exportForeignKeys) { this.exportForeignKeys = exportForeignKeys; } - public String getColumnComparatorClass() { - return columnComparatorClass; + @Override + public Class> getColumnComparatorClass() { + if (columnComparatorClass == null) { + return null; + } + try { + return (Class>) Class.forName(columnComparatorClass); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } } public void setColumnComparatorClass(String columnComparatorClass) { this.columnComparatorClass = columnComparatorClass; } - public boolean isSpatial() { - return spatial; - } - - public void setSpatial(boolean spatial) { - this.spatial = spatial; - } - public String getTableTypesToExport() { return tableTypesToExport; } @@ -645,24 +567,32 @@ public void setTableTypesToExport(String tableTypesToExport) { this.tableTypesToExport = tableTypesToExport; } - public String[] getImports() { - return imports; + public List getImports() { + if (imports == null) { + return null; + } + return Arrays.asList(imports); } public void setImports(String[] imports) { this.imports = imports; } - public String getSourceEncoding() { - return sourceEncoding; + public Charset getSourceEncoding() { + if (sourceEncoding != null) return Charset.forName(sourceEncoding); + + return StandardCharsets.UTF_8; } public void setSourceEncoding(String sourceEncoding) { this.sourceEncoding = sourceEncoding; } - public String getBeansTargetFolder() { - return beansTargetFolder; + public File getBeansTargetFolder() { + if (beansTargetFolder != null) { + return new File(beansTargetFolder); + } + return null; } public void setBeansTargetFolder(String beansTargetFolder) { @@ -683,4 +613,38 @@ public void addNumericMapping(NumericMapping mapping) { public void addRenameMapping(RenameMapping mapping) { renameMappings.add(mapping); } + + @Override + public boolean isCreateScalaSources() { + return createScalaSources; + } + + @Override + public String getCatalogPattern() { + return catalogPattern; + } + + public void setCatalogPattern(String catalogPattern) { + this.catalogPattern = catalogPattern; + } + + @Override + public String getGeneratedAnnotationClass() { + return null; + } + + @Override + public List getTypeMappings() { + return typeMappings; + } + + @Override + public List getNumericMappings() { + return numericMappings; + } + + @Override + public List getRenameMappings() { + return renameMappings; + } } diff --git a/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/support/CustomType.java b/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/support/CustomType.java index 03488b5f70..7dc25ede60 100644 --- a/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/support/CustomType.java +++ b/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/support/CustomType.java @@ -6,6 +6,10 @@ public class CustomType { public CustomType() {} + public CustomType(String className) { + this.className = className; + } + public String getClassName() { return className; } diff --git a/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/support/TypeMapping.java b/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/support/TypeMapping.java index 2d22a63ca2..46ba96abd2 100644 --- a/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/support/TypeMapping.java +++ b/querydsl-sql-codegen/src/main/java/com/querydsl/sql/codegen/support/TypeMapping.java @@ -25,6 +25,17 @@ public class TypeMapping implements Mapping { private String table, column, type; + public TypeMapping() { + super(); + } + + public TypeMapping(String table, String column, String type) { + this(); + this.table = table; + this.column = column; + this.type = type; + } + @Override public void apply(Configuration configuration) { try { diff --git a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/CustomTypesTest.java b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/CustomTypesTest.java index 00558b4c47..14c6f8f052 100644 --- a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/CustomTypesTest.java +++ b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/CustomTypesTest.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.sql.SQLException; import org.junit.Before; @@ -63,20 +64,20 @@ public void setUp() throws ClassNotFoundException, SQLException { public void export() throws SQLException, IOException { // create exporter String namePrefix = "Q"; - NamingStrategy namingStrategy = new DefaultNamingStrategy(); - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setNamePrefix(namePrefix); - exporter.setPackageName("test"); - exporter.setTargetFolder(new File("target/customExport")); - exporter.setNamingStrategy(namingStrategy); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setNamePrefix(namePrefix); + config.setPackageName("test"); + config.setTargetFolder(new File("target/customExport")); + config.setNamingStrategyClass(DefaultNamingStrategy.class); + + MetaDataExporter exporter = new MetaDataExporter(config); exporter.setConfiguration(configuration); // export exporter.export(connection.getMetaData()); - String person = - new String( - Files.readAllBytes(Paths.get("target", "customExport", "test", "QPerson.java")), - StandardCharsets.UTF_8); + Path qpersonFile = Paths.get("target", "customExport", "test", "QPerson.java"); + assertThat(qpersonFile).exists(); + String person = new String(Files.readAllBytes(qpersonFile), StandardCharsets.UTF_8); // System.err.println(person); assertThat(person).contains("createEnum(\"gender\""); } diff --git a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/Export.java b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/Export.java index 3b1501c61b..d4a08a7dfb 100644 --- a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/Export.java +++ b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/Export.java @@ -13,10 +13,12 @@ public static void main(String[] args) throws Exception { String url = "jdbc:mysql://localhost:3306/querydsl"; Connection conn = DriverManager.getConnection(url, "querydsl", "querydsl"); - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setNamePrefix("S"); - exporter.setPackageName("com.querydsl.jpa.domain.sql"); - exporter.setTargetFolder(new File("../querydsl-jpa/src/test/java")); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setNamePrefix("S"); + config.setPackageName("com.querydsl.jpa.domain.sql"); + config.setTargetFolder(new File("../querydsl-jpa/src/test/java")); + + MetaDataExporter exporter = new MetaDataExporter(config); // exporter.setLowerCase(true); exporter.export(conn.getMetaData()); diff --git a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/ExportBaseTest.java b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/ExportBaseTest.java index 6f1f6f496a..1d8c2ceaf1 100644 --- a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/ExportBaseTest.java +++ b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/ExportBaseTest.java @@ -28,13 +28,14 @@ public abstract class ExportBaseTest { @Test public void export() throws SQLException { - NamingStrategy namingStrategy = new DefaultNamingStrategy(); - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSpatial(true); - exporter.setSchemaPattern(getSchemaPattern()); - exporter.setPackageName("test"); - exporter.setTargetFolder(folder.getRoot()); - exporter.setNamingStrategy(namingStrategy); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + // config.setSpatial(true); + config.setSchemaPattern(getSchemaPattern()); + config.setPackageName("test"); + config.setTargetFolder(folder.getRoot()); + config.setNamingStrategyClass(DefaultNamingStrategy.class); + + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(Connections.getConnection().getMetaData()); assertThat(folder.getRoot().listFiles().length).isGreaterThan(0); diff --git a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/ExportH2TwoSchemasTest.java b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/ExportH2TwoSchemasTest.java index fbf6b64400..ca21d51b7b 100644 --- a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/ExportH2TwoSchemasTest.java +++ b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/ExportH2TwoSchemasTest.java @@ -41,12 +41,12 @@ public static void tearDownAfterClass() throws SQLException { @Test public void export() throws SQLException, MalformedURLException, IOException { - NamingStrategy namingStrategy = new DefaultNamingStrategy(); - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern(null); - exporter.setPackageName("test"); - exporter.setTargetFolder(folder.getRoot()); - exporter.setNamingStrategy(namingStrategy); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern(null); + config.setPackageName("test"); + config.setTargetFolder(folder.getRoot()); + + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(Connections.getConnection().getMetaData()); String contents = diff --git a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/MetaDataExporterAllTest.java b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/MetaDataExporterAllTest.java index 1d37b0a40a..f7320515bb 100644 --- a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/MetaDataExporterAllTest.java +++ b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/MetaDataExporterAllTest.java @@ -38,8 +38,8 @@ public class MetaDataExporterAllTest { private String namePrefix, nameSuffix, beanPrefix, beanSuffix; private String beanPackageName; - private BeanSerializer beanSerializer = new BeanSerializer(); - private NamingStrategy namingStrategy; + private Class beanSerializer = BeanSerializer.class; + private Class namingStrategyClass; private boolean withBeans, withInnerClasses, withOrdinalPositioning; private boolean exportColumns, schemaToPackage; @@ -61,8 +61,9 @@ public static void tearDownClass() throws SQLException { public static List parameters() { List params = new ArrayList<>(); - List ns = - Arrays.asList(new DefaultNamingStrategy(), new OriginalNamingStrategy()); + List> ns = + Arrays.>asList( + DefaultNamingStrategy.class, OriginalNamingStrategy.class); List prefixOrSuffix = Arrays.asList("", "Q"); List beanPackage = Arrays.asList(null, "test2"); List booleans = Arrays.asList(true, false); @@ -72,7 +73,7 @@ public static List parameters() { for (String beanPrefix : prefixOrSuffix) { for (String beanSuffix : prefixOrSuffix) { for (String beanPackageName : beanPackage) { - for (NamingStrategy namingStrategy : ns) { + for (Class namingStrategy : ns) { for (boolean withBeans : booleans) { for (boolean withInnerClasses : booleans) { for (boolean withOrdinalPositioning : booleans) { @@ -124,7 +125,7 @@ public MetaDataExporterAllTest( String beanPrefix, String beanSuffix, String beanPackageName, - NamingStrategy namingStrategy, + Class namingStrategy, boolean withBeans, boolean withInnerClasses, boolean withOrdinalPositioning, @@ -136,7 +137,7 @@ public MetaDataExporterAllTest( this.beanSuffix = beanSuffix; this.beanPackageName = beanPackageName; this.schemaToPackage = schemaToPackage; - this.namingStrategy = namingStrategy; + this.namingStrategyClass = namingStrategy; this.withBeans = withBeans; this.withInnerClasses = withInnerClasses; this.withOrdinalPositioning = withOrdinalPositioning; @@ -145,26 +146,27 @@ public MetaDataExporterAllTest( @Test public void export() throws SQLException, IOException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setColumnAnnotations(exportColumns); - exporter.setSchemaPattern("PUBLIC"); - exporter.setNamePrefix(namePrefix); - exporter.setNameSuffix(nameSuffix); - exporter.setBeanPrefix(beanPrefix); - exporter.setBeanSuffix(beanSuffix); - exporter.setInnerClassesForKeys(withInnerClasses); - exporter.setPackageName("test"); - exporter.setBeanPackageName(beanPackageName); - exporter.setTargetFolder(folder.getRoot()); - exporter.setNamingStrategy(namingStrategy); - exporter.setSchemaToPackage(schemaToPackage); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setColumnAnnotations(exportColumns); + config.setSchemaPattern("PUBLIC"); + config.setNamePrefix(namePrefix); + config.setNameSuffix(nameSuffix); + config.setBeanPrefix(beanPrefix); + config.setBeanSuffix(beanSuffix); + config.setInnerClassesForKeys(withInnerClasses); + config.setPackageName("test"); + config.setBeanPackageName(beanPackageName); + config.setTargetFolder(folder.getRoot()); + config.setNamingStrategyClass(namingStrategyClass); + config.setSchemaToPackage(schemaToPackage); if (withBeans) { - exporter.setBeanSerializer(beanSerializer); + config.setBeanSerializerClass(beanSerializer); } if (withOrdinalPositioning) { - exporter.setColumnComparatorClass(OrdinalPositionComparator.class); + config.setColumnComparatorClass(OrdinalPositionComparator.class); } + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); Set classes = exporter.getClasses(); diff --git a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/MetaDataExporterTest.java b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/MetaDataExporterTest.java index 00b237f032..dd89562453 100644 --- a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/MetaDataExporterTest.java +++ b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/MetaDataExporterTest.java @@ -130,36 +130,34 @@ public void setUp() throws ClassNotFoundException, SQLException { metadata = connection.getMetaData(); } - private static final NamingStrategy defaultNaming = new DefaultNamingStrategy(); - - private static final NamingStrategy originalNaming = new OriginalNamingStrategy(); - private String beanPackageName = null; @Test public void normalSettings_repetition() throws SQLException { - test("Q", "", "", "", defaultNaming, folder.getRoot(), false, false, false); + test("Q", "", "", "", DefaultNamingStrategy.class, folder.getRoot(), false, false, false); File file = new File(folder.getRoot(), "test/QEmployee.java"); long lastModified = file.lastModified(); assertThat(file).exists(); clean = false; - test("Q", "", "", "", defaultNaming, folder.getRoot(), false, false, false); + test("Q", "", "", "", DefaultNamingStrategy.class, folder.getRoot(), false, false, false); assertThat(file.lastModified()).isEqualTo(lastModified); } @Test public void explicit_configuration() throws SQLException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setCatalogPattern(connection.getCatalog()); - exporter.setSchemaPattern("PUBLIC"); - exporter.setNamePrefix("Q"); - exporter.setPackageName("test"); - exporter.setTargetFolder(folder.getRoot()); - exporter.setNamingStrategy(new DefaultNamingStrategy()); - exporter.setBeanSerializer(new BeanSerializer()); - exporter.setBeanPackageName("test2"); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setCatalogPattern(connection.getCatalog()); + config.setSchemaPattern("PUBLIC"); + config.setNamePrefix("Q"); + config.setPackageName("test"); + config.setTargetFolder(folder.getRoot()); + config.setNamingStrategyClass(DefaultNamingStrategy.class); + config.setBeanSerializerClass(BeanSerializer.class); + config.setBeanPackageName("test2"); + config.setExportBeans(true); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); assertThat(new File(folder.getRoot(), "test/QDateTest.java")).exists(); @@ -175,14 +173,16 @@ public void validation_annotations_are_not_added_to_columns_with_default_values( + "id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL," + "name VARCHAR(255) NOT NULL DEFAULT 'some default')"); - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC"); - exporter.setNamePrefix("Q"); - exporter.setPackageName("test"); - exporter.setTableNamePattern("FOO"); - exporter.setTargetFolder(folder.getRoot()); - exporter.setBeanSerializer(new BeanSerializer()); - exporter.setValidationAnnotations(true); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC"); + config.setNamePrefix("Q"); + config.setPackageName("test"); + config.setTableNamePattern("FOO"); + config.setTargetFolder(folder.getRoot()); + config.setBeanSerializerClass(BeanSerializer.class); + config.setValidationAnnotations(true); + config.setExportBeans(true); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); URLClassLoader classLoader = @@ -210,19 +210,23 @@ public void validation_annotations_are_added_to_columns_without_default_values() + "id VARCHAR(10) PRIMARY KEY NOT NULL," + "name VARCHAR(255) NOT NULL)"); - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC"); - exporter.setNamePrefix("Q"); - exporter.setPackageName("test"); - exporter.setTableNamePattern("BAR"); - exporter.setTargetFolder(folder.getRoot()); - exporter.setBeanSerializer(new BeanSerializer()); - exporter.setValidationAnnotations(true); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC"); + config.setNamePrefix("Q"); + config.setPackageName("test"); + config.setTableNamePattern("BAR"); + config.setTargetFolder(folder.getRoot()); + config.setBeanSerializerClass(BeanSerializer.class); + config.setValidationAnnotations(true); + config.setExportBeans(true); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); + File file = new File(folder.getRoot().getAbsoluteFile(), "/test/Bar.java"); + assertThat(file.exists()); URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] {folder.getRoot().toURI().toURL()}); - compiler.run(null, null, null, folder.getRoot().getAbsoluteFile() + "/test/Bar.java"); + compiler.run(null, null, null, file.toString()); Class cls = Class.forName("test.Bar", true, classLoader); assertThat( ReflectionUtils.getAnnotatedElement(cls, "id", Integer.class) @@ -238,10 +242,11 @@ public void validation_annotations_are_added_to_columns_without_default_values() @Test public void minimal_configuration() throws SQLException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC"); - exporter.setPackageName("test"); - exporter.setTargetFolder(folder.getRoot()); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC"); + config.setPackageName("test"); + config.setTargetFolder(folder.getRoot()); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); assertThat(new File(folder.getRoot(), "test/QDateTest.java")).exists(); @@ -249,10 +254,11 @@ public void minimal_configuration() throws SQLException { @Test public void minimal_configuration_with_schemas() throws SQLException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC2,PUBLIC"); - exporter.setPackageName("test"); - exporter.setTargetFolder(folder.getRoot()); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC2,PUBLIC"); + config.setPackageName("test"); + config.setTargetFolder(folder.getRoot()); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); assertThat(new File(folder.getRoot(), "test/QDateTest.java")).exists(); @@ -260,11 +266,12 @@ public void minimal_configuration_with_schemas() throws SQLException { @Test public void minimal_configuration_with_schemas_and_tables() throws SQLException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC2,PUBLIC"); - exporter.setTableNamePattern("RESERVED,UNDERSCORE,BEANGEN1"); - exporter.setPackageName("test"); - exporter.setTargetFolder(folder.getRoot()); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC2,PUBLIC"); + config.setTableNamePattern("RESERVED,UNDERSCORE,BEANGEN1"); + config.setPackageName("test"); + config.setTargetFolder(folder.getRoot()); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); assertThat(new File(folder.getRoot(), "test/QBeangen1.java")).exists(); @@ -275,11 +282,12 @@ public void minimal_configuration_with_schemas_and_tables() throws SQLException @Test public void minimal_configuration_with_tables() throws SQLException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC"); - exporter.setTableNamePattern("RESERVED,UNDERSCORE,BEANGEN1"); - exporter.setPackageName("test"); - exporter.setTargetFolder(folder.getRoot()); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC"); + config.setTableNamePattern("RESERVED,UNDERSCORE,BEANGEN1"); + config.setPackageName("test"); + config.setTargetFolder(folder.getRoot()); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); assertThat(new File(folder.getRoot(), "test/QBeangen1.java")).exists(); @@ -290,11 +298,12 @@ public void minimal_configuration_with_tables() throws SQLException { @Test(expected = IllegalStateException.class) public void minimal_configuration_with_duplicate_tables() throws SQLException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC"); - exporter.setTableNamePattern("%,%"); - exporter.setPackageName("test"); - exporter.setTargetFolder(folder.getRoot()); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC"); + config.setTableNamePattern("%,%"); + config.setPackageName("test"); + config.setTargetFolder(folder.getRoot()); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); assertThat(new File(folder.getRoot(), "test/QBeangen1.java")).exists(); @@ -305,12 +314,13 @@ public void minimal_configuration_with_duplicate_tables() throws SQLException { @Test public void minimal_configuration_with_suffix() throws SQLException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC"); - exporter.setPackageName("test"); - exporter.setNamePrefix(""); - exporter.setNameSuffix("Type"); - exporter.setTargetFolder(folder.getRoot()); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC"); + config.setPackageName("test"); + config.setNamePrefix(""); + config.setNameSuffix("Type"); + config.setTargetFolder(folder.getRoot()); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); assertThat(new File(folder.getRoot(), "test/DateTestType.java")).exists(); @@ -318,13 +328,14 @@ public void minimal_configuration_with_suffix() throws SQLException { @Test public void minimal_configuration_without_keys() throws SQLException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC"); - exporter.setPackageName("test"); - exporter.setNamePrefix(""); - exporter.setNameSuffix("Type"); - exporter.setTargetFolder(folder.getRoot()); - exporter.setExportForeignKeys(false); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC"); + config.setPackageName("test"); + config.setNamePrefix(""); + config.setNameSuffix("Type"); + config.setTargetFolder(folder.getRoot()); + config.setExportForeignKeys(false); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); assertThat(new File(folder.getRoot(), "test/DateTestType.java")).exists(); @@ -332,13 +343,14 @@ public void minimal_configuration_without_keys() throws SQLException { @Test public void minimal_configuration_only_direct_foreign_keys() throws SQLException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC"); - exporter.setPackageName("test"); - exporter.setNamePrefix(""); - exporter.setNameSuffix("Type"); - exporter.setTargetFolder(folder.getRoot()); - exporter.setExportInverseForeignKeys(false); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC"); + config.setPackageName("test"); + config.setNamePrefix(""); + config.setNameSuffix("Type"); + config.setTargetFolder(folder.getRoot()); + config.setExportInverseForeignKeys(false); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); assertThat(new File(folder.getRoot(), "test/DateTestType.java")).exists(); @@ -346,13 +358,15 @@ public void minimal_configuration_only_direct_foreign_keys() throws SQLException @Test public void minimal_configuration_with_bean_prefix() throws SQLException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC"); - exporter.setPackageName("test"); - exporter.setNamePrefix(""); - exporter.setBeanPrefix("Bean"); - exporter.setBeanSerializer(new BeanSerializer()); - exporter.setTargetFolder(folder.getRoot()); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC"); + config.setPackageName("test"); + config.setNamePrefix(""); + config.setBeanPrefix("Bean"); + config.setBeanSerializerClass(BeanSerializer.class); + config.setTargetFolder(folder.getRoot()); + config.setExportBeans(true); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); assertThat(new File(folder.getRoot(), "test/DateTest.java")).exists(); @@ -361,13 +375,15 @@ public void minimal_configuration_with_bean_prefix() throws SQLException { @Test public void minimal_configuration_with_bean_suffix() throws SQLException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC"); - exporter.setPackageName("test"); - exporter.setNamePrefix(""); - exporter.setBeanSuffix("Bean"); - exporter.setBeanSerializer(new BeanSerializer()); - exporter.setTargetFolder(folder.getRoot()); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC"); + config.setPackageName("test"); + config.setNamePrefix(""); + config.setBeanSuffix("Bean"); + config.setBeanSerializerClass(BeanSerializer.class); + config.setTargetFolder(folder.getRoot()); + config.setExportBeans(true); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); assertThat(new File(folder.getRoot(), "test/DateTest.java")).exists(); @@ -376,14 +392,16 @@ public void minimal_configuration_with_bean_suffix() throws SQLException { @Test public void minimal_configuration_with_bean_folder() throws SQLException, IOException { - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC"); - exporter.setPackageName("test"); - exporter.setNamePrefix(""); - exporter.setBeanSuffix("Bean"); - exporter.setBeanSerializer(new BeanSerializer()); - exporter.setTargetFolder(folder.getRoot()); - exporter.setBeansTargetFolder(folder.newFolder("beans")); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC"); + config.setPackageName("test"); + config.setNamePrefix(""); + config.setBeanSuffix("Bean"); + config.setBeanSerializerClass(BeanSerializer.class); + config.setTargetFolder(folder.getRoot()); + config.setBeansTargetFolder(folder.newFolder("beans")); + config.setExportBeans(true); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); assertThat(new File(folder.getRoot(), "test/DateTest.java")).exists(); @@ -404,16 +422,17 @@ public void catalog_pattern() throws SQLException, IOException, ClassNotFoundExc stmt.execute( "CREATE TABLE IF NOT EXISTS catalog_test_two.test_catalog_table_two(id INT PRIMARY KEY, foo VARCHAR(32) NOT NULL)"); - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setSchemaPattern("PUBLIC"); - exporter.setCatalogPattern("catalog_test_one"); - exporter.setPackageName("test"); - exporter.setNamePrefix(""); - exporter.setBeanSuffix("Bean"); - exporter.setBeanSerializer(new BeanSerializer()); - exporter.setTargetFolder(folder.getRoot()); - exporter.setBeansTargetFolder(folder.newFolder("beans")); - + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setSchemaPattern("PUBLIC"); + config.setCatalogPattern("catalog_test_one"); + config.setPackageName("test"); + config.setNamePrefix(""); + config.setBeanSuffix("Bean"); + config.setBeanSerializerClass(BeanSerializer.class); + config.setTargetFolder(folder.getRoot()); + config.setBeansTargetFolder(folder.newFolder("beans")); + + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(connection.getMetaData()); assertThat(new File(folder.getRoot(), "test/TestCatalogTableOne.java")).exists(); assertThat(new File(folder.getRoot(), "beans/test/TestCatalogTableOneBean.java")).exists(); @@ -422,7 +441,7 @@ public void catalog_pattern() throws SQLException, IOException, ClassNotFoundExc assertThat(new File(folder.getRoot(), "beans/test/TestCatalogTableTwoBean.java").exists()) .isFalse(); - exporter.setCatalogPattern("catalog_test_two"); + config.setCatalogPattern("catalog_test_two"); exporter.export(connection.getMetaData()); assertThat(new File(folder.getRoot(), "test/TestCatalogTableTwo.java")).exists(); @@ -440,7 +459,7 @@ private void test( String nameSuffix, String beanPrefix, String beanSuffix, - NamingStrategy namingStrategy, + Class namingStrategy, File targetDir, boolean withBeans, boolean withInnerClasses, @@ -456,25 +475,26 @@ private void test( } } - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setColumnAnnotations(exportColumns); - exporter.setSchemaPattern("PUBLIC"); - exporter.setNamePrefix(namePrefix); - exporter.setNameSuffix(nameSuffix); - exporter.setBeanPrefix(beanPrefix); - exporter.setBeanSuffix(beanSuffix); - exporter.setInnerClassesForKeys(withInnerClasses); - exporter.setPackageName("test"); - exporter.setBeanPackageName(beanPackageName); - exporter.setTargetFolder(targetDir); - exporter.setNamingStrategy(namingStrategy); - exporter.setSchemaToPackage(schemaToPackage); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setColumnAnnotations(exportColumns); + config.setSchemaPattern("PUBLIC"); + config.setNamePrefix(namePrefix); + config.setNameSuffix(nameSuffix); + config.setBeanPrefix(beanPrefix); + config.setBeanSuffix(beanSuffix); + config.setInnerClassesForKeys(withInnerClasses); + config.setPackageName("test"); + config.setBeanPackageName(beanPackageName); + config.setTargetFolder(targetDir); + config.setNamingStrategyClass(namingStrategy); + config.setSchemaToPackage(schemaToPackage); if (withBeans) { - exporter.setBeanSerializer(new BeanSerializer()); + config.setBeanSerializerClass(BeanSerializer.class); } if (withOrdinalPositioning) { - exporter.setColumnComparatorClass(OrdinalPositionComparator.class); + config.setColumnComparatorClass(OrdinalPositionComparator.class); } + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(metadata); Set classes = exporter.getClasses(); diff --git a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/MetaDataSerializerTest.java b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/MetaDataSerializerTest.java index 3d80d1a8b7..1ed74a4276 100644 --- a/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/MetaDataSerializerTest.java +++ b/querydsl-sql-codegen/src/test/java/com/querydsl/sql/codegen/MetaDataSerializerTest.java @@ -87,14 +87,15 @@ public void setUp() throws SQLException, ClassNotFoundException { @Test public void normal_serialization() throws SQLException { String namePrefix = "Q"; - NamingStrategy namingStrategy = new DefaultNamingStrategy(); // customization of serialization - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setBeanSerializerClass(BeanSerializer.class); - exporter.setNamePrefix(namePrefix); - exporter.setPackageName("test"); - exporter.setTargetFolder(folder.getRoot()); - exporter.setNamingStrategy(namingStrategy); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setBeanSerializerClass(BeanSerializer.class); + config.setNamePrefix(namePrefix); + config.setPackageName("test"); + config.setTargetFolder(folder.getRoot()); + config.setNamingStrategyClass(DefaultNamingStrategy.class); + config.setExportBeans(true); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.export(connection.getMetaData()); compile(exporter); @@ -140,16 +141,17 @@ public void setValue(PreparedStatement st, int startIndex, CustomNumber value) throw new UnsupportedOperationException(); } }); - NamingStrategy namingStrategy = new DefaultNamingStrategy(); // customization of serialization - MetaDataExporter exporter = new MetaDataExporter(); - exporter.setBeanSerializerClass(BeanSerializer.class); - exporter.setNamePrefix(namePrefix); - exporter.setPackageName("test"); - exporter.setTargetFolder(folder.getRoot()); - exporter.setNamingStrategy(namingStrategy); + MetadataExporterConfigImpl config = new MetadataExporterConfigImpl(); + config.setBeanSerializerClass(BeanSerializer.class); + config.setNamePrefix(namePrefix); + config.setPackageName("test"); + config.setTargetFolder(folder.getRoot()); + config.setNamingStrategyClass(DefaultNamingStrategy.class); + config.setGeneratedAnnotationClass("com.querydsl.core.annotations.Generated"); + config.setExportBeans(true); + MetaDataExporter exporter = new MetaDataExporter(config); exporter.setConfiguration(conf); - exporter.setGeneratedAnnotationClass("com.querydsl.core.annotations.Generated"); exporter.export(connection.getMetaData()); compile(exporter);