Skip to content

Commit dae4257

Browse files
Merge pull request #51 from landicefu/Development
support listing database views
2 parents 8dce2d5 + 7f3ce8a commit dae4257

3 files changed

Lines changed: 39 additions & 19 deletions

File tree

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: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private DatabaseHelper() {
4343

4444
public static Response getAllTableName(SQLiteDatabase database) {
4545
Response response = new Response();
46-
Cursor c = database.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
46+
Cursor c = database.rawQuery("SELECT name FROM sqlite_master WHERE type='table' OR type='view'", null);
4747
if (c.moveToFirst()) {
4848
while (!c.isAfterLast()) {
4949
response.rows.add(c.getString(0));
@@ -72,10 +72,22 @@ public static TableDataResponse getTableData(SQLiteDatabase db, String selectQue
7272
final String pragmaQuery = "PRAGMA table_info(" + tableName + ")";
7373
tableData.tableInfos = getTableInfo(db, pragmaQuery);
7474
}
75+
Cursor cursor = null;
76+
boolean isView = false;
77+
try {
78+
cursor = db.rawQuery("SELECT type FROM sqlite_master WHERE name=?", new String[]{tableName});
79+
if (cursor.moveToFirst()) {
80+
isView = "view".equalsIgnoreCase(cursor.getString(0));
81+
}
82+
} catch (Exception e) {
83+
e.printStackTrace();
84+
} finally {
85+
if (cursor != null) {
86+
cursor.close();
87+
}
88+
}
89+
tableData.isEditable = tableName != null && tableData.tableInfos != null && !isView;
7590

76-
tableData.isEditable = tableName != null && tableData.tableInfos != null;
77-
78-
Cursor cursor;
7991
try {
8092
cursor = db.rawQuery(selectQuery, null);
8193
} catch (Exception e) {

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)