Click here to Skip to main content
15,891,136 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.8K   2.8K   77  
This article aims to create a simple tool for visualizing database tables and relations, a database map to refer to.
// SelectColumnsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "querybuilder.h"
#include "SelectColumnsDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSelectColumnsDlg property page
IMPLEMENT_DYNCREATE(CSelectColumnsDlg, CPropertyPage)

extern CGlobals GlobalVars;

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

CSelectColumnsDlg::~CSelectColumnsDlg()
{
}

void CSelectColumnsDlg::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSelectColumnsDlg)
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_COMBO_TABLE, m_cmbTable);
	DDX_Control(pDX, IDC_COMBO_COLUMN, m_cmbColumn);
	DDX_Control(pDX, IDC_LIST_SELECTED_COLUMNS, m_listSelectedColumns);
}


BEGIN_MESSAGE_MAP(CSelectColumnsDlg, CPropertyPage)
	//{{AFX_MSG_MAP(CSelectColumnsDlg)
	ON_BN_CLICKED(IDC_BTN_ADD_COLUMN, OnBtnAddColumn)
	ON_BN_CLICKED(IDC_BTN_CLEAR_ALL_SEL_COLUMNS, OnBtnClearAllSelColumns)
	//}}AFX_MSG_MAP
	ON_CBN_SELCHANGE(IDC_COMBO_TABLE, OnSelchangeComboTable)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSelectColumnsDlg message handlers

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

	m_btnListBoxBuddy.SubclassDlgItem( IDC_BTN_LIST_BUDDY, this);
	m_btnListBoxBuddy.SetListbox( &m_listSelectedColumns);

	FillTableList();

	return CPropertyPage::OnSetActive();
}

void CSelectColumnsDlg::FillTableList()
{
	m_cmbTable.ResetContent();
	m_cmbTable.LockWindowUpdate();

		int nTableCount = GlobalVars.g_strArrayTables.GetSize();

		for(int nIndex = 0; nIndex < nTableCount; nIndex++)
			m_cmbTable.AddString(GlobalVars.g_strArrayTables.GetAt(nIndex));

	m_cmbTable.UnlockWindowUpdate();
}

void CSelectColumnsDlg::OnSelchangeComboTable()
{
	CString strTableName(_T(""));
	m_cmbTable.GetLBText( m_cmbTable.GetCurSel(), strTableName);

	RetrieveColumnList(strTableName);

	m_cmbColumn.ResetContent();

	m_cmbColumn.LockWindowUpdate();

		int nTableCount = GlobalVars.g_strArrayColumns.GetSize();

		// change
		m_cmbColumn.AddString("All");

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

	m_cmbColumn.UnlockWindowUpdate();
}

void CSelectColumnsDlg::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 CSelectColumnsDlg::OnBtnAddColumn() 
{
	CString strColumnDeclaration(_T(""));

	int nRowIndex = m_cmbTable.GetCurSel();

	if(nRowIndex<0)
		return;

	CString strTableName(_T(""));
	m_cmbTable.GetLBText(nRowIndex, strTableName);

	int nColIndex = m_cmbColumn.GetCurSel();

	if(nColIndex<0)
		return;

	CString strColumnName(_T(""));
	m_cmbColumn.GetLBText(nColIndex, strColumnName);

	if(strTableName.IsEmpty() || strColumnName.IsEmpty())
		return;

	// change
	if(strColumnName=="All")
	{
		// Find all columns in the table and add it if it does not exist in the list already
		int nColumnCount = GlobalVars.g_strArrayColumns.GetSize();

		// change
		m_cmbColumn.AddString("All");

		CString strColumn(_T(""));

		for(int nIndex = 0; nIndex < nColumnCount; nIndex++)
		{
			strColumn = GlobalVars.g_strArrayColumns.GetAt(nIndex);
			strColumnDeclaration.Format("%s.%s",strTableName,strColumn);

			if(m_listSelectedColumns.FindString(0,strColumnDeclaration) < 0)
				m_listSelectedColumns.AddString(strColumnDeclaration);
		}
	}// end of change
	else
	{
		strColumnDeclaration.Format("%s.%s",strTableName,strColumnName);		

		if(m_listSelectedColumns.FindString(0,strColumnDeclaration) < 0)
		{
			m_listSelectedColumns.AddString(strColumnDeclaration);
		}
		else
			AfxMessageBox("The selected column already exists in the list.");
	}

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

void CSelectColumnsDlg::OnBtnClearAllSelColumns() 
{
	m_listSelectedColumns.ResetContent();

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

LRESULT CSelectColumnsDlg::OnWizardNext() 
{
	GlobalVars.g_strSelectedColumnsList.RemoveAll();

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

	for(int nIndex = 0; nIndex < nColumnCount; nIndex++)
	{
		m_listSelectedColumns.GetText(nIndex,strTableAndColumn);
		GlobalVars.g_strSelectedColumnsList.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