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