Skip to content

Commit 0db18e5

Browse files
committed
Added new optional connection options: charset, compress, initCommand, readTimeout, reconnect, sslVerifyServer, timeout, writeTimeout
1 parent 270d915 commit 0db18e5

File tree

3 files changed

+126
-6
lines changed

3 files changed

+126
-6
lines changed

src/connection.cc

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
#include "./connection.h"
33

44
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) {
612
this->connection = new MYSQL();
713
if (this->connection == NULL) {
814
throw node_db::Exception("Cannot create MYSQL handle");
@@ -17,17 +23,67 @@ node_db_mysql::Connection::~Connection() {
1723
}
1824
}
1925

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() {
2143
this->socket = socket;
2244
}
2345

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;
2652
}
2753

2854
void node_db_mysql::Connection::open() throw(node_db::Exception&) {
2955
this->close();
3056

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+
3187
this->opened = mysql_real_connect(
3288
this->connection,
3389
this->hostname.c_str(),
@@ -37,6 +93,12 @@ void node_db_mysql::Connection::open() throw(node_db::Exception&) {
3793
this->port,
3894
!this->socket.empty() ? this->socket.c_str() : NULL,
3995
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+
40102
if (!this->opened) {
41103
throw node_db::Exception(mysql_error(this->connection));
42104
}

src/connection.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,31 @@ class Connection : public node_db::Connection {
1212
public:
1313
Connection();
1414
~Connection();
15-
std::string getSocket() const;
16-
void setSocket(const std::string& user);
15+
void setCharset(const std::string& charset) throw();
16+
void setCompress(const bool compress) throw();
17+
void setInitCommand(const std::string& initCommand) throw();
18+
void setReadTimeout(const uint32_t readTimeout) throw();
19+
void setReconnect(const bool reconnect) throw();
20+
void setSocket(const std::string& socket) throw();
21+
void setSslVerifyServer(const bool sslVerifyServer) throw();
22+
void setTimeout(const uint32_t timeout) throw();
23+
void setWriteTimeout(const uint32_t writeTimeout) throw();
1724
void open() throw(node_db::Exception&);
1825
void close();
1926
std::string escape(const std::string& string) const throw(node_db::Exception&);
2027
std::string version() const;
2128
node_db::Result* query(const std::string& query) const throw(node_db::Exception&);
2229

2330
protected:
31+
std::string charset;
32+
bool compress;
33+
std::string initCommand;
34+
uint32_t readTimeout;
35+
bool reconnect;
2436
std::string socket;
37+
bool sslVerifyServer;
38+
uint32_t timeout;
39+
uint32_t writeTimeout;
2540

2641
private:
2742
MYSQL* connection;

src/mysql.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,58 @@ v8::Handle<v8::Value> node_db_mysql::Mysql::set(const v8::Arguments& args) {
5252
v8::Handle<v8::Value> result = node_db::Binding::set(args);
5353

5454
v8::Local<v8::Object> options = args[0]->ToObject();
55+
56+
ARG_CHECK_OBJECT_ATTR_OPTIONAL_STRING(options, charset);
57+
ARG_CHECK_OBJECT_ATTR_OPTIONAL_BOOL(options, compress);
58+
ARG_CHECK_OBJECT_ATTR_OPTIONAL_STRING(options, initCommand);
59+
ARG_CHECK_OBJECT_ATTR_OPTIONAL_UINT32(options, readTimeout);
60+
ARG_CHECK_OBJECT_ATTR_OPTIONAL_BOOL(options, reconnect);
5561
ARG_CHECK_OBJECT_ATTR_OPTIONAL_STRING(options, socket);
62+
ARG_CHECK_OBJECT_ATTR_OPTIONAL_BOOL(options, sslVerifyServer);
63+
ARG_CHECK_OBJECT_ATTR_OPTIONAL_UINT32(options, timeout);
64+
ARG_CHECK_OBJECT_ATTR_OPTIONAL_UINT32(options, writeTimeout);
5665

5766
node_db_mysql::Connection* connection = static_cast<node_db_mysql::Connection*>(this->connection);
5867

68+
if (options->Has(charset_key)) {
69+
v8::String::Utf8Value charset(options->Get(charset_key)->ToString());
70+
connection->setCharset(*charset);
71+
}
72+
73+
if (options->Has(compress_key)) {
74+
connection->setCompress(options->Get(compress_key)->IsTrue());
75+
}
76+
77+
if (options->Has(initCommand_key)) {
78+
v8::String::Utf8Value initCommand(options->Get(initCommand_key)->ToString());
79+
connection->setInitCommand(*initCommand);
80+
}
81+
82+
if (options->Has(readTimeout_key)) {
83+
connection->setReadTimeout(options->Get(readTimeout_key)->ToInt32()->Value());
84+
}
85+
86+
if (options->Has(reconnect_key)) {
87+
connection->setReconnect(options->Get(reconnect_key)->IsTrue());
88+
}
89+
5990
if (options->Has(socket_key)) {
6091
v8::String::Utf8Value socket(options->Get(socket_key)->ToString());
6192
connection->setSocket(*socket);
6293
}
6394

95+
if (options->Has(sslVerifyServer_key)) {
96+
connection->setSslVerifyServer(options->Get(sslVerifyServer_key)->IsTrue());
97+
}
98+
99+
if (options->Has(timeout_key)) {
100+
connection->setTimeout(options->Get(timeout_key)->ToInt32()->Value());
101+
}
102+
103+
if (options->Has(writeTimeout_key)) {
104+
connection->setWriteTimeout(options->Get(writeTimeout_key)->ToInt32()->Value());
105+
}
106+
64107
return result;
65108
}
66109

0 commit comments

Comments
 (0)