2 #include <samchon/API.hpp> 4 #include <samchon/library/base/SQLiBase.hpp> 6 #include <samchon/library/SQLStatement.hpp> 9 #include <samchon/library/StringUtil.hpp> 42 class SQLi :
public base::SQLiBase
83 SQLi(
const std::string &driver,
int port)
108 return std::shared_ptr<SQLStatement>(
new SQLStatement(
this));
123 const std::string &ip,
const std::string &db,
124 const std::string &
id,
const std::string &pwd
127 std::unique_lock<std::mutex> uk(stmtMutex);
129 SQLHANDLE environment;
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)
135 SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)3, NULL);
137 res = SQLDriverConnectA
142 "DRIVER={0};SERVER={1}, {2};DATABASE={3};UID={4};PWD={5};",
143 driver, ip, port, db,
id, pwd
145 SQL_NTS, NULL, 1024, NULL, SQL_DRIVER_NOPROMPT
149 SQLFreeHandle(SQL_HANDLE_DBC, environment);
151 if (res != SQL_SUCCESS && res != SQL_SUCCESS_WITH_INFO)
166 std::unique_lock<std::mutex> uk(stmtMutex);
169 SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
175 auto isConnected()
const ->
bool 177 if (connected ==
false)
180 unsigned int ret = SQL_CD_FALSE;
181 SQLGetConnectAttr(hdbc, SQL_COPT_SS_CONNECTION_DEAD, &ret, SQL_IS_UINTEGER, NULL);
183 return ret != SQL_CD_TRUE;
197 if (SQLGetDiagRecA(type, hdbc, 1, (SQLCHAR *)state, NULL, (SQLCHAR *)message, 1024, NULL) == SQL_SUCCESS)
virtual auto getErrorMessage(short type) const -> std::string
Get error message.
SQLi(const std::string &driver, int port)
Construct from driver name and port.
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...
std::string driver
Driver; name of DBMS used for connection.
int port
Port number of DBMS.
A SQL interface; DBMS connector.
virtual void disconnect()
Disconnect from DBMS.
virtual auto createStatement() -> std::shared_ptr< SQLStatement >
Factory method for creating SQLStatement.
bool connected
Had connected to DBMS.
virtual void connect(const std::string &ip, const std::string &db, const std::string &id, const std::string &pwd)
Connect to the DBMS .