Search in sources :

Example 1 with Predicate

use of io.requery.util.function.Predicate 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 2 with Predicate

use of io.requery.util.function.Predicate in project requery by requery.

the class EntityReader method refresh.

private E refresh(E entity, EntityProxy<E> proxy, final Set<Attribute<E, ?>> attributes) {
    Predicate<Attribute<E, ?>> basicFilter = new Predicate<Attribute<E, ?>>() {

        @Override
        public boolean test(Attribute<E, ?> value) {
            return attributes.contains(value) && (!value.isAssociation() || value.isForeignKey());
        }
    };
    FilteringIterator<Attribute<E, ?>> filterator = new FilteringIterator<>(attributes.iterator(), basicFilter);
    if (filterator.hasNext()) {
        QueryBuilder qb = new QueryBuilder(context.getQueryBuilderOptions()).keyword(SELECT).commaSeparated(filterator, new QueryBuilder.Appender<Attribute<E, ?>>() {

            @Override
            public void append(QueryBuilder qb, Attribute<E, ?> value) {
                String versionColumn = context.getPlatform().versionColumnDefinition().columnName();
                if (value.isVersion() && versionColumn != null) {
                    qb.append(versionColumn).space().append(AS).space().append(value.getName()).space();
                } else {
                    qb.attribute(value);
                }
            }
        }).keyword(FROM).tableName(type.getName()).keyword(WHERE).appendWhereConditions(type.getKeyAttributes());
        String sql = qb.toString();
        try (Connection connection = context.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql)) {
            int index = 1;
            for (Attribute<E, ?> attribute : type.getKeyAttributes()) {
                Object value = proxy.getKey(attribute);
                if (value == null) {
                    throw new MissingKeyException(proxy);
                }
                mapping.write((Expression) attribute, statement, index++, value);
            }
            context.getStatementListener().beforeExecuteQuery(statement, sql, null);
            ResultSet results = statement.executeQuery();
            context.getStatementListener().afterExecuteQuery(statement);
            if (results.next()) {
                Attribute[] selection = new Attribute[attributes.size()];
                attributes.toArray(selection);
                // modify the given entity
                if (type.isImmutable()) {
                    entity = fromBuilder(results, selection);
                } else {
                    entity = fromResult(entity, results, selection);
                }
            }
        } catch (SQLException e) {
            throw new PersistenceException(e);
        }
    }
    // refresh associations
    for (Attribute<E, ?> attribute : attributes) {
        // if it's a foreign key its resolved as part of the basic properties
        if (attribute.isAssociation()) {
            refreshAssociation(proxy, attribute);
        }
    }
    return entity;
}
Also used : QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute) WHERE(io.requery.sql.Keyword.WHERE) SQLException(java.sql.SQLException) FilteringIterator(io.requery.util.FilteringIterator) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Predicate(io.requery.util.function.Predicate) ResultSet(java.sql.ResultSet) PersistenceException(io.requery.PersistenceException)

Aggregations

Attribute (io.requery.meta.Attribute)2 Predicate (io.requery.util.function.Predicate)2 PersistenceException (io.requery.PersistenceException)1 QueryAttribute (io.requery.meta.QueryAttribute)1 WHERE (io.requery.sql.Keyword.WHERE)1 FilteringIterator (io.requery.util.FilteringIterator)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1