240717班级,工业化控制系统,煤矿相关行业,昆仑系统
1
wangky
2024-10-31 9583a7be21958cafdf30f03526dbf68838aa883f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#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();
}