OPNROWST.CPP

//-------------------------------------------------------------------- 
// Microsoft OLE DB Sample Provider
// (C) Copyright 1994 - 1996 Microsoft Corporation. All Rights Reserved.
//
// @doc
//
// @module OPNROWST.CPP | IOpenRowset interface implementation
//

// Includes ------------------------------------------------------------------

#include "headers.h"


// Code ----------------------------------------------------------------------



// CImpIOpenRowset::OpenRowset ------------------------------------------------
//
// @mfunc Opens and returns a rowset that includes all rows from a single base table
//
// @rdesc HRESULT
// @flag S_OK | The method succeeded
// @flag E_INVALIDARG | pTableID was NULL
// @flag E_FAIL | Provider-specific error
// @flag DB_E_NOTABLE | Specified table does not exist in current Data
// | Data Source object
// @flag E_OUTOFMEMORY | Out of memory
// @flag E_NOINTERFACE | The requested interface was not available

STDMETHODIMP CImpIOpenRowset::OpenRowset
(
IUnknown* pUnkOuter, //@parm IN | Controlling unknown, if any
DBID* pTableID, //@parm IN | table to open
DBID*pIndexID,//@parm IN | DBID of the index
REFIID riid, //@parm IN | interface to return
ULONG cProperties, //@parm IN | count of properties
DBPROPSETrgProperties[], //@parm INOUT | array of property values
IUnknown** ppRowset //@parm OUT | where to return interface
)
{
int cCharsCopied;
char szFileName[MAX_PATH ];
char szFile[_MAX_PATH ] = "";

CFileIO* pFileio = NULL;
CRowset* pRowset = NULL;
HRESULT hr;

// NULL out-params in case of error
if( ppRowset )
*ppRowset = NULL;

// validate in-params
if (pUnkOuter)
return ResultFromScode( DB_E_NOAGGREGATION );

if (!pTableID && !pIndexID)
return ResultFromScode( E_INVALIDARG );

if (pTableID->eKind != DBKIND_NAME || !pTableID->uName.pwszName)
return ResultFromScode( E_INVALIDARG );

if ((cProperties != 0) && (!rgProperties))
return ResultFromScode( E_INVALIDARG );

assert( m_pObj );

// this DBSession object can only create 1 Rowset object
if (m_pObj->m_fRowsetCreated)
return ResultFromScode( E_FAIL );

// get file name
cCharsCopied = WideCharToMultiByte( CP_ACP, 0, pTableID->uName.pwszName, -1,
szFileName, sizeof( szFileName ), NULL, NULL );
if (!cCharsCopied)
return ResultFromScode( E_FAIL );

// Concatenate the path and filename
lstrcat( szFile, m_pObj->m_szPath );
lstrcat( szFile, "\\" );
lstrcat( szFile, szFileName );

// open and initialize a file object
pFileio = new CFileIO();
if (!pFileio)
return ResultFromScode( E_OUTOFMEMORY );

hr = pFileio->fInit( szFile );
if (FAILED( hr ))
{
delete pFileio;
return ResultFromScode( E_FAIL );
}

// open and initialize a rowset\cursor object
pRowset = new CRowset( NULL );
if (!pRowset)
{
delete pFileio;
return ResultFromScode( E_OUTOFMEMORY );
}
// Initialize the rowset\cursor.
// For now, since don't yet support "setable" properties, so no properties to pass.
// The rowset will always create all of its interfaces.
// This is all-or-nothing.
if (!pRowset->FInit( pFileio ))
{
delete pFileio;
delete pRowset;
return ResultFromScode( DB_E_NOTABLE );
}
//At this point we have handed off the pFileio pointer to the sample
//provider so null it out.
pFileio = NULL;

// get requested interface pointer on rowset\cursor
hr = pRowset->QueryInterface( riid, (void **) ppRowset );
if (FAILED( hr ))
{
delete pRowset;
delete pFileio;

// After a call to QI we still want to return S_OK,
// if the ppRowset was NULL because Execution was to happen
// we just don't want to return a result set.
if( ppRowset == NULL )
return ResultFromScode(S_OK);
else
return ResultFromScode( E_NOINTERFACE );
}

// all went well
m_pObj->m_fRowsetCreated = TRUE;
//Assign creator pointer. Used for IRosetInfo::GetSpecificetion
pRowset->m_pCreator = m_pObj;
pRowset->m_pCreator->AddRef();
return ResultFromScode( S_OK );
}