1 #include <samchon/library/SQLStatement.hpp> 6 #include <samchon/library/SQLi.hpp> 7 #include <samchon/library/CriticalSet.hpp> 8 #include <samchon/library/XML.hpp> 10 #define WIN32_LEAN_AND_MEAN 15 #define _SQLNCLI_ODBC_ 22 SQLStatement::SQLStatement(
SQLi *sqli)
25 bindParameterCount = 0;
27 SQLAllocHandle(SQL_HANDLE_STMT, sqli->
hdbc, &hstmt);
29 SQLStatement::~SQLStatement()
34 void SQLStatement::reset(
SQLi *sqli)
39 SQLAllocHandle(SQL_HANDLE_STMT, sqli->
hdbc, &hstmt);
41 void SQLStatement::free()
43 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
45 bindParameterCount = 0;
47 if (sqli ==
nullptr || sqli->
stmt !=
this)
53 void SQLStatement::refresh()
64 void SQLStatement::prepare(
const std::string &sql)
71 SQLPrepareA(hstmt, (SQLCHAR*)&sql[0], SQL_NTS);
73 void SQLStatement::prepare(
const std::wstring &sql)
80 SQLPrepareW(hstmt, (SQLWCHAR*)&sql[0], SQL_NTS);
83 void SQLStatement::execute()
85 SQLRETURN res = SQLExecute(hstmt);
87 throw exception(sqli->getErrorMessage(SQL_HANDLE_STMT).c_str());
89 void SQLStatement::executeDirectly(
const std::string &sql)
94 void SQLStatement::executeDirectly(
const std::wstring &sql)
105 auto SQLStatement::next() const ->
bool 107 return (SQLMoreResults(hstmt) != SQL_NO_DATA);
109 auto SQLStatement::fetch() const ->
bool 113 SQLSMALLINT colSize = 0;
114 SQLNumResultCols(hstmt, &colSize);
118 }
while (next() ==
true);
120 return (SQLFetch(hstmt) == SQL_SUCCESS);
123 auto SQLStatement::size() const ->
size_t 127 SQLNumResultCols(hstmt, &val);
134 auto SQLStatement::toXML() const -> shared_ptr<
XML>
136 shared_ptr<XML> xml(
new XML());
143 template<>
void SQLStatement::bindParameter(
const std::string &val)
145 ::SQLBindParameter(hstmt, (SQLUSMALLINT)++bindParameterCount, SQL_PARAM_INPUT, SQL_C_TCHAR, SQL_VARCHAR, val.size(), 0, (
char*)&val[0], 0, NULL);
147 template<>
void SQLStatement::bindParameter(
const std::wstring &val)
149 ::SQLBindParameter(hstmt, (SQLUSMALLINT)++bindParameterCount, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_WVARCHAR, val.size(), 0, (
wchar_t*)&val[0], 0, NULL);
151 template<>
void SQLStatement::bindParameter(
const ByteArray &val)
153 bindParameterBASizeMap.set(++bindParameterCount, (SQL_SIZE_T)val.size());
155 ::SQLBindParameter(hstmt, (SQLUSMALLINT)bindParameterCount, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_LONGVARBINARY, val.size(), 0, (
unsigned char*)&val[0], 0, &bindParameterBASizeMap.get(bindParameterCount));
161 void SQLStatement::sql_get_data(
size_t index,
short type,
void *listener)
const 164 ::SQLGetData(hstmt, (SQLUSMALLINT)index, type, listener, 0,
nullptr);
166 void SQLStatement::sql_bind_parameter(
short cppType,
short sqlType,
void *val)
169 ::SQLBindParameter(hstmt, (SQLUSMALLINT)++bindParameterCount, SQL_PARAM_INPUT, cppType, sqlType, 0, 0, val, 0,
nullptr);
175 template<>
auto SQLStatement::C_TYPE(
const bool &)
const ->
short 179 template<>
auto SQLStatement::C_TYPE(
const char &)
const ->
short 183 template<>
auto SQLStatement::C_TYPE(
const short &)
const ->
short 187 template<>
auto SQLStatement::C_TYPE(
const long &)
const ->
short 191 template<>
auto SQLStatement::C_TYPE(
const long long &)
const ->
short 193 return SQL_C_SBIGINT;
195 template<>
auto SQLStatement::C_TYPE(
const int &)
const ->
short 199 template<>
auto SQLStatement::C_TYPE(
const float &)
const ->
short 203 template<>
auto SQLStatement::C_TYPE(
const double &)
const ->
short 208 template<>
auto SQLStatement::C_TYPE(
const unsigned char &)
const ->
short 212 template<>
auto SQLStatement::C_TYPE(
const unsigned short &)
const ->
short 216 template<>
auto SQLStatement::C_TYPE(
const unsigned long &)
const ->
short 220 template<>
auto SQLStatement::C_TYPE(
const unsigned long long &)
const ->
short 222 return SQL_C_UBIGINT;
224 template<>
auto SQLStatement::C_TYPE(
const unsigned int &)
const ->
short 226 return SQL_C_UBIGINT;
228 template<>
auto SQLStatement::C_TYPE(
const long double &)
const ->
short 233 template<>
auto SQLStatement::C_TYPE(
const string &)
const ->
short 237 template<>
auto SQLStatement::C_TYPE(
const wstring &)
const ->
short 241 template<>
auto SQLStatement::C_TYPE(
const ByteArray &)
const ->
short 249 template<>
auto SQLStatement::SQL_TYPE(
const bool &)
const ->
short 253 template<>
auto SQLStatement::SQL_TYPE(
const char &)
const ->
short 257 template<>
auto SQLStatement::SQL_TYPE(
const short &)
const ->
short 261 template<>
auto SQLStatement::SQL_TYPE(
const long &)
const ->
short 265 template<>
auto SQLStatement::SQL_TYPE(
const long long &)
const ->
short 269 template<>
auto SQLStatement::SQL_TYPE(
const int &)
const ->
short 273 template<>
auto SQLStatement::SQL_TYPE(
const float &)
const ->
short 277 template<>
auto SQLStatement::SQL_TYPE(
const double &)
const ->
short 282 template<>
auto SQLStatement::SQL_TYPE(
const unsigned char &)
const ->
short 286 template<>
auto SQLStatement::SQL_TYPE(
const unsigned short &)
const ->
short 290 template<>
auto SQLStatement::SQL_TYPE(
const unsigned long &)
const ->
short 294 template<>
auto SQLStatement::SQL_TYPE(
const unsigned long long &)
const ->
short 298 template<>
auto SQLStatement::SQL_TYPE(
const unsigned int &)
const ->
short 302 template<>
auto SQLStatement::SQL_TYPE(
const long double &)
const ->
short 307 template<>
auto SQLStatement::SQL_TYPE(
const string &)
const ->
short 311 template<>
auto SQLStatement::SQL_TYPE(
const wstring &)
const ->
short 315 template<>
auto SQLStatement::SQL_TYPE(
const ByteArray &)
const ->
short 323 auto SQLStatement::_atAsString(
size_t index)
const -> std::string
327 std::string str(1, NULL);
330 if (::SQLGetData(hstmt, (SQLUSMALLINT)index, SQL_C_CHAR, &str[0], 0, &size) != SQL_SUCCESS && size != 0)
332 str.assign((
size_t)size, NULL);
333 ::SQLGetData(hstmt, (SQLUSMALLINT)index, SQL_C_CHAR, &str[0],
sizeof(
char)*size, NULL);
338 for (i = str.size() - 1; i >= 0; i--)
342 if (i != str.size() - 1)
343 str.erase(str.begin() + i + 1, str.end());
347 auto SQLStatement::_atAsWString(
size_t index)
const -> std::wstring
351 std::wstring str(1, NULL);
354 if (::SQLGetData(hstmt, (SQLUSMALLINT)index, SQL_C_WCHAR, &str[0], 0, &size) != SQL_SUCCESS && size != 0)
356 str.assign((
size_t)size, NULL);
357 ::SQLGetData(hstmt, (SQLUSMALLINT)index, SQL_C_WCHAR, &str[0],
sizeof(
wchar_t)*size, NULL);
361 auto SQLStatement::_atAsByteArray(
size_t index)
const ->
ByteArray 368 if (::SQLGetData(hstmt, (SQLUSMALLINT)index, SQL_C_BINARY, &data[0], 0, &size) != SQL_SUCCESS && size != 0)
370 data.assign(size, NULL);
371 ::SQLGetData(hstmt, (SQLUSMALLINT)index, SQL_C_BINARY, &data[0], data.size(), NULL);
std::mutex stmtMutex
Mutex ensuring concurrency with SQLStatement.
void * hdbc
Handler of DB-connector.
A SQL interface; DBMS connector.
SQLStatement * stmt
SQLStatement's pointer linked with the SQLi.
XML is a class representing xml object.
Top level namespace of products built from samchon.