The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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.CursorWindow; |
Brad Fitzpatrick | 6a35387 | 2010-02-11 18:04:49 -0800 | [diff] [blame] | 20 | import android.os.SystemClock; |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 21 | import android.text.TextUtils; |
Vasu Nori | b18f27d | 2010-08-12 18:16:35 -0700 | [diff] [blame] | 22 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * A SQLite program that represents a query that reads the resulting rows into a CursorWindow. |
| 26 | * This class is used by SQLiteCursor and isn't useful itself. |
Jeff Hamilton | f3ca9a5 | 2010-05-12 15:04:33 -0700 | [diff] [blame] | 27 | * |
| 28 | * SQLiteQuery is not internally synchronized so code using a SQLiteQuery from multiple |
| 29 | * threads should perform its own synchronization when using the SQLiteQuery. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | */ |
| 31 | public class SQLiteQuery extends SQLiteProgram { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 32 | private static final String TAG = "SQLiteQuery"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 34 | private static native int nativeFillWindow(int databasePtr, int statementPtr, int windowPtr, |
| 35 | int startPos, int offsetParam); |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 36 | |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 37 | private static native int nativeColumnCount(int statementPtr); |
| 38 | private static native String nativeColumnName(int statementPtr, int columnIndex); |
| 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | /** The index of the unbound OFFSET parameter */ |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 41 | private int mOffsetIndex = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 43 | private boolean mClosed = false; |
| 44 | |
| 45 | /** |
| 46 | * Create a persistent query object. |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 47 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | * @param db The database that this query object is associated with |
| 49 | * @param query The SQL string for this query. |
| 50 | * @param offsetIndex The 1-based index to the OFFSET parameter, |
| 51 | */ |
| 52 | /* package */ SQLiteQuery(SQLiteDatabase db, String query, int offsetIndex, String[] bindArgs) { |
| 53 | super(db, query); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | mOffsetIndex = offsetIndex; |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 55 | bindAllArgsAsStrings(bindArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
Vasu Nori | 65a8883 | 2010-07-16 15:14:08 -0700 | [diff] [blame] | 59 | * Constructor used to create new instance to replace a given instance of this class. |
| 60 | * This constructor is used when the current Query object is now associated with a different |
| 61 | * {@link SQLiteDatabase} object. |
| 62 | * |
| 63 | * @param db The database that this query object is associated with |
| 64 | * @param query the instance of {@link SQLiteQuery} to be replaced |
| 65 | */ |
| 66 | /* package */ SQLiteQuery(SQLiteDatabase db, SQLiteQuery query) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 67 | super(db, query.mSql); |
| 68 | this.mBindArgs = query.mBindArgs; |
Vasu Nori | b18f27d | 2010-08-12 18:16:35 -0700 | [diff] [blame] | 69 | this.mOffsetIndex = query.mOffsetIndex; |
Vasu Nori | 65a8883 | 2010-07-16 15:14:08 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | * Reads rows into a buffer. This method acquires the database lock. |
Brad Fitzpatrick | 722802e | 2010-03-23 22:22:16 -0700 | [diff] [blame] | 74 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | * @param window The window to fill into |
| 76 | * @return number of total rows in the query |
| 77 | */ |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 78 | /* package */ int fillWindow(CursorWindow window) { |
Vasu Nori | 16057fa | 2011-03-18 11:40:37 -0700 | [diff] [blame] | 79 | mDatabase.lock(mSql); |
Brad Fitzpatrick | 6a35387 | 2010-02-11 18:04:49 -0800 | [diff] [blame] | 80 | long timeStart = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | try { |
| 82 | acquireReference(); |
| 83 | try { |
| 84 | window.acquireReference(); |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 85 | int startPos = window.getStartPosition(); |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 86 | int numRows = nativeFillWindow(nHandle, nStatement, window.mWindowPtr, |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 87 | startPos, mOffsetIndex); |
| 88 | if (SQLiteDebug.DEBUG_LOG_SLOW_QUERIES) { |
| 89 | long elapsed = SystemClock.uptimeMillis() - timeStart; |
| 90 | if (SQLiteDebug.shouldLogSlowQuery(elapsed)) { |
| 91 | Log.d(TAG, "fillWindow took " + elapsed |
| 92 | + " ms: window=\"" + window |
| 93 | + "\", startPos=" + startPos |
| 94 | + ", offset=" + mOffsetIndex |
| 95 | + ", filledRows=" + window.getNumRows() |
| 96 | + ", countedRows=" + numRows |
| 97 | + ", query=\"" + mSql + "\"" |
| 98 | + ", args=[" + (mBindArgs != null ? |
| 99 | TextUtils.join(", ", mBindArgs.values()) : "") |
| 100 | + "]"); |
| 101 | } |
| 102 | } |
Dan Egnor | 1231195 | 2009-11-23 14:47:45 -0800 | [diff] [blame] | 103 | mDatabase.logTimeStat(mSql, timeStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | return numRows; |
| 105 | } catch (IllegalStateException e){ |
| 106 | // simply ignore it |
| 107 | return 0; |
| 108 | } catch (SQLiteDatabaseCorruptException e) { |
| 109 | mDatabase.onCorruption(); |
| 110 | throw e; |
Vasu Nori | b18f27d | 2010-08-12 18:16:35 -0700 | [diff] [blame] | 111 | } catch (SQLiteException e) { |
| 112 | Log.e(TAG, "exception: " + e.getMessage() + "; query: " + mSql); |
| 113 | throw e; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | } finally { |
Brad Fitzpatrick | 722802e | 2010-03-23 22:22:16 -0700 | [diff] [blame] | 115 | window.releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
| 117 | } finally { |
| 118 | releaseReference(); |
| 119 | mDatabase.unlock(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get the column count for the statement. Only valid on query based |
| 125 | * statements. The database must be locked |
| 126 | * when calling this method. |
| 127 | * |
| 128 | * @return The number of column in the statement's result set. |
| 129 | */ |
| 130 | /* package */ int columnCountLocked() { |
| 131 | acquireReference(); |
| 132 | try { |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 133 | return nativeColumnCount(nStatement); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | } finally { |
| 135 | releaseReference(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Retrieves the column name for the given column index. The database must be locked |
| 141 | * when calling this method. |
| 142 | * |
| 143 | * @param columnIndex the index of the column to get the name for |
| 144 | * @return The requested column's name |
| 145 | */ |
| 146 | /* package */ String columnNameLocked(int columnIndex) { |
| 147 | acquireReference(); |
| 148 | try { |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 149 | return nativeColumnName(nStatement, columnIndex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | } finally { |
| 151 | releaseReference(); |
| 152 | } |
| 153 | } |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | @Override |
| 156 | public String toString() { |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 157 | return "SQLiteQuery: " + mSql; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | } |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 159 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | @Override |
| 161 | public void close() { |
| 162 | super.close(); |
| 163 | mClosed = true; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Called by SQLiteCursor when it is requeried. |
| 168 | */ |
| 169 | /* package */ void requery() { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 170 | if (mClosed) { |
| 171 | throw new IllegalStateException("requerying a closed cursor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | } |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 173 | compileAndbindAllArgs(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | } |