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.

274 lines
9.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Shared object based on a CBaseRecord subclass
  4. //
  5. //=============================================================================
  6. #include "stdafx.h"
  7. // memdbgon must be the last include file in a .cpp file!!!
  8. #include "tier0/memdbgon.h"
  9. namespace GCSDK
  10. {
  11. #ifdef GC
  12. //----------------------------------------------------------------------------
  13. // Purpose: Returns true if the object needs to be saved to the database
  14. //----------------------------------------------------------------------------
  15. bool CSchemaSharedObjectHelper::BYieldingAddToDatabase( CRecordBase *pRecordBase )
  16. {
  17. if( pRecordBase->GetPSchema()->GetRecordInfo()->BHasIdentity() )
  18. {
  19. CSQLAccess sqlAccess;
  20. return sqlAccess.BYieldingInsertWithIdentity( pRecordBase );
  21. }
  22. return false;
  23. }
  24. //----------------------------------------------------------------------------
  25. // Purpose: Returns true if the object needs to be saved to the database
  26. //----------------------------------------------------------------------------
  27. bool CSchemaSharedObjectHelper::BYieldingAddInsertToTransaction( CSQLAccess & sqlAccess, CRecordBase *pRecordBase )
  28. {
  29. Assert( !pRecordBase->GetPSchema()->GetRecordInfo()->BHasIdentity() );
  30. return sqlAccess.BYieldingInsertRecord( pRecordBase );
  31. }
  32. //----------------------------------------------------------------------------
  33. // Purpose: Reads the object from the database via its primary key. Initialize
  34. // the primary key fields before you call this.
  35. //----------------------------------------------------------------------------
  36. bool CSchemaSharedObjectHelper::BYieldingReadFromDatabase( CRecordBase *pRecordBase )
  37. {
  38. CSQLAccess sqlAccess;
  39. CColumnSet csPK( pRecordBase->GetPSchema()->GetRecordInfo() );
  40. csPK.MakePrimaryKey();
  41. CColumnSet csReadFields( pRecordBase->GetPSchema()->GetRecordInfo() );
  42. csReadFields.MakeInverse( csPK );
  43. if( k_EResultOK != sqlAccess.YieldingReadRecordWithWhereColumns( pRecordBase, csReadFields, csPK ) )
  44. return false;
  45. return true;
  46. }
  47. //----------------------------------------------------------------------------
  48. // Purpose: Writes the non-PK fields on the object to the database
  49. //----------------------------------------------------------------------------
  50. bool CSchemaSharedObjectHelper::BYieldingAddWriteToTransaction( CSQLAccess & sqlAccess, CRecordBase *pRecordBase, const CColumnSet & csDatabaseDirty )
  51. {
  52. CColumnSet csPK( pRecordBase->GetPSchema()->GetRecordInfo() );
  53. csPK.MakePrimaryKey();
  54. if( !sqlAccess.BYieldingUpdateRecord( *pRecordBase, csPK, csDatabaseDirty ) )
  55. return false;
  56. return true;
  57. }
  58. //----------------------------------------------------------------------------
  59. // Purpose: Adds the primary key for this object to the network message
  60. //----------------------------------------------------------------------------
  61. bool CSchemaSharedObjectHelper::BYieldingAddRemoveToTransaction( CSQLAccess & sqlAccess, CRecordBase *pRecordBase )
  62. {
  63. CColumnSet csPK( pRecordBase->GetPSchema()->GetRecordInfo() );
  64. csPK.MakePrimaryKey();
  65. if( !sqlAccess.BYieldingDeleteRecords( *pRecordBase, csPK ) )
  66. return false;
  67. return true;
  68. }
  69. #endif // GC
  70. //----------------------------------------------------------------------------
  71. // Purpose: Dumps diagnostic information about the shared object
  72. //----------------------------------------------------------------------------
  73. void CSchemaSharedObjectHelper::Dump( const CRecordBase *pRecordBase )
  74. {
  75. CFmtStr1024 sOutput;
  76. const CRecordInfo *pRecordInfo = pRecordBase->GetPRecordInfo();
  77. for( uint32 unColumn = 0; unColumn < (uint32)pRecordInfo->GetNumColumns(); unColumn++ )
  78. {
  79. if( unColumn > 0 )
  80. {
  81. sOutput += ", ";
  82. }
  83. sOutput += pRecordInfo->GetColumnInfo( unColumn ).GetName();
  84. sOutput += "=";
  85. char pchFieldData[128];
  86. pRecordBase->RenderField( unColumn, sizeof(pchFieldData), pchFieldData );
  87. sOutput += pchFieldData;
  88. }
  89. EmitInfo( SPEW_CONSOLE, SPEW_ALWAYS, LOG_ALWAYS, "\t\t%s\n", sOutput.Access() );
  90. }
  91. //=============================================================================
  92. #ifdef GC
  93. //----------------------------------------------------------------------------
  94. // Purpose: Returns true if the object needs to be saved to the database
  95. //----------------------------------------------------------------------------
  96. bool CSchemaSharedObjectBase::BYieldingAddToDatabase()
  97. {
  98. if( GetPObject()->GetPSchema()->GetRecordInfo()->BHasIdentity() )
  99. {
  100. return CSchemaSharedObjectHelper::BYieldingAddToDatabase( GetPObject() );
  101. }
  102. else
  103. return CSharedObject::BYieldingAddToDatabase();
  104. }
  105. //----------------------------------------------------------------------------
  106. // Purpose: Returns true if the object needs to be saved to the database
  107. //----------------------------------------------------------------------------
  108. bool CSchemaSharedObjectBase::BYieldingAddInsertToTransaction( CSQLAccess & sqlAccess )
  109. {
  110. return CSchemaSharedObjectHelper::BYieldingAddInsertToTransaction( sqlAccess, GetPObject() );
  111. }
  112. //----------------------------------------------------------------------------
  113. // Purpose: Reads the object from the database via its primary key. Initialize
  114. // the primary key fields before you call this.
  115. //----------------------------------------------------------------------------
  116. bool CSchemaSharedObjectBase::BYieldingReadFromDatabase()
  117. {
  118. return CSchemaSharedObjectHelper::BYieldingReadFromDatabase( GetPObject() );
  119. }
  120. CColumnSet CSchemaSharedObjectBase::GetDatabaseDirtyColumnSet( const CUtlVector< int > &fields ) const
  121. {
  122. CColumnSet cs( GetPObject()->GetPRecordInfo() );
  123. FOR_EACH_VEC( fields, i )
  124. {
  125. cs.BAddColumn( fields[i] );
  126. }
  127. return cs;
  128. }
  129. //----------------------------------------------------------------------------
  130. // Purpose: Writes the non-PK fields on the object to the database
  131. //----------------------------------------------------------------------------
  132. bool CSchemaSharedObjectBase::BYieldingAddWriteToTransaction( CSQLAccess & sqlAccess, const CUtlVector< int > &fields )
  133. {
  134. CColumnSet csDatabaseDirty = GetDatabaseDirtyColumnSet(fields);
  135. return CSchemaSharedObjectHelper::BYieldingAddWriteToTransaction( sqlAccess, GetPObject(), csDatabaseDirty );
  136. }
  137. //----------------------------------------------------------------------------
  138. // Purpose: Adds the primary key for this object to the network message
  139. //----------------------------------------------------------------------------
  140. bool CSchemaSharedObjectBase::BYieldingAddRemoveToTransaction( CSQLAccess & sqlAccess )
  141. {
  142. if ( CSchemaSharedObjectHelper::BYieldingAddRemoveToTransaction( sqlAccess, GetPObject() ) )
  143. {
  144. return true;
  145. }
  146. return false;
  147. }
  148. #endif // GC
  149. //----------------------------------------------------------------------------
  150. // Purpose: Returns true if this is less than than the object in soRHS. This
  151. // comparison is deterministic, but it may not be pleasing to a user
  152. // since it is just going to compare raw memory. If you need a sort
  153. // that is user-visible you will need to do it at a higher level that
  154. // actually knows what the data in these objects means.
  155. //----------------------------------------------------------------------------
  156. bool CSchemaSharedObjectBase::BIsKeyLess( const CSharedObject & soRHS ) const
  157. {
  158. Assert( GetTypeID() == soRHS.GetTypeID() );
  159. const CSchemaSharedObjectBase & soSchemaRHS = (const CSchemaSharedObjectBase &)soRHS;
  160. CColumnSet csPK( GetPObject()->GetPSchema()->GetRecordInfo() );
  161. csPK.MakePrimaryKey();
  162. FOR_EACH_COLUMN_IN_SET( csPK, unColumnIndex )
  163. {
  164. uint8 *pubMyData, *pubTheirData;
  165. uint32 cubMyData, cubTheirData;
  166. int nColumn = csPK.GetColumn( unColumnIndex );
  167. DbgVerify( GetPObject()->BGetField( nColumn, &pubMyData, &cubMyData ) );
  168. DbgVerify( soSchemaRHS.GetPObject()->BGetField( nColumn, &pubTheirData, &cubTheirData ) );
  169. int nCmp = Q_memcmp( pubMyData, pubTheirData, MIN( cubMyData, cubTheirData ) );
  170. // longer chunks of memory are greater
  171. if( nCmp == 0 )
  172. {
  173. nCmp = cubTheirData > cubMyData;
  174. }
  175. if( nCmp != 0 )
  176. return nCmp < 0;
  177. }
  178. return false;
  179. }
  180. //----------------------------------------------------------------------------
  181. // Purpose: Copy the data from the specified schema shared object into this.
  182. // Both objects must be of the same type.
  183. //----------------------------------------------------------------------------
  184. void CSchemaSharedObjectBase::Copy( const CSharedObject & soRHS )
  185. {
  186. Assert( GetTypeID() == soRHS.GetTypeID() );
  187. CSchemaSharedObjectBase & soRHSBase = (CSchemaSharedObjectBase &)soRHS;
  188. *GetPObject() = *(soRHSBase.GetPObject());
  189. }
  190. //----------------------------------------------------------------------------
  191. // Purpose: Dumps diagnostic information about the shared object
  192. //----------------------------------------------------------------------------
  193. void CSchemaSharedObjectBase::Dump() const
  194. {
  195. CSchemaSharedObjectHelper::Dump( GetPObject() );
  196. }
  197. //----------------------------------------------------------------------------
  198. // Purpose: Returns the record info object for this type of schema object
  199. //----------------------------------------------------------------------------
  200. const CRecordInfo * CSchemaSharedObjectBase::GetRecordInfo() const
  201. {
  202. return GetPObject()->GetPRecordInfo();
  203. }
  204. //----------------------------------------------------------------------------
  205. // Purpose: Claims all memory for the object.
  206. //----------------------------------------------------------------------------
  207. #ifdef DBGFLAG_VALIDATE
  208. void CSchemaSharedObjectBase::Validate( CValidator &validator, const char *pchName )
  209. {
  210. CSharedObject::Validate( validator, pchName );
  211. // these are INSIDE the function instead of outside so the interface
  212. // doesn't change
  213. VALIDATE_SCOPE();
  214. }
  215. #endif
  216. } // namespace GCSDK