Search in sources :

Example 6 with Attribute

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

the class GenericMapping method write.

@SuppressWarnings("unchecked")
@Override
public <A> void write(Expression<A> expression, PreparedStatement statement, int index, A value) throws SQLException {
    Class<?> type;
    Converter converter = null;
    FieldType fieldType;
    if (expression.getExpressionType() == ExpressionType.ATTRIBUTE) {
        Attribute<?, A> attribute = (Attribute) expression;
        converter = attribute.getConverter();
        fieldType = mapAttribute(attribute);
        type = attribute.isAssociation() ? attribute.getReferencedAttribute().get().getClassType() : attribute.getClassType();
    } else {
        type = expression.getClassType();
        fieldType = getSubstitutedType(type);
    }
    if (converter == null && !type.isPrimitive()) {
        converter = converterForType(type);
    }
    Object converted = value;
    if (converter != null) {
        converted = converter.convertToPersisted(value);
    }
    fieldType.write(statement, index, converted);
}
Also used : Attribute(io.requery.meta.Attribute) ZonedDateTimeConverter(io.requery.converter.ZonedDateTimeConverter) LocalDateConverter(io.requery.converter.LocalDateConverter) LocalDateTimeConverter(io.requery.converter.LocalDateTimeConverter) EnumStringConverter(io.requery.converter.EnumStringConverter) URLConverter(io.requery.converter.URLConverter) URIConverter(io.requery.converter.URIConverter) CurrencyConverter(io.requery.converter.CurrencyConverter) UUIDConverter(io.requery.converter.UUIDConverter) OffsetDateTimeConverter(io.requery.converter.OffsetDateTimeConverter) LocalTimeConverter(io.requery.converter.LocalTimeConverter) Converter(io.requery.Converter)

Example 7 with Attribute

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

the class EntityReader method fromBuilder.

final <B> E fromBuilder(ResultSet results, Attribute[] selection) throws SQLException {
    EntityBuilderProxy<B, E> proxy = new EntityBuilderProxy<>(type);
    int index = 1;
    for (Attribute expression : selection) {
        @SuppressWarnings("unchecked") Attribute<E, ?> attribute = (Attribute<E, ?>) expression;
        if (attribute.getPrimitiveKind() != null) {
            readPrimitiveField(proxy, attribute, results, index);
        } else {
            Object value = mapping.read((Expression) attribute, results, index);
            proxy.setObject(attribute, value, PropertyState.LOADED);
        }
        index++;
    }
    return proxy.build();
}
Also used : EntityBuilderProxy(io.requery.proxy.EntityBuilderProxy) WHERE(io.requery.sql.Keyword.WHERE) QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute)

Example 8 with Attribute

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

the class SchemaModifier method tableCreateStatement.

/**
 * Generates the create table for a specific type.
 *
 * @param type to generate the table statement
 * @param mode creation mode
 * @param <T> Type
 * @return create table sql string
 */
public <T> String tableCreateStatement(Type<T> type, TableCreationMode mode) {
    String tableName = type.getName();
    QueryBuilder qb = createQueryBuilder();
    qb.keyword(CREATE);
    if (type.getTableCreateAttributes() != null) {
        for (String attribute : type.getTableCreateAttributes()) {
            qb.append(attribute, true);
        }
    }
    qb.keyword(TABLE);
    if (mode == TableCreationMode.CREATE_NOT_EXISTS) {
        qb.keyword(IF, NOT, EXISTS);
    }
    qb.tableName(tableName);
    qb.openParenthesis();
    int index = 0;
    // columns to define first
    Predicate<Attribute> filter = new Predicate<Attribute>() {

        @Override
        public boolean test(Attribute value) {
            if (value.isVersion() && !platform.versionColumnDefinition().createColumn()) {
                return false;
            }
            if (platform.supportsInlineForeignKeyReference()) {
                return !value.isForeignKey() && !value.isAssociation();
            } else {
                return value.isForeignKey() || !value.isAssociation();
            }
        }
    };
    Set<Attribute<T, ?>> attributes = type.getAttributes();
    for (Attribute attribute : attributes) {
        if (filter.test(attribute)) {
            if (index > 0) {
                qb.comma();
            }
            createColumn(qb, attribute);
            index++;
        }
    }
    // foreign keys
    for (Attribute attribute : attributes) {
        if (attribute.isForeignKey()) {
            if (index > 0) {
                qb.comma();
            }
            createForeignKeyColumn(qb, attribute, true, false);
            index++;
        }
    }
    // composite primary key
    if (type.getKeyAttributes().size() > 1) {
        if (index > 0) {
            qb.comma();
        }
        qb.keyword(PRIMARY, KEY);
        qb.openParenthesis();
        qb.commaSeparated(type.getKeyAttributes(), new QueryBuilder.Appender<Attribute<T, ?>>() {

            @Override
            public void append(QueryBuilder qb, Attribute<T, ?> value) {
                qb.attribute(value);
            }
        });
        qb.closeParenthesis();
    }
    qb.closeParenthesis();
    return qb.toString();
}
Also used : Attribute(io.requery.meta.Attribute) Predicate(io.requery.util.function.Predicate)

Example 9 with Attribute

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

the class SchemaModifier method createForeignKeyColumn.

private void createForeignKeyColumn(QueryBuilder qb, Attribute<?, ?> attribute, boolean forCreateStatement, boolean forceInline) {
    Type<?> referenced = model.typeOf(attribute.getReferencedClass() != null ? attribute.getReferencedClass() : attribute.getClassType());
    final Attribute referencedAttribute;
    if (attribute.getReferencedAttribute() != null) {
        referencedAttribute = attribute.getReferencedAttribute().get();
    } else if (!referenced.getKeyAttributes().isEmpty()) {
        referencedAttribute = referenced.getKeyAttributes().iterator().next();
    } else {
        referencedAttribute = null;
    }
    if (!forceInline && (!platform.supportsInlineForeignKeyReference() || !forCreateStatement)) {
        qb.keyword(FOREIGN, KEY).openParenthesis().attribute(attribute).closeParenthesis().space();
    } else {
        qb.attribute(attribute);
        FieldType fieldType = null;
        if (referencedAttribute != null) {
            fieldType = mapping.mapAttribute(referencedAttribute);
        }
        if (fieldType == null) {
            fieldType = new IntegerType(int.class);
        }
        qb.value(fieldType.getIdentifier());
    }
    qb.keyword(REFERENCES);
    qb.tableName(referenced.getName());
    if (referencedAttribute != null) {
        qb.openParenthesis().attribute(referencedAttribute).closeParenthesis().space();
    }
    if (attribute.getDeleteAction() != null) {
        qb.keyword(ON, DELETE);
        appendReferentialAction(qb, attribute.getDeleteAction());
    }
    if (platform.supportsOnUpdateCascade() && referencedAttribute != null && !referencedAttribute.isGenerated() && attribute.getUpdateAction() != null) {
        qb.keyword(ON, UPDATE);
        appendReferentialAction(qb, attribute.getUpdateAction());
    }
    if (platform.supportsInlineForeignKeyReference()) {
        if (!attribute.isNullable()) {
            qb.keyword(NOT, NULL);
        }
        if (attribute.isUnique()) {
            qb.keyword(UNIQUE);
        }
    }
}
Also used : IntegerType(io.requery.sql.type.IntegerType) Attribute(io.requery.meta.Attribute)

Example 10 with Attribute

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

the class SelectResult method createIterator.

@Override
public CloseableIterator<E> createIterator(int skip, int take) {
    Statement statement = null;
    try {
        // connection held by the iterator if statement not reused
        BoundParameters parameters = createQuery(skip, take);
        statement = createStatement(!parameters.isEmpty());
        statement.setFetchSize(limit == null ? 0 : limit);
        StatementListener listener = configuration.getStatementListener();
        listener.beforeExecuteQuery(statement, sql, parameters);
        ResultSet results;
        if (parameters.isEmpty()) {
            results = statement.executeQuery(sql);
        } else {
            PreparedStatement preparedStatement = (PreparedStatement) statement;
            Mapping mapping = configuration.getMapping();
            for (int i = 0; i < parameters.count(); i++) {
                Expression expression = parameters.expressionAt(i);
                Object value = parameters.valueAt(i);
                if (expression instanceof Attribute) {
                    // extract foreign key reference
                    Attribute attribute = (Attribute) expression;
                    if (attribute.isAssociation() && (attribute.isForeignKey() || attribute.isKey())) {
                        // get the referenced value
                        if (value != null && ((Expression<?>) expression).getClassType().isAssignableFrom(value.getClass())) {
                            value = Attributes.replaceKeyReference(value, attribute);
                        }
                    }
                }
                mapping.write(expression, preparedStatement, i + 1, value);
            }
            results = preparedStatement.executeQuery();
        }
        listener.afterExecuteQuery(statement);
        return new ResultSetIterator<>(reader, results, selection, true, closeConnection);
    } catch (Exception e) {
        throw StatementExecutionException.closing(statement, e, sql);
    }
}
Also used : Expression(io.requery.query.Expression) Attribute(io.requery.meta.Attribute) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

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