use of io.requery.sql.QueryBuilder in project requery by requery.
the class UpdateGenerator method write.
@Override
public void write(final Output output, Map<Expression<?>, Object> values) {
QueryBuilder qb = output.builder();
qb.keyword(UPDATE);
output.appendTables();
qb.keyword(SET);
int index = 0;
for (Map.Entry<Expression<?>, Object> entry : values.entrySet()) {
if (index > 0) {
qb.append(",");
}
output.appendColumn(entry.getKey());
output.appendOperator(Operator.EQUAL);
output.appendConditionValue(entry.getKey(), entry.getValue());
index++;
}
}
use of io.requery.sql.QueryBuilder in project requery by requery.
the class UpsertMergeGenerator method appendUsing.
protected void appendUsing(final Output writer, final Map<Expression<?>, Object> values) {
QueryBuilder qb = writer.builder();
qb.openParenthesis().keyword(VALUES).openParenthesis().commaSeparated(values.keySet(), new QueryBuilder.Appender<Expression>() {
@Override
public void append(QueryBuilder qb, Expression expression) {
qb.append("?");
writer.parameters().add(expression, values.get(expression));
}
}).closeParenthesis().closeParenthesis().space().keyword(AS).append(alias).openParenthesis().commaSeparatedExpressions(values.keySet()).closeParenthesis().space();
}
use of io.requery.sql.QueryBuilder in project requery by requery.
the class UpsertMergeGenerator method write.
@Override
public void write(Output output, Map<Expression<?>, Object> values) {
QueryBuilder qb = output.builder();
// TODO only supporting 1 type for now
Type<?> type = null;
for (Expression<?> expression : values.keySet()) {
if (expression.getExpressionType() == ExpressionType.ATTRIBUTE) {
Attribute attribute = (Attribute) expression;
type = attribute.getDeclaringType();
break;
}
}
if (type == null) {
throw new IllegalStateException();
}
qb.keyword(MERGE).keyword(INTO).tableName(type.getName()).keyword(USING);
appendUsing(output, values);
qb.keyword(ON).openParenthesis();
int count = 0;
Set<? extends Attribute<?, ?>> attributes = type.getKeyAttributes();
if (attributes.isEmpty()) {
attributes = type.getAttributes();
}
for (Attribute<?, ?> attribute : attributes) {
if (count > 0) {
qb.keyword(Keyword.AND);
}
qb.aliasAttribute(type.getName(), attribute);
qb.append(" = ");
qb.aliasAttribute(alias, attribute);
count++;
}
qb.closeParenthesis().space();
// update fragment
LinkedHashSet<Attribute<?, ?>> updates = new LinkedHashSet<>();
for (Expression<?> expression : values.keySet()) {
if (expression.getExpressionType() == ExpressionType.ATTRIBUTE) {
Attribute attribute = (Attribute) expression;
if (!attribute.isKey()) {
updates.add(attribute);
}
}
}
qb.keyword(WHEN, MATCHED, THEN, UPDATE, SET).commaSeparated(updates, new QueryBuilder.Appender<Attribute<?, ?>>() {
@Override
public void append(QueryBuilder qb, Attribute<?, ?> value) {
qb.attribute(value);
qb.append(" = " + alias + "." + value.getName());
}
}).space();
// insert fragment
qb.keyword(WHEN, NOT, MATCHED, THEN, INSERT).openParenthesis().commaSeparatedExpressions(values.keySet()).closeParenthesis().space().keyword(VALUES).openParenthesis().commaSeparated(values.keySet(), new QueryBuilder.Appender<Expression<?>>() {
@Override
public void append(QueryBuilder qb, Expression<?> value) {
qb.aliasAttribute(alias, (Attribute) value);
}
}).closeParenthesis();
}
use of io.requery.sql.QueryBuilder in project requery by requery.
the class WhereGenerator method write.
@Override
public void write(Output output, WhereElement query) {
QueryBuilder qb = output.builder();
ExistsElement<?> whereExists = query.getWhereExistsElement();
if (whereExists != null) {
qb.keyword(WHERE);
if (whereExists.isNotExists()) {
qb.keyword(NOT);
}
qb.keyword(EXISTS);
qb.openParenthesis();
Supplier<?> wrapper = whereExists.getQuery();
output.appendQuery((QueryWrapper) wrapper);
qb.closeParenthesis().space();
} else if (query.getWhereElements() != null && query.getWhereElements().size() > 0) {
qb.keyword(WHERE);
for (WhereConditionElement<?> w : query.getWhereElements()) {
output.appendConditional(w);
}
}
}
use of io.requery.sql.QueryBuilder in project requery by requery.
the class DefaultOutput method appendOperation.
private void appendOperation(Condition condition, int depth) {
Object leftOperand = condition.getLeftOperand();
if (leftOperand instanceof Expression) {
final Expression<?> expression = (Expression<?>) condition.getLeftOperand();
appendColumn(expression);
Object value = condition.getRightOperand();
appendOperator(condition.getOperator());
if (value instanceof Collection && (condition.getOperator() == Operator.IN || condition.getOperator() == Operator.NOT_IN)) {
Collection collection = (Collection) value;
qb.openParenthesis();
qb.commaSeparated(collection, new QueryBuilder.Appender() {
@Override
public void append(QueryBuilder qb, Object value) {
appendConditionValue(expression, value);
}
});
qb.closeParenthesis();
} else if (value instanceof Object[]) {
Object[] values = (Object[]) value;
if (condition.getOperator() == Operator.BETWEEN) {
Object begin = values[0];
Object end = values[1];
appendConditionValue(expression, begin);
qb.keyword(AND);
appendConditionValue(expression, end);
} else {
for (Object o : values) {
appendConditionValue(expression, o);
}
}
} else if (value instanceof QueryWrapper) {
QueryWrapper wrapper = (QueryWrapper) value;
qb.openParenthesis();
appendQuery(wrapper);
qb.closeParenthesis().space();
} else if (value instanceof Condition) {
appendOperation((Condition) value, depth + 1);
} else if (value != null) {
appendConditionValue(expression, value);
}
} else if (leftOperand instanceof Condition) {
if (depth > 0) {
qb.openParenthesis();
}
appendOperation((Condition) leftOperand, depth + 1);
appendOperator(condition.getOperator());
Object value = condition.getRightOperand();
if (value instanceof Condition) {
appendOperation((Condition) value, depth + 1);
} else {
throw new IllegalStateException();
}
if (depth > 0) {
qb.closeParenthesis().space();
}
} else {
throw new IllegalStateException("unknown start expression type " + leftOperand);
}
}
Aggregations