Click here to Skip to main content
15,881,380 members
Articles / Database Development / SQL Server

Database Visualization

Rate me:
Please Sign up or sign in to vote.
4.60/5 (11 votes)
31 May 20067 min read 81.5K   2.8K   77  
This article aims to create a simple tool for visualizing database tables and relations, a database map to refer to.
// SelectRelationsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "querybuilder.h"
#include "SelectRelationsDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define LIST_ITEM_HEIGHT 20

/////////////////////////////////////////////////////////////////////////////
// CSelectRelationsDlg property page

IMPLEMENT_DYNCREATE(CSelectRelationsDlg, CPropertyPage)

extern CGlobals GlobalVars;

CSelectRelationsDlg::CSelectRelationsDlg() : CPropertyPage(CSelectRelationsDlg::IDD)
{
	//{{AFX_DATA_INIT(CSelectRelationsDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CSelectRelationsDlg::~CSelectRelationsDlg()
{
}

void CSelectRelationsDlg::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSelectRelationsDlg)
	//}}AFX_DATA_MAP

	DDX_Control(pDX, IDC_COMBO_TABLE_TWO, m_cmbTableTwo);
	DDX_Control(pDX, IDC_COMBO_TABLE_ONE, m_cmbTableOne);
	DDX_Control(pDX, IDC_COMBO_COLUMN_ONE, m_cmbColumnOne);
	DDX_Control(pDX, IDC_COMBO_COLUMN_TWO, m_cmbColumnTwo);
	DDX_Control(pDX, IDC_LIST_RELATIONS, m_listRelations);
}


