The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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.database.Cursor; |
| 20 | import android.database.sqlite.SQLiteDatabase.CursorFactory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * A cursor driver that uses the given query directly. |
| 24 | * |
| 25 | * @hide |
| 26 | */ |
| 27 | public class SQLiteDirectCursorDriver implements SQLiteCursorDriver { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | mSql = sql; |
| 38 | } |
| 39 | |
| 40 | public Cursor query(CursorFactory factory, String[] selectionArgs) { |
| 41 | // Compile the query |
Vasu Nori | 9463f29 | 2010-04-30 12:22:18 -0700 | [diff] [blame] | 42 | SQLiteQuery query = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
| 44 | try { |
Vasu Nori | 9463f29 | 2010-04-30 12:22:18 -0700 | [diff] [blame] | 45 | mDatabase.lock(); |
Vasu Nori | 6f37f83 | 2010-05-19 11:53:25 -0700 | [diff] [blame] | 46 | mDatabase.closePendingStatements(); |
Vasu Nori | 9463f29 | 2010-04-30 12:22:18 -0700 | [diff] [blame] | 47 | query = new SQLiteQuery(mDatabase, mSql, 0, selectionArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
| 49 | // Create the cursor |
| 50 | if (factory == null) { |
Vasu Nori | 65a8883 | 2010-07-16 15:14:08 -0700 | [diff] [blame] | 51 | mCursor = new SQLiteCursor(this, mEditTable, query); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } 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 Nori | 9463f29 | 2010-04-30 12:22:18 -0700 | [diff] [blame] | 62 | mDatabase.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | public void cursorClosed() { |
| 67 | mCursor = null; |
| 68 | } |
| 69 | |
| 70 | public void setBindArguments(String[] bindArgs) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 71 | mQuery.bindAllArgsAsStrings(bindArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
| 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 | } |