TABLECOPY.CPP

//----------------------------------------------------------------------------- 
// Microsoft OLE DB TABLECOPY Sample
// Copyright (C) 1996 By Microsoft Corporation.
//
// @doc
//
// @module TABLECOPY.CPP
//
//-----------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////
// Includes
//
/////////////////////////////////////////////////////////////////
#define DBINITCONSTANTS //Initilaizes OLEDB Guids / Constants

#include "winmain.h"
#include "common.h"
#include "tablecopy.h"
#include "table.h"
#include "wizard.h"

#include "msdasql.h"//CLSID_MSDASQL provider guid



/////////////////////////////////////////////////////////////////
// CTableCopy::CTableCopy
//
/////////////////////////////////////////////////////////////////
CTableCopy::CTableCopy(CWizard* pCWizard)
{
ASSERT(pCWizard);

m_pCFromTable= new CTable(pCWizard);
m_pCToTable= new CTable(pCWizard);

m_dwShowTypes= SHOW_TABLES; //Default to just user Tables in the DSN

//Options
m_fShowQuery= FALSE;

//Copy Options
m_dwRowOpt= IDR_ALL_ROWS;
m_ulMaxRows= MAX_BLOCK_SIZE;

m_dwParamOpt= IDR_PARAM_SETS;
m_ulParamSets= 10;

m_dwBlobOpt= IDR_BLOB_SIZE;
m_ulBlobSize= MAX_COL_SIZE;

//Create Options
m_fCopyTables= TRUE;
m_fCopyIndexes= TRUE;

m_fTranslate= TRUE;
m_pCWizard= pCWizard; //Back pointer to windowing class
}


/////////////////////////////////////////////////////////////////
// CTableCopy::~CTableCopy
//
/////////////////////////////////////////////////////////////////
CTableCopy::~CTableCopy()
{
delete m_pCFromTable;
delete m_pCToTable;
}




/////////////////////////////////////////////////////////////////
// HRESULT CTableCopy::MapTypes
//
/////////////////////////////////////////////////////////////////
HRESULT CTableCopy::MapTypes()
{
HRESULT hr = S_OK;

// Get descriptions of each column in the Source Table
TESTC(hr = m_pCFromTable->GetColInfo());

//Now get the TypeInfo for all columns
TESTC(hr = m_pCFromTable->GetTypeInfo());

//Now map all the Types correctly from the Source to the Target
TESTC(hr = m_pCToTable->MapTableInfo(m_pCFromTable));

CLEANUP:
return hr;
}


/////////////////////////////////////////////////////////////////
// HRESULT CTableCopy::CreateTable
//
/////////////////////////////////////////////////////////////////
HRESULT CTableCopy::CreateTable()
{
HRESULT hr = S_OK;

// Create the Table (if desired)
if(m_fCopyTables)
{
TESTC(hr = m_pCToTable->CreateTable());
TESTC(hr = m_pCToTable->GetColInfo());
}

CLEANUP:
return hr;
}


///////////////////////////////////////////////////////////////////////
// HRESULT CTableCopy::CreateIndexes
//
///////////////////////////////////////////////////////////////////////
HRESULT CTableCopy::CreateIndexes()
{
HRESULT hr = S_OK;

// Create the indexes (if desired)
if(m_fCopyIndexes)
TESTC(hr = m_pCToTable->CopyIndexes(m_pCFromTable));

CLEANUP:
return hr;
}



///////////////////////////////////////////////////////////////////////
// HRESULT CTableCopy::CopyData
//
///////////////////////////////////////////////////////////////////////
HRESULT CTableCopy::CopyData(ULONG* pcRows)
{
ASSERT(pcRows);
HRESULT hr = S_OK;

//If MaxRows is specified, then copy upto number specified, otherwise
//indicate ULONG_MAX to mean all rows, (upto ULONG_MAX)
*pcRows= (m_dwRowOpt == IDR_MAX_ROWS) ? m_ulMaxRows : ULONG_MAX;

//If ParamSets are specified then use the ParamSet size, otherwise
//indicate 0 to mean no param sets
ULONG ulParamSets= (m_dwParamOpt == IDR_PARAM_SETS) ? m_ulParamSets : 0;

//If BlobSize is specified then use the Specified size, otherwise
//indicate ULONG_MAX to use ISequentialStream
ULONG ulBlobSize= (m_dwBlobOpt == IDR_BLOB_SIZE) ? m_ulBlobSize : ULONG_MAX;

//Copy the Data from the Source to the Target table
TESTC(hr = m_pCToTable->CopyData(m_pCFromTable, pcRows, ulParamSets, ulBlobSize));

CLEANUP:
return hr;
}


///////////////////////////////////////////////////////////////////////
// HRESULT CTableCopy::CopyTables
//
///////////////////////////////////////////////////////////////////////
HRESULT CTableCopy::CopyTables()
{
HRESULT hr;
ULONG cRows = 0;

//MapTypes should have already been called
ASSERT(m_pCFromTable->m_rgColDesc);
ASSERT(m_pCToTable->m_rgColDesc);

//Create the new Table
TESTC(hr = CreateTable());

//Create the Indexes
TESTC(hr = CreateIndexes());

//Now Copy the Data
TESTC(hr = CopyData(&cRows));

CLEANUP:
if(SUCCEEDED(hr))
wMessageBox(NULL, MB_ICONINFORMATION | MB_OK, wsz_SUCCESS, wsz_COPY_SUCCESS, cRows);
else
wMessageBox(NULL, MB_ICONEXCLAMATION | MB_OK, wsz_ERROR, wsz_COPY_FAILURE);

return hr;
}