nodejs-db-informix  master
nodejs bindings for Informix
 All Classes Functions Pages
connection.cxx
1 #include "connection.h"
2 
3 pthread_mutex_t nodejs_db::Connection::staticConnectionLock = PTHREAD_MUTEX_INITIALIZER;
4 
5 nodejs_db::Connection::Connection() :
6  quoteString('\'')
7  , alive(false)
8  , quoteName('\'')
9 {
10  this->connectionLock = &(this->staticConnectionLock);
11 }
12 
13 nodejs_db::Connection::~Connection() { }
14 
15 std::string nodejs_db::Connection::getHostname() const {
16  return this->hostname;
17 }
18 
19 void nodejs_db::Connection::setHostname(const std::string& h) {
20  this->hostname = h;
21 }
22 
23 std::string nodejs_db::Connection::getUser() const {
24  return this->user;
25 }
26 
27 void nodejs_db::Connection::setUser(const std::string& u) {
28  this->user = u;
29 }
30 
31 std::string nodejs_db::Connection::getPassword() const {
32  return this->password;
33 }
34 
35 void nodejs_db::Connection::setPassword(const std::string& pw) {
36  this->password = pw;
37 }
38 
39 std::string nodejs_db::Connection::getDatabase() const {
40  return this->database;
41 }
42 
43 void nodejs_db::Connection::setDatabase(const std::string& db) {
44  this->database = db;
45 }
46 
47 uint32_t nodejs_db::Connection::getPort() const {
48  return this->port;
49 }
50 
51 void nodejs_db::Connection::setPort(uint32_t p) {
52  this->port = p;
53 }
54 
55 bool nodejs_db::Connection::isAlive(bool ping) {
56  return this->alive;
57 }
58 
59 /**
60  * \fn std::string nodejs_db::Connection::escapeName
61  * escape the given string.
62  * e.g. first.second is escaped as 'first'.'second' is escapeChar is "'"
63  *
64  * \param[in] string String to be escaped
65  * \exception Exception&
66  */
67 std::string nodejs_db::Connection::escapeName(const std::string& string) const throw(Exception&) {
68  std::string escaped;
69  if (string.find_first_of('.') != string.npos) {
70  char* original = reinterpret_cast<char*>(const_cast<char*>(string.c_str()));
71  char* token;
72  char* rest;
73  bool first = true;
74 
75  while ((token = strtok_r(original, ".", &rest))) {
76  if (!first) {
77  escaped += '.';
78  } else {
79  first = false;
80  }
81  if (token[0] != '*') {
82  escaped += this->quoteName;
83  escaped += token;
84  escaped += this->quoteName;
85  } else {
86  escaped += token;
87  }
88  original = rest;
89  }
90  } else {
91  escaped = this->quoteName + string + this->quoteName;
92  }
93  return escaped;
94 }
95 
96 void nodejs_db::Connection::lock() {
97  pthread_mutex_lock(this->connectionLock);
98 }
99 
100 void nodejs_db::Connection::unlock() {
101  pthread_mutex_unlock(this->connectionLock);
102 }