2
2
#include " ./connection.h"
3
3
4
4
node_db_mysql::Connection::Connection ()
5
- : connection(NULL ) {
5
+ : compress(false ),
6
+ readTimeout(0 ),
7
+ reconnect(false ),
8
+ sslVerifyServer(false ),
9
+ timeout(0 ),
10
+ writeTimeout(0 ),
11
+ connection(NULL ) {
6
12
this ->connection = new MYSQL ();
7
13
if (this ->connection == NULL ) {
8
14
throw node_db::Exception (" Cannot create MYSQL handle" );
@@ -17,17 +23,67 @@ node_db_mysql::Connection::~Connection() {
17
23
}
18
24
}
19
25
20
- void node_db_mysql::Connection::setSocket (const std::string& socket) {
26
+ void node_db_mysql::Connection::setCharset (const std::string& charset) throw() {
27
+ this ->charset = charset;
28
+ }
29
+
30
+ void node_db_mysql::Connection::setCompress (const bool compress) throw() {
31
+ this ->compress = compress;
32
+ }
33
+
34
+ void node_db_mysql::Connection::setReadTimeout (const uint32_t readTimeout) throw() {
35
+ this ->readTimeout = readTimeout;
36
+ }
37
+
38
+ void node_db_mysql::Connection::setInitCommand (const std::string& initCommand) throw() {
39
+ this ->initCommand = initCommand;
40
+ }
41
+
42
+ void node_db_mysql::Connection::setSocket (const std::string& socket) throw() {
21
43
this ->socket = socket;
22
44
}
23
45
24
- std::string node_db_mysql::Connection::getSocket () const {
25
- return this ->socket ;
46
+ void node_db_mysql::Connection::setTimeout (const uint32_t timeout) throw() {
47
+ this ->timeout = timeout;
48
+ }
49
+
50
+ void node_db_mysql::Connection::setWriteTimeout (const uint32_t writeTimeout) throw() {
51
+ this ->writeTimeout = writeTimeout;
26
52
}
27
53
28
54
void node_db_mysql::Connection::open () throw(node_db::Exception&) {
29
55
this ->close ();
30
56
57
+ if (!this ->charset .empty ()) {
58
+ mysql_options (this ->connection , MYSQL_SET_CHARSET_NAME, this ->charset .c_str ());
59
+ }
60
+
61
+ if (this ->compress ) {
62
+ mysql_options (this ->connection , MYSQL_OPT_COMPRESS, 0 );
63
+ }
64
+
65
+ if (!this ->initCommand .empty ()) {
66
+ mysql_options (this ->connection , MYSQL_INIT_COMMAND, this ->initCommand .c_str ());
67
+ }
68
+
69
+ if (this ->readTimeout > 0 ) {
70
+ mysql_options (this ->connection , MYSQL_OPT_READ_TIMEOUT, &this ->readTimeout );
71
+ }
72
+
73
+ #if MYSQL_VERSION_ID >= 50013
74
+ mysql_options (this ->connection , MYSQL_OPT_RECONNECT, &this ->reconnect );
75
+ #endif
76
+
77
+ mysql_options (this ->connection , MYSQL_OPT_SSL_VERIFY_SERVER_CERT, &this ->sslVerifyServer );
78
+
79
+ if (this ->timeout > 0 ) {
80
+ mysql_options (this ->connection , MYSQL_OPT_CONNECT_TIMEOUT, &this ->timeout );
81
+ }
82
+
83
+ if (this ->writeTimeout > 0 ) {
84
+ mysql_options (this ->connection , MYSQL_OPT_WRITE_TIMEOUT, &this ->writeTimeout );
85
+ }
86
+
31
87
this ->opened = mysql_real_connect (
32
88
this ->connection ,
33
89
this ->hostname .c_str (),
@@ -37,6 +93,12 @@ void node_db_mysql::Connection::open() throw(node_db::Exception&) {
37
93
this ->port ,
38
94
!this ->socket .empty () ? this ->socket .c_str () : NULL ,
39
95
0 );
96
+
97
+ #if MYSQL_VERSION_ID >= 50013 && MYSQL_VERSION_ID < 50019
98
+ // MySQL incorrectly resets the MYSQL_OPT_RECONNECT option to its default value before MySQL 5.0.19
99
+ mysql_options (this ->connection , MYSQL_OPT_RECONNECT, &this ->reconnect );
100
+ #endif
101
+
40
102
if (!this ->opened ) {
41
103
throw node_db::Exception (mysql_error (this ->connection ));
42
104
}
0 commit comments