Search in sources :

Example 6 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 7 with QueryBuilder

use of io.requery.sql.QueryBuilder in project requery by requery.

the class OffsetFetchGenerator 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();
        write(qb, limit, offset);
    }
}
Also used : QueryBuilder(io.requery.sql.QueryBuilder)

Example 8 with QueryBuilder

use of io.requery.sql.QueryBuilder in project requery by requery.

the class SelectGenerator method write.

@Override
public void write(final Output output, SelectionElement query) {
    QueryBuilder qb = output.builder();
    qb.keyword(SELECT);
    if (query.isDistinct()) {
        qb.keyword(DISTINCT);
    }
    Set<? extends Expression<?>> selection = query.getSelection();
    if (selection == null || selection.isEmpty()) {
        qb.append("*");
    } else {
        qb.commaSeparated(selection, new QueryBuilder.Appender<Expression<?>>() {

            @Override
            public void append(QueryBuilder qb, Expression<?> value) {
                output.appendColumnForSelect(value);
            }
        });
    }
    qb.keyword(FROM);
    output.appendTables();
}
Also used : Expression(io.requery.query.Expression) QueryBuilder(io.requery.sql.QueryBuilder)

Example 9 with QueryBuilder

use of io.requery.sql.QueryBuilder in project requery by requery.

the class SetOperatorGenerator method write.

@Override
public void write(Output output, SetOperationElement query) {
    if (query.getInnerSetQuery() != null) {
        QueryBuilder qb = output.builder();
        switch(query.getOperator()) {
            case UNION:
                qb.keyword(UNION);
                break;
            case UNION_ALL:
                qb.keyword(UNION, ALL);
                break;
            case INTERSECT:
                qb.keyword(INTERSECT);
                break;
            case EXCEPT:
                qb.keyword(EXCEPT);
                break;
        }
        output.appendQuery(query.getInnerSetQuery());
    }
}
Also used : QueryBuilder(io.requery.sql.QueryBuilder)

Example 10 with QueryBuilder

use of io.requery.sql.QueryBuilder in project requery by requery.

the class StatementGenerator method write.

@Override
public void write(Output output, QueryElement<?> query) {
    QueryBuilder qb = output.builder();
    switch(query.queryType()) {
        case SELECT:
            select.write(output, query);
            break;
        case INSERT:
            insert.write(output, query);
            break;
        case UPDATE:
            update.write(output, checkEmpty(query.updateValues()));
            break;
        case UPSERT:
            upsert.write(output, checkEmpty(query.updateValues()));
            break;
        case DELETE:
            qb.keyword(DELETE, FROM);
            output.appendTables();
            break;
        case TRUNCATE:
            qb.keyword(TRUNCATE);
            output.appendTables();
            break;
    }
    where.write(output, query);
    groupBy.write(output, query);
    orderBy.write(output, query);
    limit.write(output, query);
    setOperation.write(output, query);
}
Also used : 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 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