Advertisement
Guest User

Untitled

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