SQLiteStatement.java revision 5bf67247d2e299f0586de65b2d024f1f835657e0
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.SystemClock;
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 = SystemClock.uptimeMillis();
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 row inserted due to this call.
62     * The SQL statement should be an INSERT for this to be a useful call.
63     *
64     * @return the row ID of the last row inserted, if this insert is successful. -1 otherwise.
65     *
66     * @throws android.database.SQLException If the SQL string is invalid for
67     *         some reason
68     */
69    public long executeInsert() {
70        long timeStart = SystemClock.uptimeMillis();
71        mDatabase.lock();
72
73        acquireReference();
74        try {
75            native_execute();
76            mDatabase.logTimeStat(mSql, timeStart);
77            return (mDatabase.lastChangeCount() > 0) ? mDatabase.lastInsertRow() : -1;
78        } finally {
79            releaseReference();
80            mDatabase.unlock();
81        }
82    }
83
84    /**
85     * Execute a statement that returns a 1 by 1 table with a numeric value.
86     * For example, SELECT COUNT(*) FROM table;
87     *
88     * @return The result of the query.
89     *
90     * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows
91     */
92    public long simpleQueryForLong() {
93        long timeStart = SystemClock.uptimeMillis();
94        mDatabase.lock();
95
96        acquireReference();
97        try {
98            long retValue = native_1x1_long();
99            mDatabase.logTimeStat(mSql, timeStart);
100            return retValue;
101        } finally {
102            releaseReference();
103            mDatabase.unlock();
104        }
105    }
106
107    /**
108     * Execute a statement that returns a 1 by 1 table with a text value.
109     * For example, SELECT COUNT(*) FROM table;
110     *
111     * @return The result of the query.
112     *
113     * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows
114     */
115    public String simpleQueryForString() {
116        long timeStart = SystemClock.uptimeMillis();
117        mDatabase.lock();
118
119        acquireReference();
120        try {
121            String retValue = native_1x1_string();
122            mDatabase.logTimeStat(mSql, timeStart);
123            return retValue;
124        } finally {
125            releaseReference();
126            mDatabase.unlock();
127        }
128    }
129
130    private final native void native_execute();
131    private final native long native_1x1_long();
132    private final native String native_1x1_string();
133}
134