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.

162 lines
4.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "utlvector.h"
  8. extern "C"
  9. {
  10. #include <WinSock.H>
  11. #include "mysql.h"
  12. };
  13. #include "utlvector.h"
  14. typedef enum_field_types ESQLFieldType;
  15. //-----------------------------------------------------------------------------
  16. // Purpose: helper interface for running queries internal to this dll
  17. //-----------------------------------------------------------------------------
  18. class ISQLHelper
  19. {
  20. public:
  21. // run a sql query on the db
  22. virtual bool BInternalQuery( const char *pchQueryString, MYSQL_RES **ppMySQLRes, bool bRecurse = true ) = 0;
  23. };
  24. //-----------------------------------------------------------------------------
  25. // Purpose: represents the data about a single SQL column
  26. //-----------------------------------------------------------------------------
  27. class CSQLColumn
  28. {
  29. public:
  30. CSQLColumn() { m_rgchName[0] = 0; }
  31. CSQLColumn( const char *pchName, ESQLFieldType eSQLFieldType );
  32. virtual const char *PchColumnName() const { return m_rgchName; };
  33. virtual EColumnType GetEColumnType() const { return m_EColumnType; };
  34. virtual ESQLFieldType GetESQLFieldType() const { return m_ESQLFieldType; };
  35. private:
  36. char m_rgchName[64];
  37. EColumnType m_EColumnType;
  38. ESQLFieldType m_ESQLFieldType;
  39. };
  40. //-----------------------------------------------------------------------------
  41. // Purpose: encapsulates a table's description
  42. //-----------------------------------------------------------------------------
  43. class CSQLTable : public ISQLTable
  44. {
  45. public:
  46. CSQLTable( const char *pchName );
  47. CSQLTable( CSQLTable const &CSQLTable ); // copy constructor
  48. void AddColumn( const char *pchName, ESQLFieldType ESQLFieldType );
  49. const char *PchName() const { return m_rgchName; };
  50. virtual int GetCSQLColumn() const { return m_VecSQLColumn.Count(); };
  51. virtual const char *PchColumnName( int iSQLColumn ) const { return m_VecSQLColumn[iSQLColumn].PchColumnName(); };
  52. virtual EColumnType GetEColumnType( int iSQLColumn ) const { return m_VecSQLColumn[iSQLColumn].GetEColumnType(); };
  53. virtual ESQLFieldType GetESQLFieldType( int iSQLColumn ) const { return m_VecSQLColumn[iSQLColumn].GetESQLFieldType(); };
  54. void Reset() { m_VecSQLColumn.RemoveAll(); }
  55. #ifdef DBGFLAG_VALIDATE
  56. void Validate( CValidator &validator, char *pchName );
  57. #endif
  58. private:
  59. char m_rgchName[64];
  60. CUtlVector<CSQLColumn> m_VecSQLColumn;
  61. };
  62. //-----------------------------------------------------------------------------
  63. // Purpose: encapsulates a db's worth of tables
  64. //-----------------------------------------------------------------------------
  65. class CSQLTableSet : public ISQLTableSet
  66. {
  67. public:
  68. CSQLTableSet() { m_bInit = false; }
  69. bool Init( ISQLHelper *pISQLHelper );
  70. bool BInit() { return m_bInit; }
  71. virtual int GetCSQLTable() const { return m_VecSQLTable.Count(); };
  72. virtual const ISQLTable *PSQLTable( int iSQLTable ) const;
  73. #ifdef DBGFLAG_VALIDATE
  74. void Validate( CValidator &validator, char *pchName );
  75. #endif
  76. private:
  77. static EColumnType EColumnTypeFromPchName( const char *pchName );
  78. CUtlVector<CSQLTable> m_VecSQLTable;
  79. bool m_bInit;
  80. };
  81. //-----------------------------------------------------------------------------
  82. // Purpose: describes a single row in a result set
  83. //-----------------------------------------------------------------------------
  84. class CSQLRow : public ISQLRow
  85. {
  86. public:
  87. CSQLRow( MYSQL_ROW *pMySQLRow, const ISQLTable *pSQLTableDescription, int cSQLRowData );
  88. ~CSQLRow();
  89. virtual int GetCSQLRowData() const { return m_VecSQLRowData.Count(); };
  90. virtual const char *PchData( int iSQLRowData ) const;
  91. virtual int NData( int iSQLRowData ) const;
  92. virtual uint64 UlData( int iSQLRowData ) const;
  93. virtual float FlData( int iSQLRowData ) const;
  94. virtual uint64 UlTime( int iSQLRowData ) const;
  95. virtual bool BData( int iSQLRowData ) const;
  96. virtual EColumnType GetEColumnType( int iSQLRowData ) const;
  97. #ifdef DBGFLAG_VALIDATE
  98. void Validate( CValidator &validator, char *pchName );
  99. #endif
  100. private:
  101. struct SQLRowData_s
  102. {
  103. union
  104. {
  105. int nData;
  106. float flData;
  107. const char *pchData;
  108. uint64 ulTime;
  109. uint64 ulData;
  110. } data;
  111. EColumnType eColumnType;
  112. };
  113. CUtlVector<struct SQLRowData_s> m_VecSQLRowData;
  114. CUtlVector<char *> m_VecPchStoredStrings;
  115. };
  116. //-----------------------------------------------------------------------------
  117. // Purpose: encapsulates a result set from a SQL query
  118. //-----------------------------------------------------------------------------
  119. class CResultSet : public IResultSet
  120. {
  121. public:
  122. CResultSet();
  123. ~CResultSet();
  124. bool Query( const char *pchQuery, ISQLHelper *pISQLHelper );
  125. virtual int GetCSQLRow() const;
  126. virtual const ISQLRow *PSQLRowNextResult();
  127. void FreeResult();
  128. #ifdef DBGFLAG_VALIDATE
  129. void Validate( CValidator &validator, char *pchName );
  130. #endif
  131. private:
  132. MYSQL_RES *m_MySQLRes;
  133. CSQLRow *m_pSQLRow;
  134. CSQLTable m_SQLTableDescription;
  135. int m_cSQLField;
  136. int m_cSQLRow;
  137. };