package com.basic.security.dao;
|
|
import android.database.Cursor;
|
import android.text.TextUtils;
|
|
import com.basic.security.utils.Constants;
|
import com.basic.security.utils.FrameUtil;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.UUID;
|
|
public class SqliteManager {
|
|
public static Map<String, List<String>> tableColumnNames = new HashMap<>();
|
|
public static Map<String, String> cursorToModelAdapter(Cursor cursor, String table) {//
|
Map<String, String> modelAdapter = new HashMap<>();
|
String[] columnNames = cursor.getColumnNames();
|
for (String columnName : columnNames) {
|
modelAdapter.put(columnName, cursor.getString(cursor.getColumnIndex(columnName)));
|
}
|
modelAdapter.put("table", table);
|
return modelAdapter;
|
}
|
|
private static String getTableName(String sql) {
|
try {
|
return sql.toLowerCase().substring(sql.indexOf("from") + "from".length()).trim().split(" ")[0].trim();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
|
public static List<Map<String, String>> findList(String sql) {
|
List<Map<String, String>> modelAdapterList = new ArrayList<>();
|
Cursor cursor = null;
|
try {
|
if (Constants.printSql) {
|
System.out.println("sql=" + sql);
|
}
|
cursor = DatabaseManager.getDatabase().rawQuery(sql, null);
|
if (cursor.moveToFirst()) {
|
while (!cursor.isAfterLast()) {
|
modelAdapterList.add(cursorToModelAdapter(cursor, getTableName(sql)));
|
cursor.moveToNext();
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (cursor != null) {
|
cursor.close();
|
}
|
}
|
return modelAdapterList;
|
}
|
|
public static void save(Map<String, String> modelAdapter) {
|
try {
|
if (modelAdapter != null) {
|
GetInsertOrUpdateSql getInsertOrUpdateSql = new GetInsertOrUpdateSql(modelAdapter).invoke();
|
String table = getInsertOrUpdateSql.getTable();
|
String insertSql = getInsertOrUpdateSql.getInsertSql();
|
if (Constants.printSql) {
|
System.out.println("insertsql=" + insertSql);
|
}
|
DatabaseManager.execSQL(insertSql);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public static class GetInsertOrUpdateSql {
|
private Map<String, String> modelAdapter;
|
private String table;
|
private String insertSql;
|
|
public GetInsertOrUpdateSql(Map<String, String> modelAdapter) {
|
this.modelAdapter = modelAdapter;
|
}
|
|
public String getTable() {
|
return table;
|
}
|
|
public String getInsertSql() {
|
return insertSql;
|
}
|
|
public GetInsertOrUpdateSql invoke() {
|
Map<String, String> sqliteModel = modelAdapter;
|
table = (String) sqliteModel.get("table");
|
insertSql = "INSERT OR REPLACE INTO " + table + " ";
|
List<String> paramNameList = new ArrayList<>();
|
List<String> paramValueList = new ArrayList<>();
|
String id = (String) sqliteModel.get("id");
|
if (id == null) {
|
id = UUID.randomUUID().toString();
|
sqliteModel.put("id", id);
|
}
|
List<String> columnsNameList = tableColumnNames.get(table);
|
boolean hasDelFlag = false;
|
if (columnsNameList != null) {
|
for (Map.Entry<String, String> entry : sqliteModel.entrySet()) {
|
String key = entry.getKey();
|
Object value = entry.getValue();
|
if (columnsNameList.contains(key)) {
|
if (key.contains("del_flag") && !TextUtils.isEmpty((String) value)) {
|
hasDelFlag = true;
|
}
|
paramNameList.add("`" + key + "`");
|
if (value == null) {
|
paramValueList.add("''");
|
} else {
|
if (value instanceof byte[]) {
|
} else {
|
paramValueList.add("'" + (String) value + "'");
|
}
|
}
|
}
|
}
|
if (!hasDelFlag) {
|
if (columnsNameList.contains("del_flag")) {
|
paramNameList.add("`del_flag`");
|
paramValueList.add("'0'");
|
}
|
}
|
insertSql += "(" + TextUtils.join(",", paramNameList) + ") VALUES (" +
|
TextUtils.join(",", paramValueList) + ")";
|
}
|
return this;
|
}
|
}
|
|
|
public static Map<String, String> findById(String table, String id) {
|
Cursor cursor = null;
|
try {
|
String sql = "select * from "+table+" where id = '" + id + "'";
|
if (Constants.printSql) {
|
System.out.println("findById="+sql
|
// + " " + FrameUtil.getFrames()
|
);
|
}
|
cursor = DatabaseManager.getDatabase().rawQuery(sql, null);
|
if (cursor.moveToFirst()) {
|
return cursorToModelAdapter(cursor, table);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (cursor != null)
|
cursor.close();
|
}
|
return null;
|
}
|
|
|
}
|