SQLiteQuery.java revision e5360fbf3efe85427f7e7f59afe7bbeddb4949ac
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.database.sqlite;
18
19import android.database.CursorWindow;
20import android.util.Log;
21
22/**
23 * Represents a query that reads the resulting rows into a {@link SQLiteQuery}.
24 * This class is used by {@link SQLiteCursor} and isn't useful itself.
25 * <p>
26 * This class is not thread-safe.
27 * </p>
28 */
29public final class SQLiteQuery extends SQLiteProgram {
30    private static final String TAG = "SQLiteQuery";
31
32    SQLiteQuery(SQLiteDatabase db, String query) {
33        super(db, query, null);
34    }
35
36    /**
37     * Reads rows into a buffer.
38     *
39     * @param window The window to fill into
40     * @param startPos The start position for filling the window.
41     * @param requiredPos The position of a row that MUST be in the window.
42     * If it won't fit, then the query should discard part of what it filled.
43     * @param countAllRows True to count all rows that the query would
44     * return regardless of whether they fit in the window.
45     * @return Number of rows that were enumerated.  Might not be all rows
46     * unless countAllRows is true.
47     */
48    int fillWindow(CursorWindow window, int startPos, int requiredPos, boolean countAllRows) {
49        acquireReference();
50        try {
51            window.acquireReference();
52            try {
53                int numRows = getSession().executeForCursorWindow(getSql(), getBindArgs(),
54                        window, startPos, requiredPos, countAllRows, getConnectionFlags());
55                return numRows;
56            } catch (SQLiteDatabaseCorruptException ex) {
57                onCorruption();
58                throw ex;
59            } catch (SQLiteException ex) {
60                Log.e(TAG, "exception: " + ex.getMessage() + "; query: " + getSql());
61                throw ex;
62            } finally {
63                window.releaseReference();
64            }
65        } finally {
66            releaseReference();
67        }
68    }
69
70    @Override
71    public String toString() {
72        return "SQLiteQuery: " + getSql();
73    }
74}
75