| | |
| | | } |
| | | 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); |
| | |
| | | } |
| | | 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); |
| | |
| | | static Json::Value findJsonArray(string sql, std::map<std::string, std::string>& whereColumns) { |
| | | sql = sql + getWhereColumnNameValuePair(whereColumns); |
| | | initConnection(); |
| | | // conn->query("SET character_set_server = utf8;"); |
| | | mysqlpp::Query query = conn->query(sql); |
| | | std::cout << sql << std::endl; |
| | | Json::Value rowList; |
| | | if (auto res = query.store()) { |
| | | for (auto it = res.begin(); it != res.end(); ++it) { |
| | |
| | | columnValue = ""; |
| | | } |
| | | string columnName = fieldNames[0].at(i); |
| | | // if (columnName == "Nickname") { |
| | | // columnValue = columnValue;//.substr(0, 20); |
| | | // } |
| | | row[columnName] = columnValue; |
| | | } |
| | | rowList.append(row); |
| | |
| | | } |
| | | return ret; |
| | | } |
| | | |
| | | static string getInsertSql(std::map<std::string, std::string>& columns, string tableName) { |
| | | string insertSql = "INSERT INTO "+tableName+" (" |
| | | + getColumnNames(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) { |
| | | |
| | | 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) |
| | | ; |
| | | getWhereColumnNameValuePair(whereColumns); |
| | | cout << "updateSql " << updateSql << endl; |
| | | return updateSql; |
| | | } |
| | | |
| | | static void doConnect() { |
| | | cout << "db_host=" << appConfig.getStringProperty("db_host").c_str() << endl; |
| | | conn->set_option(new mysqlpp::SetCharsetNameOption("utf8")); |
| | | if (conn->connect( |
| | | appConfig.getStringProperty("database").c_str(), |
| | | appConfig.getStringProperty("db_host").c_str(), |
| | |
| | | // 3306 |
| | | // )) { |
| | | cout << "connect success" << endl; |
| | | mysqlpp::Query query = conn->query("SET NAMES UTF8-"); |
| | | } 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(); |
| | |
| | | } |
| | | return columnNames; |
| | | } |
| | | |
| | | static string getColumnValues(std::map<std::string, std::string>& columns) { |
| | | string columnValues; |
| | | auto size = columns.size(); |
| | |
| | | } |
| | | return columnValues; |
| | | } |
| | | |
| | | static string getColumnNameValuePair(std::map<std::string, std::string>& columns) { |
| | | string columnNameValuePair; |
| | | auto size = columns.size(); |
| | |
| | | } |
| | | return columnNameValuePair; |
| | | } |
| | | |
| | | static string getWhereColumnNameValuePair(std::map<std::string, std::string>& columns) { |
| | | string columnNameValuePair; |
| | | auto size = columns.size(); |