派生自 development/c++

pansen
2019-01-10 ffa3485e9d36911f3b6aeac7ddbcca61c3e3dde6
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// Created by pans on 4/28/18.
//
 
#ifndef TESTSQLITE_SQLITETOOLKIT_HPP
#define TESTSQLITE_SQLITETOOLKIT_HPP
 
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
#include <basic/util/app/AppUtil.h>
 
/**
 * 字段的集合
 * @key FieldName
 * @value Value
 */
typedef std::map<std::string, std::string> FieldValues;
/***
 * 人脸特征
 */
typedef std::vector<unsigned char> FaceFeature;
 
struct AddFaceData {
    std::string uuid;
    FaceFeature feature;
    std::string faceUrl;
};
 
struct FaceInfo {
    std::string uuid;
    std::string personName;
    std::string age;
    std::string sex;
    std::string idCard;
    std::string phoneNum;
};
typedef std::vector<FaceInfo> FaceInfos;
typedef std::map<std::string, FaceInfo> FaceInfosCache;
 
struct TableInfo {
    std::string uuid;
    std::string tableName;
    std::string tableType;
    std::string bwType;
    std::string startTime;
    std::string endTime;
    std::string createBy;
};
typedef std::vector<TableInfo> TableInfos;
typedef std::map<std::string, TableInfo> TableInfosCache;
 
//typedef std::vector<FaceFeature> FaceFeatures;
 
//typedef std::map<std::string, FaceFeatures> FeatureDBCache;
 
struct FaceFeatureWithUrl {
    std::string uuid;
    std::string faceurl;
    FaceFeature faceFeature;
};
 
typedef std::map<std::string, FaceFeatureWithUrl> FeatureDBWithUrlCache;
 
namespace {
 
#define INITCOPY(_DEST, _SRC) _DEST.resize(_SRC.size());memcpy(_DEST.data(),_SRC.data(), _SRC.size());
#define COPY(NAME1, NAME2) memcpy(&NAME1.data(), &NAME2.data(), NAME2.size);
#define SQLERR(message) fprintf(stderr, "%s->%d->  SQLite error: %s\n",__FILE__,__LINE__, message);
 
 
    //#todo 数据库wenjian名称
    static std::string file_dbName = "TestFaceDB.db";
    //#todo 数据库名称
    static std::string g_dbName = "main";
    //#todo 管理表的表名
    static std::string g_tableName = "sys_o_tables";
 
    //
    std::string getFacesFromTableSql(std::string tableName) {
        std::string sql =
            "select uuid,feature,create_time,faceUrl,del_flag from " + tableName +
            "_fea where feature is not null ";
        return sql;
    }
 
    std::string getTableListSqlWithType(std::string type) {
        std::string sql = "select tableName,del_flag from " + g_tableName + " where del_flag = 0";
        if (type.size() > 0) {
            sql.append(" and tableType = '" + type + "'");
        }
        return sql;
    }
 
    std::string getTableInfosSql(std::string tableName) {
        std::string sql = "select * from " + tableName;// + " where del_flag = 0";
        return sql;
    }
 
 
    std::string getTableInfosSql() {
        //uuid,tableName,tableDesc,tableType,bwType,startTime,endTime
        std::string sql = "select uuid,tableName,tableType,bwType,startTime,endTime from " + g_tableName +
                          " where del_flag = 0";
        return sql;
    }
 
    /**
     * not used have bug
     * @param tableName
     * @param id
     * @param feature
     * @return
     */
    char *getAddFaceDataSql(std::string tableName, int id, FaceFeature &feature) {
        if (tableName.size() == 0 || feature.size() == 0) {
            //#todo errInfo
            return "tableName or FaceFeature is NULL";
        }
        std::string str_fea;
        str_fea.assign(feature.begin(), feature.end());
 
        std::stringstream sql;//= ;
        sql << "INSERT INTO " << tableName << " (";
        //std::string sqlTemp = sql;
        std::string sql2 = ") VALUES(\"";
        if (id >= 0) {
            sql << "id,";
            sql2.append(std::to_string(id) + "\",\"");
        }
        sql << "feature " << sql2 << str_fea << "\")";
 
        //sql1 delete ,
        //sql2 delete 2 ,'  add )
        return const_cast<char *>(str_fea.c_str());
    }
 
    /**
     * 删除数据
     * @param tableName 表名
     * @param fieldValues 删除的条件组合
     * @return
     */
    std::string getDeleteSql(std::string tableName, FieldValues &fieldValues) {
        if (tableName.size() == 0 || fieldValues.size() == 0) {
            //#todo errInfo
            return "tableName or fieldValues is NULL";
        }
        std::string sql = "DELETE from " + tableName + " where 1 = 1 ";
        for (auto item : fieldValues) {
            if (item.first.size() != 0) {
                sql.append("and " + item.first + " = '" + item.second + "' ");
            }
        }
        return sql;
    }
 
