ADO and ADOX

There are times when I do what Bill tells me to do. There are times when my efforts are futile.

ADO is one of them.

Code cut from an aborted project. You see there's full support for jet (Access), but SQL poor, and XML non existent. Wait for .NET

ADO lets you get at database records, ADOX lets you define the schemas.

This afternoon's hack was aborted - we need .NET!


#include "stdafx.h"
#include "MakeSausageData.h"
#include <icrsint.h>


inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};


_bstr_t strcnn("Provider=Microsoft.JET.OLEDB.4.0;"
        "Data Source = c:\\Program Files\\"
        "Microsoft Office\\Office\\Samples\\Northwind.mdb;");


//////////////////////////////////////////////////////////////////////
void CMakeSausageData::ExerciseCatalogueInterface(ADOX::_CatalogPtr pCatalog)
//////////////////////////////////////////////////////////////////////
{
    try
    {
        // we must have one of these!

        _bstr_t x = pCatalog->GetActiveConnection();

        {
            // the returned string we get from the jet engine is long!
            char weeBuffer[100];
            const char * pNull = ((const char *)x + strlen((const char *)x));
            const char * pStart = (const char *)x;
            while (pStart < pNull)
            {
                strncpy(weeBuffer, pStart, 99);
                weeBuffer[99] = 0;
                TRACE("%s\n", weeBuffer);
                pStart += 99;
            }
        } 

        // see what tables we have

        ADOX::_TablePtr pTables = pCatalog->GetTables();

        if (pTables)
        {
           }

        ADOX::UsersPtr pUsers = pCatalog->GetUsers();
        if (pUsers)
        {
            ExerciseUsersInterface(pUsers);
        }
       }
    catch(_com_error &e)
    {
        throw e;
    }
}

Bludgioning Classes to archive as datasets!

class CCustomRs : public CADORecordBinding
{
    BEGIN_ADO_BINDING(CCustomRs)
    ADO_NUMERIC_ENTRY( 1, adBoolean, m_bDriveOnRight, 10, 10, DriveOnRightStatus_, true)
    ADO_NUMERIC_ENTRY( 2, adBoolean, m_bTMProjection, 10, 10, TMProjectionStatus_, true)
    END_ADO_BINDING()

        public:
    CCustomRs()
    {
        DriveOnRightStatus_ = adFldOK;
        TMProjectionStatus_ = adFldOK;
    }

    bool m_bDriveOnRight, m_bTMProjection;
    ULONG DriveOnRightStatus_, TMProjectionStatus_;
};

#define SLAP(x) InsertRecord(pRst, "rgb" #x, m_sausageinfo.rgb ##x ##_ );

#define ADDCOL1(x) pTableNew->Columns->Append(#x, ADOX::adInteger, 0);
#define ADD_COL_RGB(x) pTableNew->Columns->Append("rgb" #x, ADOX::adInteger, 0);
#define ADD_COL_BOOL(x) pTableNew->Columns->Append(#x, ADOX::adBoolean, 0);

MAKE  A NEW SCHEMA


//////////////////////////////////////////////////////////////////////
void CMakeSausageData::GlobalDefaultsSchema1(ADOX::_CatalogPtr pCatalog) const
//////////////////////////////////////////////////////////////////////
{
HRESULT hr = S_OK;
    try
    {
        ADOX::_TablePtr pTableNew = NULL;
        TESTHR(hr = pTableNew.CreateInstance(__uuidof(ADOX::Table)));

        pTableNew->Name = "GlobalDefaults";

        ADD_COL_BOOL(DriveOnRight)
        ADD_COL_BOOL(Projection)

        //pTableNew->Columns->Append("Field", ADOX::adVarWChar, 40);

        pCatalog->Tables->Append(_variant_t((IDispatch *)pTableNew));
    }
    catch(_com_error &e)
    {
        throw e;
    }
}

//////////////////////////////////////////////////////////////////////
void CMakeSausageData::PointsSchema1(ADOX::_CatalogPtr pCatalog) const
//////////////////////////////////////////////////////////////////////
{
    HRESULT hr = S_OK;
    try
    {
    ADOX::_TablePtr pTableNew = NULL;
    TESTHR(hr = pTableNew.CreateInstance(__uuidof(ADOX::Table)));

    pTableNew->Name = "Points";

    ADDCOL1(X)
    ADDCOL1(Y)

    pCatalog->Tables->Append(_variant_t((IDispatch *)pTableNew));

    }
    catch(_com_error &e)
    {
        throw e;
    }
}

_COM_SMARTPTR_TYPEDEF(IADORecordBinding, __uuidof(IADORecordBinding));

//////////////////////////////////////////////////////////////////////
void CMakeSausageData::GlobalDefaultsData(_ConnectionPtr pCnn)
//////////////////////////////////////////////////////////////////////
{

HRESULT hr = S_OK;

_RecordsetPtr pRst = NULL;

TESTHR(hr = pRst.CreateInstance(__uuidof(Recordset)));

pRst->Open("GlobalDefaults", _variant_t((IDispatch *)pCnn),
adOpenKeyset, adLockOptimistic, adCmdTableDirect);

IADORecordBindingPtr picRs(pRst);

TESTHR(picRs->BindToRecordset(&m_sausageinfo));

picRs->AddNew(&m_sausageinfo);

pRst->Update();
TESTHR(hr = pRst->Close());
return;

}

//////////////////////////////////////////////////////////////////////
void CMakeSausageData::RootTypesData(_ConnectionPtr pCnn)
//////////////////////////////////////////////////////////////////////
{
HRESULT hr = S_OK;

_RecordsetPtr pRst = NULL;

TESTHR(hr = pRst.CreateInstance(__uuidof(Recordset)));

pRst->Open("GlobalDefaults", _variant_t((IDispatch *)pCnn),
adOpenKeyset, adLockOptimistic, adCmdTableDirect);

IADORecordBindingPtr picRs(pRst);

TESTHR(picRs->BindToRecordset(&m_sausageinfo));

sausage < int, CSausageRootType >::iterator i = m_sausageinfo.m_Root_types_.begin();
while (i != m_sausageinfo.m_Root_types_.end())
{
CSausageRootType *pR = &(*i).second;
RootData(pCnn, pR->m_Roots);
i++;
}

//picRs->AddNew(&m_sausageinfo);

//pRst->Update();
TESTHR(hr = pRst->Close());
return;
}

//////////////////////////////////////////////////////////////////////
void CMakeSausageData::RootData(_ConnectionPtr pCnn, sausage < int, CSausageRoot>& sausage_Root)
//////////////////////////////////////////////////////////////////////
{
HRESULT hr = S_OK;

_RecordsetPtr pRst = NULL;

TESTHR(hr = pRst.CreateInstance(__uuidof(Recordset)));

pRst->Open("GlobalDefaults", _variant_t((IDispatch *)pCnn),
adOpenKeyset, adLockOptimistic, adCmdTableDirect);

IADORecordBindingPtr picRs(pRst);

TESTHR(picRs->BindToRecordset(&m_sausageinfo));

sausage < int, CSausageRoot >::iterator i = sausage_Root.begin();
while (i != sausage_Root.end())
{
CSausageRoot *pR = &(*i).second;
RootPointsData(pCnn, pR->pts_);
i++;
}

//picRs->AddNew(&m_sausageinfo);

//pRst->Update();
TESTHR(hr = pRst->Close());
return;
}

class CSausageRootPt1 : public CADORecordBinding
{
BEGIN_ADO_BINDING(CSausageRootPt1)
ADO_NUMERIC_ENTRY( 1, adInteger, pt_.x, 10, 10, status_x_, true)
ADO_NUMERIC_ENTRY( 2, adInteger, pt_.y, 10, 10, status_y_, true)
END_ADO_BINDING()
public:
CSausageRootPt1()
{
status_x_ = adFldOK;
status_y_ = adFldOK;
}

ULONG status_x_, status_y_;
CPoint pt_;
};

