blob: 79527b4b8b6dda1ddea297220621f585d151ec9d [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.
26 * 2. dalvikVM wants to reclaim some memory and releases it from the cache in
27 * {@link SQLiteDatabase}.
28 */
29/* package */ class SQLiteCompiledSql {
30
31 /** The database this program is compiled against. */
32 /* package */ SQLiteDatabase mDatabase;
33
34 /**
35 * Native linkage, do not modify. This comes from the database.
36 */
37 /* package */ int nHandle = 0;
38
39 /**
40 * Native linkage, do not modify. When non-0 this holds a reference to a valid
41 * sqlite3_statement object. It is only updated by the native code, but may be
42 * checked in this class when the database lock is held to determine if there
43 * is a valid native-side program or not.
44 */
45 /* package */ int nStatement = 0;
46
47 /* package */ SQLiteCompiledSql(SQLiteDatabase db, String sql) {
48 mDatabase = db;
49 this.nHandle = db.mNativeHandle;
50 compile(sql, true);
51 }
52
53 /**
54 * Compiles the given SQL into a SQLite byte code program using sqlite3_prepare_v2(). If
55 * this method has been called previously without a call to close and forCompilation is set
56 * to false the previous compilation will be used. Setting forceCompilation to true will
57 * always re-compile the program and should be done if you pass differing SQL strings to this
58 * method.
59 *
60 * <P>Note: this method acquires the database lock.</P>
61 *
62 * @param sql the SQL string to compile
63 * @param forceCompilation forces the SQL to be recompiled in the event that there is an
64 * existing compiled SQL program already around
65 */
66 private void compile(String sql, boolean forceCompilation) {
67 // Only compile if we don't have a valid statement already or the caller has
68 // explicitly requested a recompile.
69 if (forceCompilation) {
70 mDatabase.lock();
71 try {
72 // Note that the native_compile() takes care of destroying any previously
73 // existing programs before it compiles.
74 native_compile(sql);
75 } finally {
76 mDatabase.unlock();
77 }
78 }
79 }
80
81 /* package */ void releaseSqlStatement() {
82 // Note that native_finalize() checks to make sure that nStatement is
83 // non-null before destroying it.
84 if (nStatement != 0) {
85 try {
86 mDatabase.lock();
87 native_finalize();
88 nStatement = 0;
89 } finally {
90 mDatabase.unlock();
91 }
92 }
93 }
94
95 /**
96 * Make sure that the native resource is cleaned up.
97 */
98 @Override
99 protected void finalize() {
100 releaseSqlStatement();
101 }
102
103 /**
104 * Compiles SQL into a SQLite program.
105 *
106 * <P>The database lock must be held when calling this method.
107 * @param sql The SQL to compile.
108 */
109 private final native void native_compile(String sql);
110 private final native void native_finalize();
111}