Search in sources :

Example 1 with InsertType

use of io.requery.query.element.InsertType 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)

Aggregations

Attribute (io.requery.meta.Attribute)1 Expression (io.requery.query.Expression)1 InsertType (io.requery.query.element.InsertType)1 QueryBuilder (io.requery.sql.QueryBuilder)1 Map (java.util.Map)1