| | |
| | | #include <iostream> |
| | | #include <mysql++.h> |
| | | #include <AppConfig.h> |
| | | #include <QtCore/QMutex> |
| | | |
| | | using namespace std; |
| | | |
| | | static mysqlpp::Connection *conn; |
| | | |
| | | class BaseDao { |
| | | public: |
| | | static mysqlpp::SimpleResult add(std::map<std::string, std::string> &columns, string tableName) { |
| | | 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 { |
| | |
| | | } |
| | | return simpleResult; |
| | | } |
| | | |
| | | static bool del(string tableName, std::map<std::string, std::string> &whereColumns) { |
| | | 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; |
| | | 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) { |
| | | 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); |
| | |
| | | map<string, string> rowData; |
| | | mysqlpp::Row row = *it; |
| | | auto field_list = row.field_list(); |
| | | const mysqlpp::FieldNames *fieldNames = field_list.list; |
| | | const mysqlpp::FieldNames* fieldNames = field_list.list; |
| | | for (int i = 0; i < fieldNames->size(); i++) { |
| | | string columnValue; |
| | | row[i].to_string(columnValue); |
| | |
| | | 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; |
| | | const mysqlpp::FieldNames* fieldNames = it->field_list().list; |
| | | for (int i = 0; i < fieldNames->size(); i++) { |
| | | string columnValue; |
| | | (*it)[i].to_string(columnValue); |
| | |
| | | mysqlpp::Query query = conn->query(sql); |
| | | bool ret = query.exec(); |
| | | if (!ret) { |
| | | cout << "error " << query.error() << endl; |
| | | cout << "error " <<query.error() << endl; |
| | | } |
| | | return ret; |
| | | } |
| | | |
| | | static string getInsertSql(std::map<std::string, std::string> &columns, string tableName) { |
| | | string insertSql = "INSERT INTO " + tableName + " (" |
| | | 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 " |
| | | 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); |
| | | " where 1=1 "+ |
| | | getWhereColumnNameValuePair(whereColumns) |
| | | ; |
| | | cout << "updateSql " << updateSql << endl; |
| | | return updateSql; |
| | | } |
| | | |
| | | static void doConnect() { |
| | | if (conn->connect( |
| | | appConfig.getStringProperty("database").c_str(), |
| | |
| | | cout << "connect failed" << endl; |
| | | } |
| | | } |
| | | |
| | | static void initConnection() { |
| | | static bool inited = false; |
| | | if (!inited) { |
| | |
| | | doConnect(); |
| | | } |
| | | } |
| | | |
| | | static string getColumnNames(std::map<std::string, std::string> &columns) { |
| | | static string getColumnNames(std::map<std::string, std::string>& columns) { |
| | | string columnNames; |
| | | auto size = columns.size(); |
| | | int i = 0; |
| | |
| | | } |
| | | return columnNames; |
| | | } |
| | | |
| | | static string getColumnValues(std::map<std::string, std::string> &columns) { |
| | | 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 + "'"); |
| | | columnValues.append("'"+column.second+"'"); |
| | | if (i != columns.size() - 1) { |
| | | columnValues.append(","); |
| | | } |
| | |
| | | } |
| | | return columnValues; |
| | | } |
| | | |
| | | static string getColumnNameValuePair(std::map<std::string, std::string> &columns) { |
| | | 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 + "'"); |
| | | columnNameValuePair.append("'"+column.second+"'"); |
| | | |
| | | if (i != size - 1) { |
| | | columnNameValuePair.append(","); |
| | |
| | | } |
| | | return columnNameValuePair; |
| | | } |
| | | |
| | | static string getWhereColumnNameValuePair(std::map<std::string, std::string> &columns) { |
| | | static string getWhereColumnNameValuePair(std::map<std::string, std::string>& columns) { |
| | | string columnNameValuePair; |
| | | auto size = columns.size(); |
| | | int i = 0; |
| | |
| | | columnNameValuePair.append(" and "); |
| | | columnNameValuePair.append(column.first); |
| | | columnNameValuePair.append("="); |
| | | columnNameValuePair.append("'" + column.second + "' "); |
| | | columnNameValuePair.append("'"+column.second+"' "); |
| | | |
| | | i++; |
| | | } |