SQLiteStatement.java revision 3ef94e25b4c896ecaa85aa2c12b8863ecdf98df0
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
17package android.database.sqlite;
18
19import android.os.Debug;
20
21/**
22 * A pre-compiled statement against a {@link SQLiteDatabase} that can be reused.
23 * The statement cannot return multiple rows, but 1x1 result sets are allowed.
24 * Don't use SQLiteStatement constructor directly, please use
25 * {@link SQLiteDatabase#compileStatement(String)}
26 */
27public class SQLiteStatement extends SQLiteProgram
28{
29    /**
30     * Don't use SQLiteStatement constructor directly, please use
31     * {@link SQLiteDatabase#compileStatement(String)}
32     * @param db
33     * @param sql
34     */
35    /* package */ SQLiteStatement(SQLiteDatabase db, String sql) {
36        super(db, sql);
37    }
38
39    /**
40     * Execute this SQL statement, if it is not a query. For example,
41     * CREATE TABLE, DELTE, INSERT, etc.
42     *
43     * @throws android.database.SQLException If the SQL string is invalid for
44     *         some reason
45     */
46    public void execute() {
47        long timeStart = Debug.threadCpuTimeNanos();
48        mDatabase.lock();
49
50        acquireReference();
51        try {
52            native_execute();
53            mDatabase.logTimeStat(mSql, timeStart);
54        } finally {
55            releaseReference();
56            mDatabase.unlock();
57        }
58    }
59
60    /**
61     * Execute this SQL statement and return the ID of the most
62     * recently inserted row.  The SQL statement should probably be an
63     * INSERT for this to be a useful call.
64     *
65     * @return the row ID of the last row inserted.
66     *
67     * @throws android.database.SQLException If the SQL string is invalid for
68     *         some reason
69     */
70    public long executeInsert() {
71        long timeStart = Debug.threadCpuTimeNanos();
72        mDatabase.lock();
73
74        acquireReference();
75        try {
76            native_execute();
77            mDatabase.logTimeStat(mSql, timeStart);
78            return mDatabase.lastInsertRow();
79        } finally {
80            releaseReference();
81            mDatabase.unlock();
82        }
83    }
84
85    /**
86     * Execute a statement that returns a 1 by 1 table with a numeric value.
87     * For example, SELECT COUNT(*) FROM table;
88     *
89     * @return The result of the query.
90     *
91     * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows
92     */
93    public long simpleQueryForLong() {
94        long timeStart = Debug.threadCpuTimeNanos();
95        mDatabase.lock();
96
97        acquireReference();
98        try {
99            long retValue = native_1x1_long();
100            mDatabase.logTimeStat(mSql, timeStart);
101            return retValue;
102        } finally {
103            releaseReference();
104            mDatabase.unlock();
105        }
106    }
107
108    /**
109     * Execute a statement that returns a 1 by 1 table with a text value.
110     * For example, SELECT COUNT(*) FROM table;
111     *
112     * @return The result of the query.
113     *
114     * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows
115     */
116    public String simpleQueryForString() {
117        long timeStart = Debug.threadCpuTimeNanos();
118        mDatabase.lock();
119
120        acquireReference();
121        try {
122            String retValue = native_1x1_string();
123            mDatabase.logTimeStat(mSql, timeStart);
124            return retValue;
125        } finally {
126            releaseReference();
127            mDatabase.unlock();
128        }
129    }
130
131    private final native void native_execute();
132    private final native long native_1x1_long();
133    private final native String native_1x1_string();
134}
135