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.

101 lines
3.6 KiB

  1. //========= Copyright � 1996-2004, Valve LLC, 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. // Returns a long (1024 char) string of "?,?,?,?,?"... for use in IN clauses or INSERT statements
  15. const char *GetInsertArgString();
  16. void ConvertFieldToText( EGCSQLType eFieldType, uint8 *pubRecord, int cubRecord, char *rgchField, int cchField, bool bQuoteString = true );
  17. void ConvertFieldArrayToInText( const CColumnInfo &columnInfo, byte *pubData, int cubData, char *rgchResult, bool bForPreparedStatement );
  18. char *SQLTypeFromField( const CColumnInfo &colInfo, char *pchBuf, int cchBuf );
  19. void EscapeStringValue( char *rgchField, int cchField );
  20. void AppendConstraints( const CRecordInfo *pRecordInfo, const CColumnInfo *pColumnInfo, bool bForAdd, CFmtStrMax & sCmd );
  21. void AppendTableConstraints( CRecordInfo *pRecordInfo, CFmtStrMax & sCmd );
  22. void AppendConstraint( const char *pchTableName, const char *pchColumnName, int nColFlagConstraint, bool bForAdd, bool bClustered,
  23. CFmtStrMax & sCmd, int nFillFactor );
  24. //void BuildSelectStatementText( CUtlVector<CQuery> *pVecQuery, bool bForPreparedStatement, char *pchStatement, int cchStatement );
  25. void BuildInsertStatementText( CFmtStr1024 *psStatement, const CRecordInfo *pRecordInfo );
  26. void BuildInsertAndReadStatementText( CFmtStr1024 *psStatement, CUtlVector<int> *pvecOutputFields, const CRecordInfo *pRecordInfo ) ;
  27. void BuildSelectStatementText( CFmtStr1024 *psStatement, const CColumnSet & selectSet, const char *pchTopClause = NULL );
  28. void BuildUpdateStatementText( CFmtStr1024 *psStatement, const CColumnSet & columnSet );
  29. void BuildDeleteStatementText( CFmtStr1024 *psStatement, const CRecordInfo* pRecordInfo );
  30. void BuildWhereClauseText( CFmtStr1024 *psClause, const CColumnSet & columnSet );
  31. template< typename T >
  32. bool CopyResultToSchVector( IGCSQLResultSet *pResultSet, const CColumnSet & readSet, CUtlVector< T > *pvecRecords )
  33. {
  34. if ( pResultSet->GetRowCount() == 0 )
  35. return true;
  36. FOR_EACH_COLUMN_IN_SET( readSet, nColumnIndex )
  37. {
  38. EGCSQLType eRecordType = readSet.GetColumnInfo( nColumnIndex ).GetType();
  39. EGCSQLType eResultType = pResultSet->GetColumnType( nColumnIndex );
  40. Assert( eResultType == eRecordType );
  41. if( eRecordType != eResultType )
  42. return false;
  43. }
  44. for( CSQLRecord sqlRecord( 0, pResultSet ); sqlRecord.IsValid(); sqlRecord.NextRow() )
  45. {
  46. int nRecord = pvecRecords->AddToTail();
  47. FOR_EACH_COLUMN_IN_SET( readSet, nColumnIndex )
  48. {
  49. uint8 *pubData;
  50. uint32 cubData;
  51. DbgVerify( sqlRecord.BGetColumnData( nColumnIndex, &pubData, (int*)&cubData ) );
  52. DbgVerify( pvecRecords->Element( nRecord ).BSetField( readSet.GetColumn( nColumnIndex), pubData, cubData ) );
  53. }
  54. }
  55. return true;
  56. }
  57. //EResult UpdateOrInsertUnique( CSQLAccess &sqlAccess, int iTable, int iField, CRecordBase *pRecordBase, int iIndexID );
  58. //bool BIsDuplicateInsertAttempt( const CSQLErrorInfo *pErr );
  59. #define EXIT_WITH_SQL_FAILURE( ret ) { nRet = ret; goto Exit; }
  60. #define EXIT_ON_BOOL_FAILURE( bRet, msg ) \
  61. { \
  62. if ( false == bRet ) \
  63. { \
  64. SetSQLError( msg ); \
  65. nRet = SQL_ERROR; \
  66. goto Exit; \
  67. } \
  68. }
  69. #define SAFE_CLOSE_STMT( x ) \
  70. if ( NULL != (x) ) \
  71. { \
  72. SQLFreeHandle( SQL_HANDLE_STMT, (x) ); \
  73. (x) = NULL; \
  74. }
  75. #define SAFE_FREE_HANDLE( x, y ) \
  76. if ( NULL != (x) ) \
  77. { \
  78. SQLFreeHandle( y, (x) ); \
  79. (x) = NULL; \
  80. }
  81. } // namespace GCSDK
  82. #endif // SQLUTIL_H