Search in sources :

Example 1 with QueryBuilder

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());
}
Also used : QueryBuilder(io.requery.sql.QueryBuilder) Cursor(android.database.Cursor) LinkedHashMap(java.util.LinkedHashMap) ResultSet(java.sql.ResultSet) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with QueryBuilder

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;
    }
}
Also used : QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute) Expression(io.requery.query.Expression) NamedExpression(io.requery.query.NamedExpression) RowExpression(io.requery.query.RowExpression) RowExpression(io.requery.query.RowExpression) QueryBuilder(io.requery.sql.QueryBuilder)

Example 3 with QueryBuilder

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);
            }
        }
    }
}
Also used : Expression(io.requery.query.Expression) QueryBuilder(io.requery.sql.QueryBuilder)

Example 4 with QueryBuilder

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);
        }
    }
}
Also used : QueryBuilder(io.requery.sql.QueryBuilder)

Example 5 with QueryBuilder

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++;
        }
    }
}
Also used : OrderingExpression(io.requery.query.OrderingExpression) Expression(io.requery.query.Expression) OrderingExpression(io.requery.query.OrderingExpression) QueryBuilder(io.requery.sql.QueryBuilder)

Aggregations

QueryBuilder (io.requery.sql.QueryBuilder)15 Expression (io.requery.query.Expression)9 Attribute (io.requery.meta.Attribute)3 Map (java.util.Map)3 NamedExpression (io.requery.query.NamedExpression)2 RowExpression (io.requery.query.RowExpression)2 Cursor (android.database.Cursor)1 QueryAttribute (io.requery.meta.QueryAttribute)1 Condition (io.requery.query.Condition)1 NullOperand (io.requery.query.NullOperand)1 OrderingExpression (io.requery.query.OrderingExpression)1 InsertType (io.requery.query.element.InsertType)1 QueryWrapper (io.requery.query.element.QueryWrapper)1 WhereConditionElement (io.requery.query.element.WhereConditionElement)1 ResultSet (java.sql.ResultSet)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1