#include "MysqlConn.h"
|
|
MysqlConn::MysqlConn()//³õʼ»¯Êý¾Ý¿â
|
{
|
m_result = nullptr;
|
m_mysqlRow = nullptr;
|
// ´«Èënullptr¿ÕÖ¸Õëʱ£¬»á×Ô¶¯·ÖÅäÒ»¸öMYSQL¶ÔÏó
|
m_conn = mysql_init(nullptr);
|
}
|
MysqlConn::~MysqlConn()//ÊÍ·ÅÊý¾Ý¿âÁ¬½Ó
|
{
|
freeRes(); // ÊͷŽá¹û¼¯
|
if (m_conn != nullptr) {
|
mysql_close(m_conn);
|
m_conn = nullptr;
|
}
|
}
|
void MysqlConn::freeRes()//½á¹û¼¯ÊÍ·Å
|
{
|
if (m_result) {
|
mysql_free_result(m_result);
|
m_result = nullptr;
|
}
|
}
|
bool MysqlConn::connect(const string user, const string passwd, \
|
const string dbName, string ip, \
|
const unsigned short port)//Á¬½ÓÊý¾Ý¿â
|
{
|
MYSQL* res = mysql_real_connect(m_conn, ip.c_str(), user.c_str(), \
|
passwd.c_str(), dbName.c_str(), port, nullptr, 0);
|
// Ð޸ıàÂë
|
mysql_set_character_set(m_conn, "gb2312");
|
return res != nullptr;
|
}
|
bool MysqlConn::update(const string sql) const
|
{
|
// Ö´Ðгɹ¦·µ»Ø0£»
|
int res = mysql_real_query(m_conn, sql.c_str(), static_cast<unsigned int>(sql.size()));
|
if (res != 0) {
|
return false; // Ìáʾ
|
}
|
return true;
|
}
|
bool MysqlConn::query(const string sql)
|
{
|
freeRes();
|
int res = mysql_real_query(m_conn, sql.c_str(), static_cast<unsigned int>(sql.size()));
|
if (res != 0) {
|
return false; // Ìáʾ
|
}
|
m_result = mysql_store_result(m_conn);
|
return true;
|
}
|
bool MysqlConn::getRes()
|
{
|
if (m_result != nullptr) {
|
// char** »ñÈ¡µ¥ÐмǼ
|
m_mysqlRow = mysql_fetch_row(m_result);
|
if (m_mysqlRow != nullptr) {
|
return true;
|
}
|
freeRes();
|
}
|
return false;
|
}
|
string MysqlConn::getValue(const int fieldIndex) const
|
{
|
int fieldCount = mysql_num_fields(m_result);
|
if (fieldIndex >= fieldCount || fieldIndex < 0) {
|
return string(); // Ìáʾ
|
}
|
char* value = m_mysqlRow[fieldIndex];
|
// µÃµ½Ò»¸ö±£´æ¸÷×Ö¶ÎÖµ³¤¶ÈµÄÊý×é
|
unsigned long* len = mysql_fetch_lengths(m_result);
|
unsigned long length = len[fieldIndex];
|
// ·ÀÖ¹½á¹ûÖдæÔÚ\0µ¼ÖÂÊý¾Ý¶ªÊ§
|
return string(value, length);
|
}
|
bool MysqlConn::selectDB(const string dbName) const
|
{
|
int res = mysql_select_db(m_conn, dbName.c_str());
|
if (res != 0) {
|
return false; // Ìáʾ
|
}
|
return true;
|
}
|
void MysqlConn::backupCurrentDB(const string path)
|
{
|
string sql = "show tables";
|
int r = mysql_real_query(m_conn, sql.c_str(), static_cast<unsigned long>(sql.size()));
|
if (r != 0) {
|
return; // Ìáʾ
|
}
|
MYSQL_RES* tableRes = mysql_store_result(m_conn);
|
for (int i = 0; i < mysql_num_rows(tableRes); ++i) {
|
MYSQL_ROW tableName = mysql_fetch_row(tableRes);
|
backupCurrentTable(path, tableName[0]);
|
}
|
}
|
void MysqlConn::backupCurrentTable(const string path, const string tableName)
|
{
|
string file = path + tableName + ".sql";
|
ofstream ofs(file);
|
if (!ofs.is_open()) {
|
return; // Ìáʾ
|
}
|
// ±í½á¹¹Ð´Èë
|
string showCreate = "show create table " + tableName + ";";
|
bool res = query(showCreate);
|
if (!res) {
|
return; // Ìáʾ
|
}
|
if (getRes()) {
|
string writeSQL = getValue(1) + ";\n";
|
ofs.write(writeSQL.c_str(), writeSQL.size());
|
// cout << writeSQL << endl;
|
}
|
// ±íÊý¾ÝдÈë
|
string sql = "select * from " + tableName + ";";
|
res = query(sql);
|
if (!res) {
|
return; // Ìáʾ
|
}
|
while (getRes()) {
|
string writeSQL = "insert into `" + tableName + "` values(";
|
for (int i = 0; !getValue(i).empty(); ++i) {
|
if (i != 0) {
|
writeSQL += ",";
|
}
|
MYSQL_FIELD* valueType = mysql_fetch_field_direct(m_result, i);
|
if (valueType->type == MYSQL_TYPE_DECIMAL
|
|| valueType->type == MYSQL_TYPE_TINY
|
|| valueType->type == MYSQL_TYPE_SHORT
|
|| valueType->type == MYSQL_TYPE_LONG
|
|| valueType->type == MYSQL_TYPE_FLOAT
|
|| valueType->type == MYSQL_TYPE_DOUBLE
|
|| valueType->type == MYSQL_TYPE_TIMESTAMP
|
|| valueType->type == MYSQL_TYPE_LONGLONG
|
|| valueType->type == MYSQL_TYPE_INT24) {
|
writeSQL += getValue(i);
|
}
|
else {
|
writeSQL += "'" + getValue(i) + "'";
|
}
|
}
|
writeSQL += ");\n";
|
ofs.write(writeSQL.c_str(), writeSQL.size());
|
}
|
ofs.close();
|
}
|
bool MysqlConn::transaction() const
|
{
|
// ½«ÊÂÎñÌá½»ÉèÖÃΪÊÖ¶¯Ìá½»
|
return mysql_autocommit(m_conn, false);
|
}
|
bool MysqlConn::commit() const
|
{
|
return mysql_commit(m_conn);
|
}
|
bool MysqlConn::rollback() const
|
{
|
return mysql_rollback(m_conn);
|
}
|
void MysqlConn::refreashAliveTime()
|
{
|
m_alivetime = steady_clock::now();
|
}
|
ll MysqlConn::getAliveTime()
|
{
|
// ºÁÃë <<= ÄÉÃë:¾«¶È½µµÍ
|
milliseconds res = duration_cast<milliseconds>(steady_clock::now() - m_alivetime);
|
return res.count();
|
}
|