From c67e54719f4a07343b030b9b85393137689b5a34 Mon Sep 17 00:00:00 2001 From: anandgaurav10 Date: Mon, 12 Feb 2018 17:53:20 +0530 Subject: [PATCH 1/6] Add example for inMemory room database --- app/src/main/java/com/sample/MainActivity.java | 12 ++++++++++++ .../java/com/sample/database/room/UserDBHelper.java | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/src/main/java/com/sample/MainActivity.java b/app/src/main/java/com/sample/MainActivity.java index bc3683d..a239570 100644 --- a/app/src/main/java/com/sample/MainActivity.java +++ b/app/src/main/java/com/sample/MainActivity.java @@ -125,6 +125,18 @@ protected void onCreate(Bundle savedInstanceState) { userDBHelper.insertUser(userList); } + // Room inMemory database + if (userDBHelper.countInMemory() == 0) { + List userList = new ArrayList<>(); + for (int i = 0; i < 20; i++) { + User user = new User(); + user.id = (long) (i + 1); + user.name = "in_memory_user_" + i; + userList.add(user); + } + userDBHelper.insertUserInMemory(userList); + } + Utils.setCustomDatabaseFiles(getApplicationContext()); } diff --git a/app/src/main/java/com/sample/database/room/UserDBHelper.java b/app/src/main/java/com/sample/database/room/UserDBHelper.java index cc7f54b..d92a5a3 100644 --- a/app/src/main/java/com/sample/database/room/UserDBHelper.java +++ b/app/src/main/java/com/sample/database/room/UserDBHelper.java @@ -12,19 +12,29 @@ public class UserDBHelper { private final AppDatabase appDatabase; + private final AppDatabase inMemoryAppDatabase; public UserDBHelper(Context context) { appDatabase = Room.databaseBuilder(context, AppDatabase.class, "User-Database") .allowMainThreadQueries() .build(); + inMemoryAppDatabase = Room.inMemoryDatabaseBuilder(context, AppDatabase.class).build(); } public void insertUser(List userList) { appDatabase.userDao().insertAll(userList); } + public void insertUserInMemory(List userList) { + inMemoryAppDatabase.userDao().insertAll(userList); + } + public int count() { return appDatabase.userDao().loadAll().size(); } + public int countInMemory() { + return inMemoryAppDatabase.userDao().loadAll().size(); + } + } From 52d1cbab7903ff3dda0dbdb8eb7e21f6ed6f0413 Mon Sep 17 00:00:00 2001 From: anandgaurav10 Date: Mon, 12 Feb 2018 17:54:42 +0530 Subject: [PATCH 2/6] Add example for inMemory room database --- app/src/main/java/com/sample/database/room/UserDBHelper.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/sample/database/room/UserDBHelper.java b/app/src/main/java/com/sample/database/room/UserDBHelper.java index d92a5a3..6f854b7 100644 --- a/app/src/main/java/com/sample/database/room/UserDBHelper.java +++ b/app/src/main/java/com/sample/database/room/UserDBHelper.java @@ -18,7 +18,9 @@ public UserDBHelper(Context context) { appDatabase = Room.databaseBuilder(context, AppDatabase.class, "User-Database") .allowMainThreadQueries() .build(); - inMemoryAppDatabase = Room.inMemoryDatabaseBuilder(context, AppDatabase.class).build(); + inMemoryAppDatabase = Room.inMemoryDatabaseBuilder(context, AppDatabase.class) + .allowMainThreadQueries() + .build(); } public void insertUser(List userList) { From 43e48d15e6ee435ed0b1abc6d76638dc8bf0217d Mon Sep 17 00:00:00 2001 From: anandgaurav10 Date: Mon, 12 Feb 2018 18:46:11 +0530 Subject: [PATCH 3/6] Provide database instance through interface --- .../amitshekhar/server/RequestHandler.java | 26 ++++---- .../com/amitshekhar/sqlite/DebugSQLiteDB.java | 60 +++++++++++++++++++ .../java/com/amitshekhar/sqlite/SQLiteDB.java | 30 ++++++++++ .../com/amitshekhar/utils/DatabaseHelper.java | 21 ++++--- 4 files changed, 114 insertions(+), 23 deletions(-) create mode 100644 debug-db/src/main/java/com/amitshekhar/sqlite/DebugSQLiteDB.java create mode 100644 debug-db/src/main/java/com/amitshekhar/sqlite/SQLiteDB.java diff --git a/debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java b/debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java index ddec8dc..e96c3c7 100644 --- a/debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java +++ b/debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java @@ -29,6 +29,8 @@ import com.amitshekhar.model.RowDataRequest; import com.amitshekhar.model.TableDataResponse; import com.amitshekhar.model.UpdateRowResponse; +import com.amitshekhar.sqlite.DebugSQLiteDB; +import com.amitshekhar.sqlite.SQLiteDB; import com.amitshekhar.utils.Constants; import com.amitshekhar.utils.DatabaseFileProvider; import com.amitshekhar.utils.DatabaseHelper; @@ -60,7 +62,7 @@ public class RequestHandler { private final Gson mGson; private final AssetManager mAssets; private boolean isDbOpened; - private SQLiteDatabase mDatabase; + private SQLiteDB sqLiteDB; private HashMap> mDatabaseFiles; private HashMap> mCustomDatabaseFiles; private String mSelectedDatabase = null; @@ -172,15 +174,15 @@ private void openDatabase(String database) { SQLiteDatabase.loadLibs(mContext); - mDatabase = SQLiteDatabase.openOrCreateDatabase(databaseFile.getAbsolutePath(), password, null); + sqLiteDB = new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(databaseFile.getAbsolutePath(), password, null)); isDbOpened = true; } private void closeDatabase() { - if (mDatabase != null && mDatabase.isOpen()) { - mDatabase.close(); + if (sqLiteDB != null && sqLiteDB.isOpen()) { + sqLiteDB.close(); } - mDatabase = null; + sqLiteDB = null; isDbOpened = false; } @@ -213,7 +215,7 @@ private String getAllDataFromTheTableResponse(String route) { if (isDbOpened) { String sql = "SELECT * FROM " + tableName; - response = DatabaseHelper.getTableData(mDatabase, sql, tableName); + response = DatabaseHelper.getTableData(sqLiteDB, sql, tableName); } else { response = PrefHelper.getAllPrefData(mContext, tableName); } @@ -244,13 +246,13 @@ private String executeQueryAndGetResponse(String route) { String aQuery = statements[i].trim(); first = aQuery.split(" ")[0].toLowerCase(); if (first.equals("select") || first.equals("pragma")) { - TableDataResponse response = DatabaseHelper.getTableData(mDatabase, aQuery, null); + TableDataResponse response = DatabaseHelper.getTableData(sqLiteDB, aQuery, null); data = mGson.toJson(response); if (!response.isSuccessful) { break; } } else { - TableDataResponse response = DatabaseHelper.exec(mDatabase, aQuery); + TableDataResponse response = DatabaseHelper.exec(sqLiteDB, aQuery); data = mGson.toJson(response); if (!response.isSuccessful) { break; @@ -285,7 +287,7 @@ private String getTableListResponse(String route) { mSelectedDatabase = Constants.APP_SHARED_PREFERENCES; } else { openDatabase(database); - response = DatabaseHelper.getAllTableName(mDatabase); + response = DatabaseHelper.getAllTableName(sqLiteDB); mSelectedDatabase = database; } return mGson.toJson(response); @@ -303,7 +305,7 @@ private String addTableDataAndGetResponse(String route) { if (Constants.APP_SHARED_PREFERENCES.equals(mSelectedDatabase)) { response = PrefHelper.addOrUpdateRow(mContext, tableName, rowDataRequests); } else { - response = DatabaseHelper.addRow(mDatabase, tableName, rowDataRequests); + response = DatabaseHelper.addRow(sqLiteDB, tableName, rowDataRequests); } return mGson.toJson(response); } catch (Exception e) { @@ -325,7 +327,7 @@ private String updateTableDataAndGetResponse(String route) { if (Constants.APP_SHARED_PREFERENCES.equals(mSelectedDatabase)) { response = PrefHelper.addOrUpdateRow(mContext, tableName, rowDataRequests); } else { - response = DatabaseHelper.updateRow(mDatabase, tableName, rowDataRequests); + response = DatabaseHelper.updateRow(sqLiteDB, tableName, rowDataRequests); } return mGson.toJson(response); } catch (Exception e) { @@ -348,7 +350,7 @@ private String deleteTableDataAndGetResponse(String route) { if (Constants.APP_SHARED_PREFERENCES.equals(mSelectedDatabase)) { response = PrefHelper.deleteRow(mContext, tableName, rowDataRequests); } else { - response = DatabaseHelper.deleteRow(mDatabase, tableName, rowDataRequests); + response = DatabaseHelper.deleteRow(sqLiteDB, tableName, rowDataRequests); } return mGson.toJson(response); } catch (Exception e) { diff --git a/debug-db/src/main/java/com/amitshekhar/sqlite/DebugSQLiteDB.java b/debug-db/src/main/java/com/amitshekhar/sqlite/DebugSQLiteDB.java new file mode 100644 index 0000000..385934d --- /dev/null +++ b/debug-db/src/main/java/com/amitshekhar/sqlite/DebugSQLiteDB.java @@ -0,0 +1,60 @@ +package com.amitshekhar.sqlite; + +import android.content.ContentValues; +import android.database.Cursor; +import android.database.SQLException; + +import net.sqlcipher.database.SQLiteDatabase; + +/** + * Created by anandgaurav on 12/02/18. + */ + +public class DebugSQLiteDB implements SQLiteDB { + + private final SQLiteDatabase database; + + public DebugSQLiteDB(SQLiteDatabase database) { + this.database = database; + } + + @Override + public int delete(String table, String whereClause, String[] whereArgs) { + return database.delete(table, whereClause, whereArgs); + } + + @Override + public boolean isOpen() { + return database.isOpen(); + } + + @Override + public void close() { + database.close(); + } + + @Override + public Cursor rawQuery(String sql, String[] selectionArgs) { + return database.rawQuery(sql, selectionArgs); + } + + @Override + public void execSQL(String sql) throws SQLException { + database.execSQL(sql); + } + + @Override + public long insert(String table, String nullColumnHack, ContentValues values) { + return database.insert(table, nullColumnHack, values); + } + + @Override + public int update(String table, ContentValues values, String whereClause, String[] whereArgs) { + return database.update(table, values, whereClause, whereArgs); + } + + @Override + public int getVersion() { + return database.getVersion(); + } +} diff --git a/debug-db/src/main/java/com/amitshekhar/sqlite/SQLiteDB.java b/debug-db/src/main/java/com/amitshekhar/sqlite/SQLiteDB.java new file mode 100644 index 0000000..df21e4e --- /dev/null +++ b/debug-db/src/main/java/com/amitshekhar/sqlite/SQLiteDB.java @@ -0,0 +1,30 @@ +package com.amitshekhar.sqlite; + +import android.content.ContentValues; +import android.database.Cursor; +import android.database.SQLException; + + +/** + * Created by anandgaurav on 12/02/18. + */ + +public interface SQLiteDB { + + int delete(String table, String whereClause, String[] whereArgs); + + boolean isOpen(); + + void close(); + + Cursor rawQuery(String sql, String[] selectionArgs); + + void execSQL(String sql) throws SQLException; + + long insert(String table, String nullColumnHack, ContentValues values); + + int update(String table, ContentValues values, String whereClause, String[] whereArgs); + + int getVersion(); + +} diff --git a/debug-db/src/main/java/com/amitshekhar/utils/DatabaseHelper.java b/debug-db/src/main/java/com/amitshekhar/utils/DatabaseHelper.java index b32f75f..531157a 100644 --- a/debug-db/src/main/java/com/amitshekhar/utils/DatabaseHelper.java +++ b/debug-db/src/main/java/com/amitshekhar/utils/DatabaseHelper.java @@ -20,15 +20,14 @@ package com.amitshekhar.utils; import android.content.ContentValues; +import android.database.Cursor; import android.text.TextUtils; import com.amitshekhar.model.Response; import com.amitshekhar.model.RowDataRequest; import com.amitshekhar.model.TableDataResponse; import com.amitshekhar.model.UpdateRowResponse; - -import net.sqlcipher.Cursor; -import net.sqlcipher.database.SQLiteDatabase; +import com.amitshekhar.sqlite.SQLiteDB; import java.util.ArrayList; import java.util.HashSet; @@ -44,7 +43,7 @@ private DatabaseHelper() { // This class in not publicly instantiable } - public static Response getAllTableName(SQLiteDatabase database) { + public static Response getAllTableName(SQLiteDB database) { Response response = new Response(); Cursor c = database.rawQuery("SELECT name FROM sqlite_master WHERE type='table' OR type='view' ORDER BY name COLLATE NOCASE", null); if (c.moveToFirst()) { @@ -63,7 +62,7 @@ public static Response getAllTableName(SQLiteDatabase database) { return response; } - public static TableDataResponse getTableData(SQLiteDatabase db, String selectQuery, String tableName) { + public static TableDataResponse getTableData(SQLiteDB db, String selectQuery, String tableName) { TableDataResponse tableData = new TableDataResponse(); tableData.isSelectQuery = true; @@ -173,7 +172,7 @@ private static String getQuotedTableName(String tableName) { return String.format("[%s]", tableName); } - private static List getTableInfo(SQLiteDatabase db, String pragmaQuery) { + private static List getTableInfo(SQLiteDB db, String pragmaQuery) { Cursor cursor; try { @@ -219,7 +218,7 @@ private static List getTableInfo(SQLiteDatabase db, } - public static UpdateRowResponse addRow(SQLiteDatabase db, String tableName, + public static UpdateRowResponse addRow(SQLiteDB db, String tableName, List rowDataRequests) { UpdateRowResponse updateRowResponse = new UpdateRowResponse(); @@ -261,7 +260,7 @@ public static UpdateRowResponse addRow(SQLiteDatabase db, String tableName, } - public static UpdateRowResponse updateRow(SQLiteDatabase db, String tableName, List rowDataRequests) { + public static UpdateRowResponse updateRow(SQLiteDB db, String tableName, List rowDataRequests) { UpdateRowResponse updateRowResponse = new UpdateRowResponse(); @@ -316,7 +315,7 @@ public static UpdateRowResponse updateRow(SQLiteDatabase db, String tableName, L } - public static UpdateRowResponse deleteRow(SQLiteDatabase db, String tableName, + public static UpdateRowResponse deleteRow(SQLiteDB db, String tableName, List rowDataRequests) { UpdateRowResponse updateRowResponse = new UpdateRowResponse(); @@ -363,7 +362,7 @@ public static UpdateRowResponse deleteRow(SQLiteDatabase db, String tableName, } - public static TableDataResponse exec(SQLiteDatabase database, String sql) { + public static TableDataResponse exec(SQLiteDB database, String sql) { TableDataResponse tableDataResponse = new TableDataResponse(); tableDataResponse.isSelectQuery = false; try { @@ -387,7 +386,7 @@ public static TableDataResponse exec(SQLiteDatabase database, String sql) { } private static String getTableName(String selectQuery) { - // TODO: 24/4/17 Handle JOIN Query + // TODO: Handle JOIN Query TableNameParser tableNameParser = new TableNameParser(selectQuery); HashSet tableNames = (HashSet) tableNameParser.tables(); From bd32b485160861bccbf9f5aba0f69bbebba55b94 Mon Sep 17 00:00:00 2001 From: anandgaurav10 Date: Mon, 12 Feb 2018 20:02:46 +0530 Subject: [PATCH 4/6] Add support for room inMemory databases --- .../main/java/com/sample/MainActivity.java | 1 + .../sample/database/room/UserDBHelper.java | 6 +- app/src/main/java/com/sample/utils/Utils.java | 17 ++++++ .../main/java/com/amitshekhar/DebugDB.java | 7 +++ .../com/amitshekhar/server/ClientServer.java | 10 ++-- .../amitshekhar/server/RequestHandler.java | 27 +++++++-- .../sqlite/InMemoryDebugSQLiteDB.java | 59 +++++++++++++++++++ .../java/com/amitshekhar/utils/Utils.java | 2 +- 8 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 debug-db/src/main/java/com/amitshekhar/sqlite/InMemoryDebugSQLiteDB.java diff --git a/app/src/main/java/com/sample/MainActivity.java b/app/src/main/java/com/sample/MainActivity.java index a239570..62744d0 100644 --- a/app/src/main/java/com/sample/MainActivity.java +++ b/app/src/main/java/com/sample/MainActivity.java @@ -138,6 +138,7 @@ protected void onCreate(Bundle savedInstanceState) { } Utils.setCustomDatabaseFiles(getApplicationContext()); + Utils.setInMemoryRoomDatabases(userDBHelper.getInMemoryDatabase()); } public void showDebugDbAddress(View view) { diff --git a/app/src/main/java/com/sample/database/room/UserDBHelper.java b/app/src/main/java/com/sample/database/room/UserDBHelper.java index 6f854b7..c406ac7 100644 --- a/app/src/main/java/com/sample/database/room/UserDBHelper.java +++ b/app/src/main/java/com/sample/database/room/UserDBHelper.java @@ -1,5 +1,6 @@ package com.sample.database.room; +import android.arch.persistence.db.SupportSQLiteDatabase; import android.arch.persistence.room.Room; import android.content.Context; @@ -15,7 +16,7 @@ public class UserDBHelper { private final AppDatabase inMemoryAppDatabase; public UserDBHelper(Context context) { - appDatabase = Room.databaseBuilder(context, AppDatabase.class, "User-Database") + appDatabase = Room.databaseBuilder(context, AppDatabase.class, "User.db") .allowMainThreadQueries() .build(); inMemoryAppDatabase = Room.inMemoryDatabaseBuilder(context, AppDatabase.class) @@ -39,4 +40,7 @@ public int countInMemory() { return inMemoryAppDatabase.userDao().loadAll().size(); } + public SupportSQLiteDatabase getInMemoryDatabase() { + return inMemoryAppDatabase.getOpenHelper().getWritableDatabase(); + } } diff --git a/app/src/main/java/com/sample/utils/Utils.java b/app/src/main/java/com/sample/utils/Utils.java index de75ca8..f71e60d 100644 --- a/app/src/main/java/com/sample/utils/Utils.java +++ b/app/src/main/java/com/sample/utils/Utils.java @@ -19,6 +19,7 @@ package com.sample.utils; +import android.arch.persistence.db.SupportSQLiteDatabase; import android.content.Context; import android.util.Pair; import android.widget.Toast; @@ -71,4 +72,20 @@ public static void setCustomDatabaseFiles(Context context) { } } + public static void setInMemoryRoomDatabases(SupportSQLiteDatabase... database) { + if (BuildConfig.DEBUG) { + try { + Class debugDB = Class.forName("com.amitshekhar.DebugDB"); + Class[] argTypes = new Class[]{HashMap.class}; + HashMap inMemoryDatabases = new HashMap<>(); + // set your inMemory database + inMemoryDatabases.put("InMemoryOne.db", database[0]); + Method setRoomInMemoryDatabase = debugDB.getMethod("setInMemoryRoomDatabases", argTypes); + setRoomInMemoryDatabase.invoke(null, inMemoryDatabases); + } catch (Exception ignore) { + + } + } + } + } diff --git a/debug-db/src/main/java/com/amitshekhar/DebugDB.java b/debug-db/src/main/java/com/amitshekhar/DebugDB.java index 8ec31cc..b6463e1 100644 --- a/debug-db/src/main/java/com/amitshekhar/DebugDB.java +++ b/debug-db/src/main/java/com/amitshekhar/DebugDB.java @@ -19,6 +19,7 @@ package com.amitshekhar; +import android.arch.persistence.db.SupportSQLiteDatabase; import android.content.Context; import android.util.Log; import android.util.Pair; @@ -79,6 +80,12 @@ public static void setCustomDatabaseFiles(HashMap> cu } } + public static void setInMemoryRoomDatabases(HashMap databases) { + if (clientServer != null) { + clientServer.setInMemoryRoomDatabases(databases); + } + } + public static boolean isServerRunning() { return clientServer != null && clientServer.isRunning(); } diff --git a/debug-db/src/main/java/com/amitshekhar/server/ClientServer.java b/debug-db/src/main/java/com/amitshekhar/server/ClientServer.java index 952e87e..552e5a3 100644 --- a/debug-db/src/main/java/com/amitshekhar/server/ClientServer.java +++ b/debug-db/src/main/java/com/amitshekhar/server/ClientServer.java @@ -24,6 +24,7 @@ */ +import android.arch.persistence.db.SupportSQLiteDatabase; import android.content.Context; import android.util.Log; import android.util.Pair; @@ -40,13 +41,10 @@ public class ClientServer implements Runnable { private static final String TAG = "ClientServer"; private final int mPort; - + private final RequestHandler mRequestHandler; private boolean mIsRunning; - private ServerSocket mServerSocket; - private final RequestHandler mRequestHandler; - public ClientServer(Context context, int port) { mRequestHandler = new RequestHandler(context); mPort = port; @@ -91,6 +89,10 @@ public void setCustomDatabaseFiles(HashMap> customDat mRequestHandler.setCustomDatabaseFiles(customDatabaseFiles); } + public void setInMemoryRoomDatabases(HashMap databases) { + mRequestHandler.setInMemoryRoomDatabases(databases); + } + public boolean isRunning() { return mIsRunning; } diff --git a/debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java b/debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java index e96c3c7..d8b71f3 100644 --- a/debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java +++ b/debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java @@ -19,6 +19,7 @@ package com.amitshekhar.server; +import android.arch.persistence.db.SupportSQLiteDatabase; import android.content.Context; import android.content.res.AssetManager; import android.net.Uri; @@ -30,6 +31,7 @@ import com.amitshekhar.model.TableDataResponse; import com.amitshekhar.model.UpdateRowResponse; import com.amitshekhar.sqlite.DebugSQLiteDB; +import com.amitshekhar.sqlite.InMemoryDebugSQLiteDB; import com.amitshekhar.sqlite.SQLiteDB; import com.amitshekhar.utils.Constants; import com.amitshekhar.utils.DatabaseFileProvider; @@ -66,6 +68,7 @@ public class RequestHandler { private HashMap> mDatabaseFiles; private HashMap> mCustomDatabaseFiles; private String mSelectedDatabase = null; + private HashMap mRoomInMemoryDatabases = new HashMap<>(); public RequestHandler(Context context) { mContext = context; @@ -162,6 +165,10 @@ public void setCustomDatabaseFiles(HashMap> customDat mCustomDatabaseFiles = customDatabaseFiles; } + public void setInMemoryRoomDatabases(HashMap databases) { + mRoomInMemoryDatabases = databases; + } + private void writeServerError(PrintStream output) { output.println("HTTP/1.0 500 Internal Server Error"); output.flush(); @@ -169,12 +176,14 @@ private void writeServerError(PrintStream output) { private void openDatabase(String database) { closeDatabase(); - File databaseFile = mDatabaseFiles.get(database).first; - String password = mDatabaseFiles.get(database).second; - - SQLiteDatabase.loadLibs(mContext); - - sqLiteDB = new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(databaseFile.getAbsolutePath(), password, null)); + if (mRoomInMemoryDatabases.containsKey(database)) { + sqLiteDB = new InMemoryDebugSQLiteDB(mRoomInMemoryDatabases.get(database)); + } else { + File databaseFile = mDatabaseFiles.get(database).first; + String password = mDatabaseFiles.get(database).second; + SQLiteDatabase.loadLibs(mContext); + sqLiteDB = new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(databaseFile.getAbsolutePath(), password, null)); + } isDbOpened = true; } @@ -198,6 +207,12 @@ private String getDBListResponse() { response.rows.add(dbEntry); } } + if (mRoomInMemoryDatabases != null) { + for (HashMap.Entry entry : mRoomInMemoryDatabases.entrySet()) { + String[] dbEntry = {entry.getKey(), "false"}; + response.rows.add(dbEntry); + } + } response.rows.add(new String[]{Constants.APP_SHARED_PREFERENCES, "false"}); response.isSuccessful = true; return mGson.toJson(response); diff --git a/debug-db/src/main/java/com/amitshekhar/sqlite/InMemoryDebugSQLiteDB.java b/debug-db/src/main/java/com/amitshekhar/sqlite/InMemoryDebugSQLiteDB.java new file mode 100644 index 0000000..8e0081d --- /dev/null +++ b/debug-db/src/main/java/com/amitshekhar/sqlite/InMemoryDebugSQLiteDB.java @@ -0,0 +1,59 @@ +package com.amitshekhar.sqlite; + +import android.arch.persistence.db.SupportSQLiteDatabase; +import android.content.ContentValues; +import android.database.Cursor; +import android.database.SQLException; + +/** + * Created by anandgaurav on 12/02/18. + */ + +public class InMemoryDebugSQLiteDB implements SQLiteDB { + + private final SupportSQLiteDatabase database; + + public InMemoryDebugSQLiteDB(SupportSQLiteDatabase database) { + this.database = database; + } + + @Override + public int delete(String table, String whereClause, String[] whereArgs) { + return database.delete(table, whereClause, whereArgs); + } + + @Override + public boolean isOpen() { + return database.isOpen(); + } + + @Override + public void close() { + // no ops + } + + @Override + public Cursor rawQuery(String sql, String[] selectionArgs) { + return database.query(sql, selectionArgs); + } + + @Override + public void execSQL(String sql) throws SQLException { + database.execSQL(sql); + } + + @Override + public long insert(String table, String nullColumnHack, ContentValues values) { + return database.insert(table, 0, values); + } + + @Override + public int update(String table, ContentValues values, String whereClause, String[] whereArgs) { + return database.update(table, 0, values, whereClause, whereArgs); + } + + @Override + public int getVersion() { + return database.getVersion(); + } +} diff --git a/debug-db/src/main/java/com/amitshekhar/utils/Utils.java b/debug-db/src/main/java/com/amitshekhar/utils/Utils.java index 6446b97..394d711 100644 --- a/debug-db/src/main/java/com/amitshekhar/utils/Utils.java +++ b/debug-db/src/main/java/com/amitshekhar/utils/Utils.java @@ -84,7 +84,7 @@ public static byte[] loadContent(String fileName, AssetManager assetManager) thr } public static byte[] getDatabase(String selectedDatabase, HashMap> databaseFiles) { - if (TextUtils.isEmpty(selectedDatabase)) { + if (TextUtils.isEmpty(selectedDatabase) || !databaseFiles.containsKey(selectedDatabase)) { return null; } From 8507529739dff5775443410ed58f2491c20e89a6 Mon Sep 17 00:00:00 2001 From: anandgaurav10 Date: Mon, 12 Feb 2018 21:35:01 +0530 Subject: [PATCH 5/6] Disable download for inMemoryDatabase --- debug-db/src/main/assets/app.js | 25 +++++++++++++------ .../amitshekhar/server/RequestHandler.java | 6 ++--- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/debug-db/src/main/assets/app.js b/debug-db/src/main/assets/app.js index 581879b..0c82d88 100644 --- a/debug-db/src/main/assets/app.js +++ b/debug-db/src/main/assets/app.js @@ -70,12 +70,13 @@ function getDBList() { for(var count = 0; count < dbList.length; count++){ var dbName = dbList[count][0]; var isEncrypted = dbList[count][1]; + var isDownloadable = dbList[count][2]; var dbAttribute = isEncrypted == "true" ? ' ' : ""; if(dbName.indexOf("journal") == -1){ - $("#db-list").append("" + dbName + dbAttribute + ""); + $("#db-list").append("" + dbName + dbAttribute + ""); if(!isSelectionDone){ isSelectionDone = true; - $('#db-list').find('a').trigger('click'); + $('#db-list').find('a').trigger('click'); } } } @@ -85,7 +86,7 @@ function getDBList() { } var lastTableName = getHashValue('table'); -function openDatabaseAndGetTableList(db) { +function openDatabaseAndGetTableList(db, isDownloadable) { if("APP_SHARED_PREFERENCES" == db) { $('#run-query').removeClass('active'); @@ -97,10 +98,16 @@ function openDatabaseAndGetTableList(db) { } else { $('#run-query').removeClass('disabled'); $('#run-query').addClass('active'); - $('#selected-db-info').removeClass('disabled'); - $('#selected-db-info').addClass('active'); + if("true" == isDownloadable) { + $('#selected-db-info').removeClass('disabled'); + $('#selected-db-info').addClass('active'); + $("#selected-db-info").text("Export Selected Database : "+db); + } else { + $('#selected-db-info').removeClass('active'); + $('#selected-db-info').addClass('disabled'); + $("#selected-db-info").text("Selected Database : "+db); + } isDatabaseSelected = true; - $("#selected-db-info").text("Export Selected Database : "+db); } @@ -110,7 +117,11 @@ function openDatabaseAndGetTableList(db) { var tableList = result.rows; var dbVersion = result.dbVersion; if("APP_SHARED_PREFERENCES" != db) { - $("#selected-db-info").text("Export Selected Database : "+db +" Version : "+dbVersion); + if("true" == isDownloadable) { + $("#selected-db-info").text("Export Selected Database : "+db +" Version : "+dbVersion); + } else { + $("#selected-db-info").text("Selected Database : "+db +" Version : "+dbVersion); + } } $('#table-list').empty() for(var count = 0; count < tableList.length; count++){ diff --git a/debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java b/debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java index d8b71f3..aa0d9b5 100644 --- a/debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java +++ b/debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java @@ -203,17 +203,17 @@ private String getDBListResponse() { Response response = new Response(); if (mDatabaseFiles != null) { for (HashMap.Entry> entry : mDatabaseFiles.entrySet()) { - String[] dbEntry = {entry.getKey(), !entry.getValue().second.equals("") ? "true" : "false"}; + String[] dbEntry = {entry.getKey(), !entry.getValue().second.equals("") ? "true" : "false", "true"}; response.rows.add(dbEntry); } } if (mRoomInMemoryDatabases != null) { for (HashMap.Entry entry : mRoomInMemoryDatabases.entrySet()) { - String[] dbEntry = {entry.getKey(), "false"}; + String[] dbEntry = {entry.getKey(), "false", "false"}; response.rows.add(dbEntry); } } - response.rows.add(new String[]{Constants.APP_SHARED_PREFERENCES, "false"}); + response.rows.add(new String[]{Constants.APP_SHARED_PREFERENCES, "false", "false"}); response.isSuccessful = true; return mGson.toJson(response); } From 7595b16d2f31a0e1ae641d25f1b17ccda408d195 Mon Sep 17 00:00:00 2001 From: anandgaurav10 Date: Mon, 12 Feb 2018 21:41:36 +0530 Subject: [PATCH 6/6] Add desc for room inMemory db and bump version --- README.md | 24 +++++++++++++++++-- app/src/main/java/com/sample/utils/Utils.java | 2 +- debug-db/debug-db-upload.gradle | 2 +- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 52b7db7..1f5608b 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ ### Using Android Debug Database Library in your application Add this to your app's build.gradle ```groovy -debugCompile 'com.amitshekhar.android:debug-db:1.0.2' +debugCompile 'com.amitshekhar.android:debug-db:1.0.3' ``` Use `debugCompile` so that it will only compile in your debug build and not in your release build. @@ -95,7 +95,7 @@ public static void showDebugDBAddressLogToast(Context context) { ``` ### Adding custom database files -As this library is auto-initialize, if you want to add custom database files, add the following method and call +As this library is auto-initialize, if you want to debug custom database files, add the following method and call ```java public static void setCustomDatabaseFiles(Context context) { if (BuildConfig.DEBUG) { @@ -116,6 +116,26 @@ public static void setCustomDatabaseFiles(Context context) { } ``` +### Adding InMemory Room databases +As this library is auto-initialize, if you want to debug inMemory Room databases, add the following method and call +```java +public static void setInMemoryRoomDatabases(SupportSQLiteDatabase... database) { + if (BuildConfig.DEBUG) { + try { + Class debugDB = Class.forName("com.amitshekhar.DebugDB"); + Class[] argTypes = new Class[]{HashMap.class}; + HashMap inMemoryDatabases = new HashMap<>(); + // set your inMemory databases + inMemoryDatabases.put("InMemoryOne.db", database[0]); + Method setRoomInMemoryDatabase = debugDB.getMethod("setInMemoryRoomDatabases", argTypes); + setRoomInMemoryDatabase.invoke(null, inMemoryDatabases); + } catch (Exception ignore) { + + } + } +} +``` + ### Find this project useful ? :heart: * Support it by clicking the :star: button on the upper right of this page. :v: diff --git a/app/src/main/java/com/sample/utils/Utils.java b/app/src/main/java/com/sample/utils/Utils.java index f71e60d..e4edb4d 100644 --- a/app/src/main/java/com/sample/utils/Utils.java +++ b/app/src/main/java/com/sample/utils/Utils.java @@ -78,7 +78,7 @@ public static void setInMemoryRoomDatabases(SupportSQLiteDatabase... database) { Class debugDB = Class.forName("com.amitshekhar.DebugDB"); Class[] argTypes = new Class[]{HashMap.class}; HashMap inMemoryDatabases = new HashMap<>(); - // set your inMemory database + // set your inMemory databases inMemoryDatabases.put("InMemoryOne.db", database[0]); Method setRoomInMemoryDatabase = debugDB.getMethod("setInMemoryRoomDatabases", argTypes); setRoomInMemoryDatabase.invoke(null, inMemoryDatabases); diff --git a/debug-db/debug-db-upload.gradle b/debug-db/debug-db-upload.gradle index 21b4784..05d6d12 100755 --- a/debug-db/debug-db-upload.gradle +++ b/debug-db/debug-db-upload.gradle @@ -24,7 +24,7 @@ def siteUrl = 'https://github.com/amitshekhariitbhu/Android-Debug-Database' def gitUrl = 'https://github.com/amitshekhariitbhu/Android-Debug-Database.git' group = "com.amitshekhar.android" -version = '1.0.2' +version = '1.0.3' install { repositories.mavenInstaller {