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 | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 34 | private static native long nativeFillWindow(int databasePtr, int statementPtr, int windowPtr, |
| 35 | int offsetParam, int startPos, int requiredPos, boolean countAllRows); |
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 |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 76 | * @param startPos The start position for filling the window. |
| 77 | * @param requiredPos The position of a row that MUST be in the window. |
| 78 | * If it won't fit, then the query should discard part of what it filled. |
| 79 | * @param countAllRows True to count all rows that the query would |
| 80 | * return regardless of whether they fit in the window. |
| 81 | * @return Number of rows that were enumerated. Might not be all rows |
| 82 | * unless countAllRows is true. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | */ |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 84 | /* package */ int fillWindow(CursorWindow window, |
| 85 | int startPos, int requiredPos, boolean countAllRows) { |
Vasu Nori | 16057fa | 2011-03-18 11:40:37 -0700 | [diff] [blame] | 86 | mDatabase.lock(mSql); |
Brad Fitzpatrick | 6a35387 | 2010-02-11 18:04:49 -0800 | [diff] [blame] | 87 | long timeStart = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | try { |
| 89 | acquireReference(); |
| 90 | try { |
| 91 | window.acquireReference(); |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 92 | long result = nativeFillWindow(nHandle, nStatement, window.mWindowPtr, |
| 93 | mOffsetIndex, startPos, requiredPos, countAllRows); |
| 94 | int actualPos = (int)(result >> 32); |
| 95 | int countedRows = (int)result; |
| 96 | window.setStartPosition(actualPos); |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 97 | if (SQLiteDebug.DEBUG_LOG_SLOW_QUERIES) { |
| 98 | long elapsed = SystemClock.uptimeMillis() - timeStart; |
| 99 | if (SQLiteDebug.shouldLogSlowQuery(elapsed)) { |
| 100 | Log.d(TAG, "fillWindow took " + elapsed |
| 101 | + " ms: window=\"" + window |
| 102 | + "\", startPos=" + startPos |
Jeff Brown | fa59cfb | 2011-10-31 15:10:10 -0700 | [diff] [blame] | 103 | + ", requiredPos=" + requiredPos |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 104 | + ", offset=" + mOffsetIndex |
Jeff Brown | fa59cfb | 2011-10-31 15:10:10 -0700 | [diff] [blame] | 105 | + ", actualPos=" + actualPos |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 106 | + ", filledRows=" + window.getNumRows() |
Jeff Brown | fa59cfb | 2011-10-31 15:10:10 -0700 | [diff] [blame] | 107 | + ", countedRows=" + countedRows |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 108 | + ", query=\"" + mSql + "\"" |
| 109 | + ", args=[" + (mBindArgs != null ? |
| 110 | TextUtils.join(", ", mBindArgs.values()) : "") |
| 111 | + "]"); |
| 112 | } |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 113 | } |
Dan Egnor | 1231195 | 2009-11-23 14:47:45 -0800 | [diff] [blame] | 114 | mDatabase.logTimeStat(mSql, timeStart); |
Jeff Brown | 650de3d | 2011-10-27 14:52:28 -0700 | [diff] [blame] | 115 | return countedRows; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } catch (IllegalStateException e){ |
| 117 | // simply ignore it |
| 118 | return 0; |
| 119 | } catch (SQLiteDatabaseCorruptException e) { |
| 120 | mDatabase.onCorruption(); |
| 121 | throw e; |
Vasu Nori | b18f27d | 2010-08-12 18:16:35 -0700 | [diff] [blame] | 122 | } catch (SQLiteException e) { |
| 123 | Log.e(TAG, "exception: " + e.getMessage() + "; query: " + mSql); |
| 124 | throw e; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | } finally { |
Brad Fitzpatrick | 722802e | 2010-03-23 22:22:16 -0700 | [diff] [blame] | 126 | window.releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | } |
| 128 | } finally { |
| 129 | releaseReference(); |
| 130 | mDatabase.unlock(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get the column count for the statement. Only valid on query based |
| 136 | * statements. The database must be locked |
| 137 | * when calling this method. |
| 138 | * |
| 139 | * @return The number of column in the statement's result set. |
| 140 | */ |
| 141 | /* package */ int columnCountLocked() { |
| 142 | acquireReference(); |
| 143 | try { |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 144 | return nativeColumnCount(nStatement); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } finally { |
| 146 | releaseReference(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Retrieves the column name for the given column index. The database must be locked |
| 152 | * when calling this method. |
| 153 | * |
| 154 | * @param columnIndex the index of the column to get the name for |
| 155 | * @return The requested column's name |
| 156 | */ |
| 157 | /* package */ String columnNameLocked(int columnIndex) { |
| 158 | acquireReference(); |
| 159 | try { |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 160 | return nativeColumnName(nStatement, columnIndex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | } finally { |
| 162 | releaseReference(); |
| 163 | } |
| 164 | } |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 165 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | @Override |
| 167 | public String toString() { |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 168 | return "SQLiteQuery: " + mSql; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | } |
Jeff Brown | 7ce7452 | 2011-10-07 13:29:37 -0700 | [diff] [blame] | 170 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | @Override |
| 172 | public void close() { |
| 173 | super.close(); |
| 174 | mClosed = true; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Called by SQLiteCursor when it is requeried. |
| 179 | */ |
| 180 | /* package */ void requery() { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 181 | if (mClosed) { |
| 182 | throw new IllegalStateException("requerying a closed cursor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 184 | compileAndbindAllArgs(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | } |