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 | |
Vasu Nori | ce38b98 | 2010-07-22 13:57:13 -0700 | [diff] [blame] | 19 | import android.database.DatabaseUtils; |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 20 | import android.database.Cursor; |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 21 | |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 22 | import java.util.HashMap; |
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. |
Vasu Nori | ccd9544 | 2010-05-28 17:04:16 -0700 | [diff] [blame] | 26 | *<p> |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 27 | * SQLiteProgram is NOT internally synchronized so code using a SQLiteProgram from multiple |
Jeff Hamilton | f3ca9a5 | 2010-05-12 15:04:33 -0700 | [diff] [blame] | 28 | * threads should perform its own synchronization when using the SQLiteProgram. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | */ |
| 30 | public abstract class SQLiteProgram extends SQLiteClosable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
Vasu Nori | 3695709 | 2010-03-11 14:57:53 -0800 | [diff] [blame] | 32 | private static final String TAG = "SQLiteProgram"; |
| 33 | |
Vasu Nori | 14b60e7 | 2010-03-01 14:47:47 -0800 | [diff] [blame] | 34 | /** The database this program is compiled against. |
| 35 | * @deprecated do not use this |
| 36 | */ |
| 37 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | protected SQLiteDatabase mDatabase; |
| 39 | |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 40 | /** The SQL used to create this query */ |
| 41 | /* package */ final String mSql; |
| 42 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Native linkage, do not modify. This comes from the database and should not be modified |
| 45 | * in here or in the native code. |
Vasu Nori | 14b60e7 | 2010-03-01 14:47:47 -0800 | [diff] [blame] | 46 | * @deprecated do not use this |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | */ |
Vasu Nori | 14b60e7 | 2010-03-01 14:47:47 -0800 | [diff] [blame] | 48 | @Deprecated |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 49 | protected int nHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
Vasu Nori | e495d1f | 2010-01-06 16:34:19 -0800 | [diff] [blame] | 52 | * the SQLiteCompiledSql object for the given sql statement. |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 53 | */ |
Vasu Nori | 422dad0 | 2010-09-03 16:03:08 -0700 | [diff] [blame] | 54 | /* package */ SQLiteCompiledSql mCompiledSql; |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
Vasu Nori | e495d1f | 2010-01-06 16:34:19 -0800 | [diff] [blame] | 57 | * SQLiteCompiledSql statement id is populated with the corresponding object from the above |
| 58 | * member. This member is used by the native_bind_* methods |
Vasu Nori | 14b60e7 | 2010-03-01 14:47:47 -0800 | [diff] [blame] | 59 | * @deprecated do not use this |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | */ |
Vasu Nori | 14b60e7 | 2010-03-01 14:47:47 -0800 | [diff] [blame] | 61 | @Deprecated |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 62 | protected int nStatement; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 64 | /** |
| 65 | * In the case of {@link SQLiteStatement}, this member stores the bindargs passed |
| 66 | * to the following methods, instead of actually doing the binding. |
| 67 | * <ul> |
| 68 | * <li>{@link #bindBlob(int, byte[])}</li> |
| 69 | * <li>{@link #bindDouble(int, double)}</li> |
| 70 | * <li>{@link #bindLong(int, long)}</li> |
| 71 | * <li>{@link #bindNull(int)}</li> |
| 72 | * <li>{@link #bindString(int, String)}</li> |
| 73 | * </ul> |
| 74 | * <p> |
| 75 | * Each entry in the array is a Pair of |
| 76 | * <ol> |
| 77 | * <li>bind arg position number</li> |
| 78 | * <li>the value to be bound to the bindarg</li> |
| 79 | * </ol> |
| 80 | * <p> |
| 81 | * It is lazily initialized in the above bind methods |
| 82 | * and it is cleared in {@link #clearBindings()} method. |
| 83 | * <p> |
| 84 | * It is protected (in multi-threaded environment) by {@link SQLiteProgram}.this |
| 85 | */ |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 86 | /* package */ HashMap<Integer, Object> mBindArgs = null; |
Vasu Nori | ce38b98 | 2010-07-22 13:57:13 -0700 | [diff] [blame] | 87 | /* package */ final int mStatementType; |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 88 | /* package */ static final int STATEMENT_CACHEABLE = 16; |
| 89 | /* package */ static final int STATEMENT_DONT_PREPARE = 32; |
| 90 | /* package */ static final int STATEMENT_USE_POOLED_CONN = 64; |
| 91 | /* package */ static final int STATEMENT_TYPE_MASK = 0x0f; |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | /* package */ SQLiteProgram(SQLiteDatabase db, String sql) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 94 | this(db, sql, null, true); |
Vasu Nori | 2827d6d | 2010-07-04 00:26:18 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 97 | /* package */ SQLiteProgram(SQLiteDatabase db, String sql, Object[] bindArgs, |
| 98 | boolean compileFlag) { |
Vasu Nori | d606b4b | 2010-02-24 12:54:20 -0800 | [diff] [blame] | 99 | mSql = sql.trim(); |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 100 | int n = DatabaseUtils.getSqlStatementType(mSql); |
| 101 | switch (n) { |
| 102 | case DatabaseUtils.STATEMENT_UPDATE: |
| 103 | mStatementType = n | STATEMENT_CACHEABLE; |
| 104 | break; |
| 105 | case DatabaseUtils.STATEMENT_SELECT: |
| 106 | mStatementType = n | STATEMENT_CACHEABLE | STATEMENT_USE_POOLED_CONN; |
| 107 | break; |
| 108 | case DatabaseUtils.STATEMENT_ATTACH: |
| 109 | case DatabaseUtils.STATEMENT_BEGIN: |
| 110 | case DatabaseUtils.STATEMENT_COMMIT: |
| 111 | case DatabaseUtils.STATEMENT_ABORT: |
| 112 | case DatabaseUtils.STATEMENT_DDL: |
| 113 | case DatabaseUtils.STATEMENT_UNPREPARED: |
| 114 | mStatementType = n | STATEMENT_DONT_PREPARE; |
| 115 | break; |
| 116 | default: |
| 117 | mStatementType = n; |
| 118 | } |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 119 | db.acquireReference(); |
| 120 | db.addSQLiteClosable(this); |
| 121 | mDatabase = db; |
| 122 | nHandle = db.mNativeHandle; |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 123 | if (bindArgs != null) { |
| 124 | int size = bindArgs.length; |
| 125 | for (int i = 0; i < size; i++) { |
| 126 | this.addToBindArgs(i + 1, bindArgs[i]); |
| 127 | } |
| 128 | } |
Vasu Nori | 2827d6d | 2010-07-04 00:26:18 -0700 | [diff] [blame] | 129 | if (compileFlag) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 130 | compileAndbindAllArgs(); |
Vasu Nori | 2827d6d | 2010-07-04 00:26:18 -0700 | [diff] [blame] | 131 | } |
Vasu Nori | 7501010 | 2010-07-01 16:23:06 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 134 | private void compileSql() { |
| 135 | // only cache CRUD statements |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 136 | if ((mStatementType & STATEMENT_CACHEABLE) == 0) { |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 137 | mCompiledSql = new SQLiteCompiledSql(mDatabase, mSql); |
| 138 | nStatement = mCompiledSql.nStatement; |
| 139 | // since it is not in the cache, no need to acquire() it. |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | mCompiledSql = mDatabase.getCompiledStatementForSql(mSql); |
| 144 | if (mCompiledSql == null) { |
| 145 | // create a new compiled-sql obj |
| 146 | mCompiledSql = new SQLiteCompiledSql(mDatabase, mSql); |
| 147 | |
| 148 | // add it to the cache of compiled-sqls |
| 149 | // but before adding it and thus making it available for anyone else to use it, |
| 150 | // make sure it is acquired by me. |
| 151 | mCompiledSql.acquire(); |
| 152 | mDatabase.addToCompiledQueries(mSql, mCompiledSql); |
| 153 | } else { |
| 154 | // it is already in compiled-sql cache. |
| 155 | // try to acquire the object. |
| 156 | if (!mCompiledSql.acquire()) { |
| 157 | int last = mCompiledSql.nStatement; |
| 158 | // the SQLiteCompiledSql in cache is in use by some other SQLiteProgram object. |
| 159 | // we can't have two different SQLiteProgam objects can't share the same |
| 160 | // CompiledSql object. create a new one. |
| 161 | // finalize it when I am done with it in "this" object. |
| 162 | mCompiledSql = new SQLiteCompiledSql(mDatabase, mSql); |
| 163 | // since it is not in the cache, no need to acquire() it. |
| 164 | } |
| 165 | } |
| 166 | nStatement = mCompiledSql.nStatement; |
| 167 | } |
| 168 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | @Override |
| 170 | protected void onAllReferencesReleased() { |
Vasu Nori | cc6f549 | 2010-08-23 17:05:25 -0700 | [diff] [blame] | 171 | release(); |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 172 | mDatabase.removeSQLiteClosable(this); |
| 173 | mDatabase.releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | } |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | @Override |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 177 | protected void onAllReferencesReleasedFromContainer() { |
Vasu Nori | cc6f549 | 2010-08-23 17:05:25 -0700 | [diff] [blame] | 178 | release(); |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 179 | mDatabase.releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 182 | /* package */ void release() { |
Vasu Nori | e495d1f | 2010-01-06 16:34:19 -0800 | [diff] [blame] | 183 | if (mCompiledSql == null) { |
| 184 | return; |
| 185 | } |
Jesse Wilson | 9b5a935 | 2011-02-10 11:19:09 -0800 | [diff] [blame] | 186 | mDatabase.releaseCompiledSqlObj(mSql, mCompiledSql); |
Vasu Nori | 7501010 | 2010-07-01 16:23:06 -0700 | [diff] [blame] | 187 | mCompiledSql = null; |
| 188 | nStatement = 0; |
Vasu Nori | e495d1f | 2010-01-06 16:34:19 -0800 | [diff] [blame] | 189 | } |
| 190 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | /** |
| 192 | * Returns a unique identifier for this program. |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 193 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | * @return a unique identifier for this program |
Vasu Nori | 59d6042 | 2010-07-03 15:37:21 -0700 | [diff] [blame] | 195 | * @deprecated do not use this method. it is not guaranteed to be the same across executions of |
| 196 | * the SQL statement contained in this object. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | */ |
Vasu Nori | 59d6042 | 2010-07-03 15:37:21 -0700 | [diff] [blame] | 198 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | public final int getUniqueId() { |
Vasu Nori | 59d6042 | 2010-07-03 15:37:21 -0700 | [diff] [blame] | 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * used only for testing purposes |
| 205 | */ |
| 206 | /* package */ int getSqlStatementId() { |
| 207 | synchronized(this) { |
| 208 | return (mCompiledSql == null) ? 0 : nStatement; |
| 209 | } |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* package */ String getSqlString() { |
| 213 | return mSql; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
Vasu Nori | 8d45e4e | 2010-02-05 22:35:47 -0800 | [diff] [blame] | 217 | * @deprecated This method is deprecated and must not be used. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | * |
| 219 | * @param sql the SQL string to compile |
| 220 | * @param forceCompilation forces the SQL to be recompiled in the event that there is an |
| 221 | * existing compiled SQL program already around |
| 222 | */ |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 223 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | protected void compile(String sql, boolean forceCompilation) { |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 225 | // TODO is there a need for this? |
| 226 | } |
| 227 | |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 228 | private void bind(int type, int index, Object value) { |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 229 | mDatabase.verifyDbIsOpen(); |
| 230 | addToBindArgs(index, (type == Cursor.FIELD_TYPE_NULL) ? null : value); |
| 231 | if (nStatement > 0) { |
| 232 | // bind only if the SQL statement is compiled |
| 233 | acquireReference(); |
| 234 | try { |
| 235 | switch (type) { |
| 236 | case Cursor.FIELD_TYPE_NULL: |
| 237 | native_bind_null(index); |
| 238 | break; |
| 239 | case Cursor.FIELD_TYPE_BLOB: |
| 240 | native_bind_blob(index, (byte[]) value); |
| 241 | break; |
| 242 | case Cursor.FIELD_TYPE_FLOAT: |
| 243 | native_bind_double(index, (Double) value); |
| 244 | break; |
| 245 | case Cursor.FIELD_TYPE_INTEGER: |
| 246 | native_bind_long(index, (Long) value); |
| 247 | break; |
| 248 | case Cursor.FIELD_TYPE_STRING: |
| 249 | default: |
| 250 | native_bind_string(index, (String) value); |
| 251 | break; |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 252 | } |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 253 | } finally { |
| 254 | releaseReference(); |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | /** |
| 260 | * Bind a NULL value to this statement. The value remains bound until |
| 261 | * {@link #clearBindings} is called. |
| 262 | * |
| 263 | * @param index The 1-based index to the parameter to bind null to |
| 264 | */ |
| 265 | public void bindNull(int index) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 266 | bind(Cursor.FIELD_TYPE_NULL, index, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Bind a long value to this statement. The value remains bound until |
| 271 | * {@link #clearBindings} is called. |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 272 | *addToBindArgs |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | * @param index The 1-based index to the parameter to bind |
| 274 | * @param value The value to bind |
| 275 | */ |
| 276 | public void bindLong(int index, long value) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 277 | bind(Cursor.FIELD_TYPE_INTEGER, index, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Bind a double value to this statement. The value remains bound until |
| 282 | * {@link #clearBindings} is called. |
| 283 | * |
| 284 | * @param index The 1-based index to the parameter to bind |
| 285 | * @param value The value to bind |
| 286 | */ |
| 287 | public void bindDouble(int index, double value) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 288 | bind(Cursor.FIELD_TYPE_FLOAT, index, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Bind a String value to this statement. The value remains bound until |
| 293 | * {@link #clearBindings} is called. |
| 294 | * |
| 295 | * @param index The 1-based index to the parameter to bind |
| 296 | * @param value The value to bind |
| 297 | */ |
| 298 | public void bindString(int index, String value) { |
| 299 | if (value == null) { |
| 300 | throw new IllegalArgumentException("the bind value at index " + index + " is null"); |
| 301 | } |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 302 | bind(Cursor.FIELD_TYPE_STRING, index, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Bind a byte array value to this statement. The value remains bound until |
| 307 | * {@link #clearBindings} is called. |
| 308 | * |
| 309 | * @param index The 1-based index to the parameter to bind |
| 310 | * @param value The value to bind |
| 311 | */ |
| 312 | public void bindBlob(int index, byte[] value) { |
| 313 | if (value == null) { |
| 314 | throw new IllegalArgumentException("the bind value at index " + index + " is null"); |
| 315 | } |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 316 | bind(Cursor.FIELD_TYPE_BLOB, index, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Clears all existing bindings. Unset bindings are treated as NULL. |
| 321 | */ |
| 322 | public void clearBindings() { |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 323 | mBindArgs = null; |
| 324 | if (this.nStatement == 0) { |
| 325 | return; |
| 326 | } |
| 327 | mDatabase.verifyDbIsOpen(); |
| 328 | acquireReference(); |
| 329 | try { |
| 330 | native_clear_bindings(); |
| 331 | } finally { |
| 332 | releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Release this program's resources, making it invalid. |
| 338 | */ |
| 339 | public void close() { |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 340 | mBindArgs = null; |
| 341 | if (nHandle == 0 || !mDatabase.isOpen()) { |
| 342 | return; |
Vasu Nori | c8e1f23 | 2010-04-13 15:05:09 -0700 | [diff] [blame] | 343 | } |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 344 | releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 347 | private void addToBindArgs(int index, Object value) { |
Vasu Nori | ce38b98 | 2010-07-22 13:57:13 -0700 | [diff] [blame] | 348 | if (mBindArgs == null) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 349 | mBindArgs = new HashMap<Integer, Object>(); |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 350 | } |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 351 | mBindArgs.put(index, value); |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 354 | /* package */ void compileAndbindAllArgs() { |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 355 | if ((mStatementType & STATEMENT_DONT_PREPARE) > 0) { |
| 356 | // no need to prepare this SQL statement |
| 357 | if (SQLiteDebug.DEBUG_SQL_STATEMENTS) { |
| 358 | if (mBindArgs != null) { |
| 359 | throw new IllegalArgumentException("no need to pass bindargs for this sql :" + |
| 360 | mSql); |
| 361 | } |
| 362 | } |
| 363 | return; |
| 364 | } |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 365 | if (nStatement == 0) { |
| 366 | // SQL statement is not compiled yet. compile it now. |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 367 | compileSql(); |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 368 | } |
Vasu Nori | ce38b98 | 2010-07-22 13:57:13 -0700 | [diff] [blame] | 369 | if (mBindArgs == null) { |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 370 | return; |
| 371 | } |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 372 | for (int index : mBindArgs.keySet()) { |
| 373 | Object value = mBindArgs.get(index); |
| 374 | if (value == null) { |
| 375 | native_bind_null(index); |
| 376 | } else if (value instanceof Double || value instanceof Float) { |
| 377 | native_bind_double(index, ((Number) value).doubleValue()); |
| 378 | } else if (value instanceof Number) { |
| 379 | native_bind_long(index, ((Number) value).longValue()); |
| 380 | } else if (value instanceof Boolean) { |
| 381 | Boolean bool = (Boolean)value; |
| 382 | native_bind_long(index, (bool) ? 1 : 0); |
| 383 | if (bool) { |
| 384 | native_bind_long(index, 1); |
| 385 | } else { |
| 386 | native_bind_long(index, 0); |
| 387 | } |
| 388 | } else if (value instanceof byte[]){ |
| 389 | native_bind_blob(index, (byte[]) value); |
| 390 | } else { |
| 391 | native_bind_string(index, value.toString()); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Given an array of String bindArgs, this method binds all of them in one single call. |
| 398 | * |
| 399 | * @param bindArgs the String array of bind args. |
| 400 | */ |
| 401 | public void bindAllArgsAsStrings(String[] bindArgs) { |
| 402 | if (bindArgs == null) { |
| 403 | return; |
| 404 | } |
| 405 | int size = bindArgs.length; |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 406 | for (int i = 0; i < size; i++) { |
| 407 | bindString(i + 1, bindArgs[i]); |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Vasu Nori | 1966632 | 2010-09-10 14:09:00 -0700 | [diff] [blame] | 411 | /* package */ synchronized final void setNativeHandle(int nHandle) { |
| 412 | this.nHandle = nHandle; |
| 413 | } |
| 414 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | /** |
Vasu Nori | 8d45e4e | 2010-02-05 22:35:47 -0800 | [diff] [blame] | 416 | * @deprecated This method is deprecated and must not be used. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | * Compiles SQL into a SQLite program. |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 418 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | * <P>The database lock must be held when calling this method. |
| 420 | * @param sql The SQL to compile. |
| 421 | */ |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 422 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | protected final native void native_compile(String sql); |
Vasu Nori | 8d45e4e | 2010-02-05 22:35:47 -0800 | [diff] [blame] | 424 | |
| 425 | /** |
| 426 | * @deprecated This method is deprecated and must not be used. |
| 427 | */ |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 428 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | protected final native void native_finalize(); |
| 430 | |
| 431 | protected final native void native_bind_null(int index); |
| 432 | protected final native void native_bind_long(int index, long value); |
| 433 | protected final native void native_bind_double(int index, double value); |
| 434 | protected final native void native_bind_string(int index, String value); |
| 435 | protected final native void native_bind_blob(int index, byte[] value); |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 436 | private final native void native_clear_bindings(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | } |
| 438 | |