1 /*
2  * Copyright (C) 2007 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.annotation.TestApi;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase.CursorFactory;
22 import android.os.CancellationSignal;
23 
24 /**
25  * A cursor driver that uses the given query directly.
26  *
27  * @hide
28  */
29 @TestApi
30 public final class SQLiteDirectCursorDriver implements SQLiteCursorDriver {
31     private final SQLiteDatabase mDatabase;
32     private final String mEditTable;
33     private final String mSql;
34     private final CancellationSignal mCancellationSignal;
35     private SQLiteQuery mQuery;
36 
SQLiteDirectCursorDriver(SQLiteDatabase db, String sql, String editTable, CancellationSignal cancellationSignal)37     public SQLiteDirectCursorDriver(SQLiteDatabase db, String sql, String editTable,
38             CancellationSignal cancellationSignal) {
39         mDatabase = db;
40         mEditTable = editTable;
41         mSql = sql;
42         mCancellationSignal = cancellationSignal;
43     }
44 
query(CursorFactory factory, String[] selectionArgs)45     public Cursor query(CursorFactory factory, String[] selectionArgs) {
46         final SQLiteQuery query = new SQLiteQuery(mDatabase, mSql, mCancellationSignal);
47         final Cursor cursor;
48         try {
49             query.bindAllArgsAsStrings(selectionArgs);
50 
51             if (factory == null) {
52                 cursor = new SQLiteCursor(this, mEditTable, query);
53             } else {
54                 cursor = factory.newCursor(mDatabase, this, mEditTable, query);
55             }
56         } catch (RuntimeException ex) {
57             query.close();
58             throw ex;
59         }
60 
61         mQuery = query;
62         return cursor;
63     }
64 
cursorClosed()65     public void cursorClosed() {
66         // Do nothing
67     }
68 
setBindArguments(String[] bindArgs)69     public void setBindArguments(String[] bindArgs) {
70         mQuery.bindAllArgsAsStrings(bindArgs);
71     }
72 
cursorDeactivated()73     public void cursorDeactivated() {
74         // Do nothing
75     }
76 
cursorRequeried(Cursor cursor)77     public void cursorRequeried(Cursor cursor) {
78         // Do nothing
79     }
80 
81     @Override
toString()82     public String toString() {
83         return "SQLiteDirectCursorDriver: " + mSql;
84     }
85 }
86