Advertisement
CFlyPGunP

AsyncSQL.h

Feb 10th, 2019
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "stdafx.h"
  4. #include "log.h"
  5.  
  6. #include <string>
  7. #include <queue>
  8. #include <vector>
  9. #include <map>
  10. #include <mysql/mysql.h>
  11. #include <mysql/errmsg.h>
  12. #include <mysql/mysqld_error.h>
  13.  
  14. #include <mutex>
  15. #include <thread>
  16. #include "Semaphore.h"
  17. #include <any>
  18. #define QUERY_MAX_LEN 8192
  19.  
  20. class CQueryInfo;
  21.  
  22. typedef struct _SQLResult
  23. {
  24.     _SQLResult()
  25.         : pSQLResult(NULL), uiNumRows(0), uiAffectedRows(0), uiInsertID(0)
  26.     {
  27.     }
  28.  
  29.     ~_SQLResult()
  30.     {
  31.         if (pSQLResult)
  32.         {
  33.             mysql_free_result(pSQLResult);
  34.             pSQLResult = NULL;
  35.         }
  36.     }
  37.  
  38.     MYSQL_RES * pSQLResult;
  39.     uint32_t        uiNumRows;
  40.     uint32_t        uiAffectedRows;
  41.     uint32_t        uiInsertID;
  42. } SQLResult;
  43.  
  44. struct SQLMsg
  45. {
  46.     SQLMsg() : m_pkSQL(NULL), iID(0), uiResultPos(0), bReturn(false), uiSQLErrno(0)
  47.     {
  48.     }
  49.  
  50.     ~SQLMsg()
  51.     {
  52.         for(auto& it : vec_pkResult)
  53.             delete it;
  54.  
  55.         vec_pkResult.clear();
  56.     }
  57.  
  58.     void Store()
  59.     {
  60.         do
  61.         {
  62.             SQLResult * pRes = new SQLResult;
  63.  
  64.             pRes->pSQLResult = mysql_store_result(m_pkSQL);
  65.             pRes->uiInsertID = mysql_insert_id(m_pkSQL);
  66.             pRes->uiAffectedRows = mysql_affected_rows(m_pkSQL);
  67.  
  68.             if (pRes->pSQLResult)
  69.             {
  70.                 pRes->uiNumRows = mysql_num_rows(pRes->pSQLResult);
  71.             }
  72.             else
  73.             {
  74.                 pRes->uiNumRows = 0;
  75.             }
  76.  
  77.             vec_pkResult.push_back(pRes);
  78.         } while (!mysql_next_result(m_pkSQL));
  79.     }
  80.  
  81.     SQLResult * Get()
  82.     {
  83.         if (uiResultPos >= vec_pkResult.size())
  84.             return NULL;
  85.  
  86.         return vec_pkResult[uiResultPos];
  87.     }
  88.  
  89.     bool Next()
  90.     {
  91.         if (uiResultPos + 1 >= vec_pkResult.size())
  92.             return false;
  93.  
  94.         ++uiResultPos;
  95.         return true;
  96.     }
  97.  
  98.    
  99.     MYSQL *         m_pkSQL;
  100.     int             iID;
  101.     std::string         stQuery;
  102.  
  103.     std::vector<SQLResult *>    vec_pkResult;   // result ����
  104.     unsigned int        uiResultPos;    // ���� result ��ġ
  105.  
  106.     void *          pvUserData;
  107.     bool            bReturn;
  108.  
  109.     unsigned int        uiSQLErrno;
  110. };
  111.  
  112. class CAsyncSQL
  113. {
  114.     public:
  115.         CAsyncSQL();
  116.         virtual ~CAsyncSQL();
  117.  
  118.         void        Quit();
  119.  
  120.         bool        Setup(const char * c_pszHost, const char * c_pszUser, const char * c_pszPassword, const char * c_pszDB, const char * c_pszLocale,
  121.             bool bNoThread = false, int iPort = 0);
  122.         bool        Setup(CAsyncSQL * sql, bool bNoThread = false);
  123.  
  124.         bool        Connect();
  125.         bool        IsConnected() { return m_bConnected; }
  126.         bool        QueryLocaleSet();
  127.  
  128.         void        AsyncQuery(const char * c_pszQuery);
  129.         void        ReturnQuery(const char * c_pszQuery, void*  pvUserData);
  130.         SQLMsg *    DirectQuery(const char * c_pszQuery);
  131.  
  132.         DWORD       CountQuery();
  133.         DWORD       CountResult();
  134.  
  135.         void        PushResult(SQLMsg * p);
  136.         bool        PopResult(SQLMsg ** pp);
  137.  
  138.         void        ChildLoop();
  139.  
  140.         MYSQL *     GetSQLHandle();
  141.  
  142.         int         CountQueryFinished();
  143.         void        ResetQueryFinished();
  144.  
  145.         size_t      EscapeString(char* dst, size_t dstSize, const char *src, size_t srcSize);
  146.  
  147.     protected:
  148.         void        Destroy();
  149.  
  150.         void        PushQuery(SQLMsg * p);
  151.  
  152.         bool        PeekQuery(SQLMsg ** pp);
  153.         void        PopQuery(int iID);
  154.  
  155.         bool        PeekQueryFromCopyQueue(SQLMsg ** pp );
  156.         INT         CopyQuery();
  157.         void        PopQueryFromCopyQueue();
  158.  
  159.     public:
  160.         int         GetCopiedQueryCount();
  161.         void        ResetCopiedQueryCount();
  162.         void        AddCopiedQueryCount( int iCopiedQuery );
  163.  
  164.     protected:
  165.         MYSQL m_hDB;
  166.  
  167.         std::string m_stHost;
  168.         std::string m_stUser;
  169.         std::string m_stPassword;
  170.         std::string m_stDB;
  171.         std::string m_stLocale;
  172.  
  173.         int m_iMsgCount;
  174.         int m_aiPipe[2];
  175.         int m_iPort;
  176.  
  177.         std::queue<SQLMsg *> m_queue_query;
  178.         std::queue<SQLMsg *> m_queue_query_copy;
  179.  
  180.         std::queue<SQLMsg *> m_queue_result;
  181.  
  182.         volatile bool m_bEnd;
  183.  
  184.         std::thread* m_pThread;
  185.         std::mutex m_mtxQuery;
  186.         std::mutex m_mtxResult;
  187.  
  188.         CSemaphore m_sem;
  189.  
  190.         int m_iQueryFinished;
  191.  
  192.         unsigned long m_ulThreadID;
  193.         bool m_bConnected;
  194.         int m_iCopiedQuery;
  195. };
  196.  
  197. class CAsyncSQL2 : public CAsyncSQL
  198. {
  199.     public:
  200.         void SetLocale ( const std::string & stLocale );
  201. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement