Search in sources :

Example 11 with Attribute

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

the class PreparedQueryOperation method mapParameters.

void mapParameters(PreparedStatement statement, BoundParameters parameters) throws SQLException {
    for (int i = 0; i < parameters.count(); i++) {
        Expression expression = parameters.expressionAt(i);
        Object value = parameters.valueAt(i);
        if (expression instanceof Attribute) {
            Attribute attribute = (Attribute) expression;
            if (attribute.isAssociation()) {
                // get the referenced value
                value = Attributes.replaceKeyReference(value, attribute);
            }
        }
        Class<?> type = value == null ? null : value.getClass();
        if (type != null) {
            // allows entity arguments with single keys to be remapped to their keys
            if (model.containsTypeOf(type)) {
                Type<Object> entityType = model.typeOf(type);
                Attribute<Object, ?> keyAttribute = entityType.getSingleKeyAttribute();
                if (keyAttribute != null) {
                    value = keyAttribute.getProperty().get(value);
                    expression = (Expression) keyAttribute;
                }
            }
        }
        configuration.getMapping().write(expression, statement, i + 1, value);
    }
}
Also used : Expression(io.requery.query.Expression) Attribute(io.requery.meta.Attribute)

Example 12 with Attribute

use of io.requery.meta.Attribute 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 13 with Attribute

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

the class DefaultOutput method appendColumnExpression.

private void appendColumnExpression(Expression expression) {
    switch(expression.getExpressionType()) {
        case ATTRIBUTE:
            Attribute attribute = (Attribute) expression;
            qb.attribute(attribute);
            break;
        default:
            if (expression instanceof RowExpression) {
                RowExpression collection = (RowExpression) expression;
                qb.openParenthesis();
                qb.commaSeparated(collection.getExpressions(), new QueryBuilder.Appender<Expression<?>>() {

                    @Override
                    public void append(QueryBuilder qb, Expression<?> value) {
                        appendColumnForSelect(value);
                    }
                });
                qb.closeParenthesis().space();
            } else {
                qb.append(expression.getName()).space();
            }
            break;
    }
}
Also used : QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute) Expression(io.requery.query.Expression) NamedExpression(io.requery.query.NamedExpression) RowExpression(io.requery.query.RowExpression) RowExpression(io.requery.query.RowExpression) QueryBuilder(io.requery.sql.QueryBuilder)

Example 14 with Attribute

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

the class InsertGenerator method write.

@Override
public void write(final Output output, QueryElement<?> query) {
    Map<Expression<?>, Object> values = query.updateValues();
    InsertType insertType = query.insertType();
    QueryBuilder qb = output.builder();
    qb.keyword(INSERT, INTO);
    output.appendTables();
    if (values.isEmpty()) {
        if (insertType == InsertType.VALUES) {
            qb.keyword(DEFAULT, VALUES);
        }
    } else {
        qb.openParenthesis().commaSeparated(values.entrySet(), new QueryBuilder.Appender<Map.Entry<Expression<?>, Object>>() {

            @Override
            public void append(QueryBuilder qb, Map.Entry<Expression<?>, Object> value) {
                Expression<?> key = value.getKey();
                switch(key.getExpressionType()) {
                    case ATTRIBUTE:
                        Attribute attribute = (Attribute) key;
                        if (attribute.isGenerated()) {
                            throw new IllegalStateException();
                        }
                        qb.attribute(attribute);
                        break;
                    default:
                        qb.append(key.getName()).space();
                        break;
                }
            }
        }).closeParenthesis().space();
        if (insertType == InsertType.VALUES) {
            qb.keyword(VALUES).openParenthesis().commaSeparated(values.entrySet(), new QueryBuilder.Appender<Map.Entry<Expression<?>, Object>>() {

                @Override
                public void append(QueryBuilder qb, Map.Entry<Expression<?>, Object> value) {
                    output.appendConditionValue(value.getKey(), value.getValue());
                }
            }).closeParenthesis();
        } else {
            output.appendQuery(query.subQuery());
        }
    }
}
Also used : Attribute(io.requery.meta.Attribute) QueryBuilder(io.requery.sql.QueryBuilder) Expression(io.requery.query.Expression) InsertType(io.requery.query.element.InsertType) Map(java.util.Map)

Example 15 with Attribute

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

the class EntityWriter method insert.

void insert(final E entity, EntityProxy<E> proxy, Cascade mode, GeneratedKeys<E> keys) {
    // if the type is immutable return the key(s) to the caller instead of modifying the object
    GeneratedResultReader keyReader = null;
    if (hasGeneratedKey) {
        final Settable<E> settable = keys == null ? proxy : keys;
        keyReader = new GeneratedResultReader() {

            @Override
            public void read(int index, ResultSet results) throws SQLException {
                if (results.next()) {
                    readGeneratedKeys(settable, results);
                }
            }

            @Override
            public String[] generatedColumns() {
                return generatedColumnNames;
            }
        };
    }
    EntityUpdateOperation insert = new EntityUpdateOperation(context, keyReader) {

        @Override
        public int bindParameters(PreparedStatement statement) throws SQLException {
            return EntityWriter.this.bindParameters(statement, entity, null);
        }
    };
    QueryElement<Scalar<Integer>> query = new QueryElement<>(QueryType.INSERT, model, insert);
    query.from(entityClass);
    for (Attribute<E, ?> attribute : associativeAttributes) {
        // persist the foreign key object if needed
        cascadeKeyReference(Cascade.INSERT, proxy, attribute);
    }
    incrementVersion(proxy);
    for (Attribute attribute : bindableAttributes) {
        query.value((Expression) attribute, null);
    }
    context.getStateListener().preInsert(entity, proxy);
    checkRowsAffected(query.get().value(), entity, null);
    proxy.link(context.read(entityClass));
    updateAssociations(mode, entity, proxy, null);
    context.getStateListener().postInsert(entity, proxy);
    // cache entity
    if (cacheable) {
        cache.put(entityClass, proxy.key(), entity);
    }
}
Also used : UPDATE(io.requery.query.element.QueryType.UPDATE) SQLException(java.sql.SQLException) QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute) PreparedStatement(java.sql.PreparedStatement) QueryElement(io.requery.query.element.QueryElement) Scalar(io.requery.query.Scalar) ResultSet(java.sql.ResultSet)

Aggregations

Attribute (io.requery.meta.Attribute)30 QueryAttribute (io.requery.meta.QueryAttribute)13 ClassName (com.squareup.javapoet.ClassName)5 FieldSpec (com.squareup.javapoet.FieldSpec)5 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)5 TypeName (com.squareup.javapoet.TypeName)5 TypeSpec (com.squareup.javapoet.TypeSpec)5 EntityProxy (io.requery.proxy.EntityProxy)5 Expression (io.requery.query.Expression)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 MethodSpec (com.squareup.javapoet.MethodSpec)4 PropertyState (io.requery.proxy.PropertyState)4 UPDATE (io.requery.query.element.QueryType.UPDATE)4 StringJoiner (java.util.StringJoiner)4 TypeElement (javax.lang.model.element.TypeElement)4 TypeMirror (javax.lang.model.type.TypeMirror)4 CodeBlock (com.squareup.javapoet.CodeBlock)3