1 #include <samchon/library/SQLi.hpp> 3 #ifndef WIN32_LEAN_AND_MEAN 4 # define WIN32_LEAN_AND_MEAN 16 #include <samchon/library/SQLStatement.hpp> 17 #include <samchon/library/StringUtil.hpp> 23 SQLi::SQLi(
const std::string &driver,
int port)
27 this->driver = driver;
35 auto SQLi::createStatement() -> shared_ptr<SQLStatement>
40 void SQLi::connect(
const std::string &ip,
const std::string &db,
const std::string &
id,
const std::string &pwd)
42 unique_lock<mutex> uk(stmtMutex);
44 SQLHANDLE environment;
46 if ((res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &environment)) == SQL_SUCCESS)
47 if ((res = SQLSetEnvAttr(environment, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0)) == SQL_SUCCESS)
48 if ((res = SQLAllocHandle(SQL_HANDLE_DBC, environment, &hdbc)) == SQL_SUCCESS)
54 (SQLCHAR*)&StringUtil::substitute
56 "DRIVER={0};SERVER={1}, {2};DATABASE={3};UID={4};PWD={5};",
57 driver, ip, port, db,
id, pwd
59 SQL_NTS, NULL, 1024, NULL, SQL_DRIVER_NOPROMPT
63 SQLFreeHandle(SQL_HANDLE_DBC, environment);
65 if (res != SQL_SUCCESS && res != SQL_SUCCESS_WITH_INFO)
68 throw exception(getErrorMessage(SQL_HANDLE_DBC).c_str());
71 void SQLi::disconnect()
79 SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
83 auto SQLi::getErrorMessage(
short type)
const ->
string 88 if (SQLGetDiagRecA(type, hdbc, 1, (SQLCHAR *)state, NULL, (SQLCHAR *)message, 1024, NULL) == SQL_SUCCESS)
89 return string(
"SQLSTATE: ") + state +
"\nMESSAGE: " + message;
Top level namespace of products built from samchon.