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.

368 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Sets of columns in SQL queries
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "stdafx.h"
  8. // memdbgon must be the last include file in a .cpp file!!!
  9. #include "tier0/memdbgon.h"
  10. namespace GCSDK
  11. {
  12. //-----------------------------------------------------------------------------
  13. // Purpose: Constructs a column set with no columns in it
  14. //-----------------------------------------------------------------------------
  15. CColumnSet::CColumnSet( const CRecordInfo *pRecordInfo )
  16. : m_pRecordInfo( pRecordInfo )
  17. {
  18. }
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Constructs a column set with a single column in it
  21. // Inputs: nColumn - the column to add
  22. //-----------------------------------------------------------------------------
  23. CColumnSet::CColumnSet( const CRecordInfo *pRecordInfo, int col1 )
  24. : m_pRecordInfo( pRecordInfo )
  25. {
  26. m_vecColumns.AddToTail( col1 );
  27. }
  28. CColumnSet::CColumnSet( const CRecordInfo *pRecordInfo, int col1, int col2 )
  29. : m_pRecordInfo( pRecordInfo )
  30. {
  31. m_vecColumns.EnsureCapacity( 2 );
  32. m_vecColumns.AddToTail( col1 );
  33. m_vecColumns.AddToTail( col2 );
  34. }
  35. CColumnSet::CColumnSet( const CRecordInfo *pRecordInfo, int col1, int col2, int col3 )
  36. : m_pRecordInfo( pRecordInfo )
  37. {
  38. m_vecColumns.EnsureCapacity( 3 );
  39. m_vecColumns.AddToTail( col1 );
  40. m_vecColumns.AddToTail( col2 );
  41. m_vecColumns.AddToTail( col3 );
  42. }
  43. CColumnSet::CColumnSet( const CRecordInfo *pRecordInfo, int col1, int col2, int col3, int col4 )
  44. : m_pRecordInfo( pRecordInfo )
  45. {
  46. m_vecColumns.EnsureCapacity( 4 );
  47. m_vecColumns.AddToTail( col1 );
  48. m_vecColumns.AddToTail( col2 );
  49. m_vecColumns.AddToTail( col3 );
  50. m_vecColumns.AddToTail( col4 );
  51. }
  52. CColumnSet::CColumnSet( const CRecordInfo *pRecordInfo, int col1, int col2, int col3, int col4, int col5 )
  53. : m_pRecordInfo( pRecordInfo )
  54. {
  55. m_vecColumns.EnsureCapacity( 5 );
  56. m_vecColumns.AddToTail( col1 );
  57. m_vecColumns.AddToTail( col2 );
  58. m_vecColumns.AddToTail( col3 );
  59. m_vecColumns.AddToTail( col4 );
  60. m_vecColumns.AddToTail( col5 );
  61. }
  62. CColumnSet::CColumnSet( const CRecordInfo *pRecordInfo, int col1, int col2, int col3, int col4, int col5, int col6 )
  63. : m_pRecordInfo( pRecordInfo )
  64. {
  65. m_vecColumns.EnsureCapacity( 6 );
  66. m_vecColumns.AddToTail( col1 );
  67. m_vecColumns.AddToTail( col2 );
  68. m_vecColumns.AddToTail( col3 );
  69. m_vecColumns.AddToTail( col4 );
  70. m_vecColumns.AddToTail( col5 );
  71. m_vecColumns.AddToTail( col6 );
  72. }
  73. CColumnSet::CColumnSet( const CRecordInfo *pRecordInfo, int col1, int col2, int col3, int col4, int col5, int col6, int col7 )
  74. : m_pRecordInfo( pRecordInfo )
  75. {
  76. m_vecColumns.EnsureCapacity( 7 );
  77. m_vecColumns.AddToTail( col1 );
  78. m_vecColumns.AddToTail( col2 );
  79. m_vecColumns.AddToTail( col3 );
  80. m_vecColumns.AddToTail( col4 );
  81. m_vecColumns.AddToTail( col5 );
  82. m_vecColumns.AddToTail( col6 );
  83. m_vecColumns.AddToTail( col7 );
  84. }
  85. CColumnSet::CColumnSet( const CRecordInfo *pRecordInfo, int col1, int col2, int col3, int col4, int col5, int col6, int col7, int col8 )
  86. : m_pRecordInfo( pRecordInfo )
  87. {
  88. m_vecColumns.EnsureCapacity( 8 );
  89. m_vecColumns.AddToTail( col1 );
  90. m_vecColumns.AddToTail( col2 );
  91. m_vecColumns.AddToTail( col3 );
  92. m_vecColumns.AddToTail( col4 );
  93. m_vecColumns.AddToTail( col5 );
  94. m_vecColumns.AddToTail( col6 );
  95. m_vecColumns.AddToTail( col7 );
  96. m_vecColumns.AddToTail( col8 );
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Purpose: Copy constructor
  100. //-----------------------------------------------------------------------------
  101. CColumnSet::CColumnSet( const CColumnSet & rhs )
  102. {
  103. MEM_ALLOC_CREDIT_("CColumnSet");
  104. m_vecColumns.CopyArray( rhs.m_vecColumns.Base(), rhs.m_vecColumns.Count() );
  105. m_pRecordInfo = rhs.m_pRecordInfo;
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Purpose: Assignment operator
  109. //-----------------------------------------------------------------------------
  110. CColumnSet & CColumnSet::operator=( const CColumnSet & rhs )
  111. {
  112. MEM_ALLOC_CREDIT_("CColumnSet");
  113. m_vecColumns.CopyArray( rhs.m_vecColumns.Base(), rhs.m_vecColumns.Count() );
  114. m_pRecordInfo = rhs.m_pRecordInfo;
  115. return *this;
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Purpose: Addition operator. lhs ColumnSet will be a union of the two
  119. // ColumnSets
  120. //-----------------------------------------------------------------------------
  121. CColumnSet & CColumnSet::operator+=( const CColumnSet & rhs )
  122. {
  123. Assert( this->GetRecordInfo() == rhs.GetRecordInfo() );
  124. FOR_EACH_COLUMN_IN_SET( rhs, i )
  125. {
  126. BAddColumn( rhs.GetColumn( i ) );
  127. }
  128. return *this;
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Purpose: Addition operator. Returns a union of lhs and rhs
  132. //-----------------------------------------------------------------------------
  133. const CColumnSet CColumnSet::operator+( const CColumnSet & rhs ) const
  134. {
  135. return CColumnSet( *this ) += rhs;
  136. }
  137. //-----------------------------------------------------------------------------
  138. // Purpose: Adds a column to the set if it is
  139. // Inputs: nColumn - THe column to add
  140. //-----------------------------------------------------------------------------
  141. void CColumnSet::BAddColumn( int nColumn )
  142. {
  143. if( nColumn >= 0 && nColumn < m_pRecordInfo->GetNumColumns() )
  144. {
  145. //not sure best way to handle the 'is already set case'
  146. if( !IsSet( nColumn ) )
  147. m_vecColumns.AddToTail( nColumn );
  148. }
  149. else
  150. {
  151. AssertMsg3( false, "Attempting to set an out of range column on schema type %s, %d (of %d)", GetRecordInfo()->GetName(), nColumn, m_pRecordInfo->GetNumColumns() );
  152. }
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose: Removes a column from the set
  156. // Inputs: nColumn - THe column to remove
  157. //-----------------------------------------------------------------------------
  158. void CColumnSet::BRemoveColumn( int nColumn )
  159. {
  160. m_vecColumns.FindAndRemove( nColumn );
  161. }
  162. //-----------------------------------------------------------------------------
  163. // Purpose: Returns true if a column is in the set
  164. // Inputs: nColumn - THe column to test
  165. //-----------------------------------------------------------------------------
  166. bool CColumnSet::IsSet( int nColumn ) const
  167. {
  168. int nIndex = m_vecColumns.Find( nColumn );
  169. return m_vecColumns.IsValidIndex( nIndex );
  170. }
  171. //-----------------------------------------------------------------------------
  172. // Purpose: Returns the number of columns in the set
  173. //-----------------------------------------------------------------------------
  174. uint32 CColumnSet::GetColumnCount() const
  175. {
  176. return m_vecColumns.Count();
  177. }
  178. //-----------------------------------------------------------------------------
  179. // Purpose: Returns the column index of the Nth column in the set
  180. // Inputs: nIndex - the position in the set to return a column index for.
  181. //-----------------------------------------------------------------------------
  182. int CColumnSet::GetColumn( int nIndex ) const
  183. {
  184. return m_vecColumns[nIndex];
  185. }
  186. //-----------------------------------------------------------------------------
  187. // Purpose: Returns a CColumnInfo object for the nth column in the set
  188. // Inputs: nIndex - the position in the set to return a column info for.
  189. //-----------------------------------------------------------------------------
  190. const CColumnInfo & CColumnSet::GetColumnInfo( int nIndex ) const
  191. {
  192. return m_pRecordInfo->GetColumnInfo( GetColumn( nIndex ) );
  193. }
  194. //-----------------------------------------------------------------------------
  195. // Purpose: Empties the column set
  196. //-----------------------------------------------------------------------------
  197. void CColumnSet::MakeEmpty()
  198. {
  199. m_vecColumns.RemoveAll();
  200. }
  201. //-----------------------------------------------------------------------------
  202. // Purpose: Makes the column set be the full set of all columns in the record info
  203. //-----------------------------------------------------------------------------
  204. void CColumnSet::MakeFull()
  205. {
  206. MakeEmpty();
  207. const int nNumColumns = m_pRecordInfo->GetNumColumns();
  208. m_vecColumns.EnsureCapacity( nNumColumns );
  209. for( int nColumn = 0; nColumn < m_pRecordInfo->GetNumColumns(); nColumn++ )
  210. {
  211. //do a direct add to avoid the exponential cost since we know we won't have conflicts
  212. m_vecColumns.AddToTail( nColumn );
  213. }
  214. }
  215. //-----------------------------------------------------------------------------
  216. // Purpose: Makes the column set be the full set of all insertable columns in
  217. // the record info
  218. //-----------------------------------------------------------------------------
  219. void CColumnSet::MakeInsertable()
  220. {
  221. MakeEmpty();
  222. for( int nColumn = 0; nColumn < m_pRecordInfo->GetNumColumns(); nColumn++ )
  223. {
  224. const CColumnInfo & columnInfo = m_pRecordInfo->GetColumnInfo( nColumn );
  225. if( columnInfo.BIsInsertable() )
  226. {
  227. //do a direct add to avoid the exponential cost since we know we won't have conflicts
  228. m_vecColumns.AddToTail( nColumn );
  229. }
  230. }
  231. }
  232. //-----------------------------------------------------------------------------
  233. // Purpose: Makes the column set be the full set of all noninsertable columns in
  234. // the record info
  235. //-----------------------------------------------------------------------------
  236. void CColumnSet::MakeNoninsertable()
  237. {
  238. MakeEmpty();
  239. for( int nColumn = 0; nColumn < m_pRecordInfo->GetNumColumns(); nColumn++ )
  240. {
  241. const CColumnInfo & columnInfo = m_pRecordInfo->GetColumnInfo( nColumn );
  242. if( !columnInfo.BIsInsertable() )
  243. {
  244. //do a direct add to avoid the exponential cost since we know we won't have conflicts
  245. m_vecColumns.AddToTail( nColumn );
  246. }
  247. }
  248. }
  249. //-----------------------------------------------------------------------------
  250. // Purpose: Makes the column set be the full set of all primary key columns in
  251. // the record info
  252. //-----------------------------------------------------------------------------
  253. void CColumnSet::MakePrimaryKey()
  254. {
  255. MakeEmpty();
  256. for( int nColumn = 0; nColumn < m_pRecordInfo->GetNumColumns(); nColumn++ )
  257. {
  258. const CColumnInfo & columnInfo = m_pRecordInfo->GetColumnInfo( nColumn );
  259. if( columnInfo.BIsPrimaryKey() )
  260. {
  261. //do a direct add to avoid the exponential cost since we know we won't have conflicts
  262. m_vecColumns.AddToTail( nColumn );
  263. }
  264. }
  265. }
  266. //-----------------------------------------------------------------------------
  267. // Purpose: Makes the column set be the full set of all primary key columns in
  268. // the record info
  269. //-----------------------------------------------------------------------------
  270. void CColumnSet::MakeInverse( const CColumnSet & columnSet )
  271. {
  272. MakeEmpty();
  273. for( int nColumn = 0; nColumn < m_pRecordInfo->GetNumColumns(); nColumn++ )
  274. {
  275. if( !columnSet.IsSet( nColumn ) )
  276. {
  277. //do a direct add to avoid the exponential cost since we know we won't have conflicts
  278. m_vecColumns.AddToTail( nColumn );
  279. }
  280. }
  281. }
  282. //-----------------------------------------------------------------------------
  283. // determines if the current column set has all fields set. Useful for detection of new columns being added to the schema
  284. //-----------------------------------------------------------------------------
  285. bool CColumnSet::BAreAllFieldsSet() const
  286. {
  287. for( int nColumn = 0; nColumn < m_pRecordInfo->GetNumColumns(); nColumn++ )
  288. {
  289. if( !IsSet( nColumn ) )
  290. return false;
  291. }
  292. return true;
  293. }
  294. //-----------------------------------------------------------------------------
  295. // Purpose: Returns a Column Set which is the inverse of the given column set
  296. // STATIC - Difference from MakeInverse is that it has a return value
  297. //-----------------------------------------------------------------------------
  298. CColumnSet CColumnSet::Inverse( const CColumnSet & columnSet )
  299. {
  300. CColumnSet set( columnSet.GetRecordInfo() );
  301. set.MakeInverse( columnSet );
  302. return set;
  303. }
  304. //-----------------------------------------------------------------------------
  305. // Purpose: Claims the memory for CColumnSet
  306. //-----------------------------------------------------------------------------
  307. #ifdef DBGFLAG_VALIDATE
  308. void CColumnSet::Validate( CValidator &validator, const char *pchName )
  309. {
  310. // these are INSIDE the function instead of outside so the interface
  311. // doesn't change
  312. VALIDATE_SCOPE();
  313. ValidateObj( m_vecColumns );
  314. }
  315. #endif
  316. } // namespace GCSDK