a
554325746@qq.com
2019-12-25 38492bbaa63586e2f4877da0eaa01a082fd565a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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;
    }
 
 
}