如何使用ATL OLE DB过滤视图

时间:2016-03-15 12:16:02

标签: visual-c++ mfc oledb atl

我正在开发一个访问mysql数据库的MFC应用程序。对于连接,我使用ATL模板(没什么特别的)创建了基于CCommand的消费者类。现在我想根据某些标准过滤我的数据。我知道IViewFilter接口的存在。但是,我对如何在模板的上下文中实现它一无所知。搜索互联网不会产生许多有用的结果,也不会产生微软的任何相关文档。任何指向正确方向的指针或示例代码都将受到赞赏。

模板代码:

// Orders.h : Declaration of the COrders

#pragma once

// code generated on dinsdag 15 maart 2016, 13:11

class COrdersAccessor
{
public:
    LONG m_ID;
    TCHAR m_OrderNumber[256];
    TCHAR m_Description[256];
    LONG m_CustID;
    TCHAR m_ServiceNumber[256];
    SHORT m_Year;
    TCHAR m_directory[256];

    // The following wizard-generated data members contain status
    // values for the corresponding fields in the column map. You
    // can use these values to hold NULL values that the database
    // returns or to hold error information when the compiler returns
    // errors. See Field Status Data Members in Wizard-Generated
    // Accessors in the Visual C++ documentation for more information
    // on using these fields.
    // NOTE: You must initialize these fields before setting/inserting data!

    DBSTATUS m_dwIDStatus;
    DBSTATUS m_dwOrderNumberStatus;
    DBSTATUS m_dwDescriptionStatus;
    DBSTATUS m_dwCustIDStatus;
    DBSTATUS m_dwServiceNumberStatus;
    DBSTATUS m_dwYearStatus;
    DBSTATUS m_dwdirectoryStatus;

    // The following wizard-generated data members contain length
    // values for the corresponding fields in the column map.
    // NOTE: For variable-length columns, you must initialize these
    //       fields before setting/inserting data!

    DBLENGTH m_dwIDLength;
    DBLENGTH m_dwOrderNumberLength;
    DBLENGTH m_dwDescriptionLength;
    DBLENGTH m_dwCustIDLength;
    DBLENGTH m_dwServiceNumberLength;
    DBLENGTH m_dwYearLength;
    DBLENGTH m_dwdirectoryLength;


    void GetRowsetProperties(CDBPropSet* pPropSet)
    {
        pPropSet->AddProperty(DBPROP_CANFETCHBACKWARDS, true, DBPROPOPTIONS_OPTIONAL);
        pPropSet->AddProperty(DBPROP_CANSCROLLBACKWARDS, true, DBPROPOPTIONS_OPTIONAL);
        pPropSet->AddProperty(DBPROP_IRowsetChange, true, DBPROPOPTIONS_OPTIONAL);
        pPropSet->AddProperty(DBPROP_UPDATABILITY, DBPROPVAL_UP_CHANGE | DBPROPVAL_UP_INSERT | DBPROPVAL_UP_DELETE);
    }

    HRESULT OpenDataSource()
    {
        CDataSource _db;
        HRESULT hr;
// #error Security Issue: The connection string may contain a password
// The connection string below may contain plain text passwords and/or
// other sensitive information. Please remove the #error after reviewing
// the connection string for any security related issues. You may want to
// store the password in some other form or use a different user authentication.
        hr = _db.OpenFromInitializationString(L"Provider=MSDASQL.1;Persist Security Info=False;Data Source=HCPSOrders;Extended Properties=\"DSN=HCPSOrders;\"");
        if (FAILED(hr))
        {
#ifdef _DEBUG
            AtlTraceErrorRecords(hr);
#endif
            return hr;
        }
        return m_session.Open(_db);
    }

    void CloseDataSource()
    {
        m_session.Close();
    }

    operator const CSession&()
    {
        return m_session;
    }

    CSession m_session;

    DEFINE_COMMAND_EX(COrdersAccessor, L" \
    SELECT \
        ID, \
        OrderNumber, \
        Description, \
        CustID, \
        ServiceNumber, \
        Year, \
        directory \
        FROM Orders")


    // In order to fix several issues with some providers, the code below may bind
    // columns in a different order than reported by the provider

    BEGIN_COLUMN_MAP(COrdersAccessor)
        COLUMN_ENTRY_LENGTH_STATUS(1, m_ID, m_dwIDLength, m_dwIDStatus)
        COLUMN_ENTRY_LENGTH_STATUS(2, m_OrderNumber, m_dwOrderNumberLength, m_dwOrderNumberStatus)
        COLUMN_ENTRY_LENGTH_STATUS(3, m_Description, m_dwDescriptionLength, m_dwDescriptionStatus)
        COLUMN_ENTRY_LENGTH_STATUS(4, m_CustID, m_dwCustIDLength, m_dwCustIDStatus)
        COLUMN_ENTRY_LENGTH_STATUS(5, m_ServiceNumber, m_dwServiceNumberLength, m_dwServiceNumberStatus)
        COLUMN_ENTRY_LENGTH_STATUS(6, m_Year, m_dwYearLength, m_dwYearStatus)
        COLUMN_ENTRY_LENGTH_STATUS(7, m_directory, m_dwdirectoryLength, m_dwdirectoryStatus)
    END_COLUMN_MAP()
};

class COrders : public CCommand<CAccessor<COrdersAccessor> >
{
public:
    HRESULT OpenAll()
    {
        HRESULT hr;
        hr = OpenDataSource();
        if (FAILED(hr))
            return hr;
        __if_exists(GetRowsetProperties)
        {
            CDBPropSet propset(DBPROPSET_ROWSET);
            __if_exists(HasBookmark)
            {
                if( HasBookmark() )
                    propset.AddProperty(DBPROP_IRowsetLocate, true);
            }
            GetRowsetProperties(&propset);
            return OpenRowset(&propset);
        }
        __if_not_exists(GetRowsetProperties)
        {
            __if_exists(HasBookmark)
            {
                if( HasBookmark() )
                {
                    CDBPropSet propset(DBPROPSET_ROWSET);
                    propset.AddProperty(DBPROP_IRowsetLocate, true);
                    return OpenRowset(&propset);
                }
            }
        }
        return OpenRowset();
    }

    HRESULT OpenRowset(DBPROPSET *pPropSet = NULL)
    {
        HRESULT hr = Open(m_session, NULL, pPropSet);
#ifdef _DEBUG
        if(FAILED(hr))
            AtlTraceErrorRecords(hr);
#endif
        return hr;
    }

    void CloseAll()
    {
        Close();
        ReleaseCommand();
        CloseDataSource();
    }
};

0 个答案:

没有答案