Click here to Skip to main content
12,301,394 members (60,697 online)
Click here to Skip to main content
Articles » Database » Database » General » Downloads

Stats

63.8K views
2.3K downloads
75 bookmarked
Posted

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
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
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
// Visualization.cpp: implementation of the CVisualization class.
#include "stdafx.h"
#include "Visualization.h"

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

// Interface IDs

const IID IID_IDOT		= {0xA1080582,0xD33F,0x486E,{0xBD,0x5F,0x58,0x19,0x89,0xA3,0xC7,0xE9}};
const CLSID CLSID_DOT	= {0x55811839,0x47B0,0x4854,{0x81,0xB5,0x0A,0x00,0x31,0xB8,0xAC,0xEC}};	

// Construction / Destruction

CVisualization::CVisualization()
{
	Init();
}

CVisualization::~CVisualization()
{
	CleanUp();
}

/* Private Functions */

	void CVisualization::BeginTable(CString strTableName)
	{
		m_strGraph += " ";
		m_strGraph += strTableName;
		m_strGraph += " [shape=record, style=filled, color=violetred, fillcolor=lightyellow, label = \"{ ";
		m_strGraph += strTableName;
	}

	void CVisualization::EndTable()
	{
		m_strGraph += "}\"];";
	}

	void CVisualization::AddField(CString strFieldName)
	{
		m_strGraph += "| {";
		m_strGraph += strFieldName;
		m_strGraph += " }";
	}

	void CVisualization::Link(CString strNodeOne, CString strNodeTwo, CString strLabel,CString strColor)
	{
		m_strGraph += "edge[color=";
		m_strGraph += strColor;
		m_strGraph += "];";

			m_strGraph += strNodeOne;
			m_strGraph += "->";
			m_strGraph += strNodeTwo;
			m_strGraph += "[label=\"";
			m_strGraph += strLabel;
			m_strGraph += "\"];";

		m_strGraph += "edge[color=black];";
	}

	BOOL CVisualization::Init()
	{
		m_strGraph = _T("");

		HRESULT hr;

		hr = CoInitialize(NULL);

		if(FAILED(hr))
			return FALSE;

		hr = CoCreateInstance(CLSID_DOT, NULL, CLSCTX_ALL, IID_IDOT, (void **)(&m_pVisualization));

		if(FAILED(hr))
			return FALSE;

		// Begin Graph
		m_strGraph = "digraph G { \n";

		return TRUE;
	}

	void CVisualization::CleanUp()
	{
		CoUninitialize();
	}

/* End of Private Functions */

void CVisualization::AddComment(CString strComment)
{
	m_strGraph += "Comment [shape=record, style=filled, color=red, fillcolor=white, label = \"{ ";
	m_strGraph += strComment;
	m_strGraph += "}\"];";
}

void CVisualization::AddTable(CString strTableName,CStringArray &strArrayFields)
{
	BeginTable(strTableName);

	int nArrayCount = strArrayFields.GetSize();

	for(int nIndex =0; nIndex < nArrayCount; nIndex++)
	{
		AddField(strArrayFields.GetAt(nIndex));
	}

	strArrayFields.RemoveAll();

	EndTable();
}

void CVisualization::AddLink(CString strNodeOne, CString strNodeTwo, CString strLabel,CString strColor)
{
	Link(strNodeOne,strNodeTwo,strLabel,strColor);
}

BOOL CVisualization::SaveToFile(int nImageFormat,BOOL bOpenFile)
{
	CString strFilter(_T(""));

	switch(nImageFormat)
	{
	case SVG:
		strFilter = "Svg Files (*.svg)|*.svg||";
		break;
	case JPG:
		strFilter = "Jpg Files (*.jpg)|*.jpg||";
		break;
	case GIF:
		strFilter = "Gif Files (*.gif)|*.gif||";
		break;
	case PNG:
		strFilter = "Png Files (*.png)|*.png||";
		break;
	case EMF:
		strFilter = "Emf Files (*.emf)|*.emf||";
		break;
	}

	CFileDialog SaveDialog(FALSE,NULL,NULL,OFN_OVERWRITEPROMPT,strFilter);

	int iRet = SaveDialog.DoModal();

	CString strFullPath(_T(""));
	strFullPath = SaveDialog.GetPathName();

	int nDelimiter = strFullPath.ReverseFind('.');
	BOOL bValidExtension = FALSE;

	if(nDelimiter>0)
	{
		CString strExt(_T(""));
		strExt = strFullPath.Mid(nDelimiter+1);

		strExt.MakeUpper();

		if(strExt == "SVG" || strExt == "JPG" || strExt == "GIF" || strExt == "PNG" || strExt == "EMF")
			bValidExtension = TRUE;
		else
			bValidExtension = FALSE;
	}

	if(bValidExtension==FALSE)
	{
		switch(nImageFormat)
		{
		case SVG:
			strFullPath += ".svg";
			break;
		case JPG:
			strFullPath += ".jpg";
			break;
		case GIF:
			strFullPath += ".gif";
			break;
		case PNG:
			strFullPath += ".png";
			break;
		case EMF:
			strFullPath += ".emf";
			break;
		}
	}

	if(iRet == IDOK)
	{
		// End Graph
		m_strGraph += "\n }";

		if(nImageFormat==SVG)
		{
			USES_CONVERSION;
			CComBSTR result;

			HRESULT hr = m_pVisualization->ToSvg(A2BSTR(m_strGraph),&result);

			if (SUCCEEDED(hr))
			{
				FILE *fp;
				fp=fopen(strFullPath, "w");
				fprintf(fp,OLE2T(result));
				fclose(fp);
			}
			else
				return FALSE;
		}
		else if(nImageFormat==JPG || nImageFormat==GIF || nImageFormat==PNG || nImageFormat==EMF)
		{
			IBinaryImage *pBinImage;
			HRESULT hr;
			
			switch(nImageFormat)
			{
			case JPG:
				hr= m_pVisualization->ToJPEG(A2BSTR(m_strGraph),&pBinImage);
				break;
			case GIF:
				hr= m_pVisualization->ToGIF(A2BSTR(m_strGraph),&pBinImage);
				break;
			case PNG:
				hr= m_pVisualization->ToPNG(A2BSTR(m_strGraph),&pBinImage);
				break;
			case EMF:
				hr= m_pVisualization->ToEMF(A2BSTR(m_strGraph),&pBinImage);
				break;
			}

			if(SUCCEEDED(hr))
			{
				VARIANT_BOOL vrntBoolResult = VARIANT_FALSE;
				hr = pBinImage->Save(A2BSTR(strFullPath),&vrntBoolResult);

				if(FAILED(hr))
					return FALSE;
			}
			else
				return FALSE;
		}

		if(bOpenFile==TRUE)
		{
			ShellExecute(NULL,"open",strFullPath,NULL,NULL,SW_SHOW);
		}
	}

	return TRUE;
}

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

You may also be interested in...

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