blob: 1721e0c69dc39804c2c348fdbae2b1a6d21ce0c6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Fyodor Kupolov45be5632017-08-08 16:55:03 -070019import android.annotation.TestApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.database.Cursor;
21import android.database.sqlite.SQLiteDatabase.CursorFactory;
Jeff Browna7771df2012-05-07 20:06:46 -070022import android.os.CancellationSignal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24/**
25 * A cursor driver that uses the given query directly.
26 *
27 * @hide
28 */
Fyodor Kupolov45be5632017-08-08 16:55:03 -070029@TestApi
Jeff Brownbaefdfa2012-03-05 10:33:13 -080030public final class SQLiteDirectCursorDriver implements SQLiteCursorDriver {
Jeff Browne5360fb2011-10-31 17:48:13 -070031 private final SQLiteDatabase mDatabase;
32 private final String mEditTable;
33 private final String mSql;
Jeff Brown4c1241d2012-02-02 17:05:00 -080034 private final CancellationSignal mCancellationSignal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 private SQLiteQuery mQuery;
36
Jeff Brown75ea64f2012-01-25 19:37:13 -080037 public SQLiteDirectCursorDriver(SQLiteDatabase db, String sql, String editTable,
Jeff Brown4c1241d2012-02-02 17:05:00 -080038 CancellationSignal cancellationSignal) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 mDatabase = db;
40 mEditTable = editTable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 mSql = sql;
Jeff Brown4c1241d2012-02-02 17:05:00 -080042 mCancellationSignal = cancellationSignal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 }
44
45 public Cursor query(CursorFactory factory, String[] selectionArgs) {
Jeff Brown4c1241d2012-02-02 17:05:00 -080046 final SQLiteQuery query = new SQLiteQuery(mDatabase, mSql, mCancellationSignal);
Jeff Browne5360fb2011-10-31 17:48:13 -070047 final Cursor cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 try {
Jeff Browne5360fb2011-10-31 17:48:13 -070049 query.bindAllArgsAsStrings(selectionArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 if (factory == null) {
Jeff Browne5360fb2011-10-31 17:48:13 -070052 cursor = new SQLiteCursor(this, mEditTable, query);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 } else {
Jeff Browne5360fb2011-10-31 17:48:13 -070054 cursor = factory.newCursor(mDatabase, this, mEditTable, query);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 }
Jeff Browne5360fb2011-10-31 17:48:13 -070056 } catch (RuntimeException ex) {
57 query.close();
58 throw ex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 }
Jeff Browne5360fb2011-10-31 17:48:13 -070060
61 mQuery = query;
62 return cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 }
64
65 public void cursorClosed() {
Jeff Browne5360fb2011-10-31 17:48:13 -070066 // Do nothing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 }
68
69 public void setBindArguments(String[] bindArgs) {
Vasu Nori0732f792010-07-29 17:24:12 -070070 mQuery.bindAllArgsAsStrings(bindArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 }
72
73 public void cursorDeactivated() {
74 // Do nothing
75 }
76
77 public void cursorRequeried(Cursor cursor) {
78 // Do nothing
79 }
80
81 @Override
82 public String toString() {
83 return "SQLiteDirectCursorDriver: " + mSql;
84 }
85}