Back out "Back out "[flipper][PR] Use interface SupportSQLiteDatabase in Android Databases Plugin""

Summary: jknoxville helped me debug this. The underlying problem was that we didn't include a dependency which is transitive in gradle land. We swallowed the exception and just returned an empty list. I left an error log in there to make future debugging easier. For reference, this was the error:

Reviewed By: cekkaewnumchai

Differential Revision: D23759727

fbshipit-source-id: 244510d2765913bf4c89458e336889c3c6838fc4
This commit is contained in:
Pascal Hartig
2020-09-17 10:28:42 -07:00
committed by Facebook GitHub Bot
parent 54d36876b6
commit ddc9c3e243
7 changed files with 155 additions and 54 deletions

View File

@@ -65,6 +65,7 @@ android {
implementation deps.soloader
implementation deps.jsr305
implementation deps.supportAppCompat
implementation deps.supportSqlite
testImplementation deps.mockito
testImplementation deps.robolectric

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}
}
}

View File

@@ -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;
}

View File

@@ -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,9 +89,10 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
cursor.close();
}
} finally {
database.close();
close(database);
}
} catch (SQLiteException ex) {
Log.e(TAG, "Temporary SQLite exception caught and ignored.", ex);
return Collections.emptyList();
}
}
@@ -117,7 +100,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 +118,7 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
return executeRawQuery(database, query);
}
} finally {
database.close();
close(database);
}
}
@@ -147,13 +130,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 +152,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 +210,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 +235,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 +259,7 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
definitionCursor.close();
}
} finally {
database.close();
close(database);
}
}
@@ -304,20 +292,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 +317,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 +352,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;