Use interface SupportSQLiteDatabase in Android Databases Plugin (#1196)
Summary: This change will allow to use various SQLiteDatabase implementations: standard Android implementation, [requery/sqlite-android](https://github.com/requery/sqlite-android) and so on. See issue https://github.com/facebook/flipper/issues/1183 ## Changelog Android Databases Plugin: `SqliteDatabaseConnectionProvider` returns `SupportSQLiteDatabase` instead of `SQLiteDatabase`. Pull Request resolved: https://github.com/facebook/flipper/pull/1196 Test Plan: Check that Databases Plugin shows correct data on the sample Android application. Reviewed By: mweststrate Differential Revision: D23294272 Pulled By: passy fbshipit-source-id: c07ebeb869ab01d41281f75541cbb3411f0ebae0
This commit is contained in:
committed by
Facebook GitHub Bot
parent
e13d63ea6f
commit
7a7a88bfde
@@ -65,6 +65,7 @@ android {
|
||||
implementation deps.soloader
|
||||
implementation deps.jsr305
|
||||
implementation deps.supportAppCompat
|
||||
implementation deps.supportSqlite
|
||||
|
||||
testImplementation deps.mockito
|
||||
testImplementation deps.robolectric
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.flipper.plugins.databases.impl;
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
import java.io.File;
|
||||
|
||||
public class DefaultSqliteDatabaseConnectionProvider implements SqliteDatabaseConnectionProvider {
|
||||
|
||||
@Override
|
||||
public SupportSQLiteDatabase openDatabase(File databaseFile) throws SQLiteException {
|
||||
int flags = SQLiteDatabase.OPEN_READWRITE;
|
||||
SQLiteDatabase database =
|
||||
SQLiteDatabase.openDatabase(databaseFile.getAbsolutePath(), null, flags);
|
||||
return FrameworkSQLiteDatabaseWrapping.wrap(database);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.flipper.plugins.databases.impl;
|
||||
|
||||
import android.content.Context;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DefaultSqliteDatabaseProvider implements SqliteDatabaseProvider {
|
||||
|
||||
private Context context;
|
||||
|
||||
public DefaultSqliteDatabaseProvider(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<File> getDatabaseFiles() {
|
||||
List<File> databaseFiles = new ArrayList<>();
|
||||
for (String databaseName : context.databaseList()) {
|
||||
databaseFiles.add(context.getDatabasePath(databaseName));
|
||||
}
|
||||
return databaseFiles;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.flipper.plugins.databases.impl;
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import android.os.Build;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
/** Gives access to package-private class FrameworkSQLiteDatabase */
|
||||
public class FrameworkSQLiteDatabaseWrapping {
|
||||
|
||||
public static SupportSQLiteDatabase wrap(SQLiteDatabase database) throws SQLiteException {
|
||||
try {
|
||||
Class<?> clazz = Class.forName("androidx.sqlite.db.framework.FrameworkSQLiteDatabase");
|
||||
Constructor<?> constructor = clazz.getDeclaredConstructor(SQLiteDatabase.class);
|
||||
constructor.setAccessible(true);
|
||||
return (SupportSQLiteDatabase) constructor.newInstance(database);
|
||||
} catch (Exception e) {
|
||||
String errorMessage =
|
||||
"Failed to instantiate androidx.sqlite.db.framework.FrameworkSQLiteDatabase";
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
||||
throw new SQLiteException(errorMessage, e);
|
||||
} else {
|
||||
throw new SQLiteException(errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,11 @@
|
||||
|
||||
package com.facebook.flipper.plugins.databases.impl;
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
import java.io.File;
|
||||
|
||||
public interface SqliteDatabaseConnectionProvider {
|
||||
|
||||
SQLiteDatabase openDatabase(File databaseFile) throws SQLiteException;
|
||||
SupportSQLiteDatabase openDatabase(File databaseFile) throws SQLiteException;
|
||||
}
|
||||
|
||||
@@ -9,16 +9,17 @@ package com.facebook.flipper.plugins.databases.impl;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.DatabaseUtils;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import android.database.sqlite.SQLiteStatement;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
import androidx.sqlite.db.SupportSQLiteStatement;
|
||||
import com.facebook.flipper.plugins.databases.DatabaseDescriptor;
|
||||
import com.facebook.flipper.plugins.databases.DatabaseDriver;
|
||||
import com.facebook.flipper.plugins.databases.impl.SqliteDatabaseDriver.SqliteDatabaseDescriptor;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
@@ -33,37 +34,18 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
private static final String SCHEMA_TABLE = "sqlite_master";
|
||||
private static final String[] UNINTERESTING_FILENAME_SUFFIXES =
|
||||
new String[] {"-journal", "-shm", "-uid", "-wal"};
|
||||
private static final String TAG = "SqliteDatabaseDriver";
|
||||
|
||||
private final SqliteDatabaseProvider sqliteDatabaseProvider;
|
||||
private final SqliteDatabaseConnectionProvider sqliteDatabaseConnectionProvider;
|
||||
|
||||
public SqliteDatabaseDriver(final Context context) {
|
||||
this(
|
||||
context,
|
||||
new SqliteDatabaseProvider() {
|
||||
@Override
|
||||
public List<File> getDatabaseFiles() {
|
||||
List<File> databaseFiles = new ArrayList<>();
|
||||
for (String databaseName : context.databaseList()) {
|
||||
databaseFiles.add(context.getDatabasePath(databaseName));
|
||||
}
|
||||
return databaseFiles;
|
||||
}
|
||||
});
|
||||
this(context, new DefaultSqliteDatabaseProvider(context));
|
||||
}
|
||||
|
||||
public SqliteDatabaseDriver(
|
||||
final Context context, final SqliteDatabaseProvider sqliteDatabaseProvider) {
|
||||
this(
|
||||
context,
|
||||
sqliteDatabaseProvider,
|
||||
new SqliteDatabaseConnectionProvider() {
|
||||
@Override
|
||||
public SQLiteDatabase openDatabase(File databaseFile) throws SQLiteException {
|
||||
int flags = SQLiteDatabase.OPEN_READWRITE;
|
||||
return SQLiteDatabase.openDatabase(databaseFile.getAbsolutePath(), null, flags);
|
||||
}
|
||||
});
|
||||
this(context, sqliteDatabaseProvider, new DefaultSqliteDatabaseConnectionProvider());
|
||||
}
|
||||
|
||||
public SqliteDatabaseDriver(
|
||||
@@ -90,11 +72,11 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
@Override
|
||||
public List<String> getTableNames(SqliteDatabaseDescriptor databaseDescriptor) {
|
||||
try {
|
||||
SQLiteDatabase database =
|
||||
SupportSQLiteDatabase database =
|
||||
sqliteDatabaseConnectionProvider.openDatabase(databaseDescriptor.file);
|
||||
try {
|
||||
Cursor cursor =
|
||||
database.rawQuery(
|
||||
database.query(
|
||||
"SELECT name FROM " + SCHEMA_TABLE + " WHERE type IN (?, ?)",
|
||||
new String[] {"table", "view"});
|
||||
try {
|
||||
@@ -107,7 +89,7 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
cursor.close();
|
||||
}
|
||||
} finally {
|
||||
database.close();
|
||||
close(database);
|
||||
}
|
||||
} catch (SQLiteException ex) {
|
||||
return Collections.emptyList();
|
||||
@@ -117,7 +99,7 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
@Override
|
||||
public DatabaseExecuteSqlResponse executeSQL(
|
||||
SqliteDatabaseDescriptor databaseDescriptor, String query) {
|
||||
SQLiteDatabase database =
|
||||
SupportSQLiteDatabase database =
|
||||
sqliteDatabaseConnectionProvider.openDatabase(databaseDescriptor.file);
|
||||
try {
|
||||
String firstWordUpperCase = getFirstWord(query).toUpperCase();
|
||||
@@ -135,7 +117,7 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
return executeRawQuery(database, query);
|
||||
}
|
||||
} finally {
|
||||
database.close();
|
||||
close(database);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,13 +129,19 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
boolean reverse,
|
||||
int start,
|
||||
int count) {
|
||||
SQLiteDatabase database =
|
||||
SupportSQLiteDatabase database =
|
||||
sqliteDatabaseConnectionProvider.openDatabase(databaseDescriptor.file);
|
||||
try {
|
||||
String orderBy = order != null ? order + (reverse ? " DESC" : " ASC") : null;
|
||||
String limitBy = start + ", " + count;
|
||||
Cursor cursor = database.query(table, null, null, null, null, null, orderBy, limitBy);
|
||||
long total = DatabaseUtils.queryNumEntries(database, table);
|
||||
String query;
|
||||
if (orderBy != null) {
|
||||
query = "SELECT * from " + table + " ORDER BY " + orderBy + " LIMIT ?, ?";
|
||||
} else {
|
||||
query = "SELECT * from " + table + " LIMIT ?, ?";
|
||||
}
|
||||
|
||||
Cursor cursor = database.query(query, new Object[] {start, count});
|
||||
long total = queryNumEntries(database, table);
|
||||
try {
|
||||
String[] columnNames = cursor.getColumnNames();
|
||||
List<List<Object>> rows = cursorToList(cursor);
|
||||
@@ -163,19 +151,19 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
cursor.close();
|
||||
}
|
||||
} finally {
|
||||
database.close();
|
||||
close(database);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DatabaseGetTableStructureResponse getTableStructure(
|
||||
SqliteDatabaseDescriptor databaseDescriptor, String table) {
|
||||
SQLiteDatabase database =
|
||||
SupportSQLiteDatabase database =
|
||||
sqliteDatabaseConnectionProvider.openDatabase(databaseDescriptor.file);
|
||||
try {
|
||||
Cursor structureCursor = database.rawQuery("PRAGMA table_info(" + table + ")", null);
|
||||
Cursor foreignKeysCursor = database.rawQuery("PRAGMA foreign_key_list(" + table + ")", null);
|
||||
Cursor indexesCursor = database.rawQuery("PRAGMA index_list(" + table + ")", null);
|
||||
Cursor structureCursor = database.query("PRAGMA table_info(" + table + ")", null);
|
||||
Cursor foreignKeysCursor = database.query("PRAGMA foreign_key_list(" + table + ")", null);
|
||||
Cursor indexesCursor = database.query("PRAGMA index_list(" + table + ")", null);
|
||||
|
||||
try {
|
||||
// Structure & foreign keys
|
||||
@@ -221,7 +209,7 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
while (indexesCursor.moveToNext()) {
|
||||
List<String> indexedColumnNames = new ArrayList<>();
|
||||
String indexName = indexesCursor.getString(indexesCursor.getColumnIndex("name"));
|
||||
Cursor indexInfoCursor = database.rawQuery("PRAGMA index_info(" + indexName + ")", null);
|
||||
Cursor indexInfoCursor = database.query("PRAGMA index_info(" + indexName + ")", null);
|
||||
try {
|
||||
while (indexInfoCursor.moveToNext()) {
|
||||
indexedColumnNames.add(
|
||||
@@ -246,20 +234,19 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
indexesCursor.close();
|
||||
}
|
||||
} finally {
|
||||
database.close();
|
||||
close(database);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DatabaseGetTableInfoResponse getTableInfo(
|
||||
SqliteDatabaseDescriptor databaseDescriptor, String table) {
|
||||
SQLiteDatabase database =
|
||||
SupportSQLiteDatabase database =
|
||||
sqliteDatabaseConnectionProvider.openDatabase(databaseDescriptor.file);
|
||||
try {
|
||||
|
||||
Cursor definitionCursor =
|
||||
database.rawQuery(
|
||||
"SELECT sql FROM " + SCHEMA_TABLE + " WHERE name=?", new String[] {table});
|
||||
database.query("SELECT sql FROM " + SCHEMA_TABLE + " WHERE name=?", new String[] {table});
|
||||
try {
|
||||
|
||||
// Definition
|
||||
@@ -271,7 +258,7 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
definitionCursor.close();
|
||||
}
|
||||
} finally {
|
||||
database.close();
|
||||
close(database);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -304,20 +291,22 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
}
|
||||
|
||||
private static DatabaseExecuteSqlResponse executeUpdateDelete(
|
||||
SQLiteDatabase database, String query) {
|
||||
SQLiteStatement statement = database.compileStatement(query);
|
||||
SupportSQLiteDatabase database, String query) {
|
||||
SupportSQLiteStatement statement = database.compileStatement(query);
|
||||
int count = statement.executeUpdateDelete();
|
||||
return DatabaseExecuteSqlResponse.successfulUpdateDelete(count);
|
||||
}
|
||||
|
||||
private static DatabaseExecuteSqlResponse executeInsert(SQLiteDatabase database, String query) {
|
||||
SQLiteStatement statement = database.compileStatement(query);
|
||||
private static DatabaseExecuteSqlResponse executeInsert(
|
||||
SupportSQLiteDatabase database, String query) {
|
||||
SupportSQLiteStatement statement = database.compileStatement(query);
|
||||
long insertedId = statement.executeInsert();
|
||||
return DatabaseExecuteSqlResponse.successfulInsert(insertedId);
|
||||
}
|
||||
|
||||
private static DatabaseExecuteSqlResponse executeSelect(SQLiteDatabase database, String query) {
|
||||
Cursor cursor = database.rawQuery(query, null);
|
||||
private static DatabaseExecuteSqlResponse executeSelect(
|
||||
SupportSQLiteDatabase database, String query) {
|
||||
Cursor cursor = database.query(query, null);
|
||||
try {
|
||||
String[] columnNames = cursor.getColumnNames();
|
||||
List<List<Object>> rows = cursorToList(cursor);
|
||||
@@ -327,7 +316,8 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
}
|
||||
}
|
||||
|
||||
private static DatabaseExecuteSqlResponse executeRawQuery(SQLiteDatabase database, String query) {
|
||||
private static DatabaseExecuteSqlResponse executeRawQuery(
|
||||
SupportSQLiteDatabase database, String query) {
|
||||
database.execSQL(query);
|
||||
return DatabaseExecuteSqlResponse.successfulRawQuery();
|
||||
}
|
||||
@@ -361,6 +351,24 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
|
||||
}
|
||||
}
|
||||
|
||||
private long queryNumEntries(SupportSQLiteDatabase database, String table) {
|
||||
Cursor cursor = database.query("SELECT COUNT(*) FROM " + table);
|
||||
try {
|
||||
cursor.moveToFirst();
|
||||
return cursor.getLong(0);
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void close(SupportSQLiteDatabase database) {
|
||||
try {
|
||||
database.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Failed to close SQLite database", e);
|
||||
}
|
||||
}
|
||||
|
||||
static class SqliteDatabaseDescriptor implements DatabaseDescriptor {
|
||||
|
||||
public final File file;
|
||||
|
||||
@@ -56,6 +56,7 @@ ext.deps = [
|
||||
supportCoreUi : "androidx.legacy:legacy-support-core-ui:$ANDROIDX_VERSION",
|
||||
supportRecyclerView: "androidx.recyclerview:recyclerview:$ANDROIDX_VERSION",
|
||||
supportConstraintLayout: "androidx.constraintlayout:constraintlayout:1.1.2",
|
||||
supportSqlite : "androidx.sqlite:sqlite-framework:2.1.0",
|
||||
supportEspresso : 'androidx.test.espresso:espresso-core:3.1.0',
|
||||
supportDesign : "com.google.android.material:material:1.0.0-rc01",
|
||||
supportEspressoIntents : 'androidx.test.espresso:espresso-intents:3.1.0',
|
||||
|
||||
Reference in New Issue
Block a user