Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » General » Downloads
 
Add your own
alternative version

Database Visualization

, 31 May 2006
This article aims to create a simple tool for visualizing database tables and relations, a database map to refer to.
src.zip
src
msado15.tlh
msado15.tli
QueryBuilder.dsp
QueryBuilder.dsw
QueryBuilder.exe
QueryBuilder.clw
res
QueryBuilder.ico
logo.bmp
msado15.tlh
msado15.tli
Manifest
default1.bin
ListIcons.bmp
wingraphviz.zip
WinGraphViz
WinGraphviz_v1.02.25s.cab
WinGraphviz_v1.02.24.msi
WinGraphviz_v1.02.24.cab
WinGraphviz_v1.02.20.msi
WinGraphviz_v1.02.20.cab
dotguide.pdf
// 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

Share

About the Author

VGirish
Founder
India India
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150129.1 | Last Updated 31 May 2006
Article Copyright 2006 by VGirish
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid