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 
17 package android.database.sqlite;
18 
19 import android.database.CursorWindow;
20 import android.os.CancellationSignal;
21 import android.os.OperationCanceledException;
22 import android.util.Log;
23 
24 /**
25  * Represents a query that reads the resulting rows into a {@link SQLiteQuery}.
26  * This class is used by {@link SQLiteCursor} and isn't useful itself.
27  * <p>
28  * This class is not thread-safe.
29  * </p>
30  */
31 public final class SQLiteQuery extends SQLiteProgram {
32     private static final String TAG = "SQLiteQuery";
33 
34     private final CancellationSignal mCancellationSignal;
35 
SQLiteQuery(SQLiteDatabase db, String query, CancellationSignal cancellationSignal)36     SQLiteQuery(SQLiteDatabase db, String query, CancellationSignal cancellationSignal) {
37         super(db, query, null, cancellationSignal);
38 
39         mCancellationSignal = cancellationSignal;
40     }
41 
42     /**
43      * Reads rows into a buffer.
44      *
45      * @param window The window to fill into
46      * @param startPos The start position for filling the window.
47      * @param requiredPos The position of a row that MUST be in the window.
48      * If it won't fit, then the query should discard part of what it filled.
49      * @param countAllRows True to count all rows that the query would
50      * return regardless of whether they fit in the window.
51      * @return Number of rows that were enumerated.  Might not be all rows
52      * unless countAllRows is true.
53      *
54      * @throws SQLiteException if an error occurs.
55      * @throws OperationCanceledException if the operation was canceled.
56      */
fillWindow(CursorWindow window, int startPos, int requiredPos, boolean countAllRows)57     int fillWindow(CursorWindow window, int startPos, int requiredPos, boolean countAllRows) {
58         acquireReference();
59         try {
60             window.acquireReference();
61             try {
62                 int numRows = getSession().executeForCursorWindow(getSql(), getBindArgs(),
63                         window, startPos, requiredPos, countAllRows, getConnectionFlags(),
64                         mCancellationSignal);
65                 return numRows;
66             } catch (SQLiteDatabaseCorruptException ex) {
67                 onCorruption();
68                 throw ex;
69             } catch (SQLiteException ex) {
70                 Log.e(TAG, "exception: " + ex.getMessage() + "; query: " + getSql());
71                 throw ex;
72             } finally {
73                 window.releaseReference();
74             }
75         } finally {
76             releaseReference();
77         }
78     }
79 
80     @Override
toString()81     public String toString() {
82         return "SQLiteQuery: " + getSql();
83     }
84 }
85