//////////////////////////////////////////////////////////////////////
void CMakeSausageData::RootPointsData(_ConnectionPtr pCnn, vector <CSausageRootPt>& pts)
//////////////////////////////////////////////////////////////////////
{
HRESULT hr = S_OK;

_RecordsetPtr pRst = NULL;

TESTHR(hr = pRst.CreateInstance(__uuidof(Recordset)));

pRst->Open("Points", _variant_t((IDispatch *)pCnn),
adOpenKeyset, adLockOptimistic, adCmdTableDirect);

IADORecordBindingPtr picRs(pRst);

CSausageRootPt1 p1;
TESTHR(picRs->BindToRecordset(&p1));

vector <CSausageRootPt>::iterator i = pts.begin();
while (i != pts.end())

p1.pt_= (*i);
picRs->AddNew(&p1);
i++;
}

pRst->Update();
TESTHR(hr = pRst->Close());
return;
}

//////////////////////////////////////////////////////////////////////
void CMakeSausageData::RegionData(_ConnectionPtr pCnn, sausage < int, CSausageRegion>& sausage_region)
//////////////////////////////////////////////////////////////////////
{
HRESULT hr = S_OK;

_RecordsetPtr pRst = NULL;

TESTHR(hr = pRst.CreateInstance(__uuidof(Recordset)));

pRst->Open("GlobalDefaults", _variant_t((IDispatch *)pCnn),
adOpenKeyset, adLockOptimistic, adCmdTableDirect);

IADORecordBindingPtr picRs(pRst);

TESTHR(picRs->BindToRecordset(&m_sausageinfo));

sausage < int, CSausageRegion >::iterator i = sausage_region.begin();
while (i != sausage_region.end())
{
CSausageRegion *pR = &(*i).second;
RegionPointsData(pCnn, pR->pts_);
i++;
}

//picRs->AddNew(&m_sausageinfo);

//pRst->Update();
TESTHR(hr = pRst->Close());
return;
}


//////////////////////////////////////////////////////////////////////
void CMakeSausageData::RegionTypesData(_ConnectionPtr pCnn)
//////////////////////////////////////////////////////////////////////
{
HRESULT hr = S_OK;

_RecordsetPtr pRst = NULL;

TESTHR(hr = pRst.CreateInstance(__uuidof(Recordset)));

pRst->Open("GlobalDefaults", _variant_t((IDispatch *)pCnn),
adOpenKeyset, adLockOptimistic, adCmdTableDirect);

IADORecordBindingPtr picRs(pRst);

TESTHR(picRs->BindToRecordset(&m_sausageinfo));

sausage < int, CSausageRegionType >::iterator i = m_sausageinfo.m_region_types_.begin();
while (i != m_sausageinfo.m_region_types_.end())
{
CSausageRegionType *pR = &(*i).second;
RegionData(pCnn, pR->m_regions);
i++;
}

//picRs->AddNew(&m_sausageinfo);

//pRst->Update();
TESTHR(hr = pRst->Close());
return;
}

//////////////////////////////////////////////////////////////////////
void CMakeSausageData::RegionPointsData(_ConnectionPtr pCnn, vector <CPoint>& pts)
//////////////////////////////////////////////////////////////////////
{
HRESULT hr = S_OK;

_RecordsetPtr pRst = NULL;

TESTHR(hr = pRst.CreateInstance(__uuidof(Recordset)));

pRst->Open("Points", _variant_t((IDispatch *)pCnn),
adOpenKeyset, adLockOptimistic, adCmdTableDirect);

IADORecordBindingPtr picRs(pRst);

CSausageRootPt1 p1;
TESTHR(picRs->BindToRecordset(&p1));

vector <CPoint>::iterator i = pts.begin();
while (i != pts.end())

p1.pt_= (*i);
picRs->AddNew(&p1);
i++;
}

