派生自 development/c++

pansen
2019-03-07 979bc003bce710bf300bc2bd87a8278585678763
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
 
#ifndef BASIC_BUSINESS_TABLE_BASEMANAGER_H
#define BASIC_BUSINESS_TABLE_BASEMANAGER_H
 
#include <list>
#include <vector>
#include <string>
#include <iostream>
#include <mysql++.h>
#include <AppConfig.h>
#include <QtCore/QMutex>
 
using namespace std;
 
class BaseDao {
public:
    static QMutex m_mutexVisit;
private:
    static mysqlpp::Connection* conn;
public:
    static mysqlpp::SimpleResult add(std::map<std::string, std::string>& columns, string tableName) {
        initConnection();
        mysqlpp::SimpleResult simpleResult;
        try {
            mysqlpp::Query query = conn->query(getInsertSql(columns, tableName));
            simpleResult = query.execute();
            if (simpleResult.insert_id() <= 0) {
                cout << "add error " << query.error() << endl;
            }
        } catch (exception &e) {
            cout << e.what() << endl;
        }
        return simpleResult;
    }
    static bool del(string tableName, std::map<std::string, std::string>& whereColumns) {
        initConnection();
        string sql = "DELETE FROM " + tableName + " where 1=1 " + getWhereColumnNameValuePair(whereColumns);
        mysqlpp::Query query = conn->query(sql);
        bool ret = query.exec();
        if (!ret) {
            cout << "error " <<query.error() << endl;
        }
        return ret;
    }
    static bool update(std::map<std::string, std::string>& columns, string tableName, std::map<std::string, std::string>& whereColumns) {
        string updateSql = getUpdateSql(columns, tableName, whereColumns);
        initConnection();
        mysqlpp::Query query = conn->query(updateSql);
        bool ret = query.exec();
        if (!ret) {
            cout << "add error " << query.error() << endl;
        }
        return ret;
    }
 
    static vector<map<string, string>> findList(string sql) {
        initConnection();
        vector<map<string, string>> rowDataList;
        mysqlpp::Query query = conn->query(sql);
        if (auto res = query.store()) {
            for (auto it = res.begin(); it != res.end(); ++it) {
                map<string, string> rowData;
                mysqlpp::Row row = *it;
                auto field_list = row.field_list();
                const mysqlpp::FieldNames* fieldNames = field_list.list;
                for (int i = 0; i < fieldNames->size(); i++) {
                    string columnValue;
                    row[i].to_string(columnValue);
                    string columnName = fieldNames[0].at(i);
                    rowData[columnName] = columnValue;
                }
                rowDataList.emplace_back(rowData);
            }
        } else {
            cout << "query failed" << endl;
        }
        return rowDataList;
    }
 
    static Json::Value findJsonArray(string sql) {
        initConnection();
        mysqlpp::Query query = conn->query(sql);
        Json::Value rowList;
        if (auto res = query.store()) {
            for (auto it = res.begin(); it != res.end(); ++it) {
                Json::Value row;
                const mysqlpp::FieldNames* fieldNames = it->field_list().list;
                for (int i = 0; i < fieldNames->size(); i++) {
                    string columnValue;
                    (*it)[i].to_string(columnValue);
                    if (columnValue == "NULL") {
                        columnValue = "";
                    }
                    string columnName = fieldNames[0].at(i);
                    row[columnName] = columnValue;
                }
                rowList.append(row);
            }
        } else {
            cout << "query failed" << endl;
        }
        return rowList;
    }
 
    static bool exec(string sql) {
        initConnection();
        mysqlpp::Query query = conn->query(sql);
        bool ret = query.exec();
        if (!ret) {
            cout << "error " <<query.error() << endl;
        }
        return ret;
    }
    static string getInsertSql(std::map<std::string, std::string>& columns, string tableName) {
        string insertSql = "INSERT INTO "+tableName+" ("
                           + getColumnNames(columns) +
                           " ) values ( "
                           + getColumnValues(columns) +
                           " )";
        cout << "insertSql " << insertSql << endl;
        return insertSql;
    }
    static string getUpdateSql(std::map<std::string, std::string>& columns, string tableName, std::map<std::string, std::string>& whereColumns) {
        string updateSql = "update "+tableName+" set "
                           + getColumnNameValuePair(columns) +
                           " where 1=1 "+
                           getWhereColumnNameValuePair(whereColumns)
        ;
        cout << "updateSql " << updateSql << endl;
        return updateSql;
    }
    static void doConnect() {
        if (conn->connect(
            appConfig.getStringProperty("database").c_str(),
            appConfig.getStringProperty("db_host").c_str(),
            appConfig.getStringProperty("db_user").c_str(),
            appConfig.getStringProperty("db_password").c_str(),
            appConfig.getIntProperty("db_port")
        )) {
//        if (conn->connect(
//                "EGEyesForVSS",
//                "192.168.1.148",
//                "root",
//                "123456",
//                3306
//        )) {
            cout << "connect success" << endl;
        } else {
            cout << "connect failed" << endl;
        }
    }
    static void initConnection() {
        static bool inited = false;
        if (!inited) {
            inited = true;
            conn = new mysqlpp::Connection(false);
            doConnect();
        }
        if (!conn->connected() || !conn->ping()) {
            doConnect();
        }
    }
    static string getColumnNames(std::map<std::string, std::string>& columns) {
        string columnNames;
        auto size = columns.size();
        int i = 0;
        for (auto column : columns) {
            columnNames.append(column.first);
            if (i != size - 1) {
                columnNames.append(",");
            }
            i++;
        }
        return columnNames;
    }
    static string getColumnValues(std::map<std::string, std::string>& columns) {
        string columnValues;
        auto size = columns.size();
        int i = 0;
        for (auto column : columns) {
            columnValues.append("'"+column.second+"'");
            if (i != columns.size() - 1) {
                columnValues.append(",");
            }
            i++;
        }
        return columnValues;
    }
    static string getColumnNameValuePair(std::map<std::string, std::string>& columns) {
        string columnNameValuePair;
        auto size = columns.size();
        int i = 0;
        for (auto column : columns) {
            columnNameValuePair.append(column.first);
            columnNameValuePair.append("=");
            columnNameValuePair.append("'"+column.second+"'");
 
            if (i != size - 1) {
                columnNameValuePair.append(",");
            }
            i++;
        }
        return columnNameValuePair;
    }
    static string getWhereColumnNameValuePair(std::map<std::string, std::string>& columns) {
        string columnNameValuePair;
        auto size = columns.size();
        int i = 0;
        for (auto column : columns) {
            columnNameValuePair.append(" and ");
            columnNameValuePair.append(column.first);
            columnNameValuePair.append("=");
            columnNameValuePair.append("'"+column.second+"' ");
 
            i++;
        }
        return columnNameValuePair;
    }
};
 
 
#endif //BASIC_BUSINESS_TABLE_BASEMANAGER_H