blob: a5e762e66f7a4d36db61580869fd47d3b2fea931 [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
19import android.database.Cursor;
20import android.database.sqlite.SQLiteDatabase.CursorFactory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
22/**
23 * A cursor driver that uses the given query directly.
24 *
25 * @hide
26 */
27public class SQLiteDirectCursorDriver implements SQLiteCursorDriver {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028 private String mEditTable;
29 private SQLiteDatabase mDatabase;
30 private Cursor mCursor;
31 private String mSql;
32 private SQLiteQuery mQuery;
33
34 public SQLiteDirectCursorDriver(SQLiteDatabase db, String sql, String editTable) {
35 mDatabase = db;
36 mEditTable = editTable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 mSql = sql;
38 }
39
40 public Cursor query(CursorFactory factory, String[] selectionArgs) {
41 // Compile the query
Vasu Nori9463f292010-04-30 12:22:18 -070042 SQLiteQuery query = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44 try {
Vasu Nori16057fa2011-03-18 11:40:37 -070045 mDatabase.lock(mSql);
Vasu Nori6f37f832010-05-19 11:53:25 -070046 mDatabase.closePendingStatements();
Vasu Nori9463f292010-04-30 12:22:18 -070047 query = new SQLiteQuery(mDatabase, mSql, 0, selectionArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
49 // Create the cursor
50 if (factory == null) {
Vasu Nori65a88832010-07-16 15:14:08 -070051 mCursor = new SQLiteCursor(this, mEditTable, query);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 } else {
53 mCursor = factory.newCursor(mDatabase, this, mEditTable, query);
54 }
55
56 mQuery = query;
57 query = null;
58 return mCursor;
59 } finally {
60 // Make sure this object is cleaned up if something happens
61 if (query != null) query.close();
Vasu Nori9463f292010-04-30 12:22:18 -070062 mDatabase.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 }
64 }
65
66 public void cursorClosed() {
67 mCursor = null;
68 }
69
70 public void setBindArguments(String[] bindArgs) {
Vasu Nori0732f792010-07-29 17:24:12 -070071 mQuery.bindAllArgsAsStrings(bindArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 }
73
74 public void cursorDeactivated() {
75 // Do nothing
76 }
77
78 public void cursorRequeried(Cursor cursor) {
79 // Do nothing
80 }
81
82 @Override
83 public String toString() {
84 return "SQLiteDirectCursorDriver: " + mSql;
85 }
86}