• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
21 import android.util.Log;
22 
23 /**
24  * A cursor driver that uses the given query directly.
25  *
26  * @hide
27  */
28 public class SQLiteDirectCursorDriver implements SQLiteCursorDriver {
29     private static String TAG = "SQLiteDirectCursorDriver";
30     private String mEditTable;
31     private SQLiteDatabase mDatabase;
32     private Cursor mCursor;
33     private String mSql;
34     private SQLiteQuery mQuery;
35 
SQLiteDirectCursorDriver(SQLiteDatabase db, String sql, String editTable)36     public SQLiteDirectCursorDriver(SQLiteDatabase db, String sql, String editTable) {
37         mDatabase = db;
38         mEditTable = editTable;
39         //TODO remove all callers that end in ; and remove this check
40         if (sql.charAt(sql.length() - 1) == ';') {
41             Log.w(TAG, "Found SQL string that ends in ; -- " + sql);
42             sql = sql.substring(0, sql.length() - 1);
43         }
44         mSql = sql;
45     }
46 
query(CursorFactory factory, String[] selectionArgs)47     public Cursor query(CursorFactory factory, String[] selectionArgs) {
48         // Compile the query
49         SQLiteQuery query = new SQLiteQuery(mDatabase, mSql, 0, selectionArgs);
50 
51         try {
52             // Arg binding
53             int numArgs = selectionArgs == null ? 0 : selectionArgs.length;
54             for (int i = 0; i < numArgs; i++) {
55                 query.bindString(i + 1, selectionArgs[i]);
56             }
57 
58             // Create the cursor
59             if (factory == null) {
60                 mCursor = new SQLiteCursor(mDatabase, this, mEditTable, query);
61             } else {
62                 mCursor = factory.newCursor(mDatabase, this, mEditTable, query);
63             }
64 
65             mQuery = query;
66             query = null;
67             return mCursor;
68         } finally {
69             // Make sure this object is cleaned up if something happens
70             if (query != null) query.close();
71         }
72     }
73 
cursorClosed()74     public void cursorClosed() {
75         mCursor = null;
76     }
77 
setBindArguments(String[] bindArgs)78     public void setBindArguments(String[] bindArgs) {
79         final int numArgs = bindArgs.length;
80         for (int i = 0; i < numArgs; i++) {
81             mQuery.bindString(i + 1, bindArgs[i]);
82         }
83     }
84 
cursorDeactivated()85     public void cursorDeactivated() {
86         // Do nothing
87     }
88 
cursorRequeried(Cursor cursor)89     public void cursorRequeried(Cursor cursor) {
90         // Do nothing
91     }
92 
93     @Override
toString()94     public String toString() {
95         return "SQLiteDirectCursorDriver: " + mSql;
96     }
97 }
98