Android Open Source - NexusData Predicate To S Q L






From Project

Back to project page NexusData.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCT...

If you think the Android project NexusData listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.dkharrat.nexusdata.store;
/*from  w  w w .  jav a  2 s  .  co  m*/
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.github.dkharrat.nexusdata.core.*;
import com.github.dkharrat.nexusdata.predicate.*;
import com.github.dkharrat.nexusdata.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

class DatabaseQueryService {
    private static final Logger LOG = LoggerFactory.getLogger(DatabaseQueryService.class);

    public static <T extends ManagedObject> Cursor query(SQLiteDatabase db, final IncrementalStore store, String tableName, FetchRequest<T> request) {

        LOG.debug("Constructing SQL query for request: " + request);

        String limit = null;
        if (request.getLimit() != Integer.MAX_VALUE || request.getOffset() != 0) {
            limit = String.valueOf(request.getOffset()) + "," + request.getLimit();
        }

        String orderBy = null;
        if (request.hasSortDescriptors()) {
            List<String> orderBys = new ArrayList<String>();
            for (SortDescriptor sortDesc : request.getSortDescriptors()) {
                String orderType = sortDesc.isAscending() ? " ASC" : " DESC";
                orderBys.add(sortDesc.getAttributeName() + orderType);
            }

            orderBy = StringUtil.join(orderBys, ",");
        }


        String selection = null;
        String[] selectionArgs = null;
        if (request.getPredicate() != null) {
            QueryParts queryParts = buildQuery(store, request.getPredicate());

            selection = queryParts.stringBuilder.toString();
            selectionArgs = queryParts.params.isEmpty() ? null : queryParts.params.toArray(new String[0]);
        }

        Cursor cursor = db.query(
                false,          // not distinct
                tableName,
                null,           // columns,
                selection,
                selectionArgs,  // selectionArgs
                null,           // groupBy
                null,           // having
                orderBy,        // orderBy
                limit);         // limit

        return cursor;
    }

    private static QueryParts buildQuery(IncrementalStore store, Predicate predicate) {
        return new QueryBuilder(store).visit(predicate);
    }

    private static class QueryParts {
        private final StringBuilder stringBuilder = new StringBuilder();
        private final ArrayList<String> params = new ArrayList<String>();
    }

    private static class QueryBuilder implements ExpressionVisitor<QueryParts> {

        final IncrementalStore store;
        final QueryParts queryParts = new QueryParts();

        QueryBuilder(IncrementalStore store) {
            this.store = store;
        }

        @Override
        public QueryParts visit(ConstantExpression<?> expression) {
            Object value = expression.getValue();
            if (value == null) {
                queryParts.stringBuilder.append("NULL");
            } else {
                if (value instanceof ManagedObject) {
                    ManagedObject relatedObject = (ManagedObject)value;
                    value = store.getReferenceObjectForObjectID(relatedObject.getID()).toString();
                } else if (value.getClass().isAssignableFrom(Boolean.class) || value.getClass().isAssignableFrom(boolean.class)) {
                    value = ((Boolean)value) ? "1" : "0";
                }
                queryParts.stringBuilder.append("?");
                queryParts.params.add(value.toString());
            }

            return queryParts;
        }

        @Override
        public QueryParts visit(FieldPathExpression expression) {
            queryParts.stringBuilder.append(expression.getFieldPath());
            return queryParts;
        }

        @Override
        public QueryParts visit(ThisExpression expression) {
            queryParts.stringBuilder.append(AndroidSqlPersistentStore.COLUMN_ID_NAME);
            return queryParts;
        }

        @Override
        public QueryParts visit(CompoundPredicate predicate) {
            String op = null;
            switch(predicate.getOperator()) {
                case AND:   op = " AND "; break;
                case OR:    op = " OR "; break;
            }

            queryParts.stringBuilder.append("(");
            visit(predicate.getLhs());
            queryParts.stringBuilder.append(op);
            visit(predicate.getRhs());
            queryParts.stringBuilder.append(")");

            return queryParts;
        }

        @Override
        public QueryParts visit(ComparisonPredicate predicate) {
            String op = null;
            if (predicate.getRhs() instanceof ConstantExpression<?> && ((ConstantExpression<?>)predicate.getRhs()).getValue() == null) {
                switch(predicate.getOperator()) {
                    case EQUAL:                 op = " IS "; break;
                    case NOT_EQUAL:             op = " IS NOT "; break;
                    default: throw new UnsupportedOperationException("Invalid operator " + op + " with 'null' comparison.");
                }
            } else {
                switch(predicate.getOperator()) {
                    case EQUAL:                 op = " = "; break;
                    case GREATER_THAN:          op = " > "; break;
                    case GREATER_THAN_OR_EQUAL: op = " >= "; break;
                    case LESS_THAN:             op = " < "; break;
                    case LESS_THAN_OR_EQUAL:    op = " <= "; break;
                    case NOT_EQUAL:             op = " != "; break;
                }
            }

            queryParts.stringBuilder.append("(");
            visit(predicate.getLhs());
            queryParts.stringBuilder.append(op);
            visit(predicate.getRhs());
            queryParts.stringBuilder.append(")");

            return queryParts;
        }

        @Override
        public QueryParts visit(NotPredicate predicate) {
            queryParts.stringBuilder
                    .append("NOT (")
                    .append(visit(predicate.getPredicate()))
                    .append(")");
            return queryParts;
        }

        QueryParts visit(Predicate predicate) {
            if (predicate instanceof CompoundPredicate) {
                return visit((CompoundPredicate)predicate);
            } else if (predicate instanceof ComparisonPredicate) {
                return visit((ComparisonPredicate)predicate);
            } else if (predicate instanceof NotPredicate) {
                return visit((NotPredicate)predicate);
            } else {
                throw new UnsupportedOperationException("Unsupported predicate type: " + predicate);
            }
        }

