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; 21 22/** 23 * A cursor driver that uses the given query directly. 24 * 25 * @hide 26 */ 27public class SQLiteDirectCursorDriver implements SQLiteCursorDriver { 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; 37 mSql = sql; 38 } 39 40 public Cursor query(CursorFactory factory, String[] selectionArgs) { 41 // Compile the query 42 SQLiteQuery query = new SQLiteQuery(mDatabase, mSql, 0, selectionArgs); 43 44 try { 45 // Arg binding 46 int numArgs = selectionArgs == null ? 0 : selectionArgs.length; 47 for (int i = 0; i < numArgs; i++) { 48 query.bindString(i + 1, selectionArgs[i]); 49 } 50 51 // Create the cursor 52 if (factory == null) { 53 mCursor = new SQLiteCursor(mDatabase, this, mEditTable, query); 54 } else { 55 mCursor = factory.newCursor(mDatabase, this, mEditTable, query); 56 } 57 58 mQuery = query; 59 query = null; 60 return mCursor; 61 } finally { 62 // Make sure this object is cleaned up if something happens 63 if (query != null) query.close(); 64 } 65 } 66 67 public void cursorClosed() { 68 mCursor = null; 69 } 70 71 public void setBindArguments(String[] bindArgs) { 72 final int numArgs = bindArgs.length; 73 for (int i = 0; i < numArgs; i++) { 74 mQuery.bindString(i + 1, bindArgs[i]); 75 } 76 } 77 78 public void cursorDeactivated() { 79 // Do nothing 80 } 81 82 public void cursorRequeried(Cursor cursor) { 83 // Do nothing 84 } 85 86 @Override 87 public String toString() { 88 return "SQLiteDirectCursorDriver: " + mSql; 89 } 90} 91