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