Counter Strike : Global Offensive Source Code
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.

171 lines
7.7 KiB

  1. //========= Copyright � 1996-2004, Valve LLC, All rights reserved. ============
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #ifndef GCRECORDINFO_H
  8. #define GCRECORDINFO_H
  9. namespace GCSDK
  10. {
  11. typedef CUtlMap<const char *,int> CMapIColumnInfo;
  12. // --------------------------------------------------------------------------
  13. // Information about a column in a record (table or result set)
  14. class CColumnInfo
  15. {
  16. public:
  17. CColumnInfo();
  18. ~CColumnInfo() { }
  19. void Set( const char *pchName, int nSQLColumn, EGCSQLType eGCSQLType, int cubFixedSize, int nColFlags, int cubMaxSize );
  20. const char *GetName() const { return m_rgchName; }
  21. int GetSQLColumn() const { return m_nSQLColumn; }
  22. EGCSQLType GetType() const { return m_eType; }
  23. int GetFixedSize() const { return m_cubFixedSize; }
  24. int GetMaxSize() const { return m_cchMaxSize; }
  25. int GetChecksum() const { Assert( m_bHaveChecksum ); return m_nChecksum; }
  26. bool BIsVariableLength() const;
  27. int GetColFlags() const { return m_nColFlags; }
  28. void GetColFlagDescription( char* pstrOut, int cubOutLength ) const;
  29. int GetConstraintColFlags() { return m_nColFlags & k_nColFlagAllConstraints; }
  30. void SetColFlagBits( int nColFlag );
  31. bool BIsIndexed() const { return 0 != ( m_nColFlags & k_nColFlagIndexed ); }
  32. bool BIsClustered() const { return 0 != ( m_nColFlags & k_nColFlagClustered ); }
  33. bool BIsUnique() const { return 0 != ( m_nColFlags & k_nColFlagUnique ); }
  34. bool BIsAutoIncrement() const { return 0 != ( m_nColFlags & k_nColFlagAutoIncrement ); }
  35. bool BIsPrimaryKey() const { return 0 != ( m_nColFlags & k_nColFlagPrimaryKey ); }
  36. bool BIsExplicitlyIndexed() const { return BIsIndexed() && !( BIsPrimaryKey() || BIsUnique() ); }
  37. bool BIsExplicitlyUnique() const { return BIsUnique() && !BIsPrimaryKey(); }
  38. bool BIsInsertable() const { return !BIsAutoIncrement(); }
  39. void CalculateChecksum();
  40. void ValidateColFlags() const;
  41. bool operator==( const CColumnInfo& refOther ) const;
  42. bool operator!=( const CColumnInfo& refOther ) const
  43. {
  44. return ! operator==( refOther );
  45. }
  46. #ifdef DBGFLAG_VALIDATE
  47. void Validate( CValidator &validator, const char *pchName );
  48. #endif // DBGFLAG_VALIDATE
  49. private:
  50. CColumnInfo( CColumnInfo& ); // no copy constructor, disable default copy constructor
  51. CColumnInfo& operator = ( CColumnInfo& ); // no assignment operator, disable default assignment operator
  52. char m_rgchName[k_cSQLObjectNameMax+1];
  53. EGCSQLType m_eType; // GC-based enum data type of this column
  54. int m_nColFlags; // flags for this column
  55. int m_nSQLColumn; // column # in SQL database to bind to, starts at 1.
  56. int m_cubFixedSize; // if fixed size, the fixed size in bytes; else 0
  57. int m_cchMaxSize; // if variable size, the maximum size; else 0
  58. int m_nChecksum; // checksum of this column info for quick comparisons
  59. bool m_bHaveChecksum; // have we calculated a checksum yet?
  60. };
  61. // --------------------------------------------------------------------------
  62. // Information about a record (table or result set)
  63. class CRecordInfo : public CRefCount
  64. {
  65. public:
  66. CRecordInfo();
  67. void InitFromDSSchema( CSchema *pSchema );
  68. void SetName( const char *pchName );
  69. const char *GetName() const { return m_rgchName; }
  70. void AddColumn( const char *pchName, int nSQLColumn, EGCSQLType eGCSQLType, int cubFixedSize, int nColFlags, int cubMaxSize );
  71. void SetAllColumnsAdded() { m_bAllColumnsAdded = true; }
  72. void PrepareForUse();
  73. int GetFixedSize() const { return m_cubFixedSize; }
  74. int GetNumColumns() const { return m_VecColumnInfo.Count(); }
  75. const CColumnInfo &GetColumnInfo( uint32 unColumn ) const { return m_VecColumnInfo[unColumn]; }
  76. CColumnInfo &GetColumnInfo( uint32 unColumn ) { return m_VecColumnInfo[unColumn]; }
  77. bool BFindColumnByName( const char *pchName, int *piColumn );
  78. bool BPreparedForUse() const { return m_bPreparedForUse; }
  79. void EnsureCapacity( int cColumns ) { m_VecColumnInfo.EnsureCapacity( cColumns ); }
  80. int GetChecksum();
  81. ESchemaCatalog GetESchemaCatalog() const { return m_eSchemaCatalog; }
  82. void SetESchemaCatalog( ESchemaCatalog e ) { m_eSchemaCatalog = e; }
  83. bool EqualTo( CRecordInfo* pOther );
  84. bool CompareIndexLists( CRecordInfo *pOther );
  85. bool CompareFKs( CRecordInfo *pOther );
  86. bool CompareFTSIndexLists( CRecordInfo *pOther ) const;
  87. EPrimaryKeyType GetPrimaryKeyType() const { return m_nHasPrimaryKey; }
  88. bool BHasPrimaryKey() { return GetPrimaryKeyType() != k_EPrimaryKeyTypeNone; }
  89. const FieldSet_t& GetPKFields() { Assert( BHasPrimaryKey()); return GetIndexFields( )[ m_iPKIndex ]; }
  90. const CUtlVector<FieldSet_t>& GetIndexFields() const { return m_VecIndexes; }
  91. int GetIndexFieldCount() const { return m_VecIndexes.Count(); }
  92. int GetPKIndex() const { return m_iPKIndex; }
  93. int AddIndex( const FieldSet_t& fieldSet );
  94. void GetIndexFieldList( CFmtStr1024 *pstr, int nIndents ) const;
  95. int GetTableID() const { return m_nTableID; }
  96. void SetTableID( int nTableID ) { m_nTableID = nTableID; }
  97. bool BHasIdentity() const;
  98. // full-text index
  99. CUtlVector<int> & GetFTSFields() { return m_vecFTSFields; }
  100. bool BHasFTSIndex() const { return m_vecFTSFields.Count() > 0; }
  101. void AddFTSFields( CUtlVector< int > &refVecFields );
  102. int GetFullTextCatalogIndex() { return m_nFullTextCatalogIndex; }
  103. // foreign keys
  104. void AddFK( const FKData_t &fkData );
  105. void GetFKListString( CFmtStr1024 *pstr, int nIndents );
  106. int GetFKCount();
  107. FKData_t &GetFKData( int iIndex );
  108. static CRecordInfo *Alloc();
  109. #ifdef DBGFLAG_VALIDATE
  110. static void ValidateStatics( CValidator &validator, const char *pchName );
  111. void Validate( CValidator &validator, const char *pchName );
  112. #endif //DBGFLAG_VALIDATE
  113. // note: destructor is private. This is a ref-counted object, private destructor ensures callers can't accidentally delete
  114. // directly, or declare on stack
  115. virtual ~CRecordInfo() { }
  116. private:
  117. virtual void DestroyThis();
  118. void CalculateChecksum();
  119. void BuildColumnNameIndex();
  120. char m_rgchName[k_cSQLObjectNameMax+1];
  121. int m_nTableID; // Object_ID if this table in SQL Server
  122. CUtlVector<CColumnInfo> m_VecColumnInfo; // Vector of columns in this record
  123. CMapIColumnInfo m_MapIColumnInfo; // Map of name->column index for quick lookup by name
  124. EPrimaryKeyType m_nHasPrimaryKey; // Does this table contain a column that is a primary key?
  125. int m_iPKIndex; // index info m_VecIndexes of our PK index; -1 if no PK
  126. CUtlVector<FieldSet_t> m_VecIndexes; // vector of all fields in all indexes
  127. int m_cubFixedSize; // Sum of data sizes for all fixed size columns
  128. bool m_bAllColumnsAdded; // Have all columns been added
  129. bool m_bPreparedForUse; // Have we finished being initialized?
  130. bool m_bHaveColumnNameIndex; // Have we created a column name index? (Only generated if someone asks.)
  131. bool m_bHaveChecksum; // Have we generated a checksum? (Only generated if someone asks.)
  132. int m_nChecksum; // checksum of this record info for quick comparisons - includes all columns
  133. ESchemaCatalog m_eSchemaCatalog; // what catalog owns this object?
  134. CUtlVector< int > m_vecFTSFields; // which fields have FTS indexing?
  135. int m_nFullTextCatalogIndex; // index of catalog for FTS index, if we get one
  136. CUtlVector<FKData_t> m_VecFKData; // vector of all FK relationships defined on this table
  137. CThreadMutex m_Mutex;
  138. static CThreadSafeClassMemoryPool<CRecordInfo> sm_MemPoolRecordInfo;
  139. #ifdef _DEBUG
  140. // validation tracking
  141. static CUtlRBTree<CRecordInfo *, int > sm_mapPMemPoolRecordInfo;
  142. static CThreadMutex sm_mutexMemPoolRecordInfo;
  143. #endif
  144. };
  145. int __cdecl CompareColumnInfo( const CColumnInfo *pColumnInfoLeft, const CColumnInfo *pColumnInfoRight );
  146. } // namespace GCSDK
  147. #endif // GCRECORDINFO_H