| | |
| | | #include "stdafx.h" |
| | | #include "MysqlConn.h" |
| | | #include <regex> |
| | | |
| | | |
| | | // åå§åè¿æ¥ |
| | | MysqlConn::MysqlConn() { |
| | | mysql_ = mysql_init( mysql_ ); |
| | | // 设置å符é |
| | | if( mysql_ ) mysql_set_character_set(mysql_ , "gbk"); |
| | | 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; |
| | | } |
| | | return true; |
| | | 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; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | // éæ¾èµæº |
| | | MysqlConn::~MysqlConn() { |
| | | if (mysql_) { |
| | | mysql_close(mysql_); |
| | | mysql_ = nullptr; |
| | | } |
| | | freeRes(); |
| | | if (mysql_) { |
| | | mysql_close(mysql_); |
| | | mysql_ = nullptr; |
| | | } |
| | | freeRes(); |
| | | } |
| | | |
| | | // æ´æ°æ°æ® |
| | | bool MysqlConn::update( std::string sql ) { |
| | | if (!isSqlSafe(sql)) |
| | | { |
| | | return false; |
| | | } |
| | | int ret = mysql_query( mysql_ , sql.c_str()); |
| | | if( ret != 0 ){ |
| | | return false; |
| | | } |
| | | return true; |
| | | bool MysqlConn::update(std::string sql) { |
| | | // åæ°åæ¥è¯¢ä¼ååçå®å
¨æ£æ¥ |
| | | MYSQL_STMT* stmt = mysql_stmt_init(mysql_); |
| | | if (!stmt) { |
| | | return false; |
| | | } |
| | | if (mysql_stmt_prepare(stmt, sql.c_str(), sql.length())) { |
| | | mysql_stmt_close(stmt); |
| | | return false; |
| | | } |
| | | int paramCount = mysql_stmt_param_count(stmt); |
| | | if (paramCount > 0) { |
| | | // 妿æåæ°ï¼éè¦è¿è¡åæ°ç»å®çæä½ï¼è¿éæä¸å®ç° |
| | | mysql_stmt_close(stmt); |
| | | return false; |
| | | } |
| | | if (mysql_stmt_execute(stmt)) { |
| | | mysql_stmt_close(stmt); |
| | | return false; |
| | | } |
| | | mysql_stmt_close(stmt); |
| | | return true; |
| | | } |
| | | |
| | | // æ¥è¯¢æ°æ®åº |
| | | bool MysqlConn::query(std::string sql) { |
| | | freeRes( ); |
| | | if (!isSqlSafe(sql)) |
| | | { |
| | | return false; |
| | | } |
| | | int ret = mysql_query(mysql_, sql.c_str()); |
| | | if (ret != 0) return false; |
| | | |
| | | // è·åæ¥è¯¢ç»æ |
| | | res_ = mysql_store_result(mysql_); |
| | | if (!res_) return false; |
| | | return true; |
| | | freeRes(); |
| | | // åæ°åæ¥è¯¢ä¼ååçå®å
¨æ£æ¥ |
| | | MYSQL_STMT* stmt = mysql_stmt_init(mysql_); |
| | | if (!stmt) { |
| | | return false; |
| | | } |
| | | if (mysql_stmt_prepare(stmt, sql.c_str(), sql.length())) { |
| | | mysql_stmt_close(stmt); |
| | | return false; |
| | | } |
| | | int paramCount = mysql_stmt_param_count(stmt); |
| | | if (paramCount > 0) { |
| | | // 妿æåæ°ï¼éè¦è¿è¡åæ°ç»å®çæä½ï¼è¿éæä¸å®ç° |
| | | mysql_stmt_close(stmt); |
| | | return false; |
| | | } |
| | | if (mysql_stmt_execute(stmt)) { |
| | | mysql_stmt_close(stmt); |
| | | return false; |
| | | } |
| | | res_ = mysql_stmt_result_metadata(stmt); |
| | | if (!res_) { |
| | | mysql_stmt_close(stmt); |
| | | return false; |
| | | } |
| | | mysql_stmt_close(stmt); |
| | | return true; |
| | | } |
| | | |
| | | // å¾å°ç»æé |
| | | bool MysqlConn::getResult() { |
| | | if (res_) { |
| | | row_ = mysql_fetch_row(res_); |
| | | if(row_) return true; |
| | | } |
| | | return false; |
| | | if (res_) { |
| | | row_ = mysql_fetch_row(res_); |
| | | if (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(""); |
| | | |
| | | char* value = row_[index]; |
| | | unsigned long len = mysql_fetch_lengths(res_)[index]; |
| | | return std::string(value, len); |
| | | std::string MysqlConn::getField(int index) { |
| | | int cols = mysql_num_fields(res_); |
| | | if (index >= cols || index < 0) return std::string(""); |
| | | |
| | | char* value = row_[index]; |
| | | unsigned long len = mysql_fetch_lengths(res_)[index]; |
| | | return std::string(value, len); |
| | | } |
| | | |
| | | // äºå¡æä½ |
| | | bool MysqlConn::transaction() { |
| | | return mysql_autocommit(mysql_ ,false ); |
| | | return mysql_autocommit(mysql_, false); |
| | | } |
| | | |
| | | // æäº¤äºå¡ |
| | | bool MysqlConn::commit() { |
| | | return mysql_commit(mysql_); |
| | | return mysql_commit(mysql_); |
| | | } |
| | | |
| | | // äºå¡åæ» |
| | | bool MysqlConn::rollback() { |
| | | return mysql_rollback(mysql_); |
| | | return mysql_rollback(mysql_); |
| | | } |
| | | |
| | | void MysqlConn::refreshActiveTime() |
| | | { |
| | | activeTime_ = std::chrono::steady_clock::now(); |
| | | void MysqlConn::refreshActiveTime() { |
| | | activeTime_ = std::chrono::steady_clock::now(); |
| | | } |
| | | |
| | | long long MysqlConn::getActiveTime() |
| | | { |
| | | // 纳米 |
| | | std::chrono::nanoseconds nased = std::chrono::steady_clock::now() - activeTime_; |
| | | // è½¬æ¢ææ¯«ç±³ |
| | | std::chrono::microseconds millsed = std::chrono::duration_cast<std::chrono::microseconds>( nased ); |
| | | return millsed.count( ); // å¤å°æ¯«ç§ |
| | | long long MysqlConn::getActiveTime() { |
| | | // 纳米 |
| | | std::chrono::nanoseconds nased = std::chrono::steady_clock::now() - activeTime_; |
| | | // è½¬æ¢ææ¯«ç±³ |
| | | std::chrono::microseconds millsed = std::chrono::duration_cast<std::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 std::string& sql) { |
| | | std::regex dangerousPattern(".*(['\";\\-+=]).*"); |
| | | return!std::regex_search(sql, dangerousPattern); |
| | | } |
| | | |
| | | void MysqlConn::freeRes() { |
| | | if (res_) { |
| | | mysql_free_result(res_); |
| | | res_ = nullptr; |
| | | } |
| | | if (res_) { |
| | | mysql_free_result(res_); |
| | | res_ = nullptr; |
| | | } |
| | | } |