BEGIN_MESSAGE_MAP(CSelectRelationsDlg, CPropertyPage)
	//{{AFX_MSG_MAP(CSelectRelationsDlg)
	ON_BN_CLICKED(IDC_BTN_ADD_RELATION, OnBtnAddRelation)
	ON_BN_CLICKED(IDC_BTN_CLEAR_ALL_RELATIONS, OnBtnClearAllRelations)
	ON_CBN_SELCHANGE(IDC_COMBO_TABLE_ONE, OnSelchangeComboTableOne)
	ON_CBN_SELCHANGE(IDC_COMBO_TABLE_TWO, OnSelchangeComboTableTwo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSelectRelationsDlg message handlers

BOOL CSelectRelationsDlg::OnSetActive() 
{
	CPropertySheet* psheet = (CPropertySheet*) GetParent();   
	psheet->SetWizardButtons(PSWIZB_NEXT);
	psheet->SetWizardButtons(0);

	m_btnListBoxBuddy.SubclassDlgItem( IDC_BTN_LIST_BUDDY, this);
	m_btnListBoxBuddy.SetListbox( &m_listRelations);
	
	FillTableList();

	return CPropertyPage::OnSetActive();
}

void CSelectRelationsDlg::FillTableList()
{
	m_cmbTableOne.ResetContent();
	m_cmbTableTwo.ResetContent();

	m_cmbTableOne.LockWindowUpdate();
	m_cmbTableTwo.LockWindowUpdate();

		int nTableCount = GlobalVars.g_strArrayTables.GetSize();

		for(int nIndex = 0; nIndex < nTableCount; nIndex++)
		{
			m_cmbTableOne.AddString(GlobalVars.g_strArrayTables.GetAt(nIndex));
			m_cmbTableTwo.AddString(GlobalVars.g_strArrayTables.GetAt(nIndex));
		}

	m_cmbTableOne.UnlockWindowUpdate();
	m_cmbTableTwo.UnlockWindowUpdate();
}

void CSelectRelationsDlg::RetrieveColumnList(CString strTableName)
{
	if( (GlobalVars.g_pDatabase == NULL) || (GlobalVars.g_pRecordset==NULL) )
		return;

	if(strTableName.IsEmpty())
		return;


	GlobalVars.g_pRecordset->Close();
	
	GlobalVars.g_strArrayColumns.RemoveAll();

	// Get the list of columns in the table and store it in m_strArrayColumns
	CString strSqlStmt(_T(""));
	strSqlStmt  = "Exec sp_columns ";
	strSqlStmt += strTableName;

	USES_CONVERSION;

	_variant_t varntSqlStmt;
	varntSqlStmt = A2W(strSqlStmt);

	GlobalVars.g_pRecordset->Open(varntSqlStmt,GlobalVars.g_pDatabase.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);

	_variant_t varntColName;
	_variant_t varntColType;
	_variant_t varntDataLen;

	while(!GlobalVars.g_pRecordset->EndOfFile)
	{
		 varntColName = GlobalVars.g_pRecordset->GetCollect("COLUMN_NAME"); // rs->GetFields->Field->(_variant_t(FieldNumber))->Value;

		 if(varntColName.vt!=VT_NULL)
			GlobalVars.g_strArrayColumns.Add((char*)_bstr_t(varntColName));

		 GlobalVars.g_pRecordset->MoveNext();
	 }
}

void CSelectRelationsDlg::OnSelchangeComboTableOne() 
{
	CString strTableName(_T(""));
	m_cmbTableOne.GetLBText( m_cmbTableOne.GetCurSel(), strTableName);

	RetrieveColumnList(strTableName);

	m_cmbColumnOne.ResetContent();

	m_cmbColumnOne.LockWindowUpdate();

		int nTableCount = GlobalVars.g_strArrayColumns.GetSize();

		for(int nIndex = 0; nIndex < nTableCount; nIndex++)
			m_cmbColumnOne.AddString(GlobalVars.g_strArrayColumns.GetAt(nIndex));

	m_cmbColumnOne.UnlockWindowUpdate();
}

void CSelectRelationsDlg::OnSelchangeComboTableTwo() 
{
	CString strTableName(_T(""));
	m_cmbTableTwo.GetLBText( m_cmbTableTwo.GetCurSel(), strTableName);

	RetrieveColumnList(strTableName);

	m_cmbColumnTwo.ResetContent();

	m_cmbColumnTwo.LockWindowUpdate();

		int nTableCount = GlobalVars.g_strArrayColumns.GetSize();

		for(int nIndex = 0; nIndex < nTableCount; nIndex++)
			m_cmbColumnTwo.AddString(GlobalVars.g_strArrayColumns.GetAt(nIndex));

	m_cmbColumnTwo.UnlockWindowUpdate();
}

void CSelectRelationsDlg::OnBtnAddRelation() 
{
	CString strRelation(_T(""));

	CString strTableOne, strTableTwo, strColumnOne, strColumnTwo;
	strTableOne = strTableTwo = strColumnOne = strColumnTwo = _T("");

	int nIndex = -1;

	nIndex = m_cmbTableOne.GetCurSel();
	if(nIndex > -1)
		m_cmbTableOne.GetLBText(nIndex,strTableOne);

	nIndex = m_cmbTableTwo.GetCurSel();
	if(nIndex > -1)
		m_cmbTableTwo.GetLBText(m_cmbTableTwo.GetCurSel(),strTableTwo);

	nIndex = m_cmbColumnOne.GetCurSel();
	if(nIndex > -1)
		m_cmbColumnOne.GetLBText(m_cmbColumnOne.GetCurSel(),strColumnOne);

	nIndex = m_cmbColumnTwo.GetCurSel();
	if(nIndex > -1)
		m_cmbColumnTwo.GetLBText(m_cmbColumnTwo.GetCurSel(),strColumnTwo);

	if(strTableOne.IsEmpty() || strTableTwo.IsEmpty() || strColumnOne.IsEmpty() || strColumnTwo.IsEmpty())
	{
		AfxMessageBox("Fill in all values for the following: \n\n 1) Table 1 \n 2) Table 2 \n 3) Column 1 \n 4) Column 2");
		return;
	}

	strRelation = strTableOne + "." + strColumnOne + "  >  " + strTableTwo + "." + strColumnTwo;


	if(m_listRelations.FindString(0,strRelation) < 0)
	{
		m_listRelations.AddString(strRelation);
		int nIndex = m_listRelations.FindString(0,strRelation);
		m_listRelations.SetItemHeight(nIndex,LIST_ITEM_HEIGHT);
		m_listRelations.SetCurSel(nIndex);
	}
	else
		AfxMessageBox("The relationship already exists in the list.");

	if(m_listRelations.GetCount()>=1)
	{
		CPropertySheet* psheet = (CPropertySheet*) GetParent();   
		psheet->SetWizardButtons(PSWIZB_NEXT);
	}
}

void CSelectRelationsDlg::OnBtnClearAllRelations() 
{
	if(m_listRelations.GetCount()<=0) return;

	int nResult = MessageBox("Do you really want to delete all relationships","Confirmation",MB_YESNO | MB_ICONQUESTION);

	if(nResult==IDYES)
		m_listRelations.ResetContent();

	CPropertySheet* psheet = (CPropertySheet*) GetParent();   
	psheet->SetWizardButtons(0);
}

LRESULT CSelectRelationsDlg::OnWizardNext() 
{
	GlobalVars.g_strSelectedRelationsList.RemoveAll();

	CString strTableAndColumn(_T(""));
	
	int nColumnCount = m_listRelations.GetCount();

	for(int nIndex = 0; nIndex < nColumnCount; nIndex++)
	{
		m_listRelations.GetText(nIndex,strTableAndColumn);
		GlobalVars.g_strSelectedRelationsList.Add(strTableAndColumn);
	}

	return CPropertyPage::OnWizardNext();
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions