Click here to Skip to main content
15,896,912 members
Articles / Desktop Programming / MFC

A Set of ADOX Classes

Rate me:
Please Sign up or sign in to vote.
4.95/5 (34 votes)
19 Jun 2002CPOL6 min read 950.1K   5.1K   152  
Simple database catalog access using a set of ADOX classes
//
//  MODULE:   AdoX.h
//
//	AUTHOR: Carlos Antollini 
//
//  mailto: cantollini@hotmail.com
//
//	Date: 06/14/2002
//
//	Version 1.02
// 
#ifndef _ADOX_H_
#define _ADOX_H_


#include <afx.h>
#include <afxdisp.h>

#pragma warning (disable: 4146)
#import "c:\Program Files\Common Files\system\ado\msadox.dll" no_namespace 
#import "c:\Program Files\Common Files\system\ado\msado15.dll" rename("EOF", "EndOfFile")

#include "icrsint.h"

#pragma warning (default: 4146)

class CADOXIndex;
class CADOXCatalog;

class CADOXUser
{
public:
	CADOXUser(CADOXCatalog* pCat);
	~CADOXUser();
	void GetName(CString& strName);
	bool Create(LPCTSTR lpstrUserName);
	bool Open(LPCTSTR lpstrUserName);
	bool ChangePassword(LPCTSTR lpstrOldPassword, LPCTSTR lpstrNewPassword);

public:
	_UserPtr m_pUser;

protected:
	_CatalogPtr m_pCatalog;

protected:
	void dump_com_error(_com_error &e);

};

class CADOXView
{
public:
	CADOXView(CADOXCatalog* pCat);
	~CADOXView();
	void GetCommand(CString& strCommand);
	void GetName(CString& strName);
	bool Open(LPCTSTR lpstrViewName);
	bool Create(CString strName, CString strCommand);

public:
	ViewPtr m_pView;

protected:
	_CatalogPtr m_pCatalog;

protected:
	void dump_com_error(_com_error &e);
};

class CADOXProcedure
{
public:
	CADOXProcedure(CADOXCatalog* pCat);
	~CADOXProcedure();
	void GetName(CString& strName);
	bool Open(LPCTSTR lpstrProcedureName);
	bool Create(CString strName, CString strCommand);
	void GetCommand(CString &strCommand);

public:
	ProcedurePtr m_pProcedure;

protected:
	_CatalogPtr m_pCatalog;

protected:
	void dump_com_error(_com_error &e);

};

class CADOXTable
{

public:
	enum DataType
	{
		typeSmallInt = adSmallInt,
		typeInteger = adInteger,
		typeUnsignedTinyInt = adUnsignedTinyInt,
		typeUnsignedSmallInt = adUnsignedSmallInt,
		typeUnsignedInt = adUnsignedInt,
		typeUnsignedBigInt = adUnsignedBigInt,
		typeSingle = adSingle,
		typeDouble = adDouble,
		typeCurrency = adCurrency,
		typeDecimal = adDecimal,
		typeNumeric = adNumeric,
		typeBoolean = adBoolean,
		typeDate = adDate,
		typeDBDate = adDBDate,
		typeDBTime = adDBTime,
		typeDBTimeStamp = adDBTimeStamp,
		typeBSTR = adBSTR,
		typeVarChar = adVarChar,
		typeLongVarChar = adLongVarChar,
		typeWChar = adWChar,
		typeVarWChar = adVarWChar,
		typeLongVarWChar = adLongVarWChar,
		typeBinary = adBinary,
		typeVarBinary = adVarBinary,
		typeLongVarBinary = adLongVarBinary,
		typeChapter = adChapter,
		typeFileTime = adFileTime,
		typePropVariant = adPropVariant,
		typeVarNumeric = adVarNumeric
	};
	
	_TablePtr m_pTable;

	//CADOXTable(CADOXCatalog* pCat);
	CADOXTable(CADOXCatalog* pCat, LPCTSTR lpstrTableName = _T(""));
	CADOXTable(CADOXCatalog* pCat, int nTableIndex);
	~CADOXTable();

	bool Create(LPCTSTR lpstrTableName);
	bool Open(LPCTSTR lpstrTableName);
	bool Open(long nTableIndex);
	bool AddField(LPCTSTR lpstrFieldName, enum DataType Type, int nLength = 0);
	bool AddIndex(CADOXIndex pIndex);
	bool DeleteField(LPCTSTR lpstrFieldName);
	void GetName(CString& strTableName);

protected:
	_CatalogPtr m_pCatalog;

protected:
	void dump_com_error(_com_error &e);

};


