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; |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 108 | case DatabaseUtils.STATEMENT_BEGIN: |
| 109 | case DatabaseUtils.STATEMENT_COMMIT: |
| 110 | case DatabaseUtils.STATEMENT_ABORT: |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 111 | mStatementType = n | STATEMENT_DONT_PREPARE; |
| 112 | break; |
| 113 | default: |
| 114 | mStatementType = n; |
| 115 | } |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 116 | db.acquireReference(); |
| 117 | db.addSQLiteClosable(this); |
| 118 | mDatabase = db; |
| 119 | nHandle = db.mNativeHandle; |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 120 | if (bindArgs != null) { |
| 121 | int size = bindArgs.length; |
| 122 | for (int i = 0; i < size; i++) { |
| 123 | this.addToBindArgs(i + 1, bindArgs[i]); |
| 124 | } |
| 125 | } |
Vasu Nori | 2827d6d | 2010-07-04 00:26:18 -0700 | [diff] [blame] | 126 | if (compileFlag) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 127 | compileAndbindAllArgs(); |
Vasu Nori | 2827d6d | 2010-07-04 00:26:18 -0700 | [diff] [blame] | 128 | } |
Vasu Nori | 7501010 | 2010-07-01 16:23:06 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 131 | private void compileSql() { |
| 132 | // only cache CRUD statements |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 133 | if ((mStatementType & STATEMENT_CACHEABLE) == 0) { |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 134 | mCompiledSql = new SQLiteCompiledSql(mDatabase, mSql); |
| 135 | nStatement = mCompiledSql.nStatement; |
| 136 | // since it is not in the cache, no need to acquire() it. |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | mCompiledSql = mDatabase.getCompiledStatementForSql(mSql); |
| 141 | if (mCompiledSql == null) { |
| 142 | // create a new compiled-sql obj |
| 143 | mCompiledSql = new SQLiteCompiledSql(mDatabase, mSql); |
| 144 | |
| 145 | // add it to the cache of compiled-sqls |
| 146 | // but before adding it and thus making it available for anyone else to use it, |
| 147 | // make sure it is acquired by me. |
| 148 | mCompiledSql.acquire(); |
| 149 | mDatabase.addToCompiledQueries(mSql, mCompiledSql); |
| 150 | } else { |
| 151 | // it is already in compiled-sql cache. |
| 152 | // try to acquire the object. |
| 153 | if (!mCompiledSql.acquire()) { |
| 154 | int last = mCompiledSql.nStatement; |
| 155 | // the SQLiteCompiledSql in cache is in use by some other SQLiteProgram object. |
| 156 | // we can't have two different SQLiteProgam objects can't share the same |
| 157 | // CompiledSql object. create a new one. |
| 158 | // finalize it when I am done with it in "this" object. |
| 159 | mCompiledSql = new SQLiteCompiledSql(mDatabase, mSql); |
| 160 | // since it is not in the cache, no need to acquire() it. |
| 161 | } |
| 162 | } |
| 163 | nStatement = mCompiledSql.nStatement; |
| 164 | } |
| 165 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | @Override |
| 167 | protected void onAllReferencesReleased() { |
Vasu Nori | cc6f549 | 2010-08-23 17:05:25 -0700 | [diff] [blame] | 168 | release(); |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 169 | mDatabase.removeSQLiteClosable(this); |
| 170 | mDatabase.releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | @Override |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 174 | protected void onAllReferencesReleasedFromContainer() { |
Vasu Nori | cc6f549 | 2010-08-23 17:05:25 -0700 | [diff] [blame] | 175 | release(); |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 176 | mDatabase.releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 179 | /* package */ void release() { |
Vasu Nori | e495d1f | 2010-01-06 16:34:19 -0800 | [diff] [blame] | 180 | if (mCompiledSql == null) { |
| 181 | return; |
| 182 | } |
Jesse Wilson | 9b5a935 | 2011-02-10 11:19:09 -0800 | [diff] [blame] | 183 | mDatabase.releaseCompiledSqlObj(mSql, mCompiledSql); |
Vasu Nori | 7501010 | 2010-07-01 16:23:06 -0700 | [diff] [blame] | 184 | mCompiledSql = null; |
| 185 | nStatement = 0; |
Vasu Nori | e495d1f | 2010-01-06 16:34:19 -0800 | [diff] [blame] | 186 | } |
| 187 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | /** |
| 189 | * Returns a unique identifier for this program. |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 190 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | * @return a unique identifier for this program |
Vasu Nori | 59d6042 | 2010-07-03 15:37:21 -0700 | [diff] [blame] | 192 | * @deprecated do not use this method. it is not guaranteed to be the same across executions of |
| 193 | * the SQL statement contained in this object. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | */ |
Vasu Nori | 59d6042 | 2010-07-03 15:37:21 -0700 | [diff] [blame] | 195 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | public final int getUniqueId() { |
Vasu Nori | 59d6042 | 2010-07-03 15:37:21 -0700 | [diff] [blame] | 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * used only for testing purposes |
| 202 | */ |
| 203 | /* package */ int getSqlStatementId() { |
| 204 | synchronized(this) { |
| 205 | return (mCompiledSql == null) ? 0 : nStatement; |
| 206 | } |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* package */ String getSqlString() { |
| 210 | return mSql; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
Vasu Nori | 8d45e4e | 2010-02-05 22:35:47 -0800 | [diff] [blame] | 214 | * @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] | 215 | * |
| 216 | * @param sql the SQL string to compile |
| 217 | * @param forceCompilation forces the SQL to be recompiled in the event that there is an |
| 218 | * existing compiled SQL program already around |
| 219 | */ |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 220 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | protected void compile(String sql, boolean forceCompilation) { |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 222 | // TODO is there a need for this? |
| 223 | } |
| 224 | |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 225 | private void bind(int type, int index, Object value) { |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 226 | mDatabase.verifyDbIsOpen(); |
| 227 | addToBindArgs(index, (type == Cursor.FIELD_TYPE_NULL) ? null : value); |
| 228 | if (nStatement > 0) { |
| 229 | // bind only if the SQL statement is compiled |
| 230 | acquireReference(); |
| 231 | try { |
| 232 | switch (type) { |
| 233 | case Cursor.FIELD_TYPE_NULL: |
| 234 | native_bind_null(index); |
| 235 | break; |
| 236 | case Cursor.FIELD_TYPE_BLOB: |
| 237 | native_bind_blob(index, (byte[]) value); |
| 238 | break; |
| 239 | case Cursor.FIELD_TYPE_FLOAT: |
| 240 | native_bind_double(index, (Double) value); |
| 241 | break; |
| 242 | case Cursor.FIELD_TYPE_INTEGER: |
| 243 | native_bind_long(index, (Long) value); |
| 244 | break; |
| 245 | case Cursor.FIELD_TYPE_STRING: |
| 246 | default: |
| 247 | native_bind_string(index, (String) value); |
| 248 | break; |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 249 | } |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 250 | } finally { |
| 251 | releaseReference(); |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | /** |
| 257 | * Bind a NULL value to this statement. The value remains bound until |
| 258 | * {@link #clearBindings} is called. |
| 259 | * |
| 260 | * @param index The 1-based index to the parameter to bind null to |
| 261 | */ |
| 262 | public void bindNull(int index) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 263 | bind(Cursor.FIELD_TYPE_NULL, index, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Bind a long value to this statement. The value remains bound until |
| 268 | * {@link #clearBindings} is called. |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 269 | *addToBindArgs |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | * @param index The 1-based index to the parameter to bind |
| 271 | * @param value The value to bind |
| 272 | */ |
| 273 | public void bindLong(int index, long value) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 274 | bind(Cursor.FIELD_TYPE_INTEGER, index, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Bind a double value to this statement. The value remains bound until |
| 279 | * {@link #clearBindings} is called. |
| 280 | * |
| 281 | * @param index The 1-based index to the parameter to bind |
| 282 | * @param value The value to bind |
| 283 | */ |
| 284 | public void bindDouble(int index, double value) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 285 | bind(Cursor.FIELD_TYPE_FLOAT, index, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Bind a String value to this statement. The value remains bound until |
| 290 | * {@link #clearBindings} is called. |
| 291 | * |
| 292 | * @param index The 1-based index to the parameter to bind |
| 293 | * @param value The value to bind |
| 294 | */ |
| 295 | public void bindString(int index, String value) { |
| 296 | if (value == null) { |
| 297 | throw new IllegalArgumentException("the bind value at index " + index + " is null"); |
| 298 | } |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 299 | bind(Cursor.FIELD_TYPE_STRING, index, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Bind a byte array value to this statement. The value remains bound until |
| 304 | * {@link #clearBindings} is called. |
| 305 | * |
| 306 | * @param index The 1-based index to the parameter to bind |
| 307 | * @param value The value to bind |
| 308 | */ |
| 309 | public void bindBlob(int index, byte[] value) { |
| 310 | if (value == null) { |
| 311 | throw new IllegalArgumentException("the bind value at index " + index + " is null"); |
| 312 | } |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 313 | bind(Cursor.FIELD_TYPE_BLOB, index, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Clears all existing bindings. Unset bindings are treated as NULL. |
| 318 | */ |
| 319 | public void clearBindings() { |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 320 | mBindArgs = null; |
| 321 | if (this.nStatement == 0) { |
| 322 | return; |
| 323 | } |
| 324 | mDatabase.verifyDbIsOpen(); |
| 325 | acquireReference(); |
| 326 | try { |
| 327 | native_clear_bindings(); |
| 328 | } finally { |
| 329 | releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Release this program's resources, making it invalid. |
| 335 | */ |
| 336 | public void close() { |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 337 | mBindArgs = null; |
| 338 | if (nHandle == 0 || !mDatabase.isOpen()) { |
| 339 | return; |
Vasu Nori | c8e1f23 | 2010-04-13 15:05:09 -0700 | [diff] [blame] | 340 | } |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 341 | releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 344 | private void addToBindArgs(int index, Object value) { |
Vasu Nori | ce38b98 | 2010-07-22 13:57:13 -0700 | [diff] [blame] | 345 | if (mBindArgs == null) { |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 346 | mBindArgs = new HashMap<Integer, Object>(); |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 347 | } |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 348 | mBindArgs.put(index, value); |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 351 | /* package */ void compileAndbindAllArgs() { |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 352 | if ((mStatementType & STATEMENT_DONT_PREPARE) > 0) { |
Vasu Nori | cc1eaf6 | 2011-03-14 19:22:16 -0700 | [diff] [blame] | 353 | if (mBindArgs != null) { |
| 354 | throw new IllegalArgumentException("Can't pass bindargs for this sql :" + mSql); |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 355 | } |
Vasu Nori | cc1eaf6 | 2011-03-14 19:22:16 -0700 | [diff] [blame] | 356 | // no need to prepare this SQL statement |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 357 | return; |
| 358 | } |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 359 | if (nStatement == 0) { |
| 360 | // SQL statement is not compiled yet. compile it now. |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 361 | compileSql(); |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 362 | } |
Vasu Nori | ce38b98 | 2010-07-22 13:57:13 -0700 | [diff] [blame] | 363 | if (mBindArgs == null) { |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 364 | return; |
| 365 | } |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 366 | for (int index : mBindArgs.keySet()) { |
| 367 | Object value = mBindArgs.get(index); |
| 368 | if (value == null) { |
| 369 | native_bind_null(index); |
| 370 | } else if (value instanceof Double || value instanceof Float) { |
| 371 | native_bind_double(index, ((Number) value).doubleValue()); |
| 372 | } else if (value instanceof Number) { |
| 373 | native_bind_long(index, ((Number) value).longValue()); |
| 374 | } else if (value instanceof Boolean) { |
| 375 | Boolean bool = (Boolean)value; |
| 376 | native_bind_long(index, (bool) ? 1 : 0); |
| 377 | if (bool) { |
| 378 | native_bind_long(index, 1); |
| 379 | } else { |
| 380 | native_bind_long(index, 0); |
| 381 | } |
| 382 | } else if (value instanceof byte[]){ |
| 383 | native_bind_blob(index, (byte[]) value); |
| 384 | } else { |
| 385 | native_bind_string(index, value.toString()); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Given an array of String bindArgs, this method binds all of them in one single call. |
| 392 | * |
| 393 | * @param bindArgs the String array of bind args. |
| 394 | */ |
| 395 | public void bindAllArgsAsStrings(String[] bindArgs) { |
| 396 | if (bindArgs == null) { |
| 397 | return; |
| 398 | } |
| 399 | int size = bindArgs.length; |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 400 | for (int i = 0; i < size; i++) { |
| 401 | bindString(i + 1, bindArgs[i]); |
Vasu Nori | e25539f | 2010-07-08 17:06:13 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Vasu Nori | 1966632 | 2010-09-10 14:09:00 -0700 | [diff] [blame] | 405 | /* package */ synchronized final void setNativeHandle(int nHandle) { |
| 406 | this.nHandle = nHandle; |
| 407 | } |
| 408 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | /** |
Vasu Nori | 8d45e4e | 2010-02-05 22:35:47 -0800 | [diff] [blame] | 410 | * @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] | 411 | * Compiles SQL into a SQLite program. |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 412 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | * <P>The database lock must be held when calling this method. |
| 414 | * @param sql The SQL to compile. |
| 415 | */ |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 416 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | protected final native void native_compile(String sql); |
Vasu Nori | 8d45e4e | 2010-02-05 22:35:47 -0800 | [diff] [blame] | 418 | |
| 419 | /** |
| 420 | * @deprecated This method is deprecated and must not be used. |
| 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_finalize(); |
| 424 | |
| 425 | protected final native void native_bind_null(int index); |
| 426 | protected final native void native_bind_long(int index, long value); |
| 427 | protected final native void native_bind_double(int index, double value); |
| 428 | protected final native void native_bind_string(int index, String value); |
| 429 | protected final native void native_bind_blob(int index, byte[] value); |
Vasu Nori | 0732f79 | 2010-07-29 17:24:12 -0700 | [diff] [blame] | 430 | private final native void native_clear_bindings(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | } |
| 432 | |