Search in sources :

Example 1 with Type

use of io.requery.meta.Type in project requery by requery.

the class QueryElement method fromExpressions.

public Set<Expression<?>> fromExpressions() {
    if (from == null) {
        types = new LinkedHashSet<>();
        Set<? extends Expression<?>> expressions;
        switch(queryType) {
            case SELECT:
                expressions = getSelection();
                break;
            case INSERT:
            case UPDATE:
            case UPSERT:
                expressions = updates.keySet();
                break;
            default:
                expressions = Collections.emptySet();
        }
        for (Expression<?> expression : expressions) {
            if (expression instanceof AliasedExpression) {
                expression = ((AliasedExpression) expression).getInnerExpression();
            }
            if (expression instanceof Attribute) {
                Type type = ((Attribute) expression).getDeclaringType();
                types.add(type);
            } else if (expression instanceof Function) {
                Function function = (Function) expression;
                for (Object arg : function.arguments()) {
                    Type type = null;
                    if (arg instanceof Attribute) {
                        type = ((Attribute) arg).getDeclaringType();
                        types.add(type);
                    } else if (arg instanceof Class) {
                        type = model.typeOf((Class) arg);
                    }
                    if (type != null) {
                        types.add(type);
                    }
                }
            }
        }
        if (from == null) {
            from = new LinkedHashSet<>();
        }
        if (!types.isEmpty()) {
            from.addAll(types);
        }
    }
    return from;
}
Also used : Function(io.requery.query.function.Function) Type(io.requery.meta.Type) ExpressionType(io.requery.query.ExpressionType) Attribute(io.requery.meta.Attribute) AliasedExpression(io.requery.query.AliasedExpression)

Example 2 with Type

use of io.requery.meta.Type in project requery by requery.

the class EntityMetaGenerator method generateType.

private void generateType(TypeSpec.Builder builder, TypeName targetName) {
    CodeBlock.Builder block = CodeBlock.builder().add("new $T<$T>($T.class, $S)\n", TypeBuilder.class, targetName, targetName, entity.tableName());
    block.add(".setBaseType($T.class)\n", ClassName.get(typeElement)).add(".setCacheable($L)\n", entity.isCacheable()).add(".setImmutable($L)\n", entity.isImmutable()).add(".setReadOnly($L)\n", entity.isReadOnly()).add(".setStateless($L)\n", entity.isStateless()).add(".setView($L)\n", entity.isView());
    String factoryName = entity.classFactoryName();
    if (!Names.isEmpty(factoryName)) {
        block.add(".setFactory(new $L())\n", ClassName.bestGuess(factoryName));
    } else if (entity.isImmutable()) {
        // returns this class as the builder
        TypeSpec.Builder supplier = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Supplier.class, typeName));
        supplier.addMethod(CodeGeneration.overridePublicMethod("get").returns(typeName).addStatement("return new $T()", typeName).build());
        block.add(".setBuilderFactory($L)\n", supplier.build());
        MethodSpec.Builder applyMethod = CodeGeneration.overridePublicMethod("apply").addParameter(typeName, "value").returns(targetName);
        // add embedded builder calls
        entity.attributes().values().stream().filter(AttributeDescriptor::isEmbedded).forEach(attribute -> graph.embeddedDescriptorOf(attribute).ifPresent(embedded -> embedded.builderType().ifPresent(type -> {
            String fieldName = attribute.fieldName() + "Builder";
            String methodName = attribute.setterName();
            applyMethod.addStatement("value.builder.$L(value.$L.build())", methodName, fieldName);
        })));
        applyMethod.addStatement(entity.builderType().isPresent() ? "return value.builder.build()" : "return value.build()");
        TypeSpec.Builder buildFunction = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Function.class, typeName, targetName)).addMethod(applyMethod.build());
        block.add(".setBuilderFunction($L)\n", buildFunction.build());
    } else {
        TypeSpec.Builder typeFactory = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Supplier.class, targetName)).addMethod(CodeGeneration.overridePublicMethod("get").addStatement("return new $T()", targetName).returns(targetName).build());
        block.add(".setFactory($L)\n", typeFactory.build());
    }
    ParameterizedTypeName proxyType = parameterizedTypeName(EntityProxy.class, targetName);
    TypeSpec.Builder proxyProvider = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Function.class, targetName, proxyType));
    MethodSpec.Builder proxyFunction = CodeGeneration.overridePublicMethod("apply").addParameter(targetName, "entity").returns(proxyType);
    if (entity.isImmutable() || entity.isUnimplementable()) {
        proxyFunction.addStatement("return new $T(entity, $L)", proxyType, TYPE_NAME);
    } else {
        proxyFunction.addStatement("return entity.$L", PROXY_NAME);
    }
    proxyProvider.addMethod(proxyFunction.build());
    block.add(".setProxyProvider($L)\n", proxyProvider.build());
    if (entity.tableAttributes().length > 0) {
        StringJoiner joiner = new StringJoiner(",", "new String[] {", "}");
        for (String attribute : entity.tableAttributes()) {
            joiner.add("\"" + attribute + "\"");
        }
        block.add(".setTableCreateAttributes($L)\n", joiner.toString());
    }
    if (entity.tableUniqueIndexes().length > 0) {
        StringJoiner joiner = new StringJoiner(",", "new String[] {", "}");
        for (String attribute : entity.tableUniqueIndexes()) {
            joiner.add("\"" + attribute + "\"");
        }
        block.add(".setTableUniqueIndexes($L)\n", joiner.toString());
    }
    attributeNames.forEach(name -> block.add(".addAttribute($L)\n", name));
    expressionNames.forEach(name -> block.add(".addExpression($L)\n", name));
    block.add(".build()");
    ParameterizedTypeName type = parameterizedTypeName(Type.class, targetName);
    builder.addField(FieldSpec.builder(type, TYPE_NAME, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("$L", block.build()).build());
}
Also used : EntityProxy(io.requery.proxy.EntityProxy) Modifier(javax.lang.model.element.Modifier) BooleanProperty(io.requery.proxy.BooleanProperty) FieldSpec(com.squareup.javapoet.FieldSpec) LongProperty(io.requery.proxy.LongProperty) VariableElement(javax.lang.model.element.VariableElement) ClassName(com.squareup.javapoet.ClassName) QueryAttribute(io.requery.meta.QueryAttribute) TypeBuilder(io.requery.meta.TypeBuilder) TypeElement(javax.lang.model.element.TypeElement) ReferentialAction(io.requery.ReferentialAction) HashSet(java.util.HashSet) Function(io.requery.util.function.Function) ByteProperty(io.requery.proxy.ByteProperty) Diagnostic(javax.tools.Diagnostic) Type(io.requery.meta.Type) FloatProperty(io.requery.proxy.FloatProperty) ElementFilter(javax.lang.model.util.ElementFilter) LinkedList(java.util.LinkedList) CodeBlock(com.squareup.javapoet.CodeBlock) Property(io.requery.proxy.Property) MethodSpec(com.squareup.javapoet.MethodSpec) IntProperty(io.requery.proxy.IntProperty) ExecutableElement(javax.lang.model.element.ExecutableElement) Supplier(io.requery.util.function.Supplier) Set(java.util.Set) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) IOException(java.io.IOException) Attribute(io.requery.meta.Attribute) PropertyState(io.requery.proxy.PropertyState) TypeSpec(com.squareup.javapoet.TypeSpec) Order(io.requery.query.Order) QueryExpression(io.requery.meta.QueryExpression) TypeKind(javax.lang.model.type.TypeKind) CascadeAction(io.requery.CascadeAction) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Cardinality(io.requery.meta.Cardinality) DoubleProperty(io.requery.proxy.DoubleProperty) StringJoiner(java.util.StringJoiner) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) ShortProperty(io.requery.proxy.ShortProperty) MethodSpec(com.squareup.javapoet.MethodSpec) TypeBuilder(io.requery.meta.TypeBuilder) CodeBlock(com.squareup.javapoet.CodeBlock) Function(io.requery.util.function.Function) Supplier(io.requery.util.function.Supplier) StringJoiner(java.util.StringJoiner) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 3 with Type

use of io.requery.meta.Type in project requery by requery.

the class QueryBuilder method tableNames.

public QueryBuilder tableNames(Iterable<Expression<?>> values) {
    Set<Type<?>> types = new LinkedHashSet<>();
    for (Expression<?> expression : values) {
        if (expression.getExpressionType() == ExpressionType.ATTRIBUTE) {
            Attribute attribute = (Attribute) expression;
            types.add(attribute.getDeclaringType());
        }
    }
    return commaSeparated(types, new Appender<Type<?>>() {

        @Override
        public void append(QueryBuilder qb, Type<?> value) {
            tableName(value.getName());
        }
    });
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ExpressionType(io.requery.query.ExpressionType) Type(io.requery.meta.Type) Attribute(io.requery.meta.Attribute)

Example 4 with Type

use of io.requery.meta.Type in project requery by requery.

the class SerializableEntityCache method getCache.

private Cache getCache(Class<?> type) {
    Cache cache;
    synchronized (caches) {
        cache = caches.get(type);
        if (cache == null) {
            Type declaredType = model.typeOf(type);
            String cacheName = getCacheName(declaredType);
            Class keyClass = getKeyClass(declaredType);
            cache = cacheManager.getCache(cacheName, keyClass, SerializedEntity.class);
        }
    }
    return cache;
}
Also used : Type(io.requery.meta.Type) EntityCache(io.requery.EntityCache) Cache(javax.cache.Cache)

Example 5 with Type

use of io.requery.meta.Type in project requery by requery.

the class SchemaModifier method createTables.

/**
     * Create the tables over the connection.
     *
     * @param mode creation mode.
     * @throws TableModificationException if the creation fails.
     */
public void createTables(TableCreationMode mode) {
    ArrayList<Type<?>> sorted = sortTypes();
    try (Connection connection = getConnection();
        Statement statement = connection.createStatement()) {
        connection.setAutoCommit(false);
        if (mode == TableCreationMode.DROP_CREATE) {
            executeDropStatements(statement);
        }
        for (Type<?> type : sorted) {
            String sql = tableCreateStatement(type, mode);
            statementListeners.beforeExecuteUpdate(statement, sql, null);
            statement.execute(sql);
            statementListeners.afterExecuteUpdate(statement, 0);
        }
        for (Type<?> type : sorted) {
            createIndexes(connection, mode, type);
        }
        connection.commit();
    } catch (SQLException e) {
        throw new TableModificationException(e);
    }
}
Also used : Type(io.requery.meta.Type) IntegerType(io.requery.sql.type.IntegerType) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection)

Aggregations

Type (io.requery.meta.Type)6 Attribute (io.requery.meta.Attribute)3 ExpressionType (io.requery.query.ExpressionType)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 ClassName (com.squareup.javapoet.ClassName)1 CodeBlock (com.squareup.javapoet.CodeBlock)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 CascadeAction (io.requery.CascadeAction)1 EntityCache (io.requery.EntityCache)1 ReferentialAction (io.requery.ReferentialAction)1 Cardinality (io.requery.meta.Cardinality)1 QueryAttribute (io.requery.meta.QueryAttribute)1 QueryExpression (io.requery.meta.QueryExpression)1 TypeBuilder (io.requery.meta.TypeBuilder)1 BooleanProperty (io.requery.proxy.BooleanProperty)1