class CADOXIndex
{
public:
	enum DataType
	{
		typeSmallInt = adSmallInt,
		typeInteger = adInteger,
		typeUnsignedTinyInt = adUnsignedTinyInt,
		typeUnsignedSmallInt = adUnsignedSmallInt,
		typeUnsignedInt = adUnsignedInt,
		typeUnsignedBigInt = adUnsignedBigInt,
		typeSingle = adSingle,
		typeDouble = adDouble,
		typeCurrency = adCurrency,
		typeDecimal = adDecimal,
		typeNumeric = adNumeric,
		typeBoolean = adBoolean,
		typeDate = adDate,
		typeDBDate = adDBDate,
		typeDBTime = adDBTime,
		typeDBTimeStamp = adDBTimeStamp,
		typeBSTR = adBSTR,
		typeVarChar = adVarChar,
		typeLongVarChar = adLongVarChar,
		typeWChar = adWChar,
		typeVarWChar = adVarWChar,
		typeLongVarWChar = adLongVarWChar,
		typeBinary = adBinary,
		typeVarBinary = adVarBinary,
		typeLongVarBinary = adLongVarBinary,
		typeChapter = adChapter,
		typeFileTime = adFileTime,
		typePropVariant = adPropVariant,
		typeVarNumeric = adVarNumeric
	};
	
	_IndexPtr m_pIndex;

	CADOXIndex()
	{
		//::CoInitialize(NULL);
		m_pIndex = NULL;
		m_pIndex.CreateInstance(__uuidof(Index));
	}

	~CADOXIndex()
	{
		m_pIndex.Release();
		m_pIndex = NULL;
		//::CoUninitialize();
	}

	bool Create(LPCTSTR lpstrIndexName);
	bool AddField(LPCTSTR lpstrIndexName, enum DataType Type, int nLength = 0);
	void SetPrimarKey(bool bPrimary = true);
protected:
	void dump_com_error(_com_error &e);
};


class CADOXCatalog
{
public:
	bool Open(LPCTSTR lpstrConnection);
	bool CreateDatabase(LPCTSTR lpstrCreate);
	bool AddTable(CADOXTable pTable);
	bool AddUser(CADOXUser pUser, LPCTSTR lpstrPassword);

	long GetProcedureCount()
		{return m_pCatalog->Procedures->GetCount();};
	long GetTableCount()
		{return m_pCatalog->Tables->GetCount();};
	long GetViewCount();
	long GetUserCount()
		{return m_pCatalog->Users->GetCount();};
	long GetGroupCount()
		{return m_pCatalog->Groups->GetCount();};

	void GetTableName(long nTableIndex, CString &strTableName);
	void GetProcedureName(long nProcedureIndex, CString &strProcedureName);
	void GetViewName(long nViewIndex, CString &strViewName);
	void GetUserName(long nUserIndex, CString &strUserName);
	void GetGroupName(long nGroupIndex, CString &strGroupName);
	bool DeleteTable(LPCTSTR lpstrTableName);
	bool DeleteTable(long nTableIndex);
	bool DeleteProcedure(long nProcedureIndex);
	bool DeleteProcedure(LPCTSTR lpstrProcedureName);
	bool DeleteView(LPCTSTR lpstrViewName);
	bool DeleteView(long nViewIndex);
	bool DeleteGroup(LPCTSTR lpstrGroupName);
	bool DeleteGroup(long nGroupIndex);
	bool DeleteUser(LPCTSTR lpstrUserName);
	bool DeleteUser(long nViewIndex);

	CADOXCatalog()
	{
		::CoInitialize(NULL);
		m_pCatalog = NULL;
		m_pCatalog.CreateInstance(__uuidof(Catalog));
	}

	~CADOXCatalog()
	{
		m_pCatalog.Release();
		m_pCatalog = NULL;
		::CoUninitialize();
	}

	_CatalogPtr m_pCatalog;	

protected:
	void dump_com_error(_com_error &e);
	
};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Citigroup
Argentina Argentina
Carlos Antollini is a software engineer working on Object Oriented, Visual C++, MFC, COM, ATL, ADO, Internet technologies and Business Intelligence.
Carlos is originally from Argentina, he was living for several years in Fort Lauderdale, Florida, working for Citibank. Then he started his own business.
Carlos is the creator of <a href="http://www.piFive.com">piFive</a>[<a target="_blank" title="piFive" href="http://www.piFive.com">^</a>], a family of BI Analytic Platform software, that it deals next to, <a href="http://www.latinsys.com">latinsys</a>[<a target="_blank" title="latinsys" href="http://www.latinsys.com">^</a>], his partner in businesses...
Currently he is sharing his passion for project management and BI at Citigroup.

Comments and Discussions