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