All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.greenrobot.dao.query.CursorQuery Maven / Gradle / Ivy

There is a newer version: 3.0.0-beta3
Show newest version
/*
 * Copyright (C) 2011-2015 Markus Junginger, greenrobot (http://greenrobot.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.greenrobot.dao.query;

import android.database.Cursor;
import de.greenrobot.dao.AbstractDao;

/**
 * A repeatable query returning a raw android.database.Cursor. Note, that using cursors is usually a hassle and
 * greenDAO provides a higher level abstraction using entities (see {@link de.greenrobot.dao.query.Query}). This class
 * can nevertheless be useful to work with legacy code that is based on Cursors or CursorLoaders.
 *
 * @param  The entity class the query will return results for.
 * @author Markus
 */
public class CursorQuery extends AbstractQueryWithLimit {
    private final static class QueryData extends AbstractQueryData> {
        private final int limitPosition;
        private final int offsetPosition;

        QueryData(AbstractDao dao, String sql, String[] initialValues, int limitPosition, int offsetPosition) {
            super(dao, sql, initialValues);
            this.limitPosition = limitPosition;
            this.offsetPosition = offsetPosition;
        }

        @Override
        protected CursorQuery createQuery() {
            return new CursorQuery(this, dao, sql, initialValues.clone(), limitPosition, offsetPosition);
        }

    }

    /** For internal use by greenDAO only. */
    public static  CursorQuery internalCreate(AbstractDao dao, String sql, Object[] initialValues) {
        return create(dao, sql, initialValues, -1, -1);
    }

    static  CursorQuery create(AbstractDao dao, String sql, Object[] initialValues, int limitPosition,
                                       int offsetPosition) {
        QueryData queryData = new QueryData(dao, sql, toStringArray(initialValues), limitPosition,
                offsetPosition);
        return queryData.forCurrentThread();
    }

    private final QueryData queryData;

    private CursorQuery(QueryData queryData, AbstractDao dao, String sql, String[] initialValues, int limitPosition,
                        int offsetPosition) {
        super(dao, sql, initialValues, limitPosition, offsetPosition);
        this.queryData = queryData;
    }

    public CursorQuery forCurrentThread() {
        return queryData.forCurrentThread(this);
    }

    /** Executes the query and returns a raw android.database.Cursor. Don't forget to close it. */
    public Cursor query() {
        checkThread();
        return dao.getDatabase().rawQuery(sql, parameters);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy