diff options
| author | Fyodor Kupolov <fkupolov@google.com> | 2017-08-03 17:56:44 -0700 |
|---|---|---|
| committer | Fyodor Kupolov <fkupolov@google.com> | 2017-08-07 11:12:38 -0700 |
| commit | 76436c04a42892d49be7a9df6893b4f2880d490e (patch) | |
| tree | 4ae7b24834d6831a32c6dd8c09d65fa31ddc8651 /core/java/android | |
| parent | b901f055b5796bd7d8f916c5783bb8cf4e096266 (diff) | |
API Review update for SQLiteDatabase
SQLiteDatabase openDatabase -- should take File for first parameter
instead of a String path
SQLiteDatabase.OpenParams.Builder -- make sure the javadocs says what
the default openFlags is and default idle connection timeout
SQLiteDatabase createInMemory -- throw if it has trouble instead of
returning null.
Test: cts/SQLiteDatabaseTest
Bug: 64331777
Bug: 64331778
Bug: 64330914
Change-Id: Ibecf4f4a6498795f9a5d12b94b77481e5745b523
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/database/sqlite/SQLiteDatabase.java | 19 | ||||
| -rw-r--r-- | core/java/android/database/sqlite/SQLiteOpenHelper.java | 8 |
2 files changed, 18 insertions, 9 deletions
diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java index 5b6efd4dcffe..de02ee51ac3a 100644 --- a/core/java/android/database/sqlite/SQLiteDatabase.java +++ b/core/java/android/database/sqlite/SQLiteDatabase.java @@ -703,12 +703,19 @@ public final class SQLiteDatabase extends SQLiteClosable { /** * Open the database according to the specified {@link OpenParams parameters} * - * @param path to database file to open and/or create + * @param path path to database file to open and/or create. + * <p><strong>Important:</strong> The file should be constructed either from an absolute path or + * by using {@link android.content.Context#getDatabasePath(String)}. * @param openParams configuration parameters that are used for opening {@link SQLiteDatabase} * @return the newly opened database * @throws SQLiteException if the database cannot be opened */ - public static SQLiteDatabase openDatabase(@NonNull String path, + public static SQLiteDatabase openDatabase(@NonNull File path, + @NonNull OpenParams openParams) { + return openDatabase(path.getPath(), openParams); + } + + private static SQLiteDatabase openDatabase(@NonNull String path, @NonNull OpenParams openParams) { Preconditions.checkArgument(openParams != null, "OpenParams cannot be null"); SQLiteDatabase db = new SQLiteDatabase(path, openParams.mOpenFlags, @@ -873,7 +880,8 @@ public final class SQLiteDatabase extends SQLiteClosable { * * @param factory an optional factory class that is called to instantiate a * cursor when query is called - * @return a SQLiteDatabase object, or null if the database can't be created + * @return a SQLiteDatabase instance + * @throws SQLiteException if the database cannot be created */ @NonNull public static SQLiteDatabase create(@Nullable CursorFactory factory) { @@ -889,7 +897,8 @@ public final class SQLiteDatabase extends SQLiteClosable { * <p>Sets the locale of the database to the the system's current locale. * Call {@link #setLocale} if you would like something else.</p> * @param openParams configuration parameters that are used for opening SQLiteDatabase - * @return a SQLiteDatabase object, or null if the database can't be created + * @return a SQLiteDatabase instance + * @throws SQLException if the database cannot be created */ @NonNull public static SQLiteDatabase createInMemory(@NonNull OpenParams openParams) { @@ -2322,7 +2331,7 @@ public final class SQLiteDatabase extends SQLiteClosable { } /** - * Returns flags to control database access mode + * Returns flags to control database access mode. Default value is 0. * * @see Builder#setOpenFlags(int) */ diff --git a/core/java/android/database/sqlite/SQLiteOpenHelper.java b/core/java/android/database/sqlite/SQLiteOpenHelper.java index dfaf714963eb..cc9e0f4ded84 100644 --- a/core/java/android/database/sqlite/SQLiteOpenHelper.java +++ b/core/java/android/database/sqlite/SQLiteOpenHelper.java @@ -289,12 +289,12 @@ public abstract class SQLiteOpenHelper { } else if (mName == null) { db = SQLiteDatabase.createInMemory(mOpenParamsBuilder.build()); } else { - final String path = mContext.getDatabasePath(mName).getPath(); + final File filePath = mContext.getDatabasePath(mName); SQLiteDatabase.OpenParams params = mOpenParamsBuilder.build(); try { - db = SQLiteDatabase.openDatabase(path, params); + db = SQLiteDatabase.openDatabase(filePath, params); // Keep pre-O-MR1 behavior by resetting file permissions to 660 - setFilePermissionsForDb(path); + setFilePermissionsForDb(filePath.getPath()); } catch (SQLException ex) { if (writable) { throw ex; @@ -302,7 +302,7 @@ public abstract class SQLiteOpenHelper { Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", ex); params = params.toBuilder().addOpenFlags(SQLiteDatabase.OPEN_READONLY).build(); - db = SQLiteDatabase.openDatabase(path, params); + db = SQLiteDatabase.openDatabase(filePath, params); } } |
