blob: a7ad757a4f77db702822c0ea7a36e6cbd86ca1d3 [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
Brad Fitzpatrick32e60c72010-09-30 16:22:36 -070019import android.os.StrictMode;
Vasu Nori5a03f362009-10-20 15:16:35 -070020import 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 Noriafe806a2010-03-09 11:37:52 -080027 * 2. if this is not cached in {@link SQLiteDatabase}, {@link android.database.Cursor#close()}
Vasu Norib729dcc2010-09-14 11:35:49 -070028 * releaases this obj.
Vasu Nori5a03f362009-10-20 15:16:35 -070029 */
30/* package */ class SQLiteCompiledSql {
31
Vasu Nori14b60e72010-03-01 14:47:47 -080032 private static final String TAG = "SQLiteCompiledSql";
33
Vasu Nori5a03f362009-10-20 15:16:35 -070034 /** The database this program is compiled against. */
Vasu Nori5e89ae22010-09-15 14:23:29 -070035 /* package */ final SQLiteDatabase mDatabase;
Vasu Nori5a03f362009-10-20 15:16:35 -070036
37 /**
38 * Native linkage, do not modify. This comes from the database.
39 */
Vasu Nori5e89ae22010-09-15 14:23:29 -070040 /* package */ final int nHandle;
Vasu Nori5a03f362009-10-20 15:16:35 -070041
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 Norib729dcc2010-09-14 11:35:49 -070050 /** the following are for debugging purposes */
51 private String mSqlStmt = null;
52 private Throwable mStackTrace = null;
Vasu Nori14b60e72010-03-01 14:47:47 -080053
Vasu Norib729dcc2010-09-14 11:35:49 -070054 /** 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 Nori5e89ae22010-09-15 14:23:29 -070058 db.verifyDbIsOpen();
59 db.verifyLockOwner();
Vasu Nori5a03f362009-10-20 15:16:35 -070060 mDatabase = db;
Vasu Nori14b60e72010-03-01 14:47:47 -080061 mSqlStmt = sql;
Vasu Nori08b448e2010-03-03 10:05:16 -080062 mStackTrace = new DatabaseObjectNotClosedException().fillInStackTrace();
Vasu Nori5e89ae22010-09-15 14:23:29 -070063 nHandle = db.mNativeHandle;
64 native_compile(sql);
Vasu Nori5a03f362009-10-20 15:16:35 -070065 }
66
Vasu Norib729dcc2010-09-14 11:35:49 -070067 /* package */ void releaseSqlStatement() {
Vasu Nori5a03f362009-10-20 15:16:35 -070068 // Note that native_finalize() checks to make sure that nStatement is
69 // non-null before destroying it.
70 if (nStatement != 0) {
Vasu Nori6f37f832010-05-19 11:53:25 -070071 mDatabase.finalizeStatementLater(nStatement);
72 nStatement = 0;
Vasu Nori5a03f362009-10-20 15:16:35 -070073 }
74 }
75
Vasu Noriec37e422010-02-22 12:28:16 -080076 /**
77 * returns true if acquire() succeeds. false otherwise.
78 */
79 /* package */ synchronized boolean acquire() {
Vasu Norib729dcc2010-09-14 11:35:49 -070080 if (mInUse) {
Vasu Nori5e89ae22010-09-15 14:23:29 -070081 // it is already in use.
Vasu Noriec37e422010-02-22 12:28:16 -080082 return false;
83 }
Vasu Norib729dcc2010-09-14 11:35:49 -070084 mInUse = true;
Vasu Noriec37e422010-02-22 12:28:16 -080085 return true;
Vasu Norie8de2842010-02-18 13:35:42 -080086 }
87
Vasu Norib729dcc2010-09-14 11:35:49 -070088 /* package */ synchronized void release() {
89 mInUse = false;
Vasu Norie8de2842010-02-18 13:35:42 -080090 }
91
Vasu Nori5e89ae22010-09-15 14:23:29 -070092 /* package */ synchronized void releaseIfNotInUse() {
93 // if it is not in use, release its memory from the database
Vasu Nori24675612010-09-27 14:54:19 -070094 if (!mInUse) {
Vasu Nori5e89ae22010-09-15 14:23:29 -070095 releaseSqlStatement();
96 }
97 }
98
Vasu Nori5a03f362009-10-20 15:16:35 -070099 /**
100 * Make sure that the native resource is cleaned up.
101 */
102 @Override
Vasu Nori14b60e72010-03-01 14:47:47 -0800103 protected void finalize() throws Throwable {
104 try {
105 if (nStatement == 0) return;
106 // finalizer should NEVER get called
Vasu Nori5e89ae22010-09-15 14:23:29 -0700107 // 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 Fitzpatricke0ad63b2010-10-01 13:57:04 -0700110 if (mInUse && StrictMode.vmSqliteObjectLeaksEnabled()) {
Vasu Nori5e89ae22010-09-15 14:23:29 -0700111 int len = mSqlStmt.length();
Brad Fitzpatrick32e60c72010-09-30 16:22:36 -0700112 StrictMode.onSqliteObjectLeaked(
113 "Releasing statement in a finalizer. Please ensure " +
Vasu Nori0a1344e2010-03-18 22:17:24 -0700114 "that you explicitly call close() on your cursor: " +
Brad Fitzpatrick32e60c72010-09-30 16:22:36 -0700115 mSqlStmt.substring(0, (len > 100) ? 100 : len),
116 mStackTrace);
Vasu Nori5e89ae22010-09-15 14:23:29 -0700117 }
Vasu Norib729dcc2010-09-14 11:35:49 -0700118 releaseSqlStatement();
Vasu Nori14b60e72010-03-01 14:47:47 -0800119 } finally {
120 super.finalize();
121 }
Vasu Nori5a03f362009-10-20 15:16:35 -0700122 }
123
Vasu Nori422dad02010-09-03 16:03:08 -0700124 @Override public String toString() {
125 synchronized(this) {
126 StringBuilder buff = new StringBuilder();
127 buff.append(" nStatement=");
128 buff.append(nStatement);
Vasu Nori5e89ae22010-09-15 14:23:29 -0700129 buff.append(", mInUse=");
130 buff.append(mInUse);
Vasu Nori422dad02010-09-03 16:03:08 -0700131 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 Nori5a03f362009-10-20 15:16:35 -0700142 /**
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 Nori5a03f362009-10-20 15:16:35 -0700149}