2 #include <samchon/API.hpp> 4 #include <samchon/library/base/SQLiBase.hpp> 10 #include <samchon/ByteArray.hpp> 11 #include <samchon/HashMap.hpp> 12 #include <samchon/library/XML.hpp> 51 typedef long long SQL_SIZE_T;
53 typedef long SQL_SIZE_T;
101 bindParameterCount = 0;
103 SQLAllocHandle(SQL_HANDLE_STMT, ((base::SQLiBase*)sqli)->hdbc, &hstmt);
113 void reset(
SQLi *sqli)
118 SQLAllocHandle(SQL_HANDLE_STMT, ((base::SQLiBase*)sqli)->hdbc, &hstmt);
126 SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
128 bindParameterCount = 0;
130 if (sqli ==
nullptr || ((base::SQLiBase*)sqli)->stmt !=
this)
133 ((base::SQLiBase*)sqli)->stmt =
nullptr;
134 ((base::SQLiBase*)sqli)->stmtMutex.unlock();
148 void prepare(
const std::string &sql)
152 ((base::SQLiBase*)sqli)->stmtMutex.lock();
153 ((base::SQLiBase*)sqli)->stmt =
this;
155 SQLPrepareA(hstmt, (SQLCHAR*)&sql[0], SQL_NTS);
167 template <
typename T,
typename ... _Args>
168 void prepare(
const std::string &sql,
const T& val,
const _Args& ... args)
175 template <
typename T>
void prepare(
const std::string &str,
const T& val)
182 void prepare(
const std::wstring &sql)
186 ((base::SQLiBase*)sqli)->stmtMutex.lock();
187 ((base::SQLiBase*)sqli)->stmt =
this;
189 SQLPrepareW(hstmt, (SQLWCHAR*)&sql[0], SQL_NTS);
192 template <
typename T,
typename ... _Args>
193 void prepare(
const std::wstring &sql,
const T& val,
const _Args& ... args)
200 template <
typename T>
void prepare(
const std::wstring &str,
const T& val)
214 SQLRETURN res = SQLExecute(hstmt);
216 if (res == SQL_ERROR)
217 throw std::exception(((base::SQLiBase*)sqli)->getErrorMessage(SQL_HANDLE_STMT).data());
264 SQLSMALLINT colSize = 0;
265 SQLNumResultCols(hstmt, &colSize);
269 }
while (
next() ==
true);
271 return (SQLFetch(hstmt) == SQL_SUCCESS);
281 return (SQLMoreResults(hstmt) != SQL_NO_DATA);
297 SQLNumResultCols(hstmt, &val);
308 template <
typename T>
auto at(
size_t index)
const -> T
311 sql_get_data(index + 1, C_TYPE(T()), &val);
315 template<>
auto at(
size_t index)
const -> std::string
319 std::string str(1, NULL);
322 if (::SQLGetData(hstmt, (SQLUSMALLINT)index, SQL_C_CHAR, &str[0], 0, &size) != SQL_SUCCESS && size != 0)
327 str.assign(size, NULL);
328 ::SQLGetData(hstmt, (SQLUSMALLINT)index, SQL_C_CHAR, &str[0],
sizeof(
char)*size, NULL);
334 template<>
auto at(
size_t index)
const -> std::wstring
338 std::wstring str(1, NULL);
341 if (::SQLGetData(hstmt, (SQLUSMALLINT)index, SQL_C_WCHAR, &str[0], 0, &size) != SQL_SUCCESS && size != 0)
346 str.assign((
size_t)size, NULL);
347 ::SQLGetData(hstmt, (SQLUSMALLINT)index, SQL_C_WCHAR, &str[0],
sizeof(
wchar_t)*size, NULL);
353 template<>
auto at(
size_t index)
const ->
ByteArray 360 if (::SQLGetData(hstmt, (SQLUSMALLINT)index, SQL_C_BINARY, &data[0], 0, &size) != SQL_SUCCESS && size != 0)
362 data.assign(size, NULL);
363 ::SQLGetData(hstmt, (SQLUSMALLINT)index, SQL_C_BINARY, &data[0], data.size(), NULL);
395 template <
typename T,
typename ... _Args>
403 sql_bind_parameter(C_TYPE(val), SQL_TYPE(val), (
void*)&val);
407 ::SQLBindParameter(hstmt, (SQLUSMALLINT)++bindParameterCount, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, val.size(), 0, (SQLPOINTER)val.data(), val.size(), NULL);
411 ::SQLBindParameter(hstmt, (SQLUSMALLINT)++bindParameterCount, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_WVARCHAR, val.size(), 0, (SQLPOINTER)val.data(), val.size(), NULL);
415 bindParameterBASizeMap.
set(++bindParameterCount, (SQL_SIZE_T)val.size());
417 ::SQLBindParameter(hstmt, (SQLUSMALLINT)bindParameterCount, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_LONGVARBINARY, val.size(), 0, (
unsigned char*)&val[0], 0, &bindParameterBASizeMap.
get(bindParameterCount));
423 void sql_get_data(
size_t index,
short type,
void *listener)
const 425 ::SQLGetData(hstmt, (SQLUSMALLINT)index, type, listener, 0,
nullptr);
427 void sql_bind_parameter(
short cppType,
short sqlType,
void *val)
429 ::SQLBindParameter(hstmt, (SQLUSMALLINT)++bindParameterCount, SQL_PARAM_INPUT, cppType, sqlType, 0, 0, val, 0,
nullptr);
432 template <
typename T>
auto C_TYPE(
const T &)
const -> short;
433 template<>
auto C_TYPE(
const bool &)
const ->
short 437 template<>
auto C_TYPE(
const char &)
const ->
short 441 template<>
auto C_TYPE(
const short &)
const ->
short 445 template<>
auto C_TYPE(
const long &)
const ->
short 449 template<>
auto C_TYPE(
const long long &)
const ->
short 451 return SQL_C_SBIGINT;
453 template<>
auto C_TYPE(
const int &)
const ->
short 457 template<>
auto C_TYPE(
const float &)
const ->
short 461 template<>
auto C_TYPE(
const double &)
const ->
short 466 template<>
auto C_TYPE(
const unsigned char &)
const ->
short 470 template<>
auto C_TYPE(
const unsigned short &)
const ->
short 474 template<>
auto C_TYPE(
const unsigned long &)
const ->
short 478 template<>
auto C_TYPE(
const unsigned long long &)
const ->
short 480 return SQL_C_UBIGINT;
482 template<>
auto C_TYPE(
const unsigned int &)
const ->
short 484 return SQL_C_UBIGINT;
486 template<>
auto C_TYPE(
const long double &)
const ->
short 491 template<>
auto C_TYPE(
const std::string &)
const ->
short 495 template<>
auto C_TYPE(
const std::wstring &)
const ->
short 499 template<>
auto C_TYPE(
const ByteArray &)
const ->
short 504 template <
typename T>
auto SQL_TYPE(
const T &)
const -> short;
505 template<>
auto SQL_TYPE(
const bool &)
const ->
short 509 template<>
auto SQL_TYPE(
const char &)
const ->
short 513 template<>
auto SQL_TYPE(
const short &)
const ->
short 517 template<>
auto SQL_TYPE(
const long &)
const ->
short 521 template<>
auto SQL_TYPE(
const long long &)
const ->
short 525 template<>
auto SQL_TYPE(
const int &)
const ->
short 529 template<>
auto SQL_TYPE(
const float &)
const ->
short 533 template<>
auto SQL_TYPE(
const double &)
const ->
short 538 template<>
auto SQL_TYPE(
const unsigned char &)
const ->
short 542 template<>
auto SQL_TYPE(
const unsigned short &)
const ->
short 546 template<>
auto SQL_TYPE(
const unsigned long &)
const ->
short 550 template<>
auto SQL_TYPE(
const unsigned long long &)
const ->
short 554 template<>
auto SQL_TYPE(
const unsigned int &)
const ->
short 558 template<>
auto SQL_TYPE(
const long double &)
const ->
short 563 template<>
auto SQL_TYPE(
const std::string &)
const ->
short 567 template<>
auto SQL_TYPE(
const std::wstring &)
const ->
short 571 template<>
auto SQL_TYPE(
const ByteArray &)
const ->
short void execute()
Execute the prepared sql statement.
void * hstmt
Handler of sql statement (OBDC)
size_t bindParameterCount
Count of binded parameters .
auto at(size_t index) const -> T
Get column's data by its index.
auto next() const -> bool
Move cursor to the next sql-statement.
auto size() const -> size_t
Get size of columns.
void free()
Free the sql statement.
SQLi * sqli
SQLi who created the SQLStatement.
void bindParameter(const T &val, const _Args &...args)
Get a column data by its name.
void refresh()
Refresh the sql statement.
auto get(const Key &key) -> T &
Get element.
auto fetch() const -> bool
Fetch a record.
HashMap< size_t, SQL_SIZE_T > bindParameterBASizeMap
A map for binary size.
SQLStatement(SQLi *sqli)
Protected Constructor.
A SQL interface; DBMS connector.
void executeDirectly(const std::string &sql)
Execute sql-statement direclty.
void set(const Key &key, const T &val)
Set element.
void prepare(const std::string &sql, const T &val, const _Args &...args)
Prepare a sql statement.