Search in sources :

Example 6 with SimpleType

use of com.mysema.codegen.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 : SimpleType(com.mysema.codegen.model.SimpleType) ClassType(com.mysema.codegen.model.ClassType) Type(com.mysema.codegen.model.Type) SimpleType(com.mysema.codegen.model.SimpleType)

Example 7 with SimpleType

use of com.mysema.codegen.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(Strings.nullToEmpty(nameSuffix));
        exporter.setBeanPrefix(Strings.nullToEmpty(beanPrefix));
        exporter.setBeanSuffix(Strings.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.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 (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);
            }
        }
        Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
        try {
            exporter.export(conn.getMetaData());
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
    } catch (ClassNotFoundException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (SQLException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (InstantiationException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (IllegalAccessException 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.mysema.codegen.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)

Example 8 with SimpleType

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

the class AntMetaDataExporter method execute.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void execute() {
    if (targetFolder == null) {
        throw new BuildException("targetFolder is a mandatory property");
    }
    Connection dbConn = null;
    try {
        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 (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) {
            exporter.setColumnComparatorClass((Class) Class.forName(this.columnComparatorClass).asSubclass(Comparator.class));
        }
        exporter.setConfiguration(configuration);
        exporter.export(dbConn.getMetaData());
    } catch (RuntimeException e) {
        throw new BuildException(e);
    } catch (InstantiationException e) {
        throw new BuildException(e);
    } catch (IllegalAccessException e) {
        throw new BuildException(e);
    } catch (ClassNotFoundException e) {
        throw new BuildException(e);
    } catch (SQLException e) {
        throw new BuildException(e);
    } finally {
        if (dbConn != null) {
            try {
                dbConn.close();
            } catch (SQLException e2) {
                throw new BuildException(e2);
            }
        }
    }
}
Also used : BeanSerializer(com.querydsl.codegen.BeanSerializer) Configuration(com.querydsl.sql.Configuration) SQLException(java.sql.SQLException) Connection(java.sql.Connection) SimpleType(com.mysema.codegen.model.SimpleType) NumericMapping(com.querydsl.sql.codegen.support.NumericMapping) RenameMapping(com.querydsl.sql.codegen.support.RenameMapping) TypeMapping(com.querydsl.sql.codegen.support.TypeMapping) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) MetaDataExporter(com.querydsl.sql.codegen.MetaDataExporter)

Example 9 with SimpleType

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

the class MetaDataTest method setUp.

@Before
public void setUp() {
    NamingStrategy namingStrategy = new DefaultNamingStrategy();
    String packageName = "com.myproject.domain";
    String tableName = "vwServiceName";
    String className = namingStrategy.getClassName(tableName);
    Type classTypeModel = new SimpleType(TypeCategory.ENTITY, packageName + "." + className, packageName, className, false, false);
    classModel = new EntityType(classTypeModel);
    //        classModel.addAnnotation(new TableImpl(namingStrategy.normalizeTableName(tableName)));
    classModel.getData().put("table", namingStrategy.normalizeTableName(tableName));
}
Also used : EntityType(com.querydsl.codegen.EntityType) SimpleType(com.mysema.codegen.model.SimpleType) EntityType(com.querydsl.codegen.EntityType) Type(com.mysema.codegen.model.Type) SimpleType(com.mysema.codegen.model.SimpleType) Before(org.junit.Before)

Example 10 with SimpleType

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

the class KeyDataFactory method createType.

private Type createType(@Nullable String schemaName, String table) {
    SchemaAndTable schemaAndTable = new SchemaAndTable(schemaName, table);
    String packageName = this.packageName;
    if (schemaToPackage) {
        packageName = namingStrategy.getPackage(packageName, schemaAndTable);
    }
    String simpleName = prefix + namingStrategy.getClassName(schemaAndTable) + suffix;
    return new SimpleType(packageName + "." + simpleName, packageName, simpleName);
}
Also used : SchemaAndTable(com.querydsl.sql.SchemaAndTable) SimpleType(com.mysema.codegen.model.SimpleType)

Aggregations

SimpleType (com.mysema.codegen.model.SimpleType)14 Type (com.mysema.codegen.model.Type)7 Test (org.junit.Test)4 EntityType (com.querydsl.codegen.EntityType)3 JavaWriter (com.mysema.codegen.JavaWriter)2 ClassType (com.mysema.codegen.model.ClassType)2 BeanSerializer (com.querydsl.codegen.BeanSerializer)2 Property (com.querydsl.codegen.Property)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 File (java.io.File)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Map (java.util.Map)2 MappingException (org.hibernate.MappingException)2 SchemaAndTable (com.querydsl.sql.SchemaAndTable)1