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 | |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 19 | import android.content.CancellationSignal; |
Vasu Nori | ce38b98 | 2010-07-22 13:57:13 -0700 | [diff] [blame] | 20 | import android.database.DatabaseUtils; |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 21 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 22 | import java.util.Arrays; |
Vasu Nori | 3695709 | 2010-03-11 14:57:53 -0800 | [diff] [blame] | 23 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | /** |
| 25 | * A base class for compiled SQLite programs. |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 26 | * <p> |
| 27 | * This class is not thread-safe. |
| 28 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | */ |
| 30 | public abstract class SQLiteProgram extends SQLiteClosable { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 31 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 33 | private final SQLiteDatabase mDatabase; |
| 34 | private final String mSql; |
| 35 | private final boolean mReadOnly; |
| 36 | private final String[] mColumnNames; |
| 37 | private final int mNumParameters; |
| 38 | private final Object[] mBindArgs; |
Vasu Nori | 3695709 | 2010-03-11 14:57:53 -0800 | [diff] [blame] | 39 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 40 | SQLiteProgram(SQLiteDatabase db, String sql, Object[] bindArgs, |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 41 | CancellationSignal cancellationSignalForPrepare) { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 42 | mDatabase = db; |
Vasu Nori | d606b4b | 2010-02-24 12:54:20 -0800 | [diff] [blame] | 43 | mSql = sql.trim(); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 44 | |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 45 | int n = DatabaseUtils.getSqlStatementType(mSql); |
| 46 | switch (n) { |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 47 | case DatabaseUtils.STATEMENT_BEGIN: |
| 48 | case DatabaseUtils.STATEMENT_COMMIT: |
| 49 | case DatabaseUtils.STATEMENT_ABORT: |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 50 | mReadOnly = false; |
| 51 | mColumnNames = EMPTY_STRING_ARRAY; |
| 52 | mNumParameters = 0; |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 53 | break; |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 54 | |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 55 | default: |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 56 | boolean assumeReadOnly = (n == DatabaseUtils.STATEMENT_SELECT); |
| 57 | SQLiteStatementInfo info = new SQLiteStatementInfo(); |
| 58 | db.getThreadSession().prepare(mSql, |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 59 | db.getThreadDefaultConnectionFlags(assumeReadOnly), |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 60 | cancellationSignalForPrepare, info); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 61 | mReadOnly = info.readOnly; |
| 62 | mColumnNames = info.columnNames; |
| 63 | mNumParameters = info.numParameters; |
| 64 | break; |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 67 | if (mNumParameters != 0) { |
| 68 | mBindArgs = new Object[mNumParameters]; |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 69 | } else { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 70 | mBindArgs = null; |
| 71 | } |
| 72 | |
| 73 | if (bindArgs != null) { |
| 74 | if (bindArgs.length > mNumParameters) { |
| 75 | throw new IllegalArgumentException("Too many bind arguments. " |
| 76 | + bindArgs.length + " arguments were provided but the statement needs " |
| 77 | + mNumParameters + " arguments."); |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 78 | } |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 79 | System.arraycopy(bindArgs, 0, mBindArgs, 0, bindArgs.length); |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 80 | } |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 83 | final SQLiteDatabase getDatabase() { |
| 84 | return mDatabase; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | } |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 86 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 87 | final String getSql() { |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 88 | return mSql; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 91 | final Object[] getBindArgs() { |
| 92 | return mBindArgs; |
| 93 | } |
| 94 | |
| 95 | final String[] getColumnNames() { |
| 96 | return mColumnNames; |
| 97 | } |
| 98 | |
| 99 | /** @hide */ |
| 100 | protected final SQLiteSession getSession() { |
| 101 | return mDatabase.getThreadSession(); |
| 102 | } |
| 103 | |
| 104 | /** @hide */ |
| 105 | protected final int getConnectionFlags() { |
| 106 | return mDatabase.getThreadDefaultConnectionFlags(mReadOnly); |
| 107 | } |
| 108 | |
| 109 | /** @hide */ |
| 110 | protected final void onCorruption() { |
| 111 | mDatabase.onCorruption(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Unimplemented. |
| 116 | * @deprecated This method is deprecated and must not be used. |
| 117 | */ |
| 118 | @Deprecated |
| 119 | public final int getUniqueId() { |
| 120 | return -1; |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 121 | } |
| 122 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | /** |
| 124 | * Bind a NULL value to this statement. The value remains bound until |
| 125 | * {@link #clearBindings} is called. |
| 126 | * |
| 127 | * @param index The 1-based index to the parameter to bind null to |
| 128 | */ |
| 129 | public void bindNull(int index) { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 130 | bind(index, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Bind a long value to this statement. The value remains bound until |
| 135 | * {@link #clearBindings} is called. |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 136 | *addToBindArgs |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | * @param index The 1-based index to the parameter to bind |
| 138 | * @param value The value to bind |
| 139 | */ |
| 140 | public void bindLong(int index, long value) { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 141 | bind(index, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Bind a double value to this statement. The value remains bound until |
| 146 | * {@link #clearBindings} is called. |
| 147 | * |
| 148 | * @param index The 1-based index to the parameter to bind |
| 149 | * @param value The value to bind |
| 150 | */ |
| 151 | public void bindDouble(int index, double value) { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 152 | bind(index, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Bind a String value to this statement. The value remains bound until |
| 157 | * {@link #clearBindings} is called. |
| 158 | * |
| 159 | * @param index The 1-based index to the parameter to bind |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 160 | * @param value The value to bind, must not be null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | */ |
| 162 | public void bindString(int index, String value) { |
| 163 | if (value == null) { |
| 164 | throw new IllegalArgumentException("the bind value at index " + index + " is null"); |
| 165 | } |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 166 | bind(index, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Bind a byte array value to this statement. The value remains bound until |
| 171 | * {@link #clearBindings} is called. |
| 172 | * |
| 173 | * @param index The 1-based index to the parameter to bind |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 174 | * @param value The value to bind, must not be null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | */ |
| 176 | public void bindBlob(int index, byte[] value) { |
| 177 | if (value == null) { |
| 178 | throw new IllegalArgumentException("the bind value at index " + index + " is null"); |
| 179 | } |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 180 | bind(index, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Clears all existing bindings. Unset bindings are treated as NULL. |
| 185 | */ |
| 186 | public void clearBindings() { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 187 | if (mBindArgs != null) { |
| 188 | Arrays.fill(mBindArgs, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 193 | * Given an array of String bindArgs, this method binds all of them in one single call. |
| 194 | * |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 195 | * @param bindArgs the String array of bind args, none of which must be null. |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 196 | */ |
| 197 | public void bindAllArgsAsStrings(String[] bindArgs) { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 198 | if (bindArgs != null) { |
| 199 | for (int i = bindArgs.length; i != 0; i--) { |
| 200 | bindString(i, bindArgs[i - 1]); |
| 201 | } |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 205 | @Override |
| 206 | protected void onAllReferencesReleased() { |
| 207 | clearBindings(); |
Vasu Nori | 1966632 | 2010-09-10 14:09:00 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 210 | private void bind(int index, Object value) { |
| 211 | if (index < 1 || index > mNumParameters) { |
| 212 | throw new IllegalArgumentException("Cannot bind argument at index " |
| 213 | + index + " because the index is out of range. " |
| 214 | + "The statement has " + mNumParameters + " parameters."); |
| 215 | } |
| 216 | mBindArgs[index - 1] = value; |
| 217 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | } |