Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #pragma once
  2. #include <cppconn\driver.h>
  3. #include <cppconn\exception.h>
  4. #include <cppconn\resultset.h>
  5. #include <cppconn\statement.h>
  6. #include <assert.h>
  7. #include <thread>
  8. #include <chrono>
  9.  
  10. namespace scratch
  11. {
  12.     //byte RETRY_COUNTER = 5;
  13.  
  14.     class Statement
  15.     {
  16.     public:
  17.         Statement::Statement(sql::Connection*& con) : _con(con)
  18.         {
  19.             _stmt = con->createStatement();
  20.         }
  21.  
  22.         Statement::~Statement()
  23.         {
  24.             delete _stmt;
  25.         }
  26.  
  27.         sql::ResultSet* read_db(std::string query)
  28.         {
  29.             try
  30.             {
  31.                 if (_con)
  32.                 {
  33.                     if (!_con->isValid())
  34.                         if (!_con->reconnect())
  35.                             return nullptr;
  36.  
  37.                     return _stmt->executeQuery(query);
  38.                 }
  39.             }
  40.             catch (sql::SQLException &e) {}
  41.             return nullptr;
  42.         }
  43.  
  44.         template <typename T>
  45.         T get_column(sql::ResultSet* res, std::string column_name) {}
  46.  
  47.         template<>
  48.         std::string get_column(sql::ResultSet* res, std::string column_name)
  49.         {
  50.             if (!res) return{};
  51.  
  52.             try
  53.             {
  54.                 if (res->next())
  55.                 {
  56.                     auto string = res->getString(column_name.c_str());
  57.                     delete res;
  58.                     return string;
  59.                 }
  60.  
  61.             }
  62.             catch (const std::exception&) {}
  63.             return{};
  64.         }
  65.  
  66.         template<>
  67.         bool get_column(sql::ResultSet* res, std::string column_name)
  68.         {
  69.             if (!res) return{};
  70.  
  71.             try
  72.             {
  73.                 if (res->next())
  74.                 {
  75.                     auto boolean = res->getBoolean(column_name.c_str());
  76.                     delete res;
  77.                     return boolean;
  78.                 }
  79.  
  80.             }
  81.             catch (const std::exception&) {}
  82.             return false;
  83.         }
  84.  
  85.         template<>
  86.         int get_column(sql::ResultSet* res, std::string column_name)
  87.         {
  88.             if (!res) return{};
  89.  
  90.             try
  91.             {
  92.                 if (res->next())
  93.                 {
  94.                     auto integer = res->getInt(column_name.c_str());
  95.                     delete res;
  96.                     return integer;
  97.                 }
  98.             }
  99.             catch (const std::exception&) {}
  100.             return 0;
  101.         }
  102.  
  103.         bool alter_db(std::string query)
  104.         {
  105.             try
  106.             {
  107.                 if (!_con->isValid())
  108.                     if (!_con->reconnect())
  109.                         return false;
  110.  
  111.                 return !_stmt->execute(query.c_str());
  112.             }
  113.             catch (sql::SQLException &e) {}
  114.             return false;
  115.         }
  116.  
  117.     private:
  118.         sql::Connection* _con;
  119.         sql::Statement* _stmt;
  120.     };
  121.  
  122.     class SQL
  123.     {
  124.     public:
  125.         SQL::SQL(std::string host_name, std::string port, std::string username, std::string password)
  126.         {
  127.             _con = get_driver_instance()->connect(std::string("tcp://").append(host_name).append(":").append(port).c_str(), username.c_str(), password.c_str());
  128.             if (!_con) return;
  129.  
  130.             _con->setSchema("scratch");
  131.             _is_initialized = _con->isValid();
  132.         }
  133.         SQL::~SQL()
  134.         {
  135.             delete _con;
  136.         }
  137.         bool is_initialized()
  138.         {
  139.             return _is_initialized;
  140.         }
  141.  
  142.         sql::Connection* get_connection()
  143.         {
  144.             return _con;
  145.         }
  146.  
  147.         Statement* get_statement()
  148.         {
  149.             return new Statement(_con);
  150.         }
  151.  
  152.     private:
  153.         bool _is_initialized = false;
  154.  
  155.         sql::Connection* _con;
  156.     };
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement