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());
}
}
}
Aggregations