Skip to content

Commit e304216

Browse files
author
Vinayaga Sundar
committed
Added option to export database
1 parent a8a1d7c commit e304216

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ $( document ).ready(function() {
77
});
88
});
99

10+
var isDatabaseSelected = true;
11+
1012
function getData(tableName) {
1113

1214
$.ajax({url: "getAllDataFromTheTable?tableName="+tableName, success: function(result){
@@ -31,6 +33,13 @@ function queryFunction() {
3133

3234
}
3335

36+
function downloadDb() {
37+
if (isDatabaseSelected) {
38+
var url = window.location.href;
39+
window.open(url+"downloadDb", "_blank")
40+
}
41+
}
42+
3443

3544
function getDBList() {
3645

@@ -59,9 +68,15 @@ function openDatabaseAndGetTableList(db) {
5968
if("APP_SHARED_PREFERENCES" == db) {
6069
$('#run-query').removeClass('active');
6170
$('#run-query').addClass('disabled');
71+
$('#export-db').removeClass('active');
72+
$('#export-db').addClass('disabled');
73+
isDatabaseSelected = false;
6274
} else {
6375
$('#run-query').removeClass('disabled');
6476
$('#run-query').addClass('active');
77+
$('#export-db').removeClass('disabled');
78+
$('#export-db').addClass('active');
79+
isDatabaseSelected = true;
6580
}
6681

6782
$("#selected-db-info").text("Selected Database : "+db);

debug-db/src/main/assets/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@
109109
<button id="run-query" type="submit" onclick="queryFunction()" class="btn btn-primary pull-right disabled">Run
110110
Query
111111
</button>
112+
<button id="export-db" type="button" onclick="downloadDb()" class="btn btn-primary">
113+
Export DB
114+
</button>
112115
</div>
113116
</div>
114117

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@
4040
import java.io.BufferedReader;
4141
import java.io.ByteArrayOutputStream;
4242
import java.io.File;
43+
import java.io.FileInputStream;
4344
import java.io.FileNotFoundException;
4445
import java.io.IOException;
4546
import java.io.InputStream;
4647
import java.io.InputStreamReader;
48+
import java.io.OutputStream;
4749
import java.io.PrintStream;
4850
import java.net.ServerSocket;
4951
import java.net.Socket;
@@ -84,6 +86,11 @@ public class ClientServer implements Runnable {
8486
private Gson mGson;
8587
private boolean isDbOpenned;
8688

89+
/**
90+
* Hold the selected database name
91+
*/
92+
private String mSelectedDatabase = null;
93+
8794
/**
8895
* WebServer constructor.
8996
*/
@@ -226,13 +233,17 @@ private void handle(Socket socket) throws IOException {
226233
if (Constants.APP_SHARED_PREFERENCES.equals(database)) {
227234
response = getAllPrefTableName();
228235
closeDatabase();
236+
mSelectedDatabase = null;
229237
} else {
230238
openDatabase(database);
231239
response = getAllTableName();
240+
mSelectedDatabase = database;
232241
}
233242

234243
String data = mGson.toJson(response);
235244
bytes = data.getBytes();
245+
} else if (route.startsWith("downloadDb")) {
246+
bytes = getDatabaseFile();
236247
} else {
237248
bytes = loadContent(route);
238249
}
@@ -246,7 +257,12 @@ private void handle(Socket socket) throws IOException {
246257
// Send out the content.
247258
output.println("HTTP/1.0 200 OK");
248259
output.println("Content-Type: " + detectMimeType(route));
249-
output.println("Content-Length: " + bytes.length);
260+
261+
if (route.startsWith("downloadDb")) {
262+
output.println("Content-Disposition: attachment; filename=" + mSelectedDatabase);
263+
} else {
264+
output.println("Content-Length: " + bytes.length);
265+
}
250266
output.println();
251267
output.write(bytes);
252268
output.flush();
@@ -318,6 +334,32 @@ private String detectMimeType(String fileName) {
318334
}
319335
}
320336

337+
private byte[] getDatabaseFile() {
338+
if (TextUtils.isEmpty(mSelectedDatabase)) {
339+
return null;
340+
}
341+
342+
File file = new File(mDatabaseDir, mSelectedDatabase);
343+
344+
byte[] byteArray = null;
345+
try {
346+
InputStream inputStream = new FileInputStream(file);
347+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
348+
byte[] b = new byte[(int) file.length()];
349+
int bytesRead = 0;
350+
351+
while ((bytesRead = inputStream.read(b)) != -1) {
352+
bos.write(b, 0, bytesRead);
353+
}
354+
355+
byteArray = bos.toByteArray();
356+
} catch (IOException e) {
357+
Log.e(TAG, "getDatabaseFile: ", e);
358+
}
359+
360+
return byteArray;
361+
}
362+
321363
private void getDatabaseDir() {
322364
File root = mContext.getFilesDir().getParentFile();
323365
File dbRoot = new File(root, "/databases");

0 commit comments

Comments
 (0)