The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.database.sqlite; |
| 18 | |
Mathew Inwood | f86bea9 | 2018-08-10 16:10:20 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Bjorn Bringert | a006b472 | 2010-04-14 14:43:26 +0100 | [diff] [blame] | 20 | import android.os.ParcelFileDescriptor; |
Brad Fitzpatrick | cfda9f3 | 2010-06-03 12:52:54 -0700 | [diff] [blame] | 21 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | /** |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 23 | * Represents a statement that can be executed against a database. The statement |
| 24 | * cannot return multiple rows or columns, but single value (1 x 1) result sets |
| 25 | * are supported. |
| 26 | * <p> |
| 27 | * This class is not thread-safe. |
| 28 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | */ |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 30 | public final class SQLiteStatement extends SQLiteProgram { |
Mathew Inwood | f86bea9 | 2018-08-10 16:10:20 +0100 | [diff] [blame] | 31 | @UnsupportedAppUsage |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 32 | SQLiteStatement(SQLiteDatabase db, String sql, Object[] bindArgs) { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 33 | super(db, sql, bindArgs, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /** |
Vasu Nori | fb16cbd | 2010-07-25 16:38:48 -0700 | [diff] [blame] | 37 | * Execute this SQL statement, if it is not a SELECT / INSERT / DELETE / UPDATE, for example |
| 38 | * CREATE / DROP table, view, trigger, index etc. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | * |
| 40 | * @throws android.database.SQLException If the SQL string is invalid for |
| 41 | * some reason |
| 42 | */ |
| 43 | public void execute() { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 44 | acquireReference(); |
| 45 | try { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 46 | getSession().execute(getSql(), getBindArgs(), getConnectionFlags(), null); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 47 | } catch (SQLiteDatabaseCorruptException ex) { |
| 48 | onCorruption(); |
| 49 | throw ex; |
| 50 | } finally { |
| 51 | releaseReference(); |
| 52 | } |
Vasu Nori | fb16cbd | 2010-07-25 16:38:48 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
Vasu Nori | 4e874ed | 2010-09-15 18:40:49 -0700 | [diff] [blame] | 56 | * Execute this SQL statement, if the the number of rows affected by execution of this SQL |
Vasu Nori | fb16cbd | 2010-07-25 16:38:48 -0700 | [diff] [blame] | 57 | * statement is of any importance to the caller - for example, UPDATE / DELETE SQL statements. |
| 58 | * |
| 59 | * @return the number of rows affected by this SQL statement execution. |
| 60 | * @throws android.database.SQLException If the SQL string is invalid for |
| 61 | * some reason |
| 62 | */ |
| 63 | public int executeUpdateDelete() { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 64 | acquireReference(); |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 65 | try { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 66 | return getSession().executeForChangedRowCount( |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 67 | getSql(), getBindArgs(), getConnectionFlags(), null); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 68 | } catch (SQLiteDatabaseCorruptException ex) { |
| 69 | onCorruption(); |
| 70 | throw ex; |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 71 | } finally { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 72 | releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
Vasu Nori | 5bf6724 | 2010-03-16 09:55:13 -0700 | [diff] [blame] | 77 | * Execute this SQL statement and return the ID of the row inserted due to this call. |
| 78 | * The SQL statement should be an INSERT for this to be a useful call. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | * |
Vasu Nori | 5bf6724 | 2010-03-16 09:55:13 -0700 | [diff] [blame] | 80 | * @return the row ID of the last row inserted, if this insert is successful. -1 otherwise. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | * |
| 82 | * @throws android.database.SQLException If the SQL string is invalid for |
| 83 | * some reason |
| 84 | */ |
| 85 | public long executeInsert() { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 86 | acquireReference(); |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 87 | try { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 88 | return getSession().executeForLastInsertedRowId( |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 89 | getSql(), getBindArgs(), getConnectionFlags(), null); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 90 | } catch (SQLiteDatabaseCorruptException ex) { |
| 91 | onCorruption(); |
| 92 | throw ex; |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 93 | } finally { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 94 | releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Execute a statement that returns a 1 by 1 table with a numeric value. |
| 100 | * For example, SELECT COUNT(*) FROM table; |
| 101 | * |
| 102 | * @return The result of the query. |
| 103 | * |
| 104 | * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows |
| 105 | */ |
| 106 | public long simpleQueryForLong() { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 107 | acquireReference(); |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 108 | try { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 109 | return getSession().executeForLong( |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 110 | getSql(), getBindArgs(), getConnectionFlags(), null); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 111 | } catch (SQLiteDatabaseCorruptException ex) { |
| 112 | onCorruption(); |
| 113 | throw ex; |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 114 | } finally { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 115 | releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Execute a statement that returns a 1 by 1 table with a text value. |
| 121 | * For example, SELECT COUNT(*) FROM table; |
| 122 | * |
| 123 | * @return The result of the query. |
| 124 | * |
| 125 | * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows |
| 126 | */ |
| 127 | public String simpleQueryForString() { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 128 | acquireReference(); |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 129 | try { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 130 | return getSession().executeForString( |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 131 | getSql(), getBindArgs(), getConnectionFlags(), null); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 132 | } catch (SQLiteDatabaseCorruptException ex) { |
| 133 | onCorruption(); |
| 134 | throw ex; |
Vasu Nori | 2467561 | 2010-09-27 14:54:19 -0700 | [diff] [blame] | 135 | } finally { |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 136 | releaseReference(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Vasu Nori | 7501010 | 2010-07-01 16:23:06 -0700 | [diff] [blame] | 140 | /** |
Bjorn Bringert | a006b472 | 2010-04-14 14:43:26 +0100 | [diff] [blame] | 141 | * Executes a statement that returns a 1 by 1 table with a blob value. |
| 142 | * |
| 143 | * @return A read-only file descriptor for a copy of the blob value, or {@code null} |
| 144 | * if the value is null or could not be read for some reason. |
| 145 | * |
| 146 | * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows |
| 147 | */ |
| 148 | public ParcelFileDescriptor simpleQueryForBlobFileDescriptor() { |
Vasu Nori | 7501010 | 2010-07-01 16:23:06 -0700 | [diff] [blame] | 149 | acquireReference(); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 150 | try { |
| 151 | return getSession().executeForBlobFileDescriptor( |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 152 | getSql(), getBindArgs(), getConnectionFlags(), null); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 153 | } catch (SQLiteDatabaseCorruptException ex) { |
| 154 | onCorruption(); |
| 155 | throw ex; |
| 156 | } finally { |
| 157 | releaseReference(); |
| 158 | } |
Vasu Nori | 7501010 | 2010-07-01 16:23:06 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 161 | @Override |
| 162 | public String toString() { |
| 163 | return "SQLiteProgram: " + getSql(); |
Vasu Nori | 7501010 | 2010-07-01 16:23:06 -0700 | [diff] [blame] | 164 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | } |