use of io.requery.query.Expression in project requery by requery.
the class DefaultOutput method appendColumnExpression.
private void appendColumnExpression(Expression expression) {
switch(expression.getExpressionType()) {
case ATTRIBUTE:
Attribute attribute = (Attribute) expression;
qb.attribute(attribute);
break;
default:
if (expression instanceof RowExpression) {
RowExpression collection = (RowExpression) expression;
qb.openParenthesis();
qb.commaSeparated(collection.getExpressions(), new QueryBuilder.Appender<Expression<?>>() {
@Override
public void append(QueryBuilder qb, Expression<?> value) {
appendColumnForSelect(value);
}
});
qb.closeParenthesis().space();
} else {
qb.append(expression.getName()).space();
}
break;
}
}
use of io.requery.query.Expression in project requery by requery.
the class DefaultOutput method appendConditionValue.
private void appendConditionValue(Expression expression, Object value, boolean parameterize) {
if (value instanceof QueryAttribute) {
appendColumn((Expression<?>) value);
} else if (value instanceof Supplier && ((Supplier) value).get() instanceof QueryAttribute) {
appendColumn((Expression<?>) ((Supplier) value).get());
} else if (value instanceof NamedExpression) {
NamedExpression namedExpression = (NamedExpression) value;
qb.append(namedExpression.getName());
} else if (value instanceof Function) {
appendFunction((Function) value);
} else if (value instanceof Collection && expression.getExpressionType() == ExpressionType.ROW) {
qb.openParenthesis();
qb.commaSeparated((Collection) value);
qb.closeParenthesis();
} else {
if (parameterize) {
if (parameters != null) {
parameters.add(expression, value);
}
qb.append("?").space();
} else {
if (value instanceof CharSequence) {
qb.appendQuoted(value.toString()).space();
} else {
qb.append(value).space();
}
}
}
}
use of io.requery.query.Expression in project requery by requery.
the class DefaultOutput method appendFunction.
private void appendFunction(Function function) {
if (function instanceof Case) {
appendCaseFunction((Case) function);
} else {
Function.Name name = configuration.getMapping().mapFunctionName(function);
qb.append(name.getName());
if (function.arguments().length == 0 && name.isConstant()) {
return;
}
qb.openParenthesis();
int index = 0;
for (Object arg : function.arguments()) {
if (index > 0) {
qb.comma();
}
if (arg instanceof Expression) {
Expression expression = (Expression) arg;
switch(expression.getExpressionType()) {
case ATTRIBUTE:
appendColumnForSelect(expression);
break;
case FUNCTION:
Function inner = (Function) arg;
appendFunction(inner);
break;
default:
qb.append(expression.getName());
break;
}
} else if (arg instanceof Class) {
qb.append("*");
} else {
appendConditionValue(function.expressionForArgument(index), arg);
}
index++;
}
qb.closeParenthesis().space();
}
}
use of io.requery.query.Expression in project requery by requery.
the class GroupByGenerator method write.
@Override
public void write(final Output output, GroupByElement query) {
QueryBuilder qb = output.builder();
Set<Expression<?>> groupBy = query.getGroupByExpressions();
if (groupBy != null && groupBy.size() > 0) {
qb.keyword(GROUP, BY);
qb.commaSeparated(groupBy, new QueryBuilder.Appender<Expression<?>>() {
@Override
public void append(QueryBuilder qb, Expression<?> value) {
output.appendColumn(value);
}
});
if (query.getHavingElements() != null) {
qb.keyword(HAVING);
for (HavingConditionElement<?> clause : query.getHavingElements()) {
output.appendConditional(clause);
}
}
}
}
use of io.requery.query.Expression 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