SQLiteStatement.java revision 076357b8567458d4b6dfdcf839ef751634cd2bfb
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 mDatabase.lock(); 48 boolean logStats = mDatabase.mLogStats; 49 long startTime = logStats ? SystemClock.elapsedRealtime() : 0; 50 51 acquireReference(); 52 try { 53 native_execute(); 54 if (logStats) { 55 mDatabase.logTimeStat(false /* write */, startTime, SystemClock.elapsedRealtime()); 56 } 57 } finally { 58 releaseReference(); 59 mDatabase.unlock(); 60 } 61 } 62 63 /** 64 * Execute this SQL statement and return the ID of the most 65 * recently inserted row. The SQL statement should probably be an 66 * INSERT for this to be a useful call. 67 * 68 * @return the row ID of the last row inserted. 69 * 70 * @throws android.database.SQLException If the SQL string is invalid for 71 * some reason 72 */ 73 public long executeInsert() { 74 mDatabase.lock(); 75 boolean logStats = mDatabase.mLogStats; 76 long startTime = logStats ? SystemClock.elapsedRealtime() : 0; 77 78 acquireReference(); 79 try { 80 native_execute(); 81 if (logStats) { 82 mDatabase.logTimeStat(false /* write */, startTime, SystemClock.elapsedRealtime()); 83 } 84 return mDatabase.lastInsertRow(); 85 } finally { 86 releaseReference(); 87 mDatabase.unlock(); 88 } 89 } 90 91 /** 92 * Execute a statement that returns a 1 by 1 table with a numeric value. 93 * For example, SELECT COUNT(*) FROM table; 94 * 95 * @return The result of the query. 96 * 97 * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows 98 */ 99 public long simpleQueryForLong() { 100 mDatabase.lock(); 101 boolean logStats = mDatabase.mLogStats; 102 long startTime = logStats ? SystemClock.elapsedRealtime() : 0; 103 104 acquireReference(); 105 try { 106 long retValue = native_1x1_long(); 107 if (logStats) { 108 mDatabase.logTimeStat(false /* write */, startTime, SystemClock.elapsedRealtime()); 109 } 110 return retValue; 111 } finally { 112 releaseReference(); 113 mDatabase.unlock(); 114 } 115 } 116 117 /** 118 * Execute a statement that returns a 1 by 1 table with a text value. 119 * For example, SELECT COUNT(*) FROM table; 120 * 121 * @return The result of the query. 122 * 123 * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows 124 */ 125 public String simpleQueryForString() { 126 mDatabase.lock(); 127 boolean logStats = mDatabase.mLogStats; 128 long startTime = logStats ? SystemClock.elapsedRealtime() : 0; 129 130 acquireReference(); 131 try { 132 String retValue = native_1x1_string(); 133 if (logStats) { 134 mDatabase.logTimeStat(false /* write */, startTime, SystemClock.elapsedRealtime()); 135 } 136 return retValue; 137 } finally { 138 releaseReference(); 139 mDatabase.unlock(); 140 } 141 } 142 143 private final native void native_execute(); 144 private final native long native_1x1_long(); 145 private final native String native_1x1_string(); 146} 147