shadowm

Untitled

Apr 20th, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. template<typename T> T fetch_result_long_internal_(MYSQL_STMT* stmt, const std::string& sql)
  2. {
  3.     T result;
  4.     my_bool is_null;
  5.     MYSQL_BIND result_bind[1] = { make_bind(result, &is_null) };
  6.  
  7.     if(mysql_stmt_bind_result(stmt, result_bind) != 0)
  8.         throw sql_error(mysql_stmt_error(stmt), sql);
  9.  
  10.     BOOST_SCOPE_EXIT(&stmt) {
  11.         mysql_stmt_free_result(stmt);
  12.     } ;
  13.  
  14.     int res = mysql_stmt_fetch(stmt);
  15.     if(res == MYSQL_NO_DATA)
  16.         throw sql_error("no data returned", sql);
  17.     if(is_null)
  18.         throw sql_error("null value returned", sql);
  19.     if(res != 0)
  20.         throw sql_error(mysql_stmt_error(stmt), sql);
  21.     return result;
  22. }
  23.  
  24. template<> int fetch_result<int>(MYSQL_STMT* stmt, const std::string& sql)
  25. {
  26.     return fetch_result_long_internal_<int>(stmt, sql);
  27. }
  28.  
  29. template<> unsigned int fetch_result<unsigned int>(MYSQL_STMT* stmt, const std::string& sql)
  30. {
  31.     return fetch_result_long_internal_<unsigned int>(stmt, sql);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment