Merge pull request #102 from amitshekhariitbhu/development · codeOwlAI/Android-Debug-Database@e011034 · GitHub
Skip to content

Commit e011034

Browse files
Merge pull request amitshekhariitbhu#102 from amitshekhariitbhu/development
Add support for debugging inMemory Room Database
2 parents 4b35ebc + 7595b16 commit e011034

14 files changed

Lines changed: 297 additions & 46 deletions

File tree

‎README.md‎

Lines changed: 22 additions & 2 deletions

‎app/src/main/java/com/sample/MainActivity.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,20 @@ protected void onCreate(Bundle savedInstanceState) {
125125
userDBHelper.insertUser(userList);
126126
}
127127

128+
// Room inMemory database
129+
if (userDBHelper.countInMemory() == 0) {
130+
List<User> userList = new ArrayList<>();
131+
for (int i = 0; i < 20; i++) {
132+
User user = new User();
133+
user.id = (long) (i + 1);
134+
user.name = "in_memory_user_" + i;
135+
userList.add(user);
136+
}
137+
userDBHelper.insertUserInMemory(userList);
138+
}
139+
128140
Utils.setCustomDatabaseFiles(getApplicationContext());
141+
Utils.setInMemoryRoomDatabases(userDBHelper.getInMemoryDatabase());
129142
}
130143

131144
public void showDebugDbAddress(View view) {

‎app/src/main/java/com/sample/database/room/UserDBHelper.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sample.database.room;
22

3+
import android.arch.persistence.db.SupportSQLiteDatabase;
34
import android.arch.persistence.room.Room;
45
import android.content.Context;
56

@@ -12,9 +13,13 @@
1213
public class UserDBHelper {
1314

1415
private final AppDatabase appDatabase;
16+
private final AppDatabase inMemoryAppDatabase;
1517

1618
public UserDBHelper(Context context) {
17-
appDatabase = Room.databaseBuilder(context, AppDatabase.class, "User-Database")
19+
appDatabase = Room.databaseBuilder(context, AppDatabase.class, "User.db")
20+
.allowMainThreadQueries()
21+
.build();
22+
inMemoryAppDatabase = Room.inMemoryDatabaseBuilder(context, AppDatabase.class)
1823
.allowMainThreadQueries()
1924
.build();
2025
}
@@ -23,8 +28,19 @@ public void insertUser(List<User> userList) {
2328
appDatabase.userDao().insertAll(userList);
2429
}
2530

31+
public void insertUserInMemory(List<User> userList) {
32+
inMemoryAppDatabase.userDao().insertAll(userList);
33+
}
34+
2635
public int count() {
2736
return appDatabase.userDao().loadAll().size();
2837
}
2938

39+
public int countInMemory() {
40+
return inMemoryAppDatabase.userDao().loadAll().size();
41+
}
42+
43+
public SupportSQLiteDatabase getInMemoryDatabase() {
44+
return inMemoryAppDatabase.getOpenHelper().getWritableDatabase();
45+
}
3046
}

‎app/src/main/java/com/sample/utils/Utils.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package com.sample.utils;
2121

22+
import android.arch.persistence.db.SupportSQLiteDatabase;
2223
import android.content.Context;
2324
import android.util.Pair;
2425
import android.widget.Toast;
@@ -71,4 +72,20 @@ public static void setCustomDatabaseFiles(Context context) {
7172
}
7273
}
7374

75+
public static void setInMemoryRoomDatabases(SupportSQLiteDatabase... database) {
76+
if (BuildConfig.DEBUG) {
77+
try {
78+
Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
79+
Class[] argTypes = new Class[]{HashMap.class};
80+
HashMap<String, SupportSQLiteDatabase> inMemoryDatabases = new HashMap<>();
81+
// set your inMemory databases
82+
inMemoryDatabases.put("InMemoryOne.db", database[0]);
83+
Method setRoomInMemoryDatabase = debugDB.getMethod("setInMemoryRoomDatabases", argTypes);
84+
setRoomInMemoryDatabase.invoke(null, inMemoryDatabases);
85+
} catch (Exception ignore) {
86+
87+
}
88+
}
89+
}
90+
7491
}

‎debug-db/debug-db-upload.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def siteUrl = 'https://github.com/amitshekhariitbhu/Android-Debug-Database'
2424
def gitUrl = 'https://github.com/amitshekhariitbhu/Android-Debug-Database.git'
2525

2626
group = "com.amitshekhar.android"
27-
version = '1.0.2'
27+
version = '1.0.3'
2828

2929
install {
3030
repositories.mavenInstaller {

‎debug-db/src/main/assets/app.js‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,13 @@ function getDBList() {
7070
for(var count = 0; count < dbList.length; count++){
7171
var dbName = dbList[count][0];
7272
var isEncrypted = dbList[count][1];
73+
var isDownloadable = dbList[count][2];
7374
var dbAttribute = isEncrypted == "true" ? ' <span class="glyphicon glyphicon-lock" aria-hidden="true" style="color:blue"></span>' : "";
7475
if(dbName.indexOf("journal") == -1){
75-
$("#db-list").append("<a href='#' id=" + dbName + " class='list-group-item' onClick='openDatabaseAndGetTableList(\""+ dbName + "\");'>" + dbName + dbAttribute + "</a>");
76+
$("#db-list").append("<a href='#' id=" + dbName + " class='list-group-item' onClick='openDatabaseAndGetTableList(\""+ dbName + "\", \""+ isDownloadable + "\");'>" + dbName + dbAttribute + "</a>");
7677
if(!isSelectionDone){
7778
isSelectionDone = true;
78-
$('#db-list').find('a').trigger('click');
79+
$('#db-list').find('a').trigger('click');
7980
}
8081
}
8182
}
@@ -85,7 +86,7 @@ function getDBList() {
8586
}
8687

8788
var lastTableName = getHashValue('table');
88-
function openDatabaseAndGetTableList(db) {
89+
function openDatabaseAndGetTableList(db, isDownloadable) {
8990

9091
if("APP_SHARED_PREFERENCES" == db) {
9192
$('#run-query').removeClass('active');
@@ -97,10 +98,16 @@ function openDatabaseAndGetTableList(db) {
9798
} else {
9899
$('#run-query').removeClass('disabled');
99100
$('#run-query').addClass('active');
100-
$('#selected-db-info').removeClass('disabled');
101-
$('#selected-db-info').addClass('active');
101+
if("true" == isDownloadable) {
102+
$('#selected-db-info').removeClass('disabled');
103+
$('#selected-db-info').addClass('active');
104+
$("#selected-db-info").text("Export Selected Database : "+db);
105+
} else {
106+
$('#selected-db-info').removeClass('active');
107+
$('#selected-db-info').addClass('disabled');
108+
$("#selected-db-info").text("Selected Database : "+db);
109+
}
102110
isDatabaseSelected = true;
103-
$("#selected-db-info").text("Export Selected Database : "+db);
104111
}
105112

106113

@@ -110,7 +117,11 @@ function openDatabaseAndGetTableList(db) {
110117
var tableList = result.rows;
111118
var dbVersion = result.dbVersion;
112119
if("APP_SHARED_PREFERENCES" != db) {
113-
$("#selected-db-info").text("Export Selected Database : "+db +" Version : "+dbVersion);
120+
if("true" == isDownloadable) {
121+
$("#selected-db-info").text("Export Selected Database : "+db +" Version : "+dbVersion);
122+
} else {
123+
$("#selected-db-info").text("Selected Database : "+db +" Version : "+dbVersion);
124+
}
114125
}
115126
$('#table-list').empty()
116127
for(var count = 0; count < tableList.length; count++){

‎debug-db/src/main/java/com/amitshekhar/DebugDB.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package com.amitshekhar;
2121

22+
import android.arch.persistence.db.SupportSQLiteDatabase;
2223
import android.content.Context;
2324
import android.util.Log;
2425
import android.util.Pair;
@@ -79,6 +80,12 @@ public static void setCustomDatabaseFiles(HashMap<String, Pair<File, String>> cu
7980
}
8081
}
8182

83+
public static void setInMemoryRoomDatabases(HashMap<String, SupportSQLiteDatabase> databases) {
84+
if (clientServer != null) {
85+
clientServer.setInMemoryRoomDatabases(databases);
86+
}
87+
}
88+
8289
public static boolean isServerRunning() {
8390
return clientServer != null && clientServer.isRunning();
8491
}

‎debug-db/src/main/java/com/amitshekhar/server/ClientServer.java‎

Lines changed: 6 additions & 4 deletions

0 commit comments

Comments
 (0)