znone
2020-09-04 335f8f02800eb8bc053d22c5e505d93a1443dfca
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
#ifndef _QTL_MYSQL_POOL_H_
#define _QTL_MYSQL_POOL_H_
 
#include "qtl_database_pool.hpp"
#include "qtl_mysql.hpp"
 
namespace qtl
{
 
namespace mysql
{
 
class database_pool : public qtl::database_pool<database>
{
public:
    database_pool() : m_port(0) { }
    virtual ~database_pool() { }
    virtual database* new_database() throw() override
    {
        database* db=new database;
        if(!db->open(m_host.data(), m_user.data(), m_password.data(), m_database.data(), 0, m_port))
        {
            delete db;
            db=NULL;
        }
        else
        {
            db->charset_name("utf8");
        }
        return db;
    }
 
protected:
    std::string m_host;
    unsigned short m_port;
    std::string m_database;
    std::string m_user;
    std::string m_password;
};
 
#if MARIADB_VERSION_ID >= 050500
 
template<typename EventLoop>
class async_pool : public qtl::async_pool<async_pool<EventLoop>, EventLoop, async_connection>
{
    typedef qtl::async_pool<async_pool<EventLoop>, EventLoop, async_connection> base_class;
public:
    async_pool(EventLoop& ev) : base_class(ev), m_port(0) { }
    virtual ~async_pool() { }
 
    template<typename Handler>
    void new_connection(EventLoop& ev, Handler&& handler) throw()
    {
        async_connection* db = new async_connection;
        db->open(ev, [this, handler, db](const mysql::error& e) mutable {
            if (e)
            {
                delete db;
                db = nullptr;
            }
            else
            {
                db->charset_name("utf8");
            }
            handler(e, db);
        }, m_host.data(), m_user.data(), m_password.data(), m_database.data(), 0, m_port);
    }
 
protected:
    std::string m_host;
    unsigned short m_port;
    std::string m_database;
    std::string m_user;
    std::string m_password;
};
 
#endif //MariaDB
 
}
 
}
 
#endif //_QTL_MYSQL_POOL_H_