znone
2021-10-05 a77db3e50bb5429b17e8d89bf71403dfdf39985c
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
#include "stdafx.h"
#include "../include/qtl_postgres.hpp"
#include <vector>
#include <thread>
#include <system_error>
#include <time.h>
#include <limits.h>
#include "../include/qtl_postgres_pool.hpp"
#include "../include/qtl_asio.hpp"
 
using namespace qtl::postgres;
 
void LogError(const error& e)
{
    fprintf(stderr, "PostgreSQL Error: %s\n", e.what());
}
 
const char postgres_server[] = "localhost";
const char postgres_user[] = "postgres";
const char postgres_password[] = "111111";
const char postgres_database[] = "test";
 
qtl::asio::service service;
 
void SimpleTest()
{
    async_connection connection;
    service.reset();
    std::map<std::string, std::string> params;
    params["host"] = postgres_server;
    params["dbname"] = postgres_database;
    params["user"] = postgres_user;
    params["password"] = postgres_password;
    connection.open(service, [&connection](const error& e) {
        if (e)
        {
            LogError(e);
            service.stop();
        }
        else
        {
            printf("Connect to PostgreSQL ok.\n");
            connection.simple_query("select id, name from test", [](const std::string& id, const std::string& name) {
                printf("%s\t%s\n", id.data(), name.data());
                return true;
            }, [&connection](const error& e, size_t row_count) {
                if (e)
                    LogError(e);
                else
                    printf("Total %lu rows.\n", row_count);
 
                connection.close();
            });
        }
    }, params);
 
    service.run();
}
 
void insert(async_connection& connection, int next)
{
    qtl::asio::async_execute(connection, [&connection, next](const error& e, uint64_t affected) mutable {
        if (e)
        {
            LogError(e);
            service.stop();
        }
        else
        {
            if (next)
            {
                --next;
                asio::post(service.context(), [&connection, next]() {
                    insert(connection, next);
                });
            }
            else
            {
                service.stop();
            }
        }
    }, "insert into test(name, createtime) values($1, LOCALTIMESTAMP)", 0, "test name");
}
 
void ExecuteTest()
{
    async_connection connection;
    service.reset();
    std::map<std::string, std::string> params;
    params["host"] = postgres_server;
    params["dbname"] = postgres_database;
    params["user"] = postgres_user;
    params["password"] = postgres_password;
    qtl::asio::async_open(service, connection, [&connection](const error& e) {
        if (e)
        {
            LogError(e);
            service.stop();
        }
        else
        {
            printf("Connect to PostgreSQL ok.\n");
            insert(connection, 10);
        }
    }, params);
 
    service.run();
};
 
void QueryTest()
{
    async_connection connection;
    service.reset();
    std::map<std::string, std::string> params;
    params["host"] = postgres_server;
    params["dbname"] = postgres_database;
    params["user"] = postgres_user;
    params["password"] = postgres_password;
    connection.open(service, [&connection](const error& e) {
        if (e)
        {
            LogError(e);
            service.stop();
        }
        else
        {
            printf("Connect to PostgreSQL ok.\n");
            connection.query("select id, name, CreateTime from test",
                [](int32_t id, const std::string& name, const qtl::postgres::timestamp& create_time) {
                printf("%d\t%s\t%s\n", id, name.data(), create_time.to_string().data());
            }, [&connection](const error& e) {
                printf("query has completed.\n");
                if (e)
                    LogError(e);
 
                connection.close();
            });
        }
    }, params);
 
    service.run();
}
 
int main(int argc, char* argv[])
{
    ExecuteTest();
    SimpleTest();
    QueryTest();
    return 0;
}