Search in sources :

Example 26 with SimpleType

use of com.querydsl.codegen.utils.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.querydsl.codegen.utils.model.SimpleType)

Example 27 with SimpleType

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

the class SpatialSupport method registerJTSTypes.

private static void registerJTSTypes(TypeMappings typeMappings) {
    Map<String, String> additions = new HashMap<>();
    additions.put("Geometry", "JTSGeometryPath");
    additions.put("GeometryCollection", "JTSGeometryCollectionPath");
    additions.put("LinearRing", "JTSLinearRingPath");
    additions.put("LineString", "JTSLineStringPath");
    additions.put("MultiLineString", "JTSMultiLineStringPath");
    additions.put("MultiPoint", "JTSMultiPointPath");
    additions.put("MultiPolygon", "JTSMultiPolygonPath");
    additions.put("Point", "JTSPointPath");
    additions.put("Polygon", "JTSPolygonPath");
    for (Map.Entry<String, String> entry : additions.entrySet()) {
        typeMappings.register(new SimpleType("com.vividsolutions.jts.geom." + entry.getKey()), new SimpleType("com.querydsl.spatial.jts." + entry.getValue()));
        typeMappings.register(new SimpleType("org.locationtech.jts.geom." + entry.getKey()), new SimpleType("com.querydsl.spatial.locationtech.jts." + entry.getValue()));
    }
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 28 with SimpleType

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

the class DefaultEvaluatorFactory method createEvaluator.

/**
 * Create an Evaluator for the given sources and the given optional filter
 *
 * @param metadata query metadata
 * @param joins joins
 * @param filter where condition
 * @return evaluator
 */
public Evaluator<List<Object[]>> createEvaluator(QueryMetadata metadata, List<JoinExpression> joins, @Nullable Predicate filter) {
    List<String> sourceNames = new ArrayList<String>();
    List<Type> sourceTypes = new ArrayList<Type>();
    List<Class<?>> sourceClasses = new ArrayList<Class<?>>();
    StringBuilder vars = new StringBuilder();
    CollQuerySerializer ser = new CollQuerySerializer(templates);
    ser.append("java.util.List<Object[]> rv = new java.util.ArrayList<Object[]>();\n");
    List<String> anyJoinMatchers = new ArrayList<String>();
    // creating context
    for (JoinExpression join : joins) {
        Expression<?> target = join.getTarget();
        String typeName = com.querydsl.codegen.utils.support.ClassUtils.getName(target.getType());
        if (vars.length() > 0) {
            vars.append(",");
        }
        switch(join.getType()) {
            case DEFAULT:
                ser.append("for (" + typeName + " " + target + " : " + target + "_) {\n");
                vars.append(target);
                sourceNames.add(target + "_");
                sourceTypes.add(new SimpleType(Types.ITERABLE, new ClassType(TypeCategory.SIMPLE, target.getType())));
                sourceClasses.add(Iterable.class);
                break;
            case INNERJOIN:
            case LEFTJOIN:
                Operation<?> alias = (Operation<?>) join.getTarget();
                boolean colAnyJoin = join.getCondition() != null && join.getCondition().toString().equals("any");
                boolean leftJoin = join.getType() == JoinType.LEFTJOIN;
                String matcher = null;
                if (colAnyJoin) {
                    matcher = alias.getArg(1).toString() + "_matched";
                    ser.append("boolean " + matcher + " = false;\n");
                    anyJoinMatchers.add(matcher);
                }
                ser.append("for (" + typeName + " " + alias.getArg(1) + " : ");
                if (leftJoin) {
                    ser.append(CollQueryFunctions.class.getName() + ".leftJoin(");
                }
                if (colAnyJoin) {
                    Context context = new Context();
                    Expression<?> replacement = alias.getArg(0).accept(collectionAnyVisitor, context);
                    ser.handle(replacement);
                } else {
                    ser.handle(alias.getArg(0));
                }
                if (alias.getArg(0).getType().equals(Map.class)) {
                    ser.append(".values()");
                }
                if (leftJoin) {
                    ser.append(")");
                }
                ser.append(") {\n");
                if (matcher != null) {
                    ser.append("if (!" + matcher + ") {\n");
                }
                vars.append(alias.getArg(1));
                break;
            default:
                throw new IllegalArgumentException("Illegal join expression " + join);
        }
    }
    // filter
    if (filter != null) {
        ser.append("try {\n");
        ser.append("if (");
        ser.handle(filter).append(") {\n");
        for (String matcher : anyJoinMatchers) {
            ser.append("    " + matcher + " = true;\n");
        }
        ser.append("    rv.add(new Object[]{" + vars + "});\n");
        ser.append("}\n");
        ser.append("} catch (NullPointerException npe) { }\n");
    } else {
        ser.append("rv.add(new Object[]{" + vars + "});\n");
    }
    // closing context
    int amount = joins.size() + anyJoinMatchers.size();
    for (int i = 0; i < amount; i++) {
        ser.append("}\n");
    }
    ser.append("return rv;");
    Map<Object, String> constantToLabel = ser.getConstantToLabel();
    Map<String, Object> constants = getConstants(metadata, constantToLabel);
    ClassType projectionType = new ClassType(TypeCategory.LIST, List.class, Types.OBJECTS);
    return factory.createEvaluator(ser.toString(), projectionType, sourceNames.toArray(new String[0]), sourceTypes.toArray(new Type[0]), sourceClasses.toArray(new Class<?>[0]), constants);
}
Also used : Context(com.querydsl.core.support.Context) ArrayList(java.util.ArrayList) Operation(com.querydsl.core.types.Operation) ClassType(com.querydsl.codegen.utils.model.ClassType) 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) JoinType(com.querydsl.core.JoinType) JoinExpression(com.querydsl.core.JoinExpression)

Example 29 with SimpleType

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

the class PackageSuffixTest method correct_imports.

@Test
public void correct_imports() throws IOException {
    SimpleType type = new SimpleType(TypeCategory.ENTITY, "test.Entity", "test", "Entity", false, false);
    EntityType entityType = new EntityType(type);
    typeMappings.register(entityType, queryTypeFactory.create(entityType));
    serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    assertTrue(writer.toString().contains("import test.Entity;"));
    assertTrue(writer.toString().contains("public class QEntity extends EntityPathBase<Entity> {"));
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) JavaWriter(com.querydsl.codegen.utils.JavaWriter) Test(org.junit.Test)

Example 30 with SimpleType

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

the class ProjectionSerializerTest method customGeneratedAnnotation.

@Test
public void customGeneratedAnnotation() throws IOException {
    Type typeModel = new SimpleType(TypeCategory.ENTITY, "com.querydsl.DomainClass", "com.querydsl", "DomainClass", false, false);
    EntityType type = new EntityType(typeModel);
    Writer writer = new StringWriter();
    ProjectionSerializer serializer = new DefaultProjectionSerializer(new JavaTypeMappings(), Generated.class);
    serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    String generatedSource = writer.toString();
    assertThat(generatedSource, containsString("import com.querydsl.core.annotations.Generated"));
    assertThat(generatedSource, containsString("@Generated(\"com.querydsl.codegen.DefaultProjectionSerializer\")\npublic class"));
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) SimpleType(com.querydsl.codegen.utils.model.SimpleType) StringWriter(java.io.StringWriter) JavaWriter(com.querydsl.codegen.utils.JavaWriter) Matchers.containsString(org.hamcrest.Matchers.containsString) StringWriter(java.io.StringWriter) Writer(java.io.Writer) JavaWriter(com.querydsl.codegen.utils.JavaWriter) 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