Add databases plugin v0 (android) (#441)
Summary: Adds a plugin for listing the databases, tables and contents of those tables in an android app. Right now, works with sqlite, but it should be generic enough to work with other db types. ## Changelog Add initial version of android databases plugin Creating a PR, I may need to do some cleaning up, but this is to kick off that process. Pull Request resolved: https://github.com/facebook/flipper/pull/441 Reviewed By: danielbuechele Differential Revision: D15288831 Pulled By: jknoxville fbshipit-source-id: 6379ad60d640ea6b0a9473acc03dd6ea81a3a8d4
This commit is contained in:
committed by
Facebook Github Bot
parent
f20a781bca
commit
a630b50a8f
@@ -1,14 +1,15 @@
|
||||
/**
|
||||
* 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.
|
||||
* <p>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.sample;
|
||||
|
||||
import android.content.Context;
|
||||
import com.facebook.flipper.core.FlipperClient;
|
||||
import com.facebook.flipper.plugins.crashreporter.CrashReporterPlugin;
|
||||
import com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin;
|
||||
import com.facebook.flipper.plugins.example.ExampleFlipperPlugin;
|
||||
import com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin;
|
||||
import com.facebook.flipper.plugins.inspector.DescriptorMapping;
|
||||
@@ -51,6 +52,7 @@ public final class FlipperInitializer {
|
||||
client.addPlugin(new FrescoFlipperPlugin());
|
||||
client.addPlugin(new ExampleFlipperPlugin());
|
||||
client.addPlugin(CrashReporterPlugin.getInstance());
|
||||
client.addPlugin(new DatabasesFlipperPlugin(context));
|
||||
client.start();
|
||||
|
||||
final OkHttpClient okHttpClient =
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* <p>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.sample;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
public class Database1Helper extends SQLiteOpenHelper {
|
||||
|
||||
// If you change the database schema, you must increment the database version.
|
||||
public static final int DATABASE_VERSION = 4;
|
||||
private static final String SQL_CREATE_FIRST_TABLE =
|
||||
"CREATE TABLE "
|
||||
+ "db1_first_table"
|
||||
+ " ("
|
||||
+ "_id INTEGER PRIMARY KEY,"
|
||||
+ "db1_col0_text TEXT,"
|
||||
+ "db1_col1_integer INTEGER,"
|
||||
+ "db1_col2_float FLOAT,"
|
||||
+ "db1_col3_blob TEXT,"
|
||||
+ "db1_col4_null TEXT DEFAULT NULL,"
|
||||
+ "db1_col5 TEXT,"
|
||||
+ "db1_col6 TEXT,"
|
||||
+ "db1_col7 TEXT,"
|
||||
+ "db1_col8 TEXT,"
|
||||
+ "db1_col9 TEXT"
|
||||
+ ")";
|
||||
private static final String SQL_CREATE_SECOND_TABLE =
|
||||
"CREATE TABLE "
|
||||
+ "db1_empty_table"
|
||||
+ " ("
|
||||
+ "_id INTEGER PRIMARY KEY,"
|
||||
+ "column1 TEXT,"
|
||||
+ "column2 TEXT)";
|
||||
|
||||
public Database1Helper(Context context) {
|
||||
super(context, "database1.db", null, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
db.execSQL(SQL_CREATE_FIRST_TABLE);
|
||||
db.execSQL(SQL_CREATE_SECOND_TABLE);
|
||||
insertSampleData(db);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
// This database is only a cache for online data, so its upgrade policy is
|
||||
// to simply to discard the data and start over
|
||||
db.execSQL("DROP TABLE IF EXISTS first_table");
|
||||
db.execSQL("DROP TABLE IF EXISTS second_table");
|
||||
db.execSQL("DROP TABLE IF EXISTS db1_first_table");
|
||||
db.execSQL("DROP TABLE IF EXISTS db1_empty_table");
|
||||
onCreate(db);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
onUpgrade(db, oldVersion, newVersion);
|
||||
}
|
||||
|
||||
public void insertSampleData(SQLiteDatabase db) {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
ContentValues contentValues = new ContentValues();
|
||||
contentValues.put("db1_col0_text", "Long text data for testing resizing");
|
||||
contentValues.put("db1_col1_integer", 1000 + i);
|
||||
contentValues.put("db1_col2_float", 1000.465f + i);
|
||||
contentValues.put("db1_col3_blob", new byte[] {0, 0, 0, 1, 1, 0, 1, 1});
|
||||
contentValues.put("db1_col5", "db_1_column5_value");
|
||||
contentValues.put("db1_col6", "db_1_column6_value");
|
||||
contentValues.put("db1_col7", "db_1_column7_value");
|
||||
contentValues.put("db1_col8", "db_1_column8_value");
|
||||
contentValues.put("db1_col9", "db_1_column9_value");
|
||||
db.insert("db1_first_table", null, contentValues);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* <p>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.sample;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
public class Database2Helper extends SQLiteOpenHelper {
|
||||
|
||||
// If you change the database schema, you must increment the database version.
|
||||
public static final int DATABASE_VERSION = 4;
|
||||
private static final String SQL_CREATE_FIRST_TABLE =
|
||||
"CREATE TABLE "
|
||||
+ "db2_first_table"
|
||||
+ " ("
|
||||
+ "_id INTEGER PRIMARY KEY,"
|
||||
+ "column1 TEXT,"
|
||||
+ "column2 TEXT)";
|
||||
private static final String SQL_CREATE_SECOND_TABLE =
|
||||
"CREATE TABLE "
|
||||
+ "db2_second_table"
|
||||
+ " ("
|
||||
+ "_id INTEGER PRIMARY KEY,"
|
||||
+ "column1 TEXT,"
|
||||
+ "column2 TEXT)";
|
||||
|
||||
public Database2Helper(Context context) {
|
||||
super(context, "database2.db", null, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
db.execSQL(SQL_CREATE_FIRST_TABLE);
|
||||
db.execSQL(SQL_CREATE_SECOND_TABLE);
|
||||
insertSampleData(db);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
// This database is only a cache for online data, so its upgrade policy is
|
||||
// to simply to discard the data and start over
|
||||
db.execSQL("DROP TABLE IF EXISTS first_table");
|
||||
db.execSQL("DROP TABLE IF EXISTS second_table");
|
||||
db.execSQL("DROP TABLE IF EXISTS db2_first_table");
|
||||
db.execSQL("DROP TABLE IF EXISTS db2_second_table");
|
||||
onCreate(db);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
onUpgrade(db, oldVersion, newVersion);
|
||||
}
|
||||
|
||||
public void insertSampleData(SQLiteDatabase db) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
ContentValues contentValues = new ContentValues();
|
||||
contentValues.put("column1", "Long text data for testing resizing");
|
||||
contentValues.put(
|
||||
"column2",
|
||||
"extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra extra Long text data for testing resizing");
|
||||
db.insert("db2_first_table", null, contentValues);
|
||||
db.insert("db2_second_table", null, contentValues);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,14 @@
|
||||
/**
|
||||
* 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.
|
||||
* <p>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.sample;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.database.DatabaseUtils;
|
||||
import com.facebook.drawee.backends.pipeline.Fresco;
|
||||
import com.facebook.flipper.android.AndroidFlipperClient;
|
||||
import com.facebook.flipper.core.FlipperClient;
|
||||
@@ -33,5 +34,11 @@ public class FlipperSampleApplication extends Application {
|
||||
.edit()
|
||||
.putInt("SomeKey", 1337)
|
||||
.apply();
|
||||
|
||||
Database1Helper db1Helper = new Database1Helper(this);
|
||||
Database2Helper db2Helper = new Database2Helper(this);
|
||||
|
||||
DatabaseUtils.queryNumEntries(db1Helper.getReadableDatabase(), "db1_first_table", null, null);
|
||||
DatabaseUtils.queryNumEntries(db2Helper.getReadableDatabase(), "db2_first_table", null, null);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user