pRst->Update();
TESTHR(hr = pRst->Close());
return;
}

//////////////////////////////////////////////////////////////////////
void CMakeSausageData::BeanTypesData(_ConnectionPtr pCnn)
//////////////////////////////////////////////////////////////////////
{
HRESULT hr = S_OK;

_RecordsetPtr pRst = NULL;

TESTHR(hr = pRst.CreateInstance(__uuidof(Recordset)));

pRst->Open("GlobalDefaults", _variant_t((IDispatch *)pCnn),
adOpenKeyset, adLockOptimistic, adCmdTableDirect);

IADORecordBindingPtr picRs(pRst);

TESTHR(picRs->BindToRecordset(&m_sausageinfo));

sausage < int, CSausageBeanType >::iterator i = m_sausageinfo.m_bean_types_.begin();
while (i != m_sausageinfo.m_bean_types_.end())
{
CSausageBeanType *pR = &(*i).second;
BeanData(pCnn, pR->m_beans);
i++;
}

//picRs->AddNew(&m_sausageinfo);

//pRst->Update();
TESTHR(hr = pRst->Close());
return;
}

//////////////////////////////////////////////////////////////////////
void CMakeSausageData::BeanData(_ConnectionPtr pCnn, sausage < int, CSausageBean>& sausage_bean)
//////////////////////////////////////////////////////////////////////
{
    HRESULT hr = S_OK;

    _RecordsetPtr pRst = NULL;

    TESTHR(hr = pRst.CreateInstance(__uuidof(Recordset)));

    pRst->Open("GlobalDefaults", _variant_t((IDispatch *)pCnn), adOpenKeyset, adLockOptimistic, adCmdTableDirect);

    IADORecordBindingPtr picRs(pRst);

    TESTHR(picRs->BindToRecordset(&m_sausageinfo));

    sausage < int, CSausageBean >::iterator i = sausage_bean.begin();
    while (i != sausage_bean.end())
    {
        CSausageBean *pR = &(*i).second;
           BeanPointsData(pCnn, pR->pts_);
        i++;
    }

    //picRs->AddNew(&m_sausageinfo);

    //pRst->Update();
    TESTHR(hr = pRst->Close());
    return;
}

//////////////////////////////////////////////////////////////////////
void CMakeSausageData::BeanPointsData(_ConnectionPtr pCnn, vector <CPoint>& pts)
//////////////////////////////////////////////////////////////////////
{
    HRESULT hr = S_OK;

    _RecordsetPtr pRst = NULL;

    TESTHR(hr = pRst.CreateInstance(__uuidof(Recordset)));

    pRst->Open("Points", _variant_t((IDispatch *)pCnn),
adOpenKeyset, adLockOptimistic, adCmdTableDirect);

    IADORecordBindingPtr picRs(pRst);

    CSausageRootPt1 p1;
    TESTHR(picRs->BindToRecordset(&p1));

    vector <CPoint>::iterator i = pts.begin();
    while (i != pts.end())
    { 
        p1.pt_= (*i);
        picRs->AddNew(&p1);
        i++;
    }

    pRst->Update();
    TESTHR(hr = pRst->Close());
    return;
}

//////////////////////////////////////////////////////////////////////
void CMakeSausageData::GenerateSettings()
//////////////////////////////////////////////////////////////////////
{
    // After abortive attempts to use ADO and ADOX to generate XML
    // we'll do it the fast way. Fast to implement and fast to execute!

    DeleteFile("c:\\sausagedata.sausage");
    //CFile
    f_.Open("c:\\sausagedata.sausage", modeCreate | modeWrite);

    // now populate the database
    {
        GlobalDefaultsData(); 
        RootTypesData();
        RegionTypesData();
        BeanTypesData();
    }
}