-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmysql_c_api_foundation.cxx
128 lines (115 loc) · 3 KB
/
mysql_c_api_foundation.cxx
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
#include "mysql_c_api_foundation.h"
#include <boost\date_time\posix_time\posix_time.hpp>
MySQLFoundationWrapper::MySQLFoundationWrapper(
unsigned int timeout__,
long timerinterval__,
std::string host__,
std::string user__,
std::string password__,
unsigned int port__):
timeout_seconds_(timeout__),
timer_interval_seconds_(timerinterval__),
host_(host__),
user_(user__),
password_(password__),
port_(port__),
timer_(service_, boost::posix_time::seconds(timer_interval_seconds_)),
reconnect_value_(1),
disconnect_(false),
is_connection_(false)
{
}
const char* MySQLFoundationWrapper::getHost() const
{
return host_.c_str();
}
const char* MySQLFoundationWrapper::getUser() const
{
return user_.c_str();
}
const char* MySQLFoundationWrapper::getPassword() const
{
return password_.c_str();
}
unsigned int MySQLFoundationWrapper::getPort() const
{
return port_;
}
MYSQL* MySQLFoundationWrapper::getMySQL()
{
return const_cast<MYSQL*>(static_cast<const MySQLFoundationWrapper*>(this)->getMySQL());
}
const MYSQL* MySQLFoundationWrapper::getMySQL() const
{
return static_cast<const MYSQL*>(&mysql_);
}
MySQLErrInfo MySQLFoundationWrapper::connect()
{
MySQLErrInfo err_;
mysql_init(&mysql_);
int ret = mysql_options(&mysql_, MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&timeout_seconds_);
if (ret){
err_.error_no_ = mysql_errno(&mysql_);
err_.error_info_ = mysql_error(&mysql_);
return err_;
}
MYSQL* sql_ = mysql_real_connect(&mysql_, host_.c_str(), user_.c_str(), password_.c_str(), "", port_, NULL, NULL);
if (sql_ == NULL){
err_.error_no_ = mysql_errno(&mysql_);
err_.error_info_ = mysql_error(&mysql_);
}
else{
mysql_options(&mysql_, MYSQL_OPT_RECONNECT, &reconnect_value_);
}
return err_;
}
MySQLErrInfo MySQLFoundationWrapper::start()
{
MySQLErrInfo err_ = connect();
if(err_.error_no_ == 0)
{
timer_.async_wait(boost::bind(&MySQLFoundationWrapper::ping_timer_callback, this, boost::asio::placeholders::error));
if(ios_running_thread_.get() == NULL){
ios_running_thread_.reset(new boost::thread(boost::bind(&boost::asio::io_service::run, &service_)));
}
is_connection_ = true;
}
return err_;
}
void MySQLFoundationWrapper::ping_timer_callback(const boost::system::error_code& error_code_)
{
if(disconnect_){}
else
{
mysql_thread_init();
int ping_ = mysql_ping(&mysql_);
if(ping_ == 0){}
else{
is_connection_ = false;
MySQLErrInfo err_ = connect();
if(err_.error_no_ == 0)
is_connection_ = true;
}
mysql_thread_end();
timer_.expires_from_now(boost::posix_time::seconds(timer_interval_seconds_));
timer_.async_wait(boost::bind(&MySQLFoundationWrapper::ping_timer_callback, this, boost::asio::placeholders::error));
}
}
void MySQLFoundationWrapper::end()
{
disconnect_ = true;
}
void MySQLFoundationWrapper::wait()
{
if(ios_running_thread_.get() == NULL){}
else{
ios_running_thread_->join();
ios_running_thread_.reset();
mysql_close(&mysql_);
is_connection_ = false;
}
}
MySQLFoundationWrapper::~MySQLFoundationWrapper()
{
mysql_close(&mysql_);
}