home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / devtools / samples / DTS / dtsxform / xform.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-28  |  7.0 KB  |  208 lines

  1. // Xform.cpp : Implementation of CXform
  2. #include "stdafx.h"
  3. #include "dtsxform.h"
  4. #define DBINITCONSTANTS
  5. #include "Xform.h"
  6.  
  7. //utility functions
  8.  
  9. //Do SetErrorInfo with proper error string
  10. HRESULT ReportErrorInfo(USHORT wCode, HRESULT hr, UINT idsError, const IID& iid)
  11. {
  12.     USES_CONVERSION;
  13.  
  14.     //Modify this function to also to help context and helpfiles if needed.
  15.     TCHAR szTemp[256];
  16.  
  17.     if (NOERROR == hr && 0 == wCode)
  18.         return NOERROR;    //No exceptions
  19.  
  20.     if (NOERROR == hr)
  21.         hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, wCode);
  22.  
  23.     if (LoadString(_Module.GetResourceInstance(), idsError, szTemp, 256))
  24.     {
  25.         LPOLESTR szError;
  26. #ifndef _UNICODE
  27.         szError = A2W(szTemp);
  28. #else
  29.         szError = szTemp;
  30. #endif
  31.         if (szError)
  32.         {
  33.             //
  34.             AtlSetErrorInfo(CLSID_DTSStrCatXform, szError, 0, NULL, iid, hr, NULL);
  35.         }
  36.     }
  37.     return hr; 
  38. }
  39.  
  40. //Utility macros
  41. #define ErrorOut(err)    {return ReportErrorInfo(DTSXform_Error_##err, 0, IDS_ERROR_##err, IID_IDTSDataPumpTransform);}
  42. #define ErrorOut2(err)    {return ReportErrorInfo(DTSXform_Error_##err, 0, IDS_ERROR_##err, IID_IDTSStrCatXform);}
  43. #define ErrorOutHr(hres, err)    {return ReportErrorInfo(0, hres, IDS_ERROR_##err, IID_IDTSDataPumpTransform);}
  44. #define ErrorOutHr2(hres, err)    {return ReportErrorInfo(0, hres, IDS_ERROR_##err, IID_IDTSStrCatXform);}
  45.  
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CXform
  49.  
  50. // IDTSStrCatXform
  51.  
  52.  
  53. STDMETHODIMP CXform::get_RemoveSpace( 
  54.         /* [retval][out] */ VARIANT_BOOL __RPC_FAR *pRetVal) 
  55. {
  56.     if (!pRetVal)
  57.         return E_POINTER;
  58.     *pRetVal = m_bRemoveSpace;
  59.     return NOERROR;
  60. }
  61.     
  62. STDMETHODIMP CXform::put_RemoveSpace( 
  63.         /* [in] */ VARIANT_BOOL NewValue)
  64. {
  65.     m_bRemoveSpace = NewValue;
  66.     return NOERROR;
  67. }
  68.  
  69. //*** IDTSDataPumpTransform members
  70. STDMETHODIMP CXform::Initialize(
  71.         DP_IN LPCOLESTR pwzName,                            // Transform name
  72.         VARIANT ServerParameters,                        // Parameters to server for this transform
  73.         LPBYTE *ppvTransformServerData                // Transform server state data.
  74.     )
  75. {
  76.     //Nothing to do here
  77.     return NOERROR;
  78. }
  79.  
  80. STDMETHODIMP CXform::ValidateSchema(
  81.         LPBYTE pvTransformServerData,                    // Transform server state data.
  82.         LPCDTSTransformColumnInfo pSrcColumnInfo,     // Source columns and rowdata
  83.         LPCDTSTransformColumnInfo pDestColumnInfo, // Dest columns and rowdata
  84.         IDTSDataConvert *pIDTSDataConvert,            // Pointer to the data conversion interface
  85.         DTSTransformFlags eTransformFlags                // Input Flags for Transformation validation and execution
  86.     )
  87. {
  88.     //*************************************************************************************
  89.     //NOTE: this sample transformation does not check the tranform flags to properly handle 
  90.     //truncation, type conversion etc.
  91.     //*************************************************************************************
  92.  
  93.     if (pDestColumnInfo->cColumns != 1) //You can use custom error codes 
  94.         ErrorOut(NUMDESTCOLS);
  95.  
  96.     //More validation should be done here to check to see source and dest are strings and
  97.     //they are all strings of same type (ansi/unicode match etc.)
  98.     
  99.     //destination - the sample only does ANSI strings
  100.     // sample Destination table -- one column only
  101.     const DBCOLUMNINFO*  pDestDBColumnInfo = pDestColumnInfo->rgColumnData[0].pDBColumnInfo;
  102.     WORD wDestType = (pDestDBColumnInfo->wType & (~DBTYPE_BYREF));
  103.  
  104.     if (!(wDestType & DBTYPE_STR))
  105.         ErrorOut(ONLYSTRINGCOLS);
  106.  
  107.     for (UINT i = 0; i < pSrcColumnInfo->cColumns; i++)
  108.     {
  109.         const DBCOLUMNINFO*  pSrcDBColumnInfo = pSrcColumnInfo->rgColumnData[i].pDBColumnInfo;
  110.         WORD wSourceType = (pSrcDBColumnInfo->wType & (~DBTYPE_BYREF));
  111.  
  112.         if (!(wSourceType & DBTYPE_STR))
  113.             ErrorOut(ONLYSTRINGCOLS);
  114.     }
  115.  
  116.     return NOERROR;
  117. }
  118.  
  119.  
  120. STDMETHODIMP CXform::AddVariable( 
  121.         LPBYTE pvTransformServerData,                    // Transform server state data.
  122.         LPCOLESTR pwzName,                            // Variable name
  123.         BOOL bGlobal,                                    // For ActiveX scripts, indicates whether this variable's
  124.                                                             // methods must be qualified by the object name.
  125.         VARIANT Variable                                // Variable value; passed to and updatable by Transform
  126.     )
  127. {
  128.     return NOERROR;
  129. }
  130.  
  131.  
  132. STDMETHODIMP CXform::Execute(
  133.         LPBYTE pvTransformServerData,                    // Transform server state data.
  134.         LPCDTSTransformColumnInfo pSrcColumnInfo,     // Source columns and rowdata
  135.         LPDTSTransformColumnInfo pDestColumnInfo,     // Dest columns and rowdata
  136.         IDTSDataConvert *pIDTSDataConvert,            // Pointer to the data conversion interface
  137.         LPDTSTransformStatus pTransformStatus        // Result of transform
  138.     )
  139. {
  140.     //*************************************************************************************
  141.     //NOTE: this sample transformation does not check the tranform flags to properly handle 
  142.     //truncation, type conversion etc.
  143.     //*************************************************************************************
  144.     DTSColumnData& rDTSDestColumnData = pDestColumnInfo->rgColumnData[0];
  145.     const DBCOLUMNINFO& rDBDestColumnInfo = *rDTSDestColumnData.pDBColumnInfo;
  146.  
  147.     const DBBINDING& rDBDestBinding = *rDTSDestColumnData.pDBBinding;
  148.  
  149.     // set the dest length to max length
  150.     ULONG ulDestLength = rDBDestBinding.cbMaxLen;
  151.     LPSTR pDestString = (LPSTR)(rDTSDestColumnData.pvData + rDBDestBinding.obValue);
  152.  
  153.  
  154.     if (!ulDestLength || !pDestString) //nothing to do ??
  155.         return NOERROR;
  156.  
  157.     *pDestString = 0;
  158.     for (UINT i = 0; i < pSrcColumnInfo->cColumns; i++)
  159.     {
  160.         DTSColumnData& rDTSSourceColumnData = pSrcColumnInfo->rgColumnData[i];
  161.         const DBCOLUMNINFO& rDBSourceColumnInfo = *rDTSSourceColumnData.pDBColumnInfo;
  162.  
  163.         const DBBINDING& rDBSourceBinding = *rDTSSourceColumnData.pDBBinding;
  164.         ULONG ulSourceLength = *reinterpret_cast<ULONG *>(rDTSSourceColumnData.pvData + rDBSourceBinding.obLength);
  165.         LPSTR pSourceString = *(LPSTR *)(rDTSSourceColumnData.pvData + rDBSourceBinding.obValue);
  166.  
  167.         if (strlen(pDestString) + strlen(pSourceString) < rDBDestBinding.cbMaxLen)
  168.         {
  169.             strcat(pDestString, pSourceString);
  170.             if (m_bRemoveSpace)
  171.                 RemoveTrailingSpace(pDestString, strlen(pDestString));
  172.             strcat(pDestString, " ");
  173.         }
  174.         else
  175.             break;
  176.     }
  177.     // set the dest length
  178.     ULONG* ulLength = reinterpret_cast<ULONG *>(rDTSDestColumnData.pvData + rDBDestBinding.obLength);
  179.     *ulLength = strlen(pDestString) + 1;
  180.     return NOERROR;
  181. }
  182.  
  183.  
  184. STDMETHODIMP CXform::OnRowComplete(
  185.         LPBYTE pvTransformServerData,                    // Transform server state data.
  186.         LPDTSTransformColumnInfo pSrcColumnInfo,     // Source columns and rowdata
  187.         LPDTSTransformColumnInfo pDestColumnInfo,     // Dest columns and rowdata
  188.         IDTSDataConvert *pIDTSDataConvert,            // Pointer to the data conversion interface
  189.         DTSTransformStatus eTransformStatus,            // Result of Execute()
  190.         HRESULT hrInsert                                // Result of IRowsetChange::InsertRow()
  191.     )
  192. {
  193.     return NOERROR;
  194. }
  195.  
  196.  
  197. STDMETHODIMP CXform::OnTransformComplete(
  198.         LPBYTE pvTransformServerData,                    // Transform server state data.
  199.         LPDTSTransformColumnInfo pSrcColumnInfo,     // Source columns and rowdata
  200.         LPDTSTransformColumnInfo pDestColumnInfo,     // Dest columns and rowdata
  201.         IDTSDataConvert *pIDTSDataConvert                // Pointer to the data conversion interface
  202.     )
  203. {
  204.     return NOERROR;
  205. }
  206.  
  207.  
  208.