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.

113 lines
4.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #ifndef SQLUTIL_H
  8. #define SQLUTIL_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. namespace GCSDK
  13. {
  14. typedef CFmtStrN< 4096 > TSQLCmdStr;
  15. // Returns a long (1024 char) string of "?,?,?,?,?"... for use in IN clauses or INSERT statements
  16. const char *GetInsertArgString();
  17. // Returns the number of characters that should be used for the specified number of parameters
  18. uint32 GetInsertArgStringChars( uint32 nNumParams );
  19. // Returns the maximum number of parameters that are supported by the GetInsertArgString string
  20. uint32 GetInsertArgStringMaxParams();
  21. void ConvertFieldToText( EGCSQLType eFieldType, uint8 *pubRecord, int cubRecord, char *rgchField, int cchField, bool bQuoteString = true );
  22. void ConvertFieldArrayToInText( const CColumnInfo &columnInfo, byte *pubData, int cubData, char *rgchResult, bool bForPreparedStatement );
  23. char *SQLTypeFromField( const CColumnInfo &colInfo, char *pchBuf, int cchBuf );
  24. void EscapeStringValue( char *rgchField, int cchField );
  25. void AppendConstraints( const CRecordInfo *pRecordInfo, const CColumnInfo *pColumnInfo, bool bForAdd, CFmtStrMax & sCmd );
  26. void AppendTableConstraints( CRecordInfo *pRecordInfo, CFmtStrMax & sCmd );
  27. void AppendConstraint( const char *pchTableName, const char *pchColumnName, int nColFlagConstraint, bool bForAdd, bool bClustered,
  28. CFmtStrMax & sCmd, int nFillFactor );
  29. void BuildTablePKConstraintText( TSQLCmdStr *psStatement, CRecordInfo *pRecordInfo );
  30. //void BuildSelectStatementText( CUtlVector<CQuery> *pVecQuery, bool bForPreparedStatement, char *pchStatement, int cchStatement );
  31. void BuildInsertStatementText( TSQLCmdStr *psStatement, const CRecordInfo *pRecordInfo );
  32. void BuildInsertAndReadStatementText( TSQLCmdStr *psStatement, CUtlVector<int> *pvecOutputFields, const CRecordInfo *pRecordInfo ) ;
  33. void BuildMergeStatementTextOnPKWhenMatchedUpdateWhenNotMatchedInsert( TSQLCmdStr *psStatement, const CRecordInfo *pRecordInfo );
  34. void BuildMergeStatementTextOnPKWhenNotMatchedInsert( TSQLCmdStr *psStatement, const CRecordInfo *pRecordInfo );
  35. void BuildSelectStatementText( TSQLCmdStr *psStatement, const CColumnSet & selectSet, const char *pchTopClause = NULL );
  36. void BuildUpdateStatementText( TSQLCmdStr *psStatement, const CColumnSet & columnSet );
  37. void BuildDeleteStatementText( TSQLCmdStr *psStatement, const CRecordInfo* pRecordInfo );
  38. void AppendWhereClauseText( TSQLCmdStr *psClause, const CColumnSet & columnSet );
  39. void BuildOutputClauseText( TSQLCmdStr *psClause, const CColumnSet & columnSet );
  40. template< typename T >
  41. bool CopyResultToSchVector( IGCSQLResultSet *pResultSet, const CColumnSet & readSet, CUtlVector< T > *pvecRecords )
  42. {
  43. if ( pResultSet->GetRowCount() == 0 )
  44. return true;
  45. FOR_EACH_COLUMN_IN_SET( readSet, nColumnIndex )
  46. {
  47. EGCSQLType eRecordType = readSet.GetColumnInfo( nColumnIndex ).GetType();
  48. EGCSQLType eResultType = pResultSet->GetColumnType( nColumnIndex );
  49. Assert( eResultType == eRecordType );
  50. if( eRecordType != eResultType )
  51. return false;
  52. }
  53. for( CSQLRecord sqlRecord( 0, pResultSet ); sqlRecord.IsValid(); sqlRecord.NextRow() )
  54. {
  55. int nRecord = pvecRecords->AddToTail();
  56. FOR_EACH_COLUMN_IN_SET( readSet, nColumnIndex )
  57. {
  58. uint8 *pubData;
  59. uint32 cubData;
  60. DbgVerify( sqlRecord.BGetColumnData( nColumnIndex, &pubData, (int*)&cubData ) );
  61. DbgVerify( pvecRecords->Element( nRecord ).BSetField( readSet.GetColumn( nColumnIndex), pubData, cubData ) );
  62. }
  63. }
  64. return true;
  65. }
  66. //EResult UpdateOrInsertUnique( CSQLAccess &sqlAccess, int iTable, int iField, CRecordBase *pRecordBase, int iIndexID );
  67. //bool BIsDuplicateInsertAttempt( const CSQLErrorInfo *pErr );
  68. #define EXIT_WITH_SQL_FAILURE( ret ) { nRet = ret; goto Exit; }
  69. #define EXIT_ON_BOOL_FAILURE( bRet, msg ) \
  70. { \
  71. if ( false == bRet ) \
  72. { \
  73. SetSQLError( msg ); \
  74. nRet = SQL_ERROR; \
  75. goto Exit; \
  76. } \
  77. }
  78. #define SAFE_CLOSE_STMT( x ) \
  79. if ( NULL != (x) ) \
  80. { \
  81. SQLFreeHandle( SQL_HANDLE_STMT, (x) ); \
  82. (x) = NULL; \
  83. }
  84. #define SAFE_FREE_HANDLE( x, y ) \
  85. if ( NULL != (x) ) \
  86. { \
  87. SQLFreeHandle( y, (x) ); \
  88. (x) = NULL; \
  89. }
  90. } // namespace GCSDK
  91. #endif // SQLUTIL_H