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.

283 lines
8.6 KiB

  1. //========= Copyright � 1996-2004, Valve LLC, All rights reserved. ============
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #ifndef GCSCHEMAFULL_H
  8. #define GCSCHEMAFULL_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. namespace GCSDK
  13. {
  14. //-----------------------------------------------------------------------------
  15. // SerSchemaFull
  16. // This defines the binary serialization format for a CSchemaFull
  17. //-----------------------------------------------------------------------------
  18. struct SerSchemaFull_t
  19. {
  20. enum EVersion
  21. {
  22. k_ECurrentVersion = 1,
  23. };
  24. int32 m_nVersion; // version of serialization format
  25. int32 m_cSchema; // # of schema we contain
  26. };
  27. //-----------------------------------------------------------------------------
  28. // CFTSCatalogInfo
  29. // information about a full text search catalog object in our schema
  30. //-----------------------------------------------------------------------------
  31. class CFTSCatalogInfo
  32. {
  33. public:
  34. enum ESchemaCatalog m_eCatalog;
  35. const char *m_pstrName;
  36. int m_nFileGroup;
  37. CFTSCatalogInfo()
  38. : m_pstrName( NULL ),
  39. m_eCatalog( k_ESchemaCatalogInvalid )
  40. {
  41. }
  42. ~CFTSCatalogInfo()
  43. {
  44. free( (void*) m_pstrName);
  45. }
  46. CFTSCatalogInfo( const CFTSCatalogInfo &refOther )
  47. {
  48. m_eCatalog = refOther.m_eCatalog;
  49. m_nFileGroup = refOther.m_nFileGroup;
  50. if ( refOther.m_pstrName != NULL )
  51. m_pstrName = strdup( refOther.m_pstrName );
  52. else
  53. m_pstrName = NULL;
  54. }
  55. #ifdef DBGFLAG_VALIDATE
  56. void Validate( CValidator &validator, const char *pchName ) // Validate our internal structures
  57. {
  58. validator.ClaimMemory( (void *) m_pstrName );
  59. }
  60. #endif
  61. };
  62. //-----------------------------------------------------------------------------
  63. // SchemaFull conversion instructions
  64. // These specify various operations that can be performed when converting
  65. // from one SchemaFull to another.
  66. //-----------------------------------------------------------------------------
  67. struct DeleteTable_t
  68. {
  69. char m_rgchTableName[k_cSQLObjectNameMax]; // Name of the table to delete
  70. };
  71. struct RenameTable_t
  72. {
  73. char m_rgchTableNameOld[k_cSQLObjectNameMax]; // Rename a table with this name
  74. int m_iTableDst; // to this table
  75. };
  76. enum ETriggerType
  77. {
  78. k_ETriggerType_Invalid,
  79. k_ETriggerType_After_Insert,
  80. k_ETriggerType_InsteadOf_Insert,
  81. k_ETriggerType_After_Delete,
  82. k_ETriggerType_InsteadOf_Delete,
  83. k_ETriggerType_After_Update,
  84. k_ETriggerType_InsteadOf_Update,
  85. };
  86. class CTriggerInfo
  87. {
  88. public:
  89. CTriggerInfo()
  90. : m_eTriggerType( k_ETriggerType_Invalid ),
  91. m_bMatched( false )
  92. {
  93. }
  94. // are these equal for identity?
  95. bool operator==( const CTriggerInfo& refOther ) const
  96. {
  97. if ( 0 != Q_stricmp( m_szTriggerTableName, refOther.m_szTriggerTableName ) )
  98. return false;
  99. if ( 0 != Q_stricmp( m_szTriggerName, refOther.m_szTriggerName ) )
  100. return false;
  101. // they're equal!
  102. return true;
  103. }
  104. // if the identity is the same, this will tell if text or type differs
  105. bool IsDifferent( const CTriggerInfo& refOther ) const
  106. {
  107. if ( m_eTriggerType != refOther.m_eTriggerType )
  108. return false;
  109. if ( m_strText != refOther.m_strText )
  110. return false;
  111. // they're equal!
  112. return true;
  113. }
  114. const char* GetTriggerTypeString() const
  115. {
  116. const char *pstrSQL = "~~ unknown trigger type syntax error ~~";
  117. switch ( m_eTriggerType )
  118. {
  119. case k_ETriggerType_After_Insert:
  120. pstrSQL = "AFTER INSERT";
  121. break;
  122. case k_ETriggerType_InsteadOf_Insert:
  123. pstrSQL = "INSTEAD OF INSERT";
  124. break;
  125. case k_ETriggerType_After_Delete:
  126. pstrSQL = "AFTER DELETE";
  127. break;
  128. case k_ETriggerType_InsteadOf_Delete:
  129. pstrSQL = "INSTEAD OF DELETE";
  130. break;
  131. case k_ETriggerType_After_Update:
  132. pstrSQL = "AFTER UPDATE";
  133. break;
  134. case k_ETriggerType_InsteadOf_Update:
  135. pstrSQL = "INSTEAD OF UPDATE";
  136. break;
  137. default:
  138. case k_ETriggerType_Invalid:
  139. /* initialize is fine, thanks */
  140. break;
  141. }
  142. return pstrSQL;
  143. }
  144. bool m_bMatched; // got matched during schema convert
  145. ETriggerType m_eTriggerType; // what kinda trigger is this?
  146. ESchemaCatalog m_eSchemaCatalog; // catalog where this trigger lives
  147. char m_szTriggerName[k_cSQLObjectNameMax]; // name of the trigger object
  148. char m_szTriggerTableName[k_cSQLObjectNameMax]; // name of the table hosting this trigger
  149. CUtlString m_strText; // text of the trigger
  150. // Validate our internal structures
  151. #ifdef DBGFLAG_VALIDATE
  152. void Validate( CValidator &validator, const char *pchName )
  153. {
  154. m_strText.Validate( validator, pchName );
  155. }
  156. #endif
  157. };
  158. //-----------------------------------------------------------------------------
  159. // CSchemaFull
  160. // This defines the schema for the entire data store. It's essentially just
  161. // a collection of CSchema, which define the schema for individual tables.
  162. //-----------------------------------------------------------------------------
  163. class CSchemaFull
  164. {
  165. public:
  166. // Constructors & destructors
  167. CSchemaFull();
  168. ~CSchemaFull();
  169. void Uninit();
  170. // add a new schema and return its pointer.
  171. CSchema *AddNewSchema( int iTable, ESchemaCatalog eCatalog, const char *pstrName )
  172. {
  173. CSchema &refNewSchema = m_VecSchema[m_VecSchema.AddToTail()];
  174. refNewSchema.SetName( pstrName );
  175. refNewSchema.SetESchemaCatalog( eCatalog );
  176. SetITable( &refNewSchema, iTable );
  177. return &refNewSchema;
  178. }
  179. // Accessors
  180. int GetCSchema() const { return m_VecSchema.Count(); }
  181. CSchema &GetSchema( int iSchema ) { return m_VecSchema[iSchema]; }
  182. uint32 GetCheckSum() const { return m_unCheckSum; }
  183. uint8 *GetPubScratchBuffer( );
  184. uint32 GetCubScratchBuffer() const { return m_cubScratchBuffer; }
  185. // Makes sure that a generated intrinsic schema is consistent
  186. void CheckSchema( CSchema *pSchema, int cField, uint32 cubRecord );
  187. // Find the table with a given name (returns -1 if not found)
  188. int FindITable( const char *pchName );
  189. const char *PchTableFromITable( int iTable );
  190. // Helper functions for recording schema conversion operations
  191. void AddDeleteTable( const char *pchTableName );
  192. void AddRenameTable( const char *pchTableNameOld, const char *pchTableNameNew );
  193. void AddDeleteField( const char *pchTableName, const char *pchFieldName );
  194. void AddRenameField( const char *pchTableName, const char *pchFieldNameOld, const char *pchFieldNameNew );
  195. void AddAlterField( const char *pchTableName, const char *pchFieldNameOld, const char *pchFieldNameNew, PfnAlterField_t pfnAlterField );
  196. // declare that a trigger is on a table
  197. void AddTrigger( ESchemaCatalog eCatalog, const char *pchTableName, const char *pchTriggerName, ETriggerType eTriggerType, const char *pchTriggerText );
  198. // Schema conversion helper: figure out what table to map a table from a different schema to
  199. bool BCanConvertTable( const char *pchTableSrc, int *piTableDst );
  200. // full text catalogs
  201. void AddFullTextCatalog( enum ESchemaCatalog eCatalog, const char *pstrCatalogName, int nFileGroup );
  202. int GetFTSCatalogByName( enum ESchemaCatalog eCatalog, const char *pstrCatalogName );
  203. void EnableFTS( enum ESchemaCatalog eCatalog );
  204. int GetCFTSCatalogs() const { return m_vecFTSCatalogs.Count(); }
  205. const CFTSCatalogInfo & GetFTSCatalogInfo( int nIndex ) const { return m_vecFTSCatalogs[nIndex]; }
  206. const CUtlVector< CTriggerInfo> & GetTriggerInfos( ) const { return m_VecTriggers; }
  207. // is the given schema catalog FTS enabled?
  208. bool GetFTSEnabled( enum ESchemaCatalog eCatalog );
  209. void Validate( CValidator &validator, const char *pchName ); // Validate our internal structures
  210. // sets tableID on CSchema, checking that it is not a duplicate
  211. void SetITable( CSchema* pSchema, int iTable );
  212. void FinishInit(); // Recalculates some internal fields
  213. private:
  214. CUtlVector< CSchema > m_VecSchema; // Schema for tables in all catalogs
  215. CUtlVector< CTriggerInfo > m_VecTriggers; // list of triggers in all catalogs
  216. // which schema catalogs have FTS enabled?
  217. CUtlMap< ESchemaCatalog, bool > m_mapFTSEnabled;
  218. // list of catalogs; each is marked with the schema where it lives.
  219. CUtlVector< CFTSCatalogInfo > m_vecFTSCatalogs;
  220. uint32 m_unCheckSum; // A simple checksum of our contents
  221. // SchemaFull conversion instructions
  222. CUtlVector<DeleteTable_t> m_VecDeleteTable;
  223. CUtlVector<RenameTable_t> m_VecRenameTable;
  224. uint8 *m_pubScratchBuffer; // Big enough to hold any record or sparse record in this schemafull
  225. uint32 m_cubScratchBuffer; // Size of the scratch buffer
  226. };
  227. extern CSchemaFull & GSchemaFull();
  228. } // namespace GCSDK
  229. #endif // GCSCHEMAFULL_H