The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 19 | import android.annotation.IntRange; |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Nullable; |
Mathew Inwood | 41b3194 | 2018-08-10 16:00:53 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 24 | import android.database.DatabaseErrorHandler; |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 25 | import android.database.SQLException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.database.sqlite.SQLiteDatabase.CursorFactory; |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 27 | import android.os.FileUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.util.Log; |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 29 | |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 30 | import com.android.internal.util.Preconditions; |
| 31 | |
Suprabh Shukla | 622217696 | 2016-10-06 14:41:35 -0700 | [diff] [blame] | 32 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * A helper class to manage database creation and version management. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 36 | * |
| 37 | * <p>You create a subclass implementing {@link #onCreate}, {@link #onUpgrade} and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | * optionally {@link #onOpen}, and this class takes care of opening the database |
| 39 | * if it exists, creating it if it does not, and upgrading it as necessary. |
| 40 | * Transactions are used to make sure the database is always in a sensible state. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 41 | * |
| 42 | * <p>This class makes it easy for {@link android.content.ContentProvider} |
| 43 | * implementations to defer opening and upgrading the database until first use, |
| 44 | * to avoid blocking application startup with long-running database upgrades. |
| 45 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | * <p>For an example, see the NotePadProvider class in the NotePad sample application, |
| 47 | * in the <em>samples/</em> directory of the SDK.</p> |
Brad Fitzpatrick | 44dc76a | 2010-06-02 15:12:05 -0700 | [diff] [blame] | 48 | * |
| 49 | * <p class="note"><strong>Note:</strong> this class assumes |
Eric Hassold | a5af5d6 | 2010-12-13 12:51:02 -0800 | [diff] [blame] | 50 | * monotonically increasing version numbers for upgrades.</p> |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 51 | * |
| 52 | * <p class="note"><strong>Note:</strong> the {@link AutoCloseable} interface was |
| 53 | * first added in the {@link android.os.Build.VERSION_CODES#Q} release.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | */ |
Jeff Sharkey | ec5f7d1 | 2018-08-08 09:15:04 -0600 | [diff] [blame] | 55 | public abstract class SQLiteOpenHelper implements AutoCloseable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | private static final String TAG = SQLiteOpenHelper.class.getSimpleName(); |
| 57 | |
| 58 | private final Context mContext; |
Mathew Inwood | 41b3194 | 2018-08-10 16:00:53 +0100 | [diff] [blame] | 59 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | private final String mName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | private final int mNewVersion; |
Suprabh Shukla | 622217696 | 2016-10-06 14:41:35 -0700 | [diff] [blame] | 62 | private final int mMinimumSupportedVersion; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 64 | private SQLiteDatabase mDatabase; |
| 65 | private boolean mIsInitializing; |
Fyodor Kupolov | ab05b14 | 2018-02-12 15:33:13 -0800 | [diff] [blame] | 66 | private SQLiteDatabase.OpenParams.Builder mOpenParamsBuilder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Create a helper object to create, open, and/or manage a database. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 70 | * This method always returns very quickly. The database is not actually |
| 71 | * created or opened until one of {@link #getWritableDatabase} or |
| 72 | * {@link #getReadableDatabase} is called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | * |
Fyodor Kupolov | 2abd2a4 | 2018-01-17 16:45:14 -0800 | [diff] [blame] | 74 | * @param context to use for locating paths to the the database |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | * @param name of the database file, or null for an in-memory database |
| 76 | * @param factory to use for creating cursor objects, or null for the default |
| 77 | * @param version number of the database (starting at 1); if the database is older, |
Eric Hassold | a5af5d6 | 2010-12-13 12:51:02 -0800 | [diff] [blame] | 78 | * {@link #onUpgrade} will be used to upgrade the database; if the database is |
| 79 | * newer, {@link #onDowngrade} will be used to downgrade the database |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | */ |
Fyodor Kupolov | 9d4c3d9 | 2017-11-15 16:04:24 -0800 | [diff] [blame] | 81 | public SQLiteOpenHelper(@Nullable Context context, @Nullable String name, |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 82 | @Nullable CursorFactory factory, int version) { |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 83 | this(context, name, factory, version, null); |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Create a helper object to create, open, and/or manage a database. |
| 88 | * The database is not actually created or opened until one of |
| 89 | * {@link #getWritableDatabase} or {@link #getReadableDatabase} is called. |
| 90 | * |
| 91 | * <p>Accepts input param: a concrete instance of {@link DatabaseErrorHandler} to be |
| 92 | * used to handle corruption when sqlite reports database corruption.</p> |
| 93 | * |
Fyodor Kupolov | 2abd2a4 | 2018-01-17 16:45:14 -0800 | [diff] [blame] | 94 | * @param context to use for locating paths to the the database |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 95 | * @param name of the database file, or null for an in-memory database |
| 96 | * @param factory to use for creating cursor objects, or null for the default |
| 97 | * @param version number of the database (starting at 1); if the database is older, |
Pin Ting | 1c423b8 | 2012-01-18 11:21:06 +0800 | [diff] [blame] | 98 | * {@link #onUpgrade} will be used to upgrade the database; if the database is |
| 99 | * newer, {@link #onDowngrade} will be used to downgrade the database |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 100 | * @param errorHandler the {@link DatabaseErrorHandler} to be used when sqlite reports database |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 101 | * corruption, or null to use the default error handler. |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 102 | */ |
Fyodor Kupolov | 9d4c3d9 | 2017-11-15 16:04:24 -0800 | [diff] [blame] | 103 | public SQLiteOpenHelper(@Nullable Context context, @Nullable String name, |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 104 | @Nullable CursorFactory factory, int version, |
| 105 | @Nullable DatabaseErrorHandler errorHandler) { |
Suprabh Shukla | 622217696 | 2016-10-06 14:41:35 -0700 | [diff] [blame] | 106 | this(context, name, factory, version, 0, errorHandler); |
| 107 | } |
| 108 | |
| 109 | /** |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 110 | * Create a helper object to create, open, and/or manage a database. |
| 111 | * This method always returns very quickly. The database is not actually |
| 112 | * created or opened until one of {@link #getWritableDatabase} or |
| 113 | * {@link #getReadableDatabase} is called. |
| 114 | * |
Fyodor Kupolov | 2abd2a4 | 2018-01-17 16:45:14 -0800 | [diff] [blame] | 115 | * @param context to use for locating paths to the the database |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 116 | * @param name of the database file, or null for an in-memory database |
| 117 | * @param version number of the database (starting at 1); if the database is older, |
| 118 | * {@link #onUpgrade} will be used to upgrade the database; if the database is |
| 119 | * newer, {@link #onDowngrade} will be used to downgrade the database |
| 120 | * @param openParams configuration parameters that are used for opening {@link SQLiteDatabase}. |
| 121 | * Please note that {@link SQLiteDatabase#CREATE_IF_NECESSARY} flag will always be |
| 122 | * set when the helper opens the database |
| 123 | */ |
Fyodor Kupolov | 9d4c3d9 | 2017-11-15 16:04:24 -0800 | [diff] [blame] | 124 | public SQLiteOpenHelper(@Nullable Context context, @Nullable String name, int version, |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 125 | @NonNull SQLiteDatabase.OpenParams openParams) { |
| 126 | this(context, name, version, 0, openParams.toBuilder()); |
| 127 | } |
| 128 | |
| 129 | /** |
Suprabh Shukla | 622217696 | 2016-10-06 14:41:35 -0700 | [diff] [blame] | 130 | * Same as {@link #SQLiteOpenHelper(Context, String, CursorFactory, int, DatabaseErrorHandler)} |
| 131 | * but also accepts an integer minimumSupportedVersion as a convenience for upgrading very old |
| 132 | * versions of this database that are no longer supported. If a database with older version that |
| 133 | * minimumSupportedVersion is found, it is simply deleted and a new database is created with the |
| 134 | * given name and version |
| 135 | * |
Fyodor Kupolov | 2abd2a4 | 2018-01-17 16:45:14 -0800 | [diff] [blame] | 136 | * @param context to use for locating paths to the the database |
Suprabh Shukla | 622217696 | 2016-10-06 14:41:35 -0700 | [diff] [blame] | 137 | * @param name the name of the database file, null for a temporary in-memory database |
| 138 | * @param factory to use for creating cursor objects, null for default |
| 139 | * @param version the required version of the database |
| 140 | * @param minimumSupportedVersion the minimum version that is supported to be upgraded to |
| 141 | * {@code version} via {@link #onUpgrade}. If the current database version is lower |
| 142 | * than this, database is simply deleted and recreated with the version passed in |
| 143 | * {@code version}. {@link #onBeforeDelete} is called before deleting the database |
| 144 | * when this happens. This is 0 by default. |
| 145 | * @param errorHandler the {@link DatabaseErrorHandler} to be used when sqlite reports database |
| 146 | * corruption, or null to use the default error handler. |
| 147 | * @see #onBeforeDelete(SQLiteDatabase) |
| 148 | * @see #SQLiteOpenHelper(Context, String, CursorFactory, int, DatabaseErrorHandler) |
| 149 | * @see #onUpgrade(SQLiteDatabase, int, int) |
| 150 | * @hide |
| 151 | */ |
Fyodor Kupolov | 9d4c3d9 | 2017-11-15 16:04:24 -0800 | [diff] [blame] | 152 | public SQLiteOpenHelper(@Nullable Context context, @Nullable String name, |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 153 | @Nullable CursorFactory factory, int version, |
| 154 | int minimumSupportedVersion, @Nullable DatabaseErrorHandler errorHandler) { |
| 155 | this(context, name, version, minimumSupportedVersion, |
| 156 | new SQLiteDatabase.OpenParams.Builder()); |
| 157 | mOpenParamsBuilder.setCursorFactory(factory); |
| 158 | mOpenParamsBuilder.setErrorHandler(errorHandler); |
| 159 | } |
| 160 | |
Fyodor Kupolov | 9d4c3d9 | 2017-11-15 16:04:24 -0800 | [diff] [blame] | 161 | private SQLiteOpenHelper(@Nullable Context context, @Nullable String name, int version, |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 162 | int minimumSupportedVersion, |
| 163 | @NonNull SQLiteDatabase.OpenParams.Builder openParamsBuilder) { |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 164 | Preconditions.checkNotNull(openParamsBuilder); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version); |
| 166 | |
| 167 | mContext = context; |
| 168 | mName = name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | mNewVersion = version; |
Suprabh Shukla | 622217696 | 2016-10-06 14:41:35 -0700 | [diff] [blame] | 170 | mMinimumSupportedVersion = Math.max(0, minimumSupportedVersion); |
Fyodor Kupolov | ab05b14 | 2018-02-12 15:33:13 -0800 | [diff] [blame] | 171 | setOpenParamsBuilder(openParamsBuilder); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
Pin Ting | 1c423b8 | 2012-01-18 11:21:06 +0800 | [diff] [blame] | 175 | * Return the name of the SQLite database being opened, as given to |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 176 | * the constructor. |
| 177 | */ |
| 178 | public String getDatabaseName() { |
| 179 | return mName; |
| 180 | } |
| 181 | |
| 182 | /** |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 183 | * Enables or disables the use of write-ahead logging for the database. |
| 184 | * |
| 185 | * Write-ahead logging cannot be used with read-only databases so the value of |
| 186 | * this flag is ignored if the database is opened read-only. |
| 187 | * |
| 188 | * @param enabled True if write-ahead logging should be enabled, false if it |
| 189 | * should be disabled. |
| 190 | * |
| 191 | * @see SQLiteDatabase#enableWriteAheadLogging() |
| 192 | */ |
| 193 | public void setWriteAheadLoggingEnabled(boolean enabled) { |
| 194 | synchronized (this) { |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 195 | if (mOpenParamsBuilder.isWriteAheadLoggingEnabled() != enabled) { |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 196 | if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) { |
| 197 | if (enabled) { |
| 198 | mDatabase.enableWriteAheadLogging(); |
| 199 | } else { |
| 200 | mDatabase.disableWriteAheadLogging(); |
| 201 | } |
| 202 | } |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 203 | mOpenParamsBuilder.setWriteAheadLoggingEnabled(enabled); |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 204 | } |
Narayan Kamath | b828043 | 2019-02-21 13:20:51 +0000 | [diff] [blame] | 205 | |
Fyodor Kupolov | 692573b | 2018-03-06 12:34:36 -0800 | [diff] [blame] | 206 | // Compatibility WAL is disabled if an app disables or enables WAL |
Narayan Kamath | b828043 | 2019-02-21 13:20:51 +0000 | [diff] [blame] | 207 | mOpenParamsBuilder.removeOpenFlags(SQLiteDatabase.ENABLE_LEGACY_COMPATIBILITY_WAL); |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | /** |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 212 | * Configures <a href="https://sqlite.org/malloc.html#lookaside">lookaside memory allocator</a> |
| 213 | * |
| 214 | * <p>This method should be called from the constructor of the subclass, |
| 215 | * before opening the database, since lookaside memory configuration can only be changed |
| 216 | * when no connection is using it |
| 217 | * |
| 218 | * <p>SQLite default settings will be used, if this method isn't called. |
| 219 | * Use {@code setLookasideConfig(0,0)} to disable lookaside |
| 220 | * |
Fyodor Kupolov | 05a0f0f | 2017-06-30 19:00:00 -0700 | [diff] [blame] | 221 | * <p><strong>Note:</strong> Provided slotSize/slotCount configuration is just a recommendation. |
| 222 | * The system may choose different values depending on a device, e.g. lookaside allocations |
| 223 | * can be disabled on low-RAM devices |
| 224 | * |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 225 | * @param slotSize The size in bytes of each lookaside slot. |
| 226 | * @param slotCount The total number of lookaside memory slots per database connection. |
| 227 | */ |
| 228 | public void setLookasideConfig(@IntRange(from = 0) final int slotSize, |
| 229 | @IntRange(from = 0) final int slotCount) { |
| 230 | synchronized (this) { |
| 231 | if (mDatabase != null && mDatabase.isOpen()) { |
| 232 | throw new IllegalStateException( |
| 233 | "Lookaside memory config cannot be changed after opening the database"); |
| 234 | } |
| 235 | mOpenParamsBuilder.setLookasideConfig(slotSize, slotCount); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
Fyodor Kupolov | ab05b14 | 2018-02-12 15:33:13 -0800 | [diff] [blame] | 240 | * Sets configuration parameters that are used for opening {@link SQLiteDatabase}. |
| 241 | * <p>Please note that {@link SQLiteDatabase#CREATE_IF_NECESSARY} flag will always be set when |
| 242 | * opening the database |
| 243 | * |
| 244 | * @param openParams configuration parameters that are used for opening {@link SQLiteDatabase}. |
| 245 | * @throws IllegalStateException if the database is already open |
| 246 | */ |
| 247 | public void setOpenParams(@NonNull SQLiteDatabase.OpenParams openParams) { |
| 248 | Preconditions.checkNotNull(openParams); |
| 249 | synchronized (this) { |
| 250 | if (mDatabase != null && mDatabase.isOpen()) { |
| 251 | throw new IllegalStateException( |
| 252 | "OpenParams cannot be set after opening the database"); |
| 253 | } |
| 254 | setOpenParamsBuilder(new SQLiteDatabase.OpenParams.Builder(openParams)); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | private void setOpenParamsBuilder(SQLiteDatabase.OpenParams.Builder openParamsBuilder) { |
| 259 | mOpenParamsBuilder = openParamsBuilder; |
| 260 | mOpenParamsBuilder.addOpenFlags(SQLiteDatabase.CREATE_IF_NECESSARY); |
| 261 | } |
| 262 | |
| 263 | /** |
Fyodor Kupolov | cf97b6b | 2017-07-25 14:17:33 -0700 | [diff] [blame] | 264 | * Sets the maximum number of milliseconds that SQLite connection is allowed to be idle |
| 265 | * before it is closed and removed from the pool. |
| 266 | * |
| 267 | * <p>This method should be called from the constructor of the subclass, |
| 268 | * before opening the database |
| 269 | * |
Makoto Onuki | 1a90479 | 2019-02-28 12:04:52 -0800 | [diff] [blame] | 270 | * <p><b>DO NOT USE</b> this method. |
| 271 | * This feature has negative side effects that are very hard to foresee. |
Makoto Onuki | b15186c | 2019-01-28 14:43:41 -0800 | [diff] [blame] | 272 | * See the javadoc of |
| 273 | * {@link SQLiteDatabase.OpenParams.Builder#setIdleConnectionTimeout(long)} |
| 274 | * for the details. |
| 275 | * |
Fyodor Kupolov | cf97b6b | 2017-07-25 14:17:33 -0700 | [diff] [blame] | 276 | * @param idleConnectionTimeoutMs timeout in milliseconds. Use {@link Long#MAX_VALUE} value |
| 277 | * to allow unlimited idle connections. |
Makoto Onuki | b15186c | 2019-01-28 14:43:41 -0800 | [diff] [blame] | 278 | * |
| 279 | * @see SQLiteDatabase.OpenParams.Builder#setIdleConnectionTimeout(long) |
| 280 | * |
Makoto Onuki | 1a90479 | 2019-02-28 12:04:52 -0800 | [diff] [blame] | 281 | * @deprecated DO NOT USE this method. See the javadoc of |
| 282 | * {@link SQLiteDatabase.OpenParams.Builder#setIdleConnectionTimeout(long)} |
| 283 | * for the details. |
Fyodor Kupolov | cf97b6b | 2017-07-25 14:17:33 -0700 | [diff] [blame] | 284 | */ |
Makoto Onuki | b15186c | 2019-01-28 14:43:41 -0800 | [diff] [blame] | 285 | @Deprecated |
Fyodor Kupolov | cf97b6b | 2017-07-25 14:17:33 -0700 | [diff] [blame] | 286 | public void setIdleConnectionTimeout(@IntRange(from = 0) final long idleConnectionTimeoutMs) { |
| 287 | synchronized (this) { |
| 288 | if (mDatabase != null && mDatabase.isOpen()) { |
| 289 | throw new IllegalStateException( |
| 290 | "Connection timeout setting cannot be changed after opening the database"); |
| 291 | } |
| 292 | mOpenParamsBuilder.setIdleConnectionTimeout(idleConnectionTimeoutMs); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | * Create and/or open a database that will be used for reading and writing. |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 298 | * The first time this is called, the database will be opened and |
| 299 | * {@link #onCreate}, {@link #onUpgrade} and/or {@link #onOpen} will be |
| 300 | * called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | * |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 302 | * <p>Once opened successfully, the database is cached, so you can |
| 303 | * call this method every time you need to write to the database. |
| 304 | * (Make sure to call {@link #close} when you no longer need the database.) |
| 305 | * Errors such as bad permissions or a full disk may cause this method |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | * to fail, but future attempts may succeed if the problem is fixed.</p> |
| 307 | * |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 308 | * <p class="caution">Database upgrade may take a long time, you |
| 309 | * should not call this method from the application main thread, including |
| 310 | * from {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}. |
| 311 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | * @throws SQLiteException if the database cannot be opened for writing |
| 313 | * @return a read/write database object valid until {@link #close} is called |
| 314 | */ |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 315 | public SQLiteDatabase getWritableDatabase() { |
| 316 | synchronized (this) { |
| 317 | return getDatabaseLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Create and/or open a database. This will be the same object returned by |
| 323 | * {@link #getWritableDatabase} unless some problem, such as a full disk, |
| 324 | * requires the database to be opened read-only. In that case, a read-only |
| 325 | * database object will be returned. If the problem is fixed, a future call |
| 326 | * to {@link #getWritableDatabase} may succeed, in which case the read-only |
| 327 | * database object will be closed and the read/write object will be returned |
| 328 | * in the future. |
| 329 | * |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 330 | * <p class="caution">Like {@link #getWritableDatabase}, this method may |
| 331 | * take a long time to return, so you should not call it from the |
| 332 | * application main thread, including from |
| 333 | * {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}. |
| 334 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | * @throws SQLiteException if the database cannot be opened |
| 336 | * @return a database object valid until {@link #getWritableDatabase} |
| 337 | * or {@link #close} is called. |
| 338 | */ |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 339 | public SQLiteDatabase getReadableDatabase() { |
| 340 | synchronized (this) { |
| 341 | return getDatabaseLocked(false); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | private SQLiteDatabase getDatabaseLocked(boolean writable) { |
Vasu Nori | cc6f549 | 2010-08-23 17:05:25 -0700 | [diff] [blame] | 346 | if (mDatabase != null) { |
| 347 | if (!mDatabase.isOpen()) { |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 348 | // Darn! The user closed the database by calling mDatabase.close(). |
Vasu Nori | cc6f549 | 2010-08-23 17:05:25 -0700 | [diff] [blame] | 349 | mDatabase = null; |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 350 | } else if (!writable || !mDatabase.isReadOnly()) { |
| 351 | // The database is already open for business. |
| 352 | return mDatabase; |
Vasu Nori | cc6f549 | 2010-08-23 17:05:25 -0700 | [diff] [blame] | 353 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | if (mIsInitializing) { |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 357 | throw new IllegalStateException("getDatabase called recursively"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 360 | SQLiteDatabase db = mDatabase; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | try { |
| 362 | mIsInitializing = true; |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 363 | |
| 364 | if (db != null) { |
| 365 | if (writable && db.isReadOnly()) { |
| 366 | db.reopenReadWrite(); |
| 367 | } |
| 368 | } else if (mName == null) { |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 369 | db = SQLiteDatabase.createInMemory(mOpenParamsBuilder.build()); |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 370 | } else { |
Fyodor Kupolov | 76436c0 | 2017-08-03 17:56:44 -0700 | [diff] [blame] | 371 | final File filePath = mContext.getDatabasePath(mName); |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 372 | SQLiteDatabase.OpenParams params = mOpenParamsBuilder.build(); |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 373 | try { |
Fyodor Kupolov | 76436c0 | 2017-08-03 17:56:44 -0700 | [diff] [blame] | 374 | db = SQLiteDatabase.openDatabase(filePath, params); |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 375 | // Keep pre-O-MR1 behavior by resetting file permissions to 660 |
Fyodor Kupolov | 76436c0 | 2017-08-03 17:56:44 -0700 | [diff] [blame] | 376 | setFilePermissionsForDb(filePath.getPath()); |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 377 | } catch (SQLException ex) { |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 378 | if (writable) { |
| 379 | throw ex; |
| 380 | } |
| 381 | Log.e(TAG, "Couldn't open " + mName |
| 382 | + " for writing (will try read-only):", ex); |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 383 | params = params.toBuilder().addOpenFlags(SQLiteDatabase.OPEN_READONLY).build(); |
Fyodor Kupolov | 76436c0 | 2017-08-03 17:56:44 -0700 | [diff] [blame] | 384 | db = SQLiteDatabase.openDatabase(filePath, params); |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 385 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Jeff Brown | 96496ad | 2012-03-23 14:38:06 -0700 | [diff] [blame] | 388 | onConfigure(db); |
| 389 | |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 390 | final int version = db.getVersion(); |
| 391 | if (version != mNewVersion) { |
| 392 | if (db.isReadOnly()) { |
| 393 | throw new SQLiteException("Can't upgrade read-only database from version " + |
| 394 | db.getVersion() + " to " + mNewVersion + ": " + mName); |
| 395 | } |
| 396 | |
Suprabh Shukla | 622217696 | 2016-10-06 14:41:35 -0700 | [diff] [blame] | 397 | if (version > 0 && version < mMinimumSupportedVersion) { |
| 398 | File databaseFile = new File(db.getPath()); |
| 399 | onBeforeDelete(db); |
| 400 | db.close(); |
| 401 | if (SQLiteDatabase.deleteDatabase(databaseFile)) { |
| 402 | mIsInitializing = false; |
| 403 | return getDatabaseLocked(writable); |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 404 | } else { |
Suprabh Shukla | 622217696 | 2016-10-06 14:41:35 -0700 | [diff] [blame] | 405 | throw new IllegalStateException("Unable to delete obsolete database " |
| 406 | + mName + " with version " + version); |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 407 | } |
Suprabh Shukla | 622217696 | 2016-10-06 14:41:35 -0700 | [diff] [blame] | 408 | } else { |
| 409 | db.beginTransaction(); |
| 410 | try { |
| 411 | if (version == 0) { |
| 412 | onCreate(db); |
| 413 | } else { |
| 414 | if (version > mNewVersion) { |
| 415 | onDowngrade(db, version, mNewVersion); |
| 416 | } else { |
| 417 | onUpgrade(db, version, mNewVersion); |
| 418 | } |
| 419 | } |
| 420 | db.setVersion(mNewVersion); |
| 421 | db.setTransactionSuccessful(); |
| 422 | } finally { |
| 423 | db.endTransaction(); |
| 424 | } |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 425 | } |
| 426 | } |
Jeff Brown | 96496ad | 2012-03-23 14:38:06 -0700 | [diff] [blame] | 427 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | onOpen(db); |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 429 | |
| 430 | if (db.isReadOnly()) { |
| 431 | Log.w(TAG, "Opened " + mName + " in read-only mode"); |
| 432 | } |
| 433 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | mDatabase = db; |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 435 | return db; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | } finally { |
| 437 | mIsInitializing = false; |
Jeff Brown | 559d064 | 2012-02-29 10:19:12 -0800 | [diff] [blame] | 438 | if (db != null && db != mDatabase) { |
| 439 | db.close(); |
| 440 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 444 | private static void setFilePermissionsForDb(String dbPath) { |
| 445 | int perms = FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IRGRP | FileUtils.S_IWGRP; |
| 446 | FileUtils.setPermissions(dbPath, perms, -1, -1); |
| 447 | } |
| 448 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | /** |
| 450 | * Close any open database object. |
| 451 | */ |
| 452 | public synchronized void close() { |
| 453 | if (mIsInitializing) throw new IllegalStateException("Closed during initialization"); |
| 454 | |
| 455 | if (mDatabase != null && mDatabase.isOpen()) { |
| 456 | mDatabase.close(); |
| 457 | mDatabase = null; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | /** |
Suprabh Shukla | 622217696 | 2016-10-06 14:41:35 -0700 | [diff] [blame] | 462 | * Called when the database connection is being configured, to enable features such as |
| 463 | * write-ahead logging or foreign key support. |
Jeff Brown | 96496ad | 2012-03-23 14:38:06 -0700 | [diff] [blame] | 464 | * <p> |
Suprabh Shukla | 622217696 | 2016-10-06 14:41:35 -0700 | [diff] [blame] | 465 | * This method is called before {@link #onCreate}, {@link #onUpgrade}, {@link #onDowngrade}, or |
| 466 | * {@link #onOpen} are called. It should not modify the database except to configure the |
| 467 | * database connection as required. |
| 468 | * </p> |
| 469 | * <p> |
| 470 | * This method should only call methods that configure the parameters of the database |
| 471 | * connection, such as {@link SQLiteDatabase#enableWriteAheadLogging} |
| 472 | * {@link SQLiteDatabase#setForeignKeyConstraintsEnabled}, {@link SQLiteDatabase#setLocale}, |
| 473 | * {@link SQLiteDatabase#setMaximumSize}, or executing PRAGMA statements. |
Jeff Brown | 96496ad | 2012-03-23 14:38:06 -0700 | [diff] [blame] | 474 | * </p> |
| 475 | * |
| 476 | * @param db The database. |
| 477 | */ |
| 478 | public void onConfigure(SQLiteDatabase db) {} |
| 479 | |
| 480 | /** |
Suprabh Shukla | 622217696 | 2016-10-06 14:41:35 -0700 | [diff] [blame] | 481 | * Called before the database is deleted when the version returned by |
| 482 | * {@link SQLiteDatabase#getVersion()} is lower than the minimum supported version passed (if at |
| 483 | * all) while creating this helper. After the database is deleted, a fresh database with the |
| 484 | * given version is created. This will be followed by {@link #onConfigure(SQLiteDatabase)} and |
| 485 | * {@link #onCreate(SQLiteDatabase)} being called with a new SQLiteDatabase object |
| 486 | * |
| 487 | * @param db the database opened with this helper |
| 488 | * @see #SQLiteOpenHelper(Context, String, CursorFactory, int, int, DatabaseErrorHandler) |
| 489 | * @hide |
| 490 | */ |
| 491 | public void onBeforeDelete(SQLiteDatabase db) { |
| 492 | } |
| 493 | |
| 494 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | * Called when the database is created for the first time. This is where the |
| 496 | * creation of tables and the initial population of the tables should happen. |
| 497 | * |
| 498 | * @param db The database. |
| 499 | */ |
| 500 | public abstract void onCreate(SQLiteDatabase db); |
| 501 | |
| 502 | /** |
| 503 | * Called when the database needs to be upgraded. The implementation |
| 504 | * should use this method to drop tables, add tables, or do anything else it |
| 505 | * needs to upgrade to the new schema version. |
| 506 | * |
Jeff Brown | 96496ad | 2012-03-23 14:38:06 -0700 | [diff] [blame] | 507 | * <p> |
| 508 | * The SQLite ALTER TABLE documentation can be found |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | * <a href="http://sqlite.org/lang_altertable.html">here</a>. If you add new columns |
| 510 | * you can use ALTER TABLE to insert them into a live table. If you rename or remove columns |
| 511 | * you can use ALTER TABLE to rename the old table, then create the new table and then |
| 512 | * populate the new table with the contents of the old table. |
Jeff Brown | 96496ad | 2012-03-23 14:38:06 -0700 | [diff] [blame] | 513 | * </p><p> |
| 514 | * This method executes within a transaction. If an exception is thrown, all changes |
| 515 | * will automatically be rolled back. |
| 516 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | * |
| 518 | * @param db The database. |
| 519 | * @param oldVersion The old database version. |
| 520 | * @param newVersion The new database version. |
| 521 | */ |
| 522 | public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion); |
| 523 | |
| 524 | /** |
Pin Ting | 1c423b8 | 2012-01-18 11:21:06 +0800 | [diff] [blame] | 525 | * Called when the database needs to be downgraded. This is strictly similar to |
Jeff Brown | 96496ad | 2012-03-23 14:38:06 -0700 | [diff] [blame] | 526 | * {@link #onUpgrade} method, but is called whenever current version is newer than requested one. |
Eric Hassold | a5af5d6 | 2010-12-13 12:51:02 -0800 | [diff] [blame] | 527 | * However, this method is not abstract, so it is not mandatory for a customer to |
| 528 | * implement it. If not overridden, default implementation will reject downgrade and |
| 529 | * throws SQLiteException |
| 530 | * |
Jeff Brown | 96496ad | 2012-03-23 14:38:06 -0700 | [diff] [blame] | 531 | * <p> |
| 532 | * This method executes within a transaction. If an exception is thrown, all changes |
| 533 | * will automatically be rolled back. |
| 534 | * </p> |
| 535 | * |
Eric Hassold | a5af5d6 | 2010-12-13 12:51:02 -0800 | [diff] [blame] | 536 | * @param db The database. |
| 537 | * @param oldVersion The old database version. |
| 538 | * @param newVersion The new database version. |
| 539 | */ |
| 540 | public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 541 | throw new SQLiteException("Can't downgrade database from version " + |
| 542 | oldVersion + " to " + newVersion); |
| 543 | } |
| 544 | |
| 545 | /** |
Dan Egnor | 6fcc0f073 | 2010-07-27 16:32:17 -0700 | [diff] [blame] | 546 | * Called when the database has been opened. The implementation |
| 547 | * should check {@link SQLiteDatabase#isReadOnly} before updating the |
| 548 | * database. |
Jeff Brown | 96496ad | 2012-03-23 14:38:06 -0700 | [diff] [blame] | 549 | * <p> |
| 550 | * This method is called after the database connection has been configured |
| 551 | * and after the database schema has been created, upgraded or downgraded as necessary. |
| 552 | * If the database connection must be configured in some way before the schema |
| 553 | * is created, upgraded, or downgraded, do it in {@link #onConfigure} instead. |
| 554 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | * |
| 556 | * @param db The database. |
| 557 | */ |
| 558 | public void onOpen(SQLiteDatabase db) {} |
| 559 | } |