Back out "Use interface SupportSQLiteDatabase in Android Databases Plugin"

Summary: This broke DB opening in FB apps. Will try to fix and reland this, but need to unblock people first.

Reviewed By: cekkaewnumchai

Differential Revision: D23705962

fbshipit-source-id: 3c9ff3a74e5a6d34e6cb2c75e7e2cec749b5a60e
This commit is contained in:
Pascal Hartig
2020-09-15 17:04:08 -07:00
committed by Facebook GitHub Bot
parent 09e3c7a80f
commit bb176577e9
7 changed files with 54 additions and 154 deletions

View File

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

View File

@@ -1,24 +0,0 @@
/*
* 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

@@ -1,31 +0,0 @@
/*
* 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

@@ -1,35 +0,0 @@
/*
* 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 {
SupportSQLiteDatabase openDatabase(File databaseFile) throws SQLiteException;
SQLiteDatabase openDatabase(File databaseFile) throws SQLiteException;
}

View File

@@ -9,17 +9,16 @@ 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;
@@ -34,18 +33,37 @@ 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 DefaultSqliteDatabaseProvider(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;
}
});
}
public SqliteDatabaseDriver(
final Context context, final SqliteDatabaseProvider sqliteDatabaseProvider) {
this(context, sqliteDatabaseProvider, new DefaultSqliteDatabaseConnectionProvider());
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);
}
});
}
public SqliteDatabaseDriver(
@@ -72,11 +90,11 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
@Override
public List<String> getTableNames(SqliteDatabaseDescriptor databaseDescriptor) {
try {
SupportSQLiteDatabase database =
SQLiteDatabase database =
sqliteDatabaseConnectionProvider.openDatabase(databaseDescriptor.file);
try {
Cursor cursor =
database.query(
database.rawQuery(
"SELECT name FROM " + SCHEMA_TABLE + " WHERE type IN (?, ?)",
new String[] {"table", "view"});
try {
@@ -89,7 +107,7 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
cursor.close();
}
} finally {
close(database);
database.close();
}
} catch (SQLiteException ex) {
return Collections.emptyList();
@@ -99,7 +117,7 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
@Override
public DatabaseExecuteSqlResponse executeSQL(
SqliteDatabaseDescriptor databaseDescriptor, String query) {
SupportSQLiteDatabase database =
SQLiteDatabase database =
sqliteDatabaseConnectionProvider.openDatabase(databaseDescriptor.file);
try {
String firstWordUpperCase = getFirstWord(query).toUpperCase();
@@ -117,7 +135,7 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
return executeRawQuery(database, query);
}
} finally {
close(database);
database.close();
}
}
@@ -129,19 +147,13 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
boolean reverse,
int start,
int count) {
SupportSQLiteDatabase database =
SQLiteDatabase database =
sqliteDatabaseConnectionProvider.openDatabase(databaseDescriptor.file);
try {
String orderBy = order != null ? order + (reverse ? " DESC" : " ASC") : null;
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);
String limitBy = start + ", " + count;
Cursor cursor = database.query(table, null, null, null, null, null, orderBy, limitBy);
long total = DatabaseUtils.queryNumEntries(database, table);
try {
String[] columnNames = cursor.getColumnNames();
List<List<Object>> rows = cursorToList(cursor);
@@ -151,19 +163,19 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
cursor.close();
}
} finally {
close(database);
database.close();
}
}
@Override
public DatabaseGetTableStructureResponse getTableStructure(
SqliteDatabaseDescriptor databaseDescriptor, String table) {
SupportSQLiteDatabase database =
SQLiteDatabase database =
sqliteDatabaseConnectionProvider.openDatabase(databaseDescriptor.file);
try {
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);
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);
try {
// Structure & foreign keys
@@ -209,7 +221,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.query("PRAGMA index_info(" + indexName + ")", null);
Cursor indexInfoCursor = database.rawQuery("PRAGMA index_info(" + indexName + ")", null);
try {
while (indexInfoCursor.moveToNext()) {
indexedColumnNames.add(
@@ -234,19 +246,20 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
indexesCursor.close();
}
} finally {
close(database);
database.close();
}
}
@Override
public DatabaseGetTableInfoResponse getTableInfo(
SqliteDatabaseDescriptor databaseDescriptor, String table) {
SupportSQLiteDatabase database =
SQLiteDatabase database =
sqliteDatabaseConnectionProvider.openDatabase(databaseDescriptor.file);
try {
Cursor definitionCursor =
database.query("SELECT sql FROM " + SCHEMA_TABLE + " WHERE name=?", new String[] {table});
database.rawQuery(
"SELECT sql FROM " + SCHEMA_TABLE + " WHERE name=?", new String[] {table});
try {
// Definition
@@ -258,7 +271,7 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
definitionCursor.close();
}
} finally {
close(database);
database.close();
}
}
@@ -291,22 +304,20 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
}
private static DatabaseExecuteSqlResponse executeUpdateDelete(
SupportSQLiteDatabase database, String query) {
SupportSQLiteStatement statement = database.compileStatement(query);
SQLiteDatabase database, String query) {
SQLiteStatement statement = database.compileStatement(query);
int count = statement.executeUpdateDelete();
return DatabaseExecuteSqlResponse.successfulUpdateDelete(count);
}
private static DatabaseExecuteSqlResponse executeInsert(
SupportSQLiteDatabase database, String query) {
SupportSQLiteStatement statement = database.compileStatement(query);
private static DatabaseExecuteSqlResponse executeInsert(SQLiteDatabase database, String query) {
SQLiteStatement statement = database.compileStatement(query);
long insertedId = statement.executeInsert();
return DatabaseExecuteSqlResponse.successfulInsert(insertedId);
}
private static DatabaseExecuteSqlResponse executeSelect(
SupportSQLiteDatabase database, String query) {
Cursor cursor = database.query(query, null);
private static DatabaseExecuteSqlResponse executeSelect(SQLiteDatabase database, String query) {
Cursor cursor = database.rawQuery(query, null);
try {
String[] columnNames = cursor.getColumnNames();
List<List<Object>> rows = cursorToList(cursor);
@@ -316,8 +327,7 @@ public class SqliteDatabaseDriver extends DatabaseDriver<SqliteDatabaseDescripto
}
}
private static DatabaseExecuteSqlResponse executeRawQuery(
SupportSQLiteDatabase database, String query) {
private static DatabaseExecuteSqlResponse executeRawQuery(SQLiteDatabase database, String query) {
database.execSQL(query);
return DatabaseExecuteSqlResponse.successfulRawQuery();
}
@@ -351,24 +361,6 @@ 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;

View File

@@ -56,7 +56,6 @@ 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',