Search in sources :

Example 1 with TableDataResponse

use of com.amitshekhar.model.TableDataResponse in project Android-Debug-Database by amitshekhariitbhu.

the class DatabaseHelper method exec.

public static TableDataResponse exec(SQLiteDatabase database, String sql) {
    TableDataResponse tableDataResponse = new TableDataResponse();
    tableDataResponse.isSelectQuery = false;
    try {
        database.execSQL(sql);
    } catch (Exception e) {
        e.printStackTrace();
        tableDataResponse.isSuccessful = false;
        tableDataResponse.errorMessage = e.getMessage();
        return tableDataResponse;
    }
    tableDataResponse.isSuccessful = true;
    return tableDataResponse;
}
Also used : TableDataResponse(com.amitshekhar.model.TableDataResponse)

Example 2 with TableDataResponse

use of com.amitshekhar.model.TableDataResponse in project Android-Debug-Database by amitshekhariitbhu.

the class DatabaseHelper method getTableData.

public static TableDataResponse getTableData(SQLiteDatabase db, String selectQuery, String tableName) {
    TableDataResponse tableData = new TableDataResponse();
    tableData.isSelectQuery = true;
    if (tableName == null) {
        tableName = getTableName(selectQuery);
    }
    if (tableName != null) {
        final String pragmaQuery = "PRAGMA table_info(" + tableName + ")";
        tableData.tableInfos = getTableInfo(db, pragmaQuery);
    }
    tableData.isEditable = tableName != null && tableData.tableInfos != null;
    Cursor cursor;
    try {
        cursor = db.rawQuery(selectQuery, null);
    } catch (Exception e) {
        e.printStackTrace();
        tableData.isSuccessful = false;
        tableData.errorMessage = e.getMessage();
        return tableData;
    }
    if (cursor != null) {
        cursor.moveToFirst();
        // it non-editable also by making isPrimary true for all
        if (tableData.tableInfos == null) {
            tableData.tableInfos = new ArrayList<>();
            for (int i = 0; i < cursor.getColumnCount(); i++) {
                TableDataResponse.TableInfo tableInfo = new TableDataResponse.TableInfo();
                tableInfo.title = cursor.getColumnName(i);
                tableInfo.isPrimary = true;
                tableData.tableInfos.add(tableInfo);
            }
        }
        tableData.isSuccessful = true;
        tableData.rows = new ArrayList<>();
        if (cursor.getCount() > 0) {
            do {
                List<TableDataResponse.ColumnData> row = new ArrayList<>();
                for (int i = 0; i < cursor.getColumnCount(); i++) {
                    TableDataResponse.ColumnData columnData = new TableDataResponse.ColumnData();
                    switch(cursor.getType(i)) {
                        case Cursor.FIELD_TYPE_BLOB:
                            columnData.dataType = DataType.TEXT;
                            columnData.value = ConverterUtils.blobToString(cursor.getBlob(i));
                            break;
                        case Cursor.FIELD_TYPE_FLOAT:
                            columnData.dataType = DataType.REAL;
                            columnData.value = cursor.getDouble(i);
                            break;
                        case Cursor.FIELD_TYPE_INTEGER:
                            columnData.dataType = DataType.INTEGER;
                            columnData.value = cursor.getLong(i);
                            break;
                        case Cursor.FIELD_TYPE_STRING:
                            columnData.dataType = DataType.TEXT;
                            columnData.value = cursor.getString(i);
                            break;
                        default:
                            columnData.dataType = DataType.TEXT;
                            columnData.value = cursor.getString(i);
                    }
                    row.add(columnData);
                }
                tableData.rows.add(row);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return tableData;
    } else {
        tableData.isSuccessful = false;
        tableData.errorMessage = "Cursor is null";
        return tableData;
    }
}
Also used : TableDataResponse(com.amitshekhar.model.TableDataResponse) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Example 3 with TableDataResponse

use of com.amitshekhar.model.TableDataResponse in project Android-Debug-Database by amitshekhariitbhu.

the class DatabaseHelper method getTableData.

public static TableDataResponse getTableData(SQLiteDB db, String selectQuery, String tableName) {
    TableDataResponse tableData = new TableDataResponse();
    tableData.isSelectQuery = true;
    if (tableName == null) {
        tableName = getTableName(selectQuery);
    }
    final String quotedTableName = getQuotedTableName(tableName);
    if (tableName != null) {
        final String pragmaQuery = "PRAGMA table_info(" + quotedTableName + ")";
        tableData.tableInfos = getTableInfo(db, pragmaQuery);
    }
    Cursor cursor = null;
    boolean isView = false;
    try {
        cursor = db.rawQuery("SELECT type FROM sqlite_master WHERE name=?", new String[] { quotedTableName });
        if (cursor.moveToFirst()) {
            isView = "view".equalsIgnoreCase(cursor.getString(0));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    tableData.isEditable = tableName != null && tableData.tableInfos != null && !isView;
    if (!TextUtils.isEmpty(tableName)) {
        selectQuery = selectQuery.replace(tableName, quotedTableName);
    }
    try {
        cursor = db.rawQuery(selectQuery, null);
    } catch (Exception e) {
        e.printStackTrace();
        tableData.isSuccessful = false;
        tableData.errorMessage = e.getMessage();
        return tableData;
    }
    if (cursor != null) {
        cursor.moveToFirst();
        // it non-editable also by making isPrimary true for all
        if (tableData.tableInfos == null) {
            tableData.tableInfos = new ArrayList<>();
            for (int i = 0; i < cursor.getColumnCount(); i++) {
                TableDataResponse.TableInfo tableInfo = new TableDataResponse.TableInfo();
                tableInfo.title = cursor.getColumnName(i);
                tableInfo.isPrimary = true;
                tableData.tableInfos.add(tableInfo);
            }
        }
        tableData.isSuccessful = true;
        tableData.rows = new ArrayList<>();
        String[] columnNames = cursor.getColumnNames();
        List<TableDataResponse.TableInfo> tableInfoListModified = new ArrayList<>();
        for (String columnName : columnNames) {
            for (TableDataResponse.TableInfo tableInfo : tableData.tableInfos) {
                if (columnName.equals(tableInfo.title)) {
                    tableInfoListModified.add(tableInfo);
                    break;
                }
            }
        }
        if (tableData.tableInfos.size() != tableInfoListModified.size()) {
            tableData.tableInfos = tableInfoListModified;
            tableData.isEditable = false;
        }
        if (cursor.getCount() > 0) {
            do {
                List<TableDataResponse.ColumnData> row = new ArrayList<>();
                for (int i = 0; i < cursor.getColumnCount(); i++) {
                    TableDataResponse.ColumnData columnData = new TableDataResponse.ColumnData();
                    switch(cursor.getType(i)) {
                        case Cursor.FIELD_TYPE_BLOB:
                            columnData.dataType = DataType.TEXT;
                            columnData.value = ConverterUtils.blobToString(cursor.getBlob(i));
                            break;
                        case Cursor.FIELD_TYPE_FLOAT:
                            columnData.dataType = DataType.REAL;
                            columnData.value = cursor.getDouble(i);
                            break;
                        case Cursor.FIELD_TYPE_INTEGER:
                            columnData.dataType = DataType.INTEGER;
                            columnData.value = cursor.getLong(i);
                            break;
                        case Cursor.FIELD_TYPE_STRING:
                            columnData.dataType = DataType.TEXT;
                            columnData.value = cursor.getString(i);
                            break;
                        default:
                            columnData.dataType = DataType.TEXT;
                            columnData.value = cursor.getString(i);
                    }
                    row.add(columnData);
                }
                tableData.rows.add(row);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return tableData;
    } else {
        tableData.isSuccessful = false;
        tableData.errorMessage = "Cursor is null";
        return tableData;
    }
}
Also used : ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) TableDataResponse(com.amitshekhar.model.TableDataResponse)

Example 4 with TableDataResponse

use of com.amitshekhar.model.TableDataResponse in project Android-Debug-Database by amitshekhariitbhu.

the class RequestHandler method executeQueryAndGetResponse.

private String executeQueryAndGetResponse(String route) {
    String query = null;
    String data = null;
    String first;
    try {
        if (route.contains("?query=")) {
            query = route.substring(route.indexOf("=") + 1, route.length());
        }
        try {
            query = URLDecoder.decode(query, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (query != null) {
            String[] statements = query.split(";");
            for (int i = 0; i < statements.length; i++) {
                String aQuery = statements[i].trim();
                first = aQuery.split(" ")[0].toLowerCase();
                if (first.equals("select") || first.equals("pragma")) {
                    TableDataResponse response = DatabaseHelper.getTableData(sqLiteDB, aQuery, null);
                    data = mGson.toJson(response);
                    if (!response.isSuccessful) {
                        break;
                    }
                } else {
                    TableDataResponse response = DatabaseHelper.exec(sqLiteDB, aQuery);
                    data = mGson.toJson(response);
                    if (!response.isSuccessful) {
                        break;
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (data == null) {
        Response response = new Response();
        response.isSuccessful = false;
        data = mGson.toJson(response);
    }
    return data;
}
Also used : Response(com.amitshekhar.model.Response) UpdateRowResponse(com.amitshekhar.model.UpdateRowResponse) TableDataResponse(com.amitshekhar.model.TableDataResponse) TableDataResponse(com.amitshekhar.model.TableDataResponse) IOException(java.io.IOException)

Example 5 with TableDataResponse

use of com.amitshekhar.model.TableDataResponse in project Android-Debug-Database by amitshekhariitbhu.

the class DatabaseHelper method exec.

public static TableDataResponse exec(SQLiteDB database, String sql) {
    TableDataResponse tableDataResponse = new TableDataResponse();
    tableDataResponse.isSelectQuery = false;
    try {
        String tableName = getTableName(sql);
        if (!TextUtils.isEmpty(tableName)) {
            String quotedTableName = getQuotedTableName(tableName);
            sql = sql.replace(tableName, quotedTableName);
        }
        database.execSQL(sql);
    } catch (Exception e) {
        e.printStackTrace();
        tableDataResponse.isSuccessful = false;
        tableDataResponse.errorMessage = e.getMessage();
        return tableDataResponse;
    }
    tableDataResponse.isSuccessful = true;
    return tableDataResponse;
}
Also used : TableDataResponse(com.amitshekhar.model.TableDataResponse)

Aggregations

TableDataResponse (com.amitshekhar.model.TableDataResponse)7 ArrayList (java.util.ArrayList)3 Cursor (android.database.Cursor)2 SharedPreferences (android.content.SharedPreferences)1 Response (com.amitshekhar.model.Response)1 UpdateRowResponse (com.amitshekhar.model.UpdateRowResponse)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1