    /**
     * 添加数据 || 创建人脸表之后,将该记录插入管理表
     * @param tableName 表名
     * @param fieldValues 将要插入字段的集合
     * @return
     */
    std::string getInsertSql(std::string tableName, FieldValues &fieldValues) {
        if (tableName.size() == 0 || fieldValues.size() == 0) {
            //#todo errInfo
            return "tableName or fieldValues is NULL";
        }
        std::string sql = "INSERT INTO " + tableName + " (";
        std::string sqlTemp = sql;
        std::string sql2 = ") VALUES('";
        for (auto item : fieldValues) {
            if (item.first.size() != 0) {
                sql.append(item.first + ",");
                sql2.append(item.second + "','");
            }
        }
        //sql1 delete ,
        sql = sql.substr(0, sql.length() - 1);
        //sql2 delete 2 ,'  add )
        sql2 = sql2.substr(0, sql2.length() - 2);
        sql2.append(" )");
        sql.append(sql2);
        return sql;
    }
 
    /**
     * 创建管理表的sql语句
     *
     * @return sql
     */
    std::string getInitDBSql() {
        std::string sql = "CREATE TABLE \"main\".\"sys_o_tables\" (";
        sql.append(" uuid        varchar(255) PRIMARY KEY, ");
//        sql.append(" ClusterName varchar(255) DEFAULT NULL,");//本地库不需要
        sql.append(" tableName   varchar(255) UNIQUE,");
        sql.append(" tableDesc   varchar(255) DEFAULT NULL,");
        sql.append(" tableType   varchar(255) DEFAULT NULL,");
        sql.append(" bwType      varchar(255) DEFAULT NULL,");
        sql.append(" startTime   varchar(255) DEFAULT NULL,");
        sql.append(" endTime     varchar(255) DEFAULT NULL,");
        sql.append(" create_time BLOB default (datetime('now', 'localtime')),");
        sql.append(" update_time varchar(255) DEFAULT NULL,");
        sql.append(" create_by   varchar(255) DEFAULT NULL,");
        sql.append(" del_flag    varchar(255) DEFAULT 0");
        sql.append(");");
        return sql;
    }
 
 
    /**
     * 创建人脸表的sql语句
     * @param tableName 想要创建的表名
     * @return
     */
    std::string getCreateFaceTableSql(std::string tableName) {
        if (tableName.size() == 0) {
            //#todo errInfo
            return "tableName is NULL";
        }
        // 人员信息表
        std::string sql = "CREATE TABLE " + g_dbName + ".";
        sql.append(tableName);
        sql.append(" (  uuid        varchar(255) PRIMARY KEY,");
        sql.append("personName  varchar(255) DEFAULT NULL,");
        sql.append("age         varchar(255) DEFAULT NULL,");
        sql.append("sex         varchar(255) DEFAULT NULL,");
        sql.append("idCard      varchar(255) DEFAULT NULL,");
        sql.append("phoneNum    varchar(255) DEFAULT NULL,");
        sql.append("create_time BLOB         DEFAULT (datetime('now', 'localtime')),");
        sql.append("update_time DATETIME     DEFAULT NULL,");
        sql.append("create_by   varchar(255) DEFAULT NULL,");
        sql.append("del_flag    INTEGER      DEFAULT 0");
        sql.append(");");
        // 人脸特征表
        sql.append("CREATE TABLE " + g_dbName + ".");
        sql.append(tableName + "_fea");
        sql.append(" (  uuid        varchar(255) PRIMARY KEY,");
        sql.append("    feature     BLOB NOT NULL,");
        sql.append("    faceUrl     BLOB NOT NULL,");
        sql.append("    create_time  BLOB default (datetime('now', 'localtime')),");
        sql.append("    update_time varchar(255) DEFAULT NULL,");
        sql.append("    create_by   varchar(255) DEFAULT NULL,");
        sql.append("    del_flag     INTEGER DEFAULT 0");
        sql.append(");");
        return sql;
    }
 
 
    std::string getUpdateFaceTableSql(std::string tableName, FieldValues &fieldValues) {
        if (tableName.size() == 0) {
            //#todo errInfo
            return "tableName is NULL";
        }
        std::string uuid = fieldValues["uuid"];
        if (uuid.size() <= 0) {
            SQLERR("uuid size is error");
            return "";
        }
        fieldValues.erase("uuid");
        if (fieldValues.size() <= 0) {
            SQLERR("fieldValues size is error");
            return "";
        }
        std::string sql = "update ";
        sql.append(tableName + " set ");
        for (auto &item :fieldValues) {
            sql.append(item.first + " =  '" + item.second + "',");
        }
        sql.append(" update_time ='" + AppUtil::getTimeSecString() + "'");
        sql.append(" where uuid ='" + uuid + "'");
        return sql;
    }
 
 
    //#TODO 人脸表需要删除两个
    std::string getDeleteFaceTableSql(std::string tableName) {
        if (tableName.size() == 0) {
            //#todo errInfo
            return "tableName is NULL";
        }
        std::string sql = "DROP TABLE " + g_dbName + "." + tableName + ";";
        sql.append("DROP TABLE " + g_dbName + "." + tableName + "_fea;");
        return sql;
    }
 
    /**
     * 根据表名判断数据表是否存在
     * @param existRet true is exist.
     * @param nCount 字段数量
     * @param pValue 字段值数组
     * @param azColName 字段名称数组
     * @return
     */
    static int isExist(void *existRet, int nCount, char **pValue, char **azColName) {
        bool *testRet = (bool *) existRet;
        int num = atoi(pValue[0]);
        if (num > 0) {
            *testRet = true;
        } else {
            *testRet = false;
        }
        return 0;
    }
 
}
 
 
#endif //TESTSQLITE_SQLITETOOLKIT_HPP