Search in sources :

Example 11 with SimpleType

use of com.querydsl.codegen.utils.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 (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);
        exporter.export(dbConn.getMetaData());
    } catch (RuntimeException | SQLException | ClassNotFoundException | IllegalAccessException | InstantiationException e) {
        throw new BuildException(e);
    } finally {
        if (dbConn != null) {
            try {
                dbConn.close();
            } catch (SQLException e2) {
                throw new BuildException(e2);
            }
        }
    }
}
Also used : CustomType(com.querydsl.sql.codegen.support.CustomType) BeanSerializer(com.querydsl.codegen.BeanSerializer) Configuration(com.querydsl.sql.Configuration) SQLException(java.sql.SQLException) Connection(java.sql.Connection) 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) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) MetaDataExporter(com.querydsl.sql.codegen.MetaDataExporter)

Example 12 with SimpleType

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

the class SpatialSupport method registerTypes.

private static void registerTypes(TypeMappings typeMappings) {
    Map<String, String> additions = new HashMap<>();
    additions.put("Geometry", "GeometryPath");
    additions.put("GeometryCollection", "GeometryCollectionPath");
    additions.put("LinearRing", "LinearRingPath");
    additions.put("LineString", "LineStringPath");
    additions.put("MultiLineString", "MultiLineStringPath");
    additions.put("MultiPoint", "MultiPointPath");
    additions.put("MultiPolygon", "MultiPolygonPath");
    additions.put("Point", "PointPath");
    additions.put("Polygon", "PolygonPath");
    for (Map.Entry<String, String> entry : additions.entrySet()) {
        typeMappings.register(new SimpleType("org.geolatte.geom." + entry.getKey()), new SimpleType("com.querydsl.spatial." + entry.getValue()));
    }
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 13 with SimpleType

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

the class JPADomainExporter method handleProperty.

private void handleProperty(EntityType entityType, Class<?> cl, Attribute<?, ?> p) throws NoSuchMethodException, ClassNotFoundException {
    Class<?> clazz = Object.class;
    try {
        clazz = p.getJavaType();
    } catch (MappingException e) {
    // ignore
    }
    Type propertyType = getType(cl, clazz, p.getName());
    AnnotatedElement annotated = getAnnotatedElement(cl, p.getName());
    propertyType = getTypeOverride(propertyType, annotated);
    if (propertyType == null) {
        return;
    }
    if (p.isCollection()) {
        if (p instanceof MapAttribute) {
            MapAttribute<?, ?, ?> map = (MapAttribute<?, ?, ?>) p;
            Type keyType = typeFactory.get(map.getKeyJavaType());
            Type valueType = typeFactory.get(map.getElementType().getJavaType());
            valueType = getPropertyType(p, valueType);
            propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), keyType), normalize(propertyType.getParameters().get(1), valueType));
        } else {
            Type valueType = typeFactory.get(((PluralAttribute<?, ?, ?>) p).getElementType().getJavaType());
            valueType = getPropertyType(p, valueType);
            propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), valueType));
        }
    } else {
        propertyType = getPropertyType(p, propertyType);
    }
    Property property = createProperty(entityType, p.getName(), propertyType, annotated);
    entityType.addProperty(property);
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) AnnotatedElement(java.lang.reflect.AnnotatedElement) Property(com.querydsl.codegen.Property) MappingException(org.hibernate.MappingException)

Example 14 with SimpleType

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

the class CustomTypeTest method customType.

@Test
public void customType() throws IOException {
    SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity", false, false);
    EntityType entityType = new EntityType(type);
    entityType.addProperty(new Property(entityType, "property", new ClassType(Double[].class)));
    typeMappings.register(new ClassType(Double[].class), new ClassType(Point.class));
    typeMappings.register(entityType, queryTypeFactory.create(entityType));
    assertTrue(typeMappings.isRegistered(entityType.getProperties().iterator().next().getType()));
    serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    assertTrue(writer.toString().contains("public final com.querydsl.codegen.Point property = " + "new com.querydsl.codegen.Point(forProperty(\"property\"));"));
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) JavaWriter(com.querydsl.codegen.utils.JavaWriter) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Example 15 with SimpleType

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

the class BeanSerializerTest method properties.

@Test
public void properties() throws IOException {
    // property
    type.addProperty(new Property(type, "entityField", type));
    type.addProperty(new Property(type, "collection", new SimpleType(Types.COLLECTION, typeModel)));
    type.addProperty(new Property(type, "listField", new SimpleType(Types.LIST, typeModel)));
    type.addProperty(new Property(type, "setField", new SimpleType(Types.SET, typeModel)));
    type.addProperty(new Property(type, "arrayField", new ClassType(TypeCategory.ARRAY, String[].class)));
    type.addProperty(new Property(type, "mapField", new SimpleType(Types.MAP, typeModel, typeModel)));
    for (Class<?> cl : Arrays.<Class<?>>asList(Boolean.class, Comparable.class, Integer.class, Date.class, java.sql.Date.class, java.sql.Time.class)) {
        Type classType = new ClassType(TypeCategory.get(cl.getName()), cl);
        type.addProperty(new Property(type, StringUtils.uncapitalize(cl.getSimpleName()), classType));
    }
    BeanSerializer serializer = new BeanSerializer();
    serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    String str = writer.toString();
    // System.err.println(str);
    for (String prop : Arrays.asList("String[] arrayField;", "Boolean boolean$;", "Collection<DomainClass> collection;", "Comparable comparable;", "java.util.Date date;", "DomainClass entityField;", "Integer integer;", "List<DomainClass> listField;", "Map<DomainClass, DomainClass> mapField;", "Set<DomainClass> setField;", "java.sql.Time time;")) {
        assertTrue(prop + " was not contained", str.contains(prop));
    }
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) JavaWriter(com.querydsl.codegen.utils.JavaWriter) Matchers.containsString(org.hamcrest.Matchers.containsString) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

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