Skip to content

Commit df6d385

Browse files
Merge branch 'Development' of https://github.com/amitshekhariitbhu/Android-Debug-Database into Development
2 parents b89fb84 + 4ef9568 commit df6d385

5 files changed

Lines changed: 328 additions & 19 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
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

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,9 @@ 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-
$(tableId).dataTable({
153-
"data": columnData,
154-
"columnDefs": columnHeader,
155-
'bPaginate': true,
156-
'searching': true,
157-
'bFilter': true,
158-
'bInfo': true,
159-
"bSort" : true,
160-
"scrollX": true,
161-
"iDisplayLength": 10,
162-
"dom": "Bfrtip",
163-
select: 'single',
164-
altEditor: true, // Enable altEditor
165-
buttons: [
152+
var availableButtons;
153+
if (result.isEditable) {
154+
availableButtons = [
166155
{
167156
text : 'Add',
168157
name : 'add' // don not change name
@@ -177,7 +166,25 @@ function inflateData(result){
177166
text: 'Delete',
178167
name: 'delete'
179168
}
180-
]
169+
];
170+
} else {
171+
availableButtons = [];
172+
}
173+
174+
$(tableId).dataTable({
175+
"data": columnData,
176+
"columnDefs": columnHeader,
177+
'bPaginate': true,
178+
'searching': true,
179+
'bFilter': true,
180+
'bInfo': true,
181+
"bSort" : true,
182+
"scrollX": true,
183+
"iDisplayLength": 10,
184+
"dom": "Bfrtip",
185+
select: 'single',
186+
altEditor: true, // Enable altEditor
187+
buttons: availableButtons
181188
})
182189

183190
//attach row-updated listener

debug-db/src/main/java/com/amitshekhar/utils/DatabaseHelper.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222
import android.content.ContentValues;
2323
import android.database.Cursor;
2424
import android.database.sqlite.SQLiteDatabase;
25+
import android.text.TextUtils;
26+
import android.util.Log;
2527

2628
import com.amitshekhar.model.Response;
2729
import com.amitshekhar.model.RowDataRequest;
2830
import com.amitshekhar.model.TableDataResponse;
2931
import com.amitshekhar.model.UpdateRowResponse;
3032

3133
import java.util.ArrayList;
34+
import java.util.HashSet;
3235
import java.util.List;
3336

3437
/**
@@ -43,7 +46,7 @@ private DatabaseHelper() {
4346

4447
public static Response getAllTableName(SQLiteDatabase database) {
4548
Response response = new Response();
46-
Cursor c = database.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
49+
Cursor c = database.rawQuery("SELECT name FROM sqlite_master WHERE type='table' OR type='view'", null);
4750
if (c.moveToFirst()) {
4851
while (!c.isAfterLast()) {
4952
response.rows.add(c.getString(0));
@@ -72,10 +75,27 @@ public static TableDataResponse getTableData(SQLiteDatabase db, String selectQue
7275
final String pragmaQuery = "PRAGMA table_info(" + tableName + ")";
7376
tableData.tableInfos = getTableInfo(db, pragmaQuery);
7477
}
78+
Cursor cursor = null;
79+
boolean isView = false;
80+
try {
81+
cursor = db.rawQuery("SELECT type FROM sqlite_master WHERE name=?", new String[]{tableName});
82+
if (cursor.moveToFirst()) {
83+
isView = "view".equalsIgnoreCase(cursor.getString(0));
84+
}
85+
} catch (Exception e) {
86+
e.printStackTrace();
87+
} finally {
88+
if (cursor != null) {
89+
cursor.close();
90+
}
91+
}
92+
tableData.isEditable = tableName != null && tableData.tableInfos != null && !isView;
7593

76-
tableData.isEditable = tableName != null && tableData.tableInfos != null;
7794

78-
Cursor cursor;
95+
if (!TextUtils.isEmpty(tableName)) {
96+
selectQuery = selectQuery.replace(tableName, "[" + tableName + "]");
97+
}
98+
7999
try {
80100
cursor = db.rawQuery(selectQuery, null);
81101
} catch (Exception e) {
@@ -345,7 +365,16 @@ public static TableDataResponse exec(SQLiteDatabase database, String sql) {
345365
}
346366

347367
private static String getTableName(String selectQuery) {
348-
// TODO find tableName from query and also handle JOIN query
368+
// TODO: 24/4/17 Handle JOIN Query
369+
TableNameParser tableNameParser = new TableNameParser(selectQuery);
370+
HashSet<String> tableName = (HashSet<String>) tableNameParser.tables();
371+
372+
for (String table : tableName) {
373+
if (!TextUtils.isEmpty(table)) {
374+
return table;
375+
}
376+
}
377+
349378
return null;
350379
}
351380

debug-db/src/main/java/com/amitshekhar/utils/PrefHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public static Response getAllPrefTableName(Context context) {
8787
public static TableDataResponse getAllPrefData(Context context, String tag) {
8888

8989
TableDataResponse response = new TableDataResponse();
90+
response.isEditable = true;
9091
response.isSuccessful = true;
9192
response.isSelectQuery = true;
9293

0 commit comments

Comments
 (0)