znone
2021-02-02 31e241f559537a176869bf2aab5d37289e857a82
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
#include "stdafx.h"
#include "../include/qtl_mysql.hpp"
#include <vector>
#include <thread>
#include <system_error>
#include <time.h>
#include <limits.h>
#include "../include/qtl_mysql_pool.hpp"
#include "../include/qtl_asio.hpp"
 
#if MARIADB_VERSION_ID < 0100000
#error "The program need mariadb version > 10.0"
#endif 
 
using namespace qtl::mysql;
 
void LogError(const error& e)
{
    fprintf(stderr, "MySQL Error(%d): %s\n", e.code(), e.what());
}
 
const char mysql_server[]="localhost";
const char mysql_user[]="root";
const char mysql_password[]="123456";
const char mysql_database[]="test";
 
class MariaDBTest
{
public:
    MariaDBTest()
    {
        Open();
        _service.run();
    }
 
private:
    void Open();
    void Close();
    void SimpleQuery();
    void Execute();
    void Query();
    void MultiQuery();
 
private:
    async_connection _connection;
    qtl::asio::service _service;
 
};
 
void MariaDBTest::Open()
{
    _connection.open(_service, [this](const error& e) {
        if (e)
        {
            LogError(e);
            _service.stop();
        }
        else
        {
            printf("Connect to mysql ok.\n");
            SimpleQuery();
        }
    }, mysql_server, mysql_user, mysql_password, mysql_database);
}
 
void MariaDBTest::Close()
{
    _connection.close([this]() {
        printf("Connection is closed.\n");
        _service.stop();
    });
}
 
void MariaDBTest::SimpleQuery()
{
    _connection.simple_query("select * from test", 0, [](MYSQL_ROW row, int field_count) {
        for (int i = 0; i != field_count; i++)
            printf("%s\t", row[i]);
        printf("\n");
        return true;
    }, [this](const error& e, size_t row_count) {
        if (e)
        {
            LogError(e);
            Close();
        }
        else
        {
            printf("Total %lu rows.\n", row_count);
            Execute();
        }
    });
}
 
void MariaDBTest::Execute()
{
    _connection.execute([this](const error& e, uint64_t affected) mutable {
        if (e)
        {
            LogError(e);
            Close();
        }
        else
        {
            printf("Insert %llu records ok.\n", affected);
            Query();
        }
    }, "insert into test(name, createtime, company) values(?, now(), ?)", 0, std::make_tuple("test name", "test company"));
}
 
void MariaDBTest::Query()
{
    _connection.query("select id, name, CreateTime, Company from test", 0, 
        [](int64_t id, const std::string& name, const qtl::mysql::time& create_time, const std::string& company) {
            char szTime[128] = { 0 };
            if (create_time.year != 0)
            {
                struct tm tm;
                create_time.as_tm(tm);
                strftime(szTime, 128, "%c", &tm);
            }
            printf("%lld\t%s\t%s\t%s\n", id, name.data(), szTime, company.data());
    }, [this](const error& e) {
        printf("query has completed.\n");
        if (e)
        {
            LogError(e);
            Close();
        }
        else
        {
            MultiQuery();
        }
    });
}
 
void MariaDBTest::MultiQuery()
{
    _connection.query_multi("call test_proc",
        [this](const error& e) {
            if (e)
                LogError(e);
            Close();
            
    }, [](uint32_t i, const std::string& str) {
        printf("0=\"%d\", 'hello world'=\"%s\"\n", i, str.data());
    }, [](const qtl::mysql::time& time) {
        struct tm tm;
        time.as_tm(tm);
        printf("current time is: %s\n", asctime(&tm));
    });
}
 
int main(int argc, char* argv[])
{
    MariaDBTest test;
    return 0;
}