        QueryParts visit(Expression expression) {
            if (expression instanceof ConstantExpression) {
                return visit((ConstantExpression)expression);
            } else if (expression instanceof FieldPathExpression) {
                return visit((FieldPathExpression)expression);
            } else if (expression instanceof ThisExpression) {
                return visit((ThisExpression)expression);
            } else {
                throw new UnsupportedOperationException("Unsupported expression type: " + expression);
            }
        }
    }
}




Java Source Code List

com.github.dkharrat.nexusdata.core.AtomicStore.java
com.github.dkharrat.nexusdata.core.ChangedObjectsSet.java
com.github.dkharrat.nexusdata.core.FaultingSet.java
com.github.dkharrat.nexusdata.core.FetchRequest.java
com.github.dkharrat.nexusdata.core.IncrementalStore.java
com.github.dkharrat.nexusdata.core.ManagedObject.java
com.github.dkharrat.nexusdata.core.NoSuchPropertyException.java
com.github.dkharrat.nexusdata.core.ObjectContextNotifier.java
com.github.dkharrat.nexusdata.core.ObjectContext.java
com.github.dkharrat.nexusdata.core.ObjectID.java
com.github.dkharrat.nexusdata.core.ObjectsChangedNotification.java
com.github.dkharrat.nexusdata.core.PersistentStoreCoordinator.java
com.github.dkharrat.nexusdata.core.PersistentStoreRequest.java
com.github.dkharrat.nexusdata.core.PersistentStore.java
com.github.dkharrat.nexusdata.core.SaveChangesRequest.java
com.github.dkharrat.nexusdata.core.SortDescriptor.java
com.github.dkharrat.nexusdata.core.StoreCacheNode.java
com.github.dkharrat.nexusdata.metamodel.Attribute.java
com.github.dkharrat.nexusdata.metamodel.Entity.java
com.github.dkharrat.nexusdata.metamodel.ObjectModelJsonParser.java
com.github.dkharrat.nexusdata.metamodel.ObjectModel.java
com.github.dkharrat.nexusdata.metamodel.Property.java
com.github.dkharrat.nexusdata.metamodel.Relationship.java
com.github.dkharrat.nexusdata.modelgen.ModelGenerator.java
com.github.dkharrat.nexusdata.modelgen.Startup.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Attribute.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Entity.java
com.github.dkharrat.nexusdata.modelgen.metamodel.EnumProperty.java
com.github.dkharrat.nexusdata.modelgen.metamodel.ModelWrapper.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Model.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Property.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Relationship.java
com.github.dkharrat.nexusdata.predicate.ComparisonPredicate.java
com.github.dkharrat.nexusdata.predicate.CompoundPredicate.java
com.github.dkharrat.nexusdata.predicate.ConstantExpression.java
com.github.dkharrat.nexusdata.predicate.ExpressionBuilder.java
com.github.dkharrat.nexusdata.predicate.ExpressionVisitor.java
com.github.dkharrat.nexusdata.predicate.Expression.java
com.github.dkharrat.nexusdata.predicate.FieldPathExpression.java
com.github.dkharrat.nexusdata.predicate.NotPredicate.java
com.github.dkharrat.nexusdata.predicate.PredicateBuilder.java
com.github.dkharrat.nexusdata.predicate.Predicate.java
com.github.dkharrat.nexusdata.predicate.ThisExpression.java
com.github.dkharrat.nexusdata.predicate.parser.ComparisonParselet.java
com.github.dkharrat.nexusdata.predicate.parser.ConstantParselet.java
com.github.dkharrat.nexusdata.predicate.parser.ExpressionNode.java
com.github.dkharrat.nexusdata.predicate.parser.GroupParselet.java
com.github.dkharrat.nexusdata.predicate.parser.InfixParselet.java
com.github.dkharrat.nexusdata.predicate.parser.LexerGrammar.java
com.github.dkharrat.nexusdata.predicate.parser.Lexer.java
com.github.dkharrat.nexusdata.predicate.parser.LogicalParselet.java
com.github.dkharrat.nexusdata.predicate.parser.NameParselet.java
com.github.dkharrat.nexusdata.predicate.parser.ParseException.java
com.github.dkharrat.nexusdata.predicate.parser.Parser.java
com.github.dkharrat.nexusdata.predicate.parser.PredicateParser.java
com.github.dkharrat.nexusdata.predicate.parser.PrefixParselet.java
com.github.dkharrat.nexusdata.predicate.parser.Token.java
com.github.dkharrat.nexusdata.store.AndroidSqlPersistentStore.java
com.github.dkharrat.nexusdata.store.DatabaseHelper.java
com.github.dkharrat.nexusdata.store.InMemoryPersistentStore.java
com.github.dkharrat.nexusdata.store.PredicateToSQL.java
com.github.dkharrat.nexusdata.utils.DateUtil.java
com.github.dkharrat.nexusdata.utils.ObjectUtil.java
com.github.dkharrat.nexusdata.utils.SqlTableBuilder.java
com.github.dkharrat.nexusdata.utils.StreamUtil.java
com.github.dkharrat.nexusdata.utils.StringUtil.java
com.github.dkharrat.nexusdata.utils.android.CursorUtil.java
com.github.dkharrat.nexusdata.utils.android.SQLiteDatabaseHelper.java
org.example.todo.MainActivity.java
org.example.todo.NewTaskActivity.java
org.example.todo.Task.java
org.example.todo.TodoApp.java
org.example.todo.User.java
org.example.todo._Task.java
org.example.todo._User.java