Skip to content

Commit e011034

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

File tree

14 files changed

+297
-46
lines changed

14 files changed

+297
-46
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
### Using Android Debug Database Library in your application
3737
Add this to your app's build.gradle
3838
```groovy
39-
debugCompile 'com.amitshekhar.android:debug-db:1.0.2'
39+
debugCompile 'com.amitshekhar.android:debug-db:1.0.3'
4040
```
4141

4242
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) {
9595
```
9696

9797
### Adding custom database files
98-
As this library is auto-initialize, if you want to add custom database files, add the following method and call
98+
As this library is auto-initialize, if you want to debug custom database files, add the following method and call
9999
```java
100100
public static void setCustomDatabaseFiles(Context context) {
101101
if (BuildConfig.DEBUG) {
@@ -116,6 +116,26 @@ public static void setCustomDatabaseFiles(Context context) {
116116
}
117117
```
118118

119+
### Adding InMemory Room databases
120+
As this library is auto-initialize, if you want to debug inMemory Room databases, add the following method and call
121+
```java
122+
public static void setInMemoryRoomDatabases(SupportSQLiteDatabase... database) {
123+
if (BuildConfig.DEBUG) {
124+
try {
125+
Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
126+
Class[] argTypes = new Class[]{HashMap.class};
127+
HashMap<String, SupportSQLiteDatabase> inMemoryDatabases = new HashMap<>();
128+
// set your inMemory databases
129+
inMemoryDatabases.put("InMemoryOne.db", database[0]);
130+
Method setRoomInMemoryDatabase = debugDB.getMethod("setInMemoryRoomDatabases", argTypes);
131+
setRoomInMemoryDatabase.invoke(null, inMemoryDatabases);
132+
} catch (Exception ignore) {
133+
134+
}
135+
}
136+
}
137+
```
138+
119139
### Find this project useful ? :heart:
120140
* Support it by clicking the :star: button on the upper right of this page. :v:
121141

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
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626

27+
import android.arch.persistence.db.SupportSQLiteDatabase;
2728
import android.content.Context;
2829
import android.util.Log;
2930
import android.util.Pair;
@@ -40,13 +41,10 @@ public class ClientServer implements Runnable {
4041
private static final String TAG = "ClientServer";
4142

4243
private final int mPort;
43-
44+
private final RequestHandler mRequestHandler;
4445
private boolean mIsRunning;
45-
4646
private ServerSocket mServerSocket;
4747

48-
private final RequestHandler mRequestHandler;
49-
5048
public ClientServer(Context context, int port) {
5149
mRequestHandler = new RequestHandler(context);
5250
mPort = port;
@@ -91,6 +89,10 @@ public void setCustomDatabaseFiles(HashMap<String, Pair<File, String>> customDat
9189
mRequestHandler.setCustomDatabaseFiles(customDatabaseFiles);
9290
}
9391

92+
public void setInMemoryRoomDatabases(HashMap<String, SupportSQLiteDatabase> databases) {
93+
mRequestHandler.setInMemoryRoomDatabases(databases);
94+
}
95+
9496
public boolean isRunning() {
9597
return mIsRunning;
9698
}

0 commit comments

Comments
 (0)