use of io.requery.sql.QueryBuilder in project requery by requery.
the class SqliteMetaData method getTables.
@Override
public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException {
if (types == null) {
types = new String[] { "TABLE", "VIEW" };
}
if (tableNamePattern == null) {
tableNamePattern = "%";
}
Map<String, String> select = new LinkedHashMap<>();
select.put("TABLE_CAT", null);
select.put("TABLE_SCHEM", null);
select.put("TABLE_NAME", "name");
select.put("TABLE_TYPE", "type");
select.put("REMARKS", null);
select.put("TYPE_CAT", null);
select.put("TYPE_SCHEM", null);
select.put("TYPE_NAME", null);
select.put("SELF_REFERENCING_COL_NAME", null);
select.put("REF_GENERATION", null);
QueryBuilder qb = new QueryBuilder(new QueryBuilder.Options(getIdentifierQuoteString(), true, null, null, false, false)).keyword(Keyword.SELECT).commaSeparated(select.entrySet(), new QueryBuilder.Appender<Map.Entry<String, String>>() {
@Override
public void append(QueryBuilder qb, Map.Entry<String, String> entry) {
String value = entry.getValue() == null ? "null" : entry.getValue();
qb.append(value).append(" as ").append(entry.getKey());
}
}).keyword(Keyword.FROM).openParenthesis().append("select name, type from sqlite_master").closeParenthesis().keyword(Keyword.WHERE).append(" TABLE_NAME like ").append(tableNamePattern).append(" && TABLE_TYPE in ").openParenthesis().commaSeparated(Arrays.asList(types)).closeParenthesis().keyword(Keyword.ORDER, Keyword.BY).append(" TABLE_TYPE, TABLE_NAME");
return queryMemory(new Function<Cursor, ResultSet>() {
@Override
public ResultSet apply(Cursor cursor) {
return new CursorResultSet(null, cursor, true);
}
}, qb.toString());
}
use of io.requery.sql.QueryBuilder 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.sql.QueryBuilder 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.sql.QueryBuilder in project requery by requery.
the class LimitGenerator method write.
@Override
public void write(Output output, LimitedElement query) {
QueryBuilder qb = output.builder();
Integer limit = query.getLimit();
if (limit != null && limit > 0) {
Integer offset = query.getOffset();
qb.keyword(LIMIT).value(limit);
if (offset != null) {
qb.keyword(OFFSET).value(offset);
}
}
}
use of io.requery.sql.QueryBuilder in project requery by requery.
the class OrderByGenerator method write.
@Override
public void write(Output output, OrderByElement query) {
Set<Expression<?>> orderBy = query.getOrderByExpressions();
if (orderBy != null && orderBy.size() > 0) {
QueryBuilder qb = output.builder();
qb.keyword(ORDER, BY);
int i = 0;
int size = orderBy.size();
for (Expression<?> order : orderBy) {
if (order.getExpressionType() == ExpressionType.ORDERING) {
OrderingExpression ordering = (OrderingExpression) order;
output.appendColumn(ordering.getInnerExpression());
qb.keyword(ordering.getOrder() == Order.ASC ? ASC : DESC);
if (ordering.getNullOrder() != null) {
qb.keyword(NULLS);
switch(ordering.getNullOrder()) {
case FIRST:
qb.keyword(FIRST);
break;
case LAST:
qb.keyword(LAST);
break;
}
}
} else {
output.appendColumn(order);
}
if (i < size - 1) {
qb.append(",");
}
i++;
}
}
}
Aggregations