Skip to content

Commit 25467db

Browse files
Merge pull request #64 from amitshekhariitbhu/Development
Development
2 parents f0ebaf0 + dbb4a31 commit 25467db

17 files changed

Lines changed: 782 additions & 55 deletions

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ You will see something like this :
7575
- Android Default Emulator: run `adb forward tcp:8080 tcp:8080` and open http://localhost:8080
7676
- Genymotion Emulator: Enable bridge from configure virtual device (option available in genymotion)
7777

78-
### Getting address With toast, in case you missed the address log in logcat
78+
### Getting address with toast, in case you missed the address log in logcat
7979
As this library is auto-initialize, if you want to get the address log, add the following method and call
8080
```java
8181
public static void showDebugDBAddressLogToast(Context context) {
@@ -92,6 +92,28 @@ public static void showDebugDBAddressLogToast(Context context) {
9292
}
9393
```
9494

95+
### Adding custom database files
96+
As this library is auto-initialize, if you want to add custom database files, add the following method and call
97+
```java
98+
public static void setCustomDatabaseFiles(Context context) {
99+
if (BuildConfig.DEBUG) {
100+
try {
101+
Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
102+
Class[] argTypes = new Class[]{HashMap.class};
103+
Method setCustomDatabaseFiles = debugDB.getMethod("setCustomDatabaseFiles", argTypes);
104+
HashMap<String, File> customDatabaseFiles = new HashMap<>();
105+
// set your custom database files
106+
customDatabaseFiles.put(ExtTestDBHelper.DATABASE_NAME,
107+
new File(context.getFilesDir() + "/" + ExtTestDBHelper.DIR_NAME +
108+
"/" + ExtTestDBHelper.DATABASE_NAME));
109+
setCustomDatabaseFiles.invoke(null, customDatabaseFiles);
110+
} catch (Exception ignore) {
111+
112+
}
113+
}
114+
}
115+
```
116+
95117
### Find this project useful ? :heart:
96118
* Support it by clicking the :star: button on the upper right of this page. :v:
97119

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<!--
1+
<?xml version="1.0" encoding="utf-8"?><!--
32
~ /*
43
~ * Copyright (C) 2016 Amit Shekhar
54
~ * Copyright (C) 2011 Android Open Source Project
@@ -34,6 +33,7 @@
3433
<category android:name="android.intent.category.LAUNCHER" />
3534
</intent-filter>
3635
</activity>
36+
3737
</application>
3838

3939
</manifest>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import com.sample.database.CarDBHelper;
3131
import com.sample.database.ContactDBHelper;
32+
import com.sample.database.ExtTestDBHelper;
3233
import com.sample.utils.Utils;
3334

3435
import java.util.HashSet;
@@ -86,6 +87,16 @@ protected void onCreate(Bundle savedInstanceState) {
8687
carDBHelper.insertCar(name, color, mileage);
8788
}
8889
}
90+
91+
ExtTestDBHelper extTestDBHelper = new ExtTestDBHelper(getApplicationContext());
92+
if (extTestDBHelper.count() == 0) {
93+
for (int i = 0; i < 20; i++) {
94+
String value = "value_" + i;
95+
extTestDBHelper.insertTest(value);
96+
}
97+
}
98+
99+
Utils.setCustomDatabaseFiles(getApplicationContext());
89100
}
90101

91102
public void showDebugDbAddress(View view) {

app/src/main/java/com/sample/database/CarDBHelper.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public void onCreate(SQLiteDatabase db) {
5252
"create table cars " +
5353
"(id integer primary key, name text, color text, mileage real)"
5454
);
55+
56+
db.execSQL("create table [transaction] (id integer primary key, name text)");
57+
58+
for (int i = 0; i < 10; i++) {
59+
db.execSQL("insert into [transaction] (name) values ('hello');");
60+
}
5561
}
5662

5763
@Override
@@ -108,7 +114,7 @@ public ArrayList<String> getAllCars() {
108114
Cursor res = db.rawQuery("select * from cars", null);
109115
res.moveToFirst();
110116

111-
while (res.isAfterLast() == false) {
117+
while (!res.isAfterLast()) {
112118
arrayList.add(res.getString(res.getColumnIndex(CARS_COLUMN_NAME)));
113119
res.moveToNext();
114120
}

app/src/main/java/com/sample/database/ContactDBHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public ArrayList<String> getAllCotacts() {
116116
Cursor res = db.rawQuery("select * from contacts", null);
117117
res.moveToFirst();
118118

119-
while (res.isAfterLast() == false) {
119+
while (!res.isAfterLast()) {
120120
arrayList.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
121121
res.moveToNext();
122122
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.sample.database;
2+
3+
import android.content.ContentValues;
4+
import android.content.Context;
5+
import android.content.ContextWrapper;
6+
import android.database.Cursor;
7+
import android.database.DatabaseErrorHandler;
8+
import android.database.sqlite.SQLiteDatabase;
9+
import android.database.sqlite.SQLiteOpenHelper;
10+
11+
import java.io.File;
12+
import java.util.Calendar;
13+
14+
public class ExtTestDBHelper extends SQLiteOpenHelper {
15+
16+
public static final String DIR_NAME = "custom_dir";
17+
public static final String DATABASE_NAME = "ExtTest.db";
18+
public static final String TEST_TABLE_NAME = "test";
19+
public static final String TEST_ID = "id";
20+
public static final String TEST_COLUMN_VALUE = "value";
21+
public static final String TEST_CREATED_AT = "createdAt";
22+
23+
public ExtTestDBHelper(Context context) {
24+
super(new CustomDatabasePathContext(context), DATABASE_NAME, null, 1);
25+
}
26+
27+
@Override
28+
public void onCreate(SQLiteDatabase db) {
29+
// TODO Auto-generated method stub
30+
db.execSQL(
31+
String.format(
32+
"create table %s (%s integer primary key, %s text, %s integer)",
33+
TEST_TABLE_NAME,
34+
TEST_ID,
35+
TEST_COLUMN_VALUE,
36+
TEST_CREATED_AT
37+
)
38+
);
39+
}
40+
41+
@Override
42+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
43+
// TODO Auto-generated method stub
44+
db.execSQL("DROP TABLE IF EXISTS " + TEST_TABLE_NAME);
45+
onCreate(db);
46+
}
47+
48+
public boolean insertTest(String value) {
49+
SQLiteDatabase db = this.getWritableDatabase();
50+
ContentValues contentValues = new ContentValues();
51+
contentValues.put("value", value);
52+
contentValues.put(TEST_CREATED_AT, Calendar.getInstance().getTimeInMillis());
53+
db.insert(TEST_TABLE_NAME, null, contentValues);
54+
return true;
55+
}
56+
57+
public int count() {
58+
SQLiteDatabase db = getReadableDatabase();
59+
Cursor cursor = db.rawQuery("select COUNT(*) from " + TEST_TABLE_NAME, null);
60+
if (cursor != null && cursor.getCount() > 0) {
61+
cursor.moveToFirst();
62+
return cursor.getInt(0);
63+
} else {
64+
return 0;
65+
}
66+
}
67+
68+
private static class CustomDatabasePathContext extends ContextWrapper {
69+
70+
public CustomDatabasePathContext(Context base) {
71+
super(base);
72+
}
73+
74+
@Override
75+
public File getDatabasePath(String name) {
76+
File databaseDir = new File(String.format("%s/%s", getFilesDir(), ExtTestDBHelper.DIR_NAME));
77+
databaseDir.mkdirs();
78+
File databaseFile = new File(String.format("%s/%s/%s", getFilesDir(), ExtTestDBHelper.DIR_NAME, name));
79+
return databaseFile;
80+
}
81+
82+
@Override
83+
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory) {
84+
return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), null);
85+
}
86+
87+
@Override
88+
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) {
89+
return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), null);
90+
}
91+
}
92+
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
import android.widget.Toast;
2424

2525
import com.sample.BuildConfig;
26+
import com.sample.database.ExtTestDBHelper;
2627

28+
import java.io.File;
2729
import java.lang.reflect.Method;
30+
import java.util.HashMap;
2831

2932
/**
3033
* Created by amitshekhar on 07/02/17.
@@ -33,7 +36,7 @@
3336
public class Utils {
3437

3538
private Utils() {
36-
39+
// This class is not publicly instantiable
3740
}
3841

3942
public static void showDebugDBAddressLogToast(Context context) {
@@ -48,4 +51,23 @@ public static void showDebugDBAddressLogToast(Context context) {
4851
}
4952
}
5053
}
54+
55+
public static void setCustomDatabaseFiles(Context context) {
56+
if (BuildConfig.DEBUG) {
57+
try {
58+
Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
59+
Class[] argTypes = new Class[]{HashMap.class};
60+
Method setCustomDatabaseFiles = debugDB.getMethod("setCustomDatabaseFiles", argTypes);
61+
HashMap<String, File> customDatabaseFiles = new HashMap<>();
62+
// set your custom database files
63+
customDatabaseFiles.put(ExtTestDBHelper.DATABASE_NAME,
64+
new File(context.getFilesDir() + "/" + ExtTestDBHelper.DIR_NAME +
65+
"/" + ExtTestDBHelper.DATABASE_NAME));
66+
setCustomDatabaseFiles.invoke(null, customDatabaseFiles);
67+
} catch (Exception ignore) {
68+
69+
}
70+
}
71+
}
72+
5173
}

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

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,28 @@ function inflateData(result){
149149
$("#db-data-div").remove();
150150
$("#parent-data-div").append('<div id="db-data-div"><table class="display nowrap" cellpadding="0" border="0" cellspacing="0" width="100%" class="table table-striped table-bordered display" id="db-data"></table></div>');
151151

152+
var availableButtons;
153+
if (result.isEditable) {
154+
availableButtons = [
155+
{
156+
text : 'Add',
157+
name : 'add' // don not change name
158+
},
159+
{
160+
extend: 'selected', // Bind to Selected row
161+
text: 'Edit',
162+
name: 'edit' // do not change name
163+
},
164+
{
165+
extend: 'selected',
166+
text: 'Delete',
167+
name: 'delete'
168+
}
169+
];
170+
} else {
171+
availableButtons = [];
172+
}
173+
152174
$(tableId).dataTable({
153175
"data": columnData,
154176
"columnDefs": columnHeader,
@@ -162,18 +184,7 @@ function inflateData(result){
162184
"dom": "Bfrtip",
163185
select: 'single',
164186
altEditor: true, // Enable altEditor
165-
buttons: [
166-
{
167-
extend: 'selected', // Bind to Selected row
168-
text: 'Edit',
169-
name: 'edit' // do not change name
170-
},
171-
{
172-
extend: 'selected',
173-
text: 'Delete',
174-
name: 'delete'
175-
}
176-
]
187+
buttons: availableButtons
177188
})
178189

179190
//attach row-updated listener
@@ -208,6 +219,24 @@ function inflateData(result){
208219
deleteTableData(data, callback);
209220
});
210221

222+
223+
224+
$(tableId).on('add-row.dt', function (e, updatedRowData, callback) {
225+
var deleteRowDataArray = JSON.parse(updatedRowData);
226+
227+
console.log(deleteRowDataArray);
228+
229+
//add value for each column
230+
var data = columnHeader;
231+
for(var i = 0; i < data.length; i++) {
232+
data[i].value = deleteRowDataArray[i].value;
233+
data[i].dataType = deleteRowDataArray[i].dataType;
234+
}
235+
236+
//send delete table data request to server
237+
addTableData(data, callback);
238+
});
239+
211240
// hack to fix alignment issue when scrollX is enabled
212241
$(".dataTables_scrollHeadInner").css({"width":"100%"});
213242
$(".table ").css({"width":"100%"});
@@ -272,8 +301,6 @@ function deleteTableData(deleteData, callback) {
272301
}
273302
});
274303

275-
console.log(filteredUpdatedData);
276-
277304
//build request parameters
278305
var requestParameters = {};
279306
requestParameters.dbName = selectedTableElement.attr('data-db-name');
@@ -299,6 +326,48 @@ function deleteTableData(deleteData, callback) {
299326
})
300327
}
301328

329+
function addTableData(deleteData, callback) {
330+
331+
var selectedTableElement = $("#table-list .list-group-item.selected");
332+
var filteredUpdatedData = deleteData.map(function(columnData){
333+
return {
334+
title: columnData.title,
335+
isPrimary: columnData.isPrimary,
336+
value: columnData.value,
337+
dataType: columnData.dataType
338+
}
339+
});
340+
341+
console.log(filteredUpdatedData);
342+
343+
//build request parameters
344+
var requestParameters = {};
345+
requestParameters.dbName = selectedTableElement.attr('data-db-name');
346+
requestParameters.tableName = selectedTableElement.attr('data-table-name');;
347+
requestParameters.addData = encodeURIComponent(JSON.stringify(filteredUpdatedData));
348+
349+
console.log(requestParameters);
350+
351+
//execute request
352+
$.ajax({
353+
url: "addTableData",
354+
type: 'GET',
355+
data: requestParameters,
356+
success: function(response) {
357+
response = JSON.parse(response);
358+
if(response.isSuccessful){
359+
console.log("Data Added successfully");
360+
callback(true);
361+
getData(requestParameters.tableName);
362+
showSuccessInfo("Data Added Successfully");
363+
} else {
364+
console.log("Data Adding failed");
365+
callback(false);
366+
}
367+
}
368+
});
369+
}
370+
302371
function showSuccessInfo(message){
303372
var snackbarId = "snackbar";
304373
var snackbarElement = $("#"+snackbarId);

0 commit comments

Comments
 (0)