Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 19 | import android.os.StrictMode; |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 20 | import android.util.Log; |
| 21 | |
| 22 | /** |
| 23 | * This class encapsulates compilation of sql statement and release of the compiled statement obj. |
| 24 | * Once a sql statement is compiled, it is cached in {@link SQLiteDatabase} |
| 25 | * and it is released in one of the 2 following ways |
| 26 | * 1. when {@link SQLiteDatabase} object is closed. |
Vasu Nori | afe806a | 2010-03-09 11:37:52 -0800 | [diff] [blame] | 27 | * 2. if this is not cached in {@link SQLiteDatabase}, {@link android.database.Cursor#close()} |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 28 | * releaases this obj. |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 29 | */ |
| 30 | /* package */ class SQLiteCompiledSql { |
| 31 | |
Vasu Nori | 14b60e7 | 2010-03-01 14:47:47 -0800 | [diff] [blame] | 32 | private static final String TAG = "SQLiteCompiledSql"; |
| 33 | |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 34 | /** The database this program is compiled against. */ |
Vasu Nori | 5e89ae2 | 2010-09-15 14:23:29 -0700 | [diff] [blame] | 35 | /* package */ final SQLiteDatabase mDatabase; |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Native linkage, do not modify. This comes from the database. |
| 39 | */ |
Vasu Nori | 5e89ae2 | 2010-09-15 14:23:29 -0700 | [diff] [blame] | 40 | /* package */ final int nHandle; |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Native linkage, do not modify. When non-0 this holds a reference to a valid |
| 44 | * sqlite3_statement object. It is only updated by the native code, but may be |
| 45 | * checked in this class when the database lock is held to determine if there |
| 46 | * is a valid native-side program or not. |
| 47 | */ |
| 48 | /* package */ int nStatement = 0; |
| 49 | |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 50 | /** the following are for debugging purposes */ |
| 51 | private String mSqlStmt = null; |
| 52 | private Throwable mStackTrace = null; |
Vasu Nori | 14b60e7 | 2010-03-01 14:47:47 -0800 | [diff] [blame] | 53 | |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 54 | /** when in cache and is in use, this member is set */ |
| 55 | private boolean mInUse = false; |
| 56 | |
| 57 | /* package */ SQLiteCompiledSql(SQLiteDatabase db, String sql) { |
Vasu Nori | 5e89ae2 | 2010-09-15 14:23:29 -0700 | [diff] [blame] | 58 | db.verifyDbIsOpen(); |
| 59 | db.verifyLockOwner(); |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 60 | mDatabase = db; |
Vasu Nori | 14b60e7 | 2010-03-01 14:47:47 -0800 | [diff] [blame] | 61 | mSqlStmt = sql; |
Vasu Nori | 08b448e | 2010-03-03 10:05:16 -0800 | [diff] [blame] | 62 | mStackTrace = new DatabaseObjectNotClosedException().fillInStackTrace(); |
Vasu Nori | 5e89ae2 | 2010-09-15 14:23:29 -0700 | [diff] [blame] | 63 | nHandle = db.mNativeHandle; |
| 64 | native_compile(sql); |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 67 | /* package */ void releaseSqlStatement() { |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 68 | // Note that native_finalize() checks to make sure that nStatement is |
| 69 | // non-null before destroying it. |
| 70 | if (nStatement != 0) { |
Vasu Nori | 6f37f83 | 2010-05-19 11:53:25 -0700 | [diff] [blame] | 71 | mDatabase.finalizeStatementLater(nStatement); |
| 72 | nStatement = 0; |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Vasu Nori | ec37e42 | 2010-02-22 12:28:16 -0800 | [diff] [blame] | 76 | /** |
| 77 | * returns true if acquire() succeeds. false otherwise. |
| 78 | */ |
| 79 | /* package */ synchronized boolean acquire() { |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 80 | if (mInUse) { |
Vasu Nori | 5e89ae2 | 2010-09-15 14:23:29 -0700 | [diff] [blame] | 81 | // it is already in use. |
Vasu Nori | ec37e42 | 2010-02-22 12:28:16 -0800 | [diff] [blame] | 82 | return false; |
| 83 | } |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 84 | mInUse = true; |
Vasu Nori | ec37e42 | 2010-02-22 12:28:16 -0800 | [diff] [blame] | 85 | return true; |
Vasu Nori | e8de284 | 2010-02-18 13:35:42 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 88 | /* package */ synchronized void release() { |
| 89 | mInUse = false; |
Vasu Nori | e8de284 | 2010-02-18 13:35:42 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Vasu Nori | 5e89ae2 | 2010-09-15 14:23:29 -0700 | [diff] [blame] | 92 | /* package */ synchronized void releaseIfNotInUse() { |
| 93 | // if it is not in use, release its memory from the database |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 94 | if (!mInUse) { |
Vasu Nori | 5e89ae2 | 2010-09-15 14:23:29 -0700 | [diff] [blame] | 95 | releaseSqlStatement(); |
| 96 | } |
| 97 | } |
| 98 | |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Make sure that the native resource is cleaned up. |
| 101 | */ |
| 102 | @Override |
Vasu Nori | 14b60e7 | 2010-03-01 14:47:47 -0800 | [diff] [blame] | 103 | protected void finalize() throws Throwable { |
| 104 | try { |
| 105 | if (nStatement == 0) return; |
| 106 | // finalizer should NEVER get called |
Vasu Nori | 5e89ae2 | 2010-09-15 14:23:29 -0700 | [diff] [blame] | 107 | // but if the database itself is not closed and is GC'ed, then |
| 108 | // all sub-objects attached to the database could end up getting GC'ed too. |
| 109 | // in that case, don't print any warning. |
Brad Fitzpatrick | e0ad63b | 2010-10-01 13:57:04 -0700 | [diff] [blame] | 110 | if (mInUse && StrictMode.vmSqliteObjectLeaksEnabled()) { |
Vasu Nori | 5e89ae2 | 2010-09-15 14:23:29 -0700 | [diff] [blame] | 111 | int len = mSqlStmt.length(); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 112 | StrictMode.onSqliteObjectLeaked( |
| 113 | "Releasing statement in a finalizer. Please ensure " + |
Vasu Nori | 0a1344e | 2010-03-18 22:17:24 -0700 | [diff] [blame] | 114 | "that you explicitly call close() on your cursor: " + |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 115 | mSqlStmt.substring(0, (len > 100) ? 100 : len), |
| 116 | mStackTrace); |
Vasu Nori | 5e89ae2 | 2010-09-15 14:23:29 -0700 | [diff] [blame] | 117 | } |
Vasu Nori | b729dcc | 2010-09-14 11:35:49 -0700 | [diff] [blame] | 118 | releaseSqlStatement(); |
Vasu Nori | 14b60e7 | 2010-03-01 14:47:47 -0800 | [diff] [blame] | 119 | } finally { |
| 120 | super.finalize(); |
| 121 | } |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Vasu Nori | 422dad0 | 2010-09-03 16:03:08 -0700 | [diff] [blame] | 124 | @Override public String toString() { |
| 125 | synchronized(this) { |
| 126 | StringBuilder buff = new StringBuilder(); |
| 127 | buff.append(" nStatement="); |
| 128 | buff.append(nStatement); |
Vasu Nori | 5e89ae2 | 2010-09-15 14:23:29 -0700 | [diff] [blame] | 129 | buff.append(", mInUse="); |
| 130 | buff.append(mInUse); |
Vasu Nori | 422dad0 | 2010-09-03 16:03:08 -0700 | [diff] [blame] | 131 | buff.append(", db="); |
| 132 | buff.append(mDatabase.getPath()); |
| 133 | buff.append(", db_connectionNum="); |
| 134 | buff.append(mDatabase.mConnectionNum); |
| 135 | buff.append(", sql="); |
| 136 | int len = mSqlStmt.length(); |
| 137 | buff.append(mSqlStmt.substring(0, (len > 100) ? 100 : len)); |
| 138 | return buff.toString(); |
| 139 | } |
| 140 | } |
| 141 | |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Compiles SQL into a SQLite program. |
| 144 | * |
| 145 | * <P>The database lock must be held when calling this method. |
| 146 | * @param sql The SQL to compile. |
| 147 | */ |
| 148 | private final native void native_compile(String sql); |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 149 | } |