| | |
| | | #include "stdafx.h" |
| | | #include "MysqlConn.h" |
| | | #include <regex> |
| | | |
| | | |
| | | |
| | | // åå§åè¿æ¥ |
| | | // åå§åè¿æ¥ |
| | | MysqlConn::MysqlConn() { |
| | | mysql_ = mysql_init(mysql_); |
| | | // 设置å符é |
| | | if (mysql_) mysql_set_character_set(mysql_, "gbk"); |
| | | } |
| | | |
| | | // è¿æ¥æ°æ®åº |
| | | bool MysqlConn::connect(std::string ip, std::string userName, std::string passwd, std::string db, int port) { |
| | | mysql_ = mysql_real_connect(mysql_, ip.c_str(), userName.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0); |
| | | if (!mysql_) { |
| | | return false; |
| | | m_mysql = mysql_init(nullptr); |
| | | if (!m_mysql) { |
| | | cout << "Failed to initialize MySQL." << endl; |
| | | return; |
| | | } |
| | | return true; |
| | | // 使ç¨åæ»çåæ°è¿æ¥æ°æ®åº |
| | | m_mysql = mysql_real_connect(m_mysql, "127.0.0.1", "root", "123456", "mayi_kunlun", 3306, nullptr, 0); |
| | | if (!m_mysql) { |
| | | cout << "MysqlConn connect to mysql is failed!" << endl; |
| | | // ç¡®ä¿å¨è¿æ¥å¤±è´¥æ¶æ£ç¡®æ¸
çèµæº |
| | | mysql_close(m_mysql); |
| | | m_mysql = nullptr; |
| | | } |
| | | // åå§å row_ å res_ |
| | | if (m_mysql) mysql_set_character_set(m_mysql, "gbk"); |
| | | m_row = nullptr; |
| | | m_res = nullptr; |
| | | } |
| | | bool MysqlConn::isConnected() { |
| | | return m_mysql != nullptr; |
| | | } |
| | | |
| | | // éæ¾èµæº |
| | | MysqlConn::~MysqlConn() { |
| | | if (mysql_) { |
| | | mysql_close(mysql_); |
| | | mysql_ = nullptr; |
| | | if (m_mysql) { |
| | | mysql_close(m_mysql); |
| | | m_mysql = nullptr; |
| | | } |
| | | freeRes(); |
| | | } |
| | | |
| | | // æ´æ°æ°æ® |
| | | bool MysqlConn::update(std::string sql) { |
| | | bool MysqlConn::update(string sql) { |
| | | // åæ°åæ¥è¯¢ä¼ååçå®å
¨æ£æ¥ |
| | | MYSQL_STMT* stmt = mysql_stmt_init(mysql_); |
| | | MYSQL_STMT* stmt = mysql_stmt_init(m_mysql); |
| | | if (!stmt) { |
| | | return false; |
| | | } |
| | |
| | | } |
| | | int paramCount = mysql_stmt_param_count(stmt); |
| | | if (paramCount > 0) { |
| | | // 妿æåæ°ï¼éè¦è¿è¡åæ°ç»å®çæä½ï¼è¿éæä¸å®ç° |
| | | mysql_stmt_close(stmt); |
| | | return false; |
| | | } |
| | |
| | | return true; |
| | | } |
| | | |
| | | // æ¥è¯¢æ°æ®åº |
| | | bool MysqlConn::query(std::string sql) { |
| | | bool MysqlConn::query(string sql, vector<vector<string>>& result) |
| | | { |
| | | freeRes(); |
| | | // åæ°åæ¥è¯¢ä¼ååçå®å
¨æ£æ¥ |
| | | MYSQL_STMT* stmt = mysql_stmt_init(mysql_); |
| | | int ret = mysql_query(m_mysql, sql.c_str()); |
| | | if (ret != 0) { |
| | | fprintf(stderr, "Query failed: %s\n", mysql_error(m_mysql)); |
| | | return false; |
| | | } |
| | | // è·åæ¥è¯¢ç»æ |
| | | m_res = mysql_store_result(m_mysql); |
| | | if (!m_res) return false; |
| | | //å°ç»æé转å°äºç»´æ°ç»resultéé¢ |
| | | //int col_cnt = m_mysql->field_count; |
| | | int col_cnt = mysql_field_count(m_mysql); |
| | | cout << col_cnt << endl; |
| | | MYSQL_ROW m_row; |
| | | while (m_row = mysql_fetch_row(m_res)) { |
| | | vector<string> tmp; |
| | | for (int i = 0; i < col_cnt; i++) { |
| | | tmp.push_back(m_row[i]); |
| | | } |
| | | result.push_back(tmp); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | // æ¥è¯¢æ°æ®åº |
| | | bool MysqlConn::query(string sql) { |
| | | freeRes(); |
| | | //åæ°åæ¥è¯¢ä¼ååçå®å
¨æ£æ¥ |
| | | MYSQL_STMT* stmt = mysql_stmt_init(m_mysql); |
| | | if (!stmt) { |
| | | return false; |
| | | } |
| | |
| | | mysql_stmt_close(stmt); |
| | | return false; |
| | | } |
| | | res_ = mysql_stmt_result_metadata(stmt); |
| | | if (!res_) { |
| | | m_res = mysql_stmt_result_metadata(stmt); |
| | | if (!m_row) { |
| | | mysql_stmt_close(stmt); |
| | | return false; |
| | | } |
| | |
| | | |
| | | // å¾å°ç»æé |
| | | bool MysqlConn::getResult() { |
| | | if (res_) { |
| | | row_ = mysql_fetch_row(res_); |
| | | if (row_) return true; |
| | | if (m_row) { |
| | | m_row = mysql_fetch_row(m_res); |
| | | if (m_row) return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | // è·åç»æéçåæ®µ |
| | | std::string MysqlConn::getField(int index) { |
| | | int cols = mysql_num_fields(res_); |
| | | if (index >= cols || index < 0) return std::string(""); |
| | | string MysqlConn::getField(int index) { |
| | | int cols = mysql_num_fields(m_res); |
| | | if (!m_res || index >= cols || index < 0) return string(""); |
| | | |
| | | char* value = row_[index]; |
| | | unsigned long len = mysql_fetch_lengths(res_)[index]; |
| | | return std::string(value, len); |
| | | char* value = m_row[index]; |
| | | // ä½¿ç¨æ¾å¼ç±»åè½¬æ¢ |
| | | unsigned long len = static_cast<unsigned long>(mysql_fetch_lengths(m_res)[index]); |
| | | return string(value, len); |
| | | } |
| | | |
| | | // äºå¡æä½ |
| | | bool MysqlConn::transaction() { |
| | | return mysql_autocommit(mysql_, false); |
| | | return mysql_autocommit(m_mysql, false); |
| | | } |
| | | |
| | | // æäº¤äºå¡ |
| | | bool MysqlConn::commit() { |
| | | return mysql_commit(mysql_); |
| | | return mysql_commit(m_mysql); |
| | | } |
| | | |
| | | // äºå¡åæ» |
| | | bool MysqlConn::rollback() { |
| | | return mysql_rollback(mysql_); |
| | | return mysql_rollback(m_mysql); |
| | | } |
| | | |
| | | void MysqlConn::refreshActiveTime() { |
| | | activeTime_ = std::chrono::steady_clock::now(); |
| | | m_activeTime = chrono::steady_clock::now(); |
| | | } |
| | | |
| | | long long MysqlConn::getActiveTime() { |
| | | // 纳米 |
| | | std::chrono::nanoseconds nased = std::chrono::steady_clock::now() - activeTime_; |
| | | chrono::nanoseconds nased = chrono::steady_clock::now() - m_activeTime; |
| | | // è½¬æ¢ææ¯«ç±³ |
| | | std::chrono::microseconds millsed = std::chrono::duration_cast<std::chrono::microseconds>(nased); |
| | | chrono::microseconds millsed = chrono::duration_cast<chrono::microseconds>(nased); |
| | | return millsed.count(); // å¤å°æ¯«ç§ |
| | | } |
| | | |
| | | // å®å
¨æ ¡éªå®ç°ï¼è¿éç®åä½¿ç¨æ£å表达å¼å¤ææ¯å¦å
å«å±é©å符 |
| | | bool MysqlConn::isSqlSafe(const std::string& sql) { |
| | | std::regex dangerousPattern(".*(['\";\\-+=]).*"); |
| | | return!std::regex_search(sql, dangerousPattern); |
| | | bool MysqlConn::isSqlSafe(const string& sql) { |
| | | regex dangerousPattern(".*(['\";\\-+=]).*"); |
| | | return!regex_search(sql, dangerousPattern); |
| | | } |
| | | |
| | | void MysqlConn::freeRes() { |
| | | if (res_) { |
| | | mysql_free_result(res_); |
| | | res_ = nullptr; |
| | | if (m_res) { |
| | | mysql_free_result(m_res); |
| | | m_res = nullptr; |
| | | } |
| | | } |