blob: 6ed1a90efcb7b2b9e56d35911c5985284ca7fe22 [file] [log] [blame]
Vasu Nori5a03f362009-10-20 15:16:35 -07001/*
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
17package android.database.sqlite;
18
19import android.util.Log;
20
21/**
22 * This class encapsulates compilation of sql statement and release of the compiled statement obj.
23 * Once a sql statement is compiled, it is cached in {@link SQLiteDatabase}
24 * and it is released in one of the 2 following ways
25 * 1. when {@link SQLiteDatabase} object is closed.
Vasu Noriafe806a2010-03-09 11:37:52 -080026 * 2. if this is not cached in {@link SQLiteDatabase}, {@link android.database.Cursor#close()}
Vasu Norib729dcc2010-09-14 11:35:49 -070027 * releaases this obj.
Vasu Nori5a03f362009-10-20 15:16:35 -070028 */
29/* package */ class SQLiteCompiledSql {
30
Vasu Nori14b60e72010-03-01 14:47:47 -080031 private static final String TAG = "SQLiteCompiledSql";
32
Vasu Nori5a03f362009-10-20 15:16:35 -070033 /** The database this program is compiled against. */
Vasu Nori5e89ae22010-09-15 14:23:29 -070034 /* package */ final SQLiteDatabase mDatabase;
Vasu Nori5a03f362009-10-20 15:16:35 -070035
36 /**
37 * Native linkage, do not modify. This comes from the database.
38 */
Vasu Nori5e89ae22010-09-15 14:23:29 -070039 /* package */ final int nHandle;
Vasu Nori5a03f362009-10-20 15:16:35 -070040
41 /**
42 * Native linkage, do not modify. When non-0 this holds a reference to a valid
43 * sqlite3_statement object. It is only updated by the native code, but may be
44 * checked in this class when the database lock is held to determine if there
45 * is a valid native-side program or not.
46 */
47 /* package */ int nStatement = 0;
48
Vasu Norib729dcc2010-09-14 11:35:49 -070049 /** the following are for debugging purposes */
50 private String mSqlStmt = null;
51 private Throwable mStackTrace = null;
Vasu Nori14b60e72010-03-01 14:47:47 -080052
Vasu Norib729dcc2010-09-14 11:35:49 -070053 /** when in cache and is in use, this member is set */
54 private boolean mInUse = false;
55
56 /* package */ SQLiteCompiledSql(SQLiteDatabase db, String sql) {
Vasu Nori5e89ae22010-09-15 14:23:29 -070057 db.verifyDbIsOpen();
58 db.verifyLockOwner();
Vasu Nori5a03f362009-10-20 15:16:35 -070059 mDatabase = db;
Vasu Nori14b60e72010-03-01 14:47:47 -080060 mSqlStmt = sql;
Vasu Nori08b448e2010-03-03 10:05:16 -080061 mStackTrace = new DatabaseObjectNotClosedException().fillInStackTrace();
Vasu Nori5e89ae22010-09-15 14:23:29 -070062 nHandle = db.mNativeHandle;
63 native_compile(sql);
Vasu Nori5a03f362009-10-20 15:16:35 -070064 }
65
Vasu Norib729dcc2010-09-14 11:35:49 -070066 /* package */ void releaseSqlStatement() {
Vasu Nori5a03f362009-10-20 15:16:35 -070067 // Note that native_finalize() checks to make sure that nStatement is
68 // non-null before destroying it.
69 if (nStatement != 0) {
Vasu Nori6f37f832010-05-19 11:53:25 -070070 mDatabase.finalizeStatementLater(nStatement);
71 nStatement = 0;
Vasu Nori5a03f362009-10-20 15:16:35 -070072 }
73 }
74
Vasu Noriec37e422010-02-22 12:28:16 -080075 /**
76 * returns true if acquire() succeeds. false otherwise.
77 */
78 /* package */ synchronized boolean acquire() {
Vasu Norib729dcc2010-09-14 11:35:49 -070079 if (mInUse) {
Vasu Nori5e89ae22010-09-15 14:23:29 -070080 // it is already in use.
Vasu Noriec37e422010-02-22 12:28:16 -080081 return false;
82 }
Vasu Norib729dcc2010-09-14 11:35:49 -070083 mInUse = true;
Vasu Noriec37e422010-02-22 12:28:16 -080084 return true;
Vasu Norie8de2842010-02-18 13:35:42 -080085 }
86
Vasu Norib729dcc2010-09-14 11:35:49 -070087 /* package */ synchronized void release() {
88 mInUse = false;
Vasu Norie8de2842010-02-18 13:35:42 -080089 }
90
Vasu Nori5e89ae22010-09-15 14:23:29 -070091 /* package */ synchronized void releaseIfNotInUse() {
92 // if it is not in use, release its memory from the database
Vasu Nori24675612010-09-27 14:54:19 -070093 if (!mInUse) {
Vasu Nori5e89ae22010-09-15 14:23:29 -070094 releaseSqlStatement();
95 }
96 }
97
Vasu Nori5a03f362009-10-20 15:16:35 -070098 /**
99 * Make sure that the native resource is cleaned up.
100 */
101 @Override
Vasu Nori14b60e72010-03-01 14:47:47 -0800102 protected void finalize() throws Throwable {
103 try {
104 if (nStatement == 0) return;
105 // finalizer should NEVER get called
Vasu Nori5e89ae22010-09-15 14:23:29 -0700106 // but if the database itself is not closed and is GC'ed, then
107 // all sub-objects attached to the database could end up getting GC'ed too.
108 // in that case, don't print any warning.
Vasu Nori24675612010-09-27 14:54:19 -0700109 if (mInUse) {
Vasu Nori5e89ae22010-09-15 14:23:29 -0700110 int len = mSqlStmt.length();
111 Log.w(TAG, "Releasing statement in a finalizer. Please ensure " +
112 "that you explicitly call close() on your cursor: " +
113 mSqlStmt.substring(0, (len > 100) ? 100 : len), mStackTrace);
114 }
Vasu Norib729dcc2010-09-14 11:35:49 -0700115 releaseSqlStatement();
Vasu Nori14b60e72010-03-01 14:47:47 -0800116 } finally {
117 super.finalize();
118 }
Vasu Nori5a03f362009-10-20 15:16:35 -0700119 }
120
Vasu Nori422dad02010-09-03 16:03:08 -0700121 @Override public String toString() {
122 synchronized(this) {
123 StringBuilder buff = new StringBuilder();
124 buff.append(" nStatement=");
125 buff.append(nStatement);
Vasu Nori5e89ae22010-09-15 14:23:29 -0700126 buff.append(", mInUse=");
127 buff.append(mInUse);
Vasu Nori422dad02010-09-03 16:03:08 -0700128 buff.append(", db=");
129 buff.append(mDatabase.getPath());
130 buff.append(", db_connectionNum=");
131 buff.append(mDatabase.mConnectionNum);
132 buff.append(", sql=");
133 int len = mSqlStmt.length();
134 buff.append(mSqlStmt.substring(0, (len > 100) ? 100 : len));
135 return buff.toString();
136 }
137 }
138
Vasu Nori5a03f362009-10-20 15:16:35 -0700139 /**
140 * Compiles SQL into a SQLite program.
141 *
142 * <P>The database lock must be held when calling this method.
143 * @param sql The SQL to compile.
144 */
145 private final native void native_compile(String sql);
Vasu Nori5a03f362009-10-20 15:16:35 -0700146}