Skip to content

Commit adf2f6a

Browse files
committed
Adding support for connecting through unix sockets
1 parent 87778e9 commit adf2f6a

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

src/connection.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ node_db_mysql::Connection::~Connection() {
1717
}
1818
}
1919

20+
void node_db_mysql::Connection::setSocket(const std::string& socket) {
21+
this->socket = socket;
22+
}
23+
24+
std::string node_db_mysql::Connection::getSocket() const {
25+
return this->socket;
26+
}
27+
2028
void node_db_mysql::Connection::open() throw(node_db::Exception&) {
2129
this->close();
2230

@@ -27,7 +35,7 @@ void node_db_mysql::Connection::open() throw(node_db::Exception&) {
2735
this->password.c_str(),
2836
this->database.c_str(),
2937
this->port,
30-
NULL,
38+
!this->socket.empty() ? this->socket.c_str() : NULL,
3139
0);
3240
if (!this->opened) {
3341
throw node_db::Exception(mysql_error(this->connection));

src/connection.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ class Connection : public node_db::Connection {
1212
public:
1313
Connection();
1414
~Connection();
15+
std::string getSocket() const;
16+
void setSocket(const std::string& user);
1517
void open() throw(node_db::Exception&);
1618
void close();
1719
std::string escape(const std::string& string) const throw(node_db::Exception&);
1820
std::string version() const;
1921
node_db::Result* query(const std::string& query) const throw(node_db::Exception&);
2022

23+
protected:
24+
std::string socket;
25+
2126
private:
2227
MYSQL* connection;
2328
};

src/mysql.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ v8::Handle<v8::Value> node_db_mysql::Mysql::New(const v8::Arguments& args) {
4848
return scope.Close(args.This());
4949
}
5050

51+
v8::Handle<v8::Value> node_db_mysql::Mysql::set(const v8::Arguments& args) {
52+
v8::Handle<v8::Value> result = node_db::Binding::set(args);
53+
54+
v8::Local<v8::Object> options = args[0]->ToObject();
55+
ARG_CHECK_OBJECT_ATTR_OPTIONAL_STRING(options, socket);
56+
57+
node_db_mysql::Connection* connection = static_cast<node_db_mysql::Connection*>(this->connection);
58+
59+
if (options->Has(socket_key)) {
60+
v8::String::Utf8Value socket(options->Get(socket_key)->ToString());
61+
connection->setSocket(*socket);
62+
}
63+
64+
return result;
65+
}
66+
5167
v8::Persistent<v8::Object> node_db_mysql::Mysql::createQuery() const {
5268
v8::Persistent<v8::Object> query(
5369
node_db_mysql::Query::constructorTemplate->GetFunction()->NewInstance());

src/mysql.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Mysql : public node_db::Binding {
1818
Mysql();
1919
~Mysql();
2020
static v8::Handle<v8::Value> New(const v8::Arguments& args);
21+
v8::Handle<v8::Value> set(const v8::Arguments& args);
2122
v8::Persistent<v8::Object> createQuery() const;
2223
};
2324
}

0 commit comments

Comments
 (0)