Samchon Framework for CPP  1.0.0
SQLi.hpp
1 #pragma once
2 #include <samchon/API.hpp>
3 
4 #include <samchon/library/base/SQLiBase.hpp>
5 
6 #include <samchon/library/SQLStatement.hpp>
7 
8 #include <memory>
9 #include <samchon/library/StringUtil.hpp>
10 
11 namespace samchon
12 {
13 namespace library
14 {
42  class SQLi : public base::SQLiBase
43  {
44  friend class SQLStatement;
45 
46  protected:
55  std::string driver;
56 
65  int port;
66 
74  bool connected;
75 
76  public:
83  SQLi(const std::string &driver, int port)
84  {
85  stmt = nullptr;
86  connected = false;
87 
88  this->driver = driver;
89  this->port = port;
90  };
91 
92  virtual ~SQLi()
93  {
94  disconnect();
95  };
96 
106  virtual auto createStatement() -> std::shared_ptr<SQLStatement>
107  {
108  return std::shared_ptr<SQLStatement>(new SQLStatement(this));
109  };
110 
121  virtual void connect
122  (
123  const std::string &ip, const std::string &db,
124  const std::string &id, const std::string &pwd
125  )
126  {
127  std::unique_lock<std::mutex> uk(stmtMutex);
128  SQLRETURN res;
129  SQLHANDLE environment;
130 
131  if ((res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &environment)) == SQL_SUCCESS)
132  if ((res = SQLSetEnvAttr(environment, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0)) == SQL_SUCCESS)
133  if ((res = SQLAllocHandle(SQL_HANDLE_DBC, environment, &hdbc)) == SQL_SUCCESS)
134  {
135  SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)3, NULL);
136 
137  res = SQLDriverConnectA
138  (
139  hdbc, NULL,
140  (SQLCHAR*)&StringUtil::substitute
141  (
142  "DRIVER={0};SERVER={1}, {2};DATABASE={3};UID={4};PWD={5};",
143  driver, ip, port, db, id, pwd
144  )[0],
145  SQL_NTS, NULL, 1024, NULL, SQL_DRIVER_NOPROMPT
146  );
147  }
148 
149  SQLFreeHandle(SQL_HANDLE_DBC, environment);
150 
151  if (res != SQL_SUCCESS && res != SQL_SUCCESS_WITH_INFO)
152  {
153  disconnect();
154  throw std::exception(getErrorMessage(SQL_HANDLE_DBC).c_str());
155  }
156  else
157  connected = true;
158  };
159 
163  virtual void disconnect()
164  {
165  //FOR STATIC DESTRUCTION
166  std::unique_lock<std::mutex> uk(stmtMutex);
167 
168  SQLDisconnect(hdbc);
169  SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
170  connected = false;
171 
172  stmtMutex.unlock();
173  };
174 
175  auto isConnected() const -> bool
176  {
177  if (connected == false)
178  return false;
179 
180  unsigned int ret = SQL_CD_FALSE;
181  SQLGetConnectAttr(hdbc, SQL_COPT_SS_CONNECTION_DEAD, &ret, SQL_IS_UINTEGER, NULL);
182 
183  return ret != SQL_CD_TRUE;
184  };
185 
186  protected:
192  virtual auto getErrorMessage(short type) const -> std::string
193  {
194  char state[1024];
195  char message[1024];
196 
197  if (SQLGetDiagRecA(type, hdbc, 1, (SQLCHAR *)state, NULL, (SQLCHAR *)message, 1024, NULL) == SQL_SUCCESS)
198  return message;
199  else
200  return "Unknown";
201  };
202  };
203 };
204 };
virtual auto getErrorMessage(short type) const -> std::string
Get error message.
Definition: SQLi.hpp:192
SQLi(const std::string &driver, int port)
Construct from driver name and port.
Definition: SQLi.hpp:83
static auto substitute(const std::string &format, const T &val, const _Args &...args) -> std::string
Substitutes "{n}" tokens within the specified string with the respective arguments passed in...
Definition: StringUtil.hpp:54
std::string driver
Driver; name of DBMS used for connection.
Definition: SQLi.hpp:55
int port
Port number of DBMS.
Definition: SQLi.hpp:65
A SQL interface; DBMS connector.
Definition: SQLi.hpp:42
virtual void disconnect()
Disconnect from DBMS.
Definition: SQLi.hpp:163
virtual auto createStatement() -> std::shared_ptr< SQLStatement >
Factory method for creating SQLStatement.
Definition: SQLi.hpp:106
bool connected
Had connected to DBMS.
Definition: SQLi.hpp:74
virtual void connect(const std::string &ip, const std::string &db, const std::string &id, const std::string &pwd)
Connect to the DBMS .
Definition: SQLi.hpp:122