Search in sources :

Example 6 with SimpleType

use of com.querydsl.codegen.utils.model.SimpleType in project querydsl by querydsl.

the class JavaWriterTest method setUp.

@Before
public void setUp() {
    w = new StringWriter();
    writer = new JavaWriter(w);
    testType = new ClassType(JavaWriterTest.class);
    testType2 = new SimpleType("com.querydsl.codegen.utils.Test", "com.querydsl.codegen.utils", "Test");
    testSuperType = new SimpleType("com.querydsl.codegen.utils.Superclass", "com.querydsl.codegen.utils", "Superclass");
    testInterface1 = new SimpleType("com.querydsl.codegen.utils.TestInterface1", "com.querydsl.codegen.utils", "TestInterface1");
    testInterface2 = new SimpleType("com.querydsl.codegen.utils.TestInterface2", "com.querydsl.codegen.utils", "TestInterface2");
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) StringWriter(java.io.StringWriter) ClassType(com.querydsl.codegen.utils.model.ClassType) Before(org.junit.Before)

Example 7 with SimpleType

use of com.querydsl.codegen.utils.model.SimpleType in project querydsl by querydsl.

the class JavaWriterTest method Arrays.

@Test
public void Arrays() throws IOException {
    writer.beginClass(new SimpleType("Main"));
    writer.field(Types.STRING.asArrayType(), "stringArray");
    writer.beginPublicMethod(Types.VOID, "main", new Parameter("args", Types.STRING.asArrayType()));
    writer.line("//");
    writer.end();
    writer.beginPublicMethod(Types.VOID, "main2", new Parameter("args", new ClassType(TypeCategory.ARRAY, String[].class)));
    writer.line("//");
    writer.end();
    writer.end();
    System.out.println(w);
    assertTrue(w.toString().contains("String[] stringArray;"));
    assertTrue(w.toString().contains("public void main(String[] args) {"));
    assertTrue(w.toString().contains("public void main2(String[] args) {"));
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Parameter(com.querydsl.codegen.utils.model.Parameter) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Example 8 with SimpleType

use of com.querydsl.codegen.utils.model.SimpleType in project querydsl by querydsl.

the class MetaDataExporter method createEntityType.

protected EntityType createEntityType(SchemaAndTable schemaAndTable, final String className) {
    EntityType classModel;
    if (beanSerializer == null) {
        String packageName = normalizePackage(module.getPackageName(), schemaAndTable);
        String simpleName = module.getPrefix() + className + module.getSuffix();
        Type classTypeModel = new SimpleType(TypeCategory.ENTITY, packageName + "." + simpleName, packageName, simpleName, false, false);
        classModel = new EntityType(classTypeModel, module.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS));
        typeMappings.register(classModel, classModel);
    } else {
        String beanPackage = normalizePackage(beanPackageName, schemaAndTable);
        String simpleName = module.getBeanPrefix() + className + module.getBeanSuffix();
        Type classTypeModel = new SimpleType(TypeCategory.ENTITY, beanPackage + "." + simpleName, beanPackage, simpleName, false, false);
        classModel = new EntityType(classTypeModel, module.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS));
        Type mappedType = queryTypeFactory.create(classModel);
        entityToWrapped.put(classModel, mappedType);
        typeMappings.register(classModel, mappedType);
    }
    classModel.getData().put("schema", schemaAndTable.getSchema());
    classModel.getData().put("table", schemaAndTable.getTable());
    return classModel;
}
Also used : EntityType(com.querydsl.codegen.EntityType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType)

Example 9 with SimpleType

use of com.querydsl.codegen.utils.model.SimpleType in project querydsl by querydsl.

the class MetaDataSerializer method serializeProperties.

@SuppressWarnings("unchecked")
@Override
protected void serializeProperties(EntityType model, SerializerConfig config, CodeWriter writer) throws IOException {
    Collection<PrimaryKeyData> primaryKeys = (Collection<PrimaryKeyData>) model.getData().get(PrimaryKeyData.class);
    Collection<ForeignKeyData> foreignKeys = (Collection<ForeignKeyData>) model.getData().get(ForeignKeyData.class);
    Collection<InverseForeignKeyData> inverseForeignKeys = (Collection<InverseForeignKeyData>) model.getData().get(InverseForeignKeyData.class);
    if (innerClassesForKeys) {
        Type primaryKeyType = new SimpleType(namingStrategy.getPrimaryKeysClassName());
        Type foreignKeysType = new SimpleType(namingStrategy.getForeignKeysClassName());
        // primary keys
        if (primaryKeys != null) {
            writer.beginClass(primaryKeyType);
            serializePrimaryKeys(model, writer, primaryKeys);
            writer.end();
        }
        // foreign keys
        if (foreignKeys != null || inverseForeignKeys != null) {
            writer.beginClass(foreignKeysType);
            if (foreignKeys != null) {
                serializeForeignKeys(model, writer, foreignKeys, false);
            }
            // inverse foreign keys
            if (inverseForeignKeys != null) {
                serializeForeignKeys(model, writer, inverseForeignKeys, true);
            }
            writer.end();
        }
        super.serializeProperties(model, config, writer);
        if (primaryKeys != null) {
            writer.publicFinal(primaryKeyType, namingStrategy.getPrimaryKeysVariable(model), "new " + primaryKeyType.getSimpleName() + "()");
        }
        if (foreignKeys != null || inverseForeignKeys != null) {
            writer.publicFinal(foreignKeysType, namingStrategy.getForeignKeysVariable(model), "new " + foreignKeysType.getSimpleName() + "()");
        }
    } else {
        super.serializeProperties(model, config, writer);
        // primary keys
        if (primaryKeys != null) {
            serializePrimaryKeys(model, writer, primaryKeys);
        }
        // foreign keys
        if (foreignKeys != null) {
            serializeForeignKeys(model, writer, foreignKeys, false);
        }
        // inverse foreign keys
        if (inverseForeignKeys != null) {
            serializeForeignKeys(model, writer, inverseForeignKeys, true);
        }
    }
}
Also used : PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) SimpleType(com.querydsl.codegen.utils.model.SimpleType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData) Collection(java.util.Collection) ForeignKeyData(com.querydsl.sql.codegen.support.ForeignKeyData) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData)

Example 10 with SimpleType

use of com.querydsl.codegen.utils.model.SimpleType in project querydsl by querydsl.

the class AbstractMetaDataExportMojo method execute.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (isForTest()) {
        project.addTestCompileSourceRoot(targetFolder);
    } else {
        project.addCompileSourceRoot(targetFolder);
    }
    if (skip) {
        return;
    }
    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) {
            if (serializerClass == null) {
                serializerClass = "com.querydsl.scala.sql.ScalaMetaDataSerializer";
            }
            if (exportBeans && beanSerializerClass == null) {
                beanSerializerClass = "com.querydsl.scala.ScalaBeanSerializer";
            }
        }
        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);
        Class.forName(jdbcDriver);
        String user;
        String password;
        if (server == null) {
            user = jdbcUser;
            password = jdbcPassword;
        } else {
            AuthenticationInfo info = wagonManager.getAuthenticationInfo(server);
            if (info == null) {
                throw new MojoExecutionException("No authentication info for server " + server);
            }
            user = info.getUserName();
            if (user == null) {
                throw new MojoExecutionException("Missing username from server " + server);
            }
            password = info.getPassword();
            if (password == null) {
                throw new MojoExecutionException("Missing password from server " + server);
            }
        }
        try (Connection conn = DriverManager.getConnection(jdbcUrl, user, password)) {
            exporter.export(conn.getMetaData());
        }
    } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | SQLException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : BeanSerializer(com.querydsl.codegen.BeanSerializer) Configuration(com.querydsl.sql.Configuration) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) DefaultNamingStrategy(com.querydsl.sql.codegen.DefaultNamingStrategy) NamingStrategy(com.querydsl.sql.codegen.NamingStrategy) SimpleType(com.querydsl.codegen.utils.model.SimpleType) NumericMapping(com.querydsl.sql.codegen.support.NumericMapping) RenameMapping(com.querydsl.sql.codegen.support.RenameMapping) TypeMapping(com.querydsl.sql.codegen.support.TypeMapping) DefaultNamingStrategy(com.querydsl.sql.codegen.DefaultNamingStrategy) File(java.io.File) MetaDataExporter(com.querydsl.sql.codegen.MetaDataExporter)

Aggregations

SimpleType (com.querydsl.codegen.utils.model.SimpleType)35 Type (com.querydsl.codegen.utils.model.Type)17 Test (org.junit.Test)17 ClassType (com.querydsl.codegen.utils.model.ClassType)15 JavaWriter (com.querydsl.codegen.utils.JavaWriter)8 StringWriter (java.io.StringWriter)6 EntityType (com.querydsl.codegen.EntityType)5 Parameter (com.querydsl.codegen.utils.model.Parameter)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Before (org.junit.Before)5 File (java.io.File)3 Writer (java.io.Writer)3 Map (java.util.Map)3 BeanSerializer (com.querydsl.codegen.BeanSerializer)2 Configuration (com.querydsl.sql.Configuration)2 MetaDataExporter (com.querydsl.sql.codegen.MetaDataExporter)2 NumericMapping (com.querydsl.sql.codegen.support.NumericMapping)2 RenameMapping (com.querydsl.sql.codegen.support.RenameMapping)2 TypeMapping (com.querydsl.sql.codegen.support.TypeMapping)2 Connection (java.sql.Connection)2