Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #ifndef GCSQLQUERY_H
  8. #define GCSQLQUERY_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "gamecoordinator/igcsqlquery.h"
  13. #include "refcount.h"
  14. #include "bufferpool.h"
  15. namespace GCSDK
  16. {
  17. struct GCSQLBindParam_t
  18. {
  19. EGCSQLType m_eType;
  20. size_t m_nOffset;
  21. size_t m_cubData;
  22. };
  23. class CGCSQLQuery
  24. {
  25. DECLARE_CLASS_MEMPOOL( CGCSQLQuery );
  26. public:
  27. CGCSQLQuery();
  28. virtual ~CGCSQLQuery();
  29. static CBufferPool &GetBufferPool();
  30. void SetCommand( const char *pchCommand ) { m_sCommand = pchCommand; }
  31. void AddBindParam( const char *pchValue )
  32. {
  33. AddBindParamRaw( k_EGCSQLType_String, (byte *)pchValue, Q_strlen( pchValue ) );
  34. }
  35. void AddBindParam( const int16 nValue )
  36. {
  37. AddBindParamRaw( k_EGCSQLType_int16, (byte *)&nValue, sizeof( nValue ) );
  38. }
  39. void AddBindParam( const uint16 uValue )
  40. {
  41. AddBindParamRaw( k_EGCSQLType_int16, (byte *)&uValue, sizeof( uValue ) );
  42. }
  43. void AddBindParam( const int32 nValue )
  44. {
  45. AddBindParamRaw( k_EGCSQLType_int32, (byte *)&nValue, sizeof( nValue ) );
  46. }
  47. void AddBindParam( const uint32 uValue )
  48. {
  49. AddBindParamRaw( k_EGCSQLType_int32, (byte *)&uValue, sizeof( uValue ) );
  50. }
  51. void AddBindParam( const uint64 ulValue )
  52. {
  53. AddBindParamRaw( k_EGCSQLType_int64, (byte *)&ulValue, sizeof( ulValue ) );
  54. }
  55. void AddBindParam( const uint8 *ubValue, const int cubValue )
  56. {
  57. AddBindParamRaw( k_EGCSQLType_Blob, (byte *)ubValue, cubValue );
  58. }
  59. void AddBindParam( const float fValue )
  60. {
  61. AddBindParamRaw( k_EGCSQLType_float, (byte *)&fValue, sizeof ( fValue ) );
  62. }
  63. void AddBindParam( const double dValue )
  64. {
  65. AddBindParamRaw( k_EGCSQLType_double, (byte *)&dValue, sizeof ( dValue ) );
  66. }
  67. // Image needs a special type since the default var data is blob
  68. void AddBindParamImage( const uint8 *ubValue, const int cubValue )
  69. {
  70. AddBindParamRaw( k_EGCSQLType_Image, (byte *)ubValue, cubValue );
  71. }
  72. void ClearParams();
  73. // this is used internally to bind a field with its type. You probably want
  74. // some version of AddBindParam instead of this.
  75. void AddBindParamRaw( EGCSQLType eType, const byte *pubData, uint32 cubData );
  76. // ------- Interface implementation from IGCSQLQuery -----
  77. // get the null-terminated query string itself
  78. virtual const char *PchCommand() { return m_sCommand.Get(); }
  79. // gets the parameter data
  80. virtual uint32 CnParams() { return m_vecParams.Count(); }
  81. virtual EGCSQLType EParamType( uint32 uIndex ) { return m_vecParams[uIndex].m_eType; }
  82. virtual byte *PubParam( uint32 uIndex ) { return (byte *)m_pBufParams->Base() + m_vecParams[uIndex].m_nOffset; }
  83. virtual uint32 CubParam( uint32 uIndex ) { return ( uint32 )m_vecParams[uIndex].m_cubData; }
  84. private:
  85. CUtlString m_sCommand;
  86. CUtlBuffer *m_pBufParams;
  87. CUtlVectorFixedGrowable< GCSQLBindParam_t, 10 > m_vecParams;
  88. };
  89. class CGCSQLQueryGroup : public IGCSQLQuery, public CRefCount
  90. {
  91. DECLARE_CLASS_MEMPOOL( CGCSQLQuery );
  92. private:
  93. // create query groups on the heap with alloc
  94. CGCSQLQueryGroup();
  95. // destroy query groups by releasing them
  96. virtual ~CGCSQLQueryGroup();
  97. public:
  98. static CGCSQLQueryGroup *Alloc() { return new CGCSQLQueryGroup(); }
  99. void AddQuery( CGCSQLQuery *pQuery );
  100. void SetName( const char *sName );
  101. // returns the number of statements in the transaction
  102. // represented by this query object
  103. virtual uint32 GetStatementCount() { return m_vecQueries.Count(); }
  104. // returns a string that represents where in the GC this
  105. // query came from. Usually this is FILE_AND_LINE.
  106. virtual const char *PchName() { return m_sName; }
  107. // get the null-terminated query string itself
  108. virtual const char *PchCommand( uint32 unStatement ) { return m_vecQueries[unStatement]->PchCommand(); }
  109. // gets the parameter data
  110. virtual uint32 CnParams( uint32 unStatement ) { return m_vecQueries[unStatement]->CnParams(); }
  111. virtual EGCSQLType EParamType( uint32 unStatement, uint32 uIndex ) { return m_vecQueries[unStatement]->EParamType( uIndex ); }
  112. virtual byte *PubParam( uint32 unStatement, uint32 uIndex ) { return m_vecQueries[unStatement]->PubParam( uIndex ); }
  113. virtual uint32 CubParam( uint32 unStatement, uint32 uIndex ) { return m_vecQueries[unStatement]->CubParam( uIndex ); };
  114. // reports the result
  115. virtual void SetResults( IGCSQLResultSetList *pResults );
  116. IGCSQLResultSetList *GetResults() { return m_pResults; }
  117. // clears all the queries in the query group and resets its name
  118. void Clear();
  119. private:
  120. CUtlVector< CGCSQLQuery * > m_vecQueries;
  121. CUtlString m_sName;
  122. IGCSQLResultSetList *m_pResults;
  123. };
  124. } // namespace GCSDK
  125. #include "tier0/memdbgoff.h"
  126. #endif // GCSQLQUERY_H