Click here to Skip to main content
12,063,909 members (64,262 online)
Click here to Skip to main content
Articles » Database » Database » General » Downloads

Stats

55.2K views
1.4K downloads
35 bookmarked
Posted

PostgreSQL/libpqxx Class Generator

, 18 Aug 2006
Automated generation of PostgreSQL data transfer classes.
#pragma once

/***************************************************************************
 PGWizTarget.h  -  This is the wizard page where the user chooses where the
                   generated files are to be stored. The user also chooses
				   whether or not to have a "C" prefix applied to each of
				   the result set class names.

 begin     : December 2004
 copyright : (C) 2004-2006 by Phil Cairns
 email     : oti169@hotmail.com
 $Id: PGWizTarget.h 2080 2006-08-18 01:53:19Z phil $

 This code may be used in compiled form in any way you desire (including
 commercial use). The code may be redistributed unmodified by any means
 providing it is not sold for profit without the authors written consent,
 and providing that this notice and the authors name and all copyright
 notices remains intact.

 This software is provided "as is" without express or implied warranty. Use
 it at your own risk!
 ***************************************************************************/

#include "GlobalData.h"
#include "WtlEditBrowserCtrl.h"

class PGWizTarget
	: public CPropertyPageImpl<PGWizTarget>
	, public CPWinDataExchange<PGWizTarget>
{
public:
	enum { IDD = IDD_TARGET };

	PGWizTarget(void)
	{
		GlobalData& gd = GlobalDataS::instance();
		m_Target = gd.targetPath().c_str();
		m_Prefix = gd.usePrefix();
		m_GenBase = gd.genBase();
		m_DropS = gd.dropTrailingS();
	}

	CString m_Target;
	BOOL m_Prefix;
	BOOL m_GenBase;
	BOOL m_DropS;
	CWtlEditBrowserCtrl m_EditCtl;

	BEGIN_MSG_MAP(PGWizTarget)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		COMMAND_HANDLER(IDC_TARGET, WM_EB_PRESS, OnBnClickedBrowse)
		COMMAND_HANDLER(IDC_GENBASE, BN_CLICKED, OnBnClickedGenBase)
		CHAIN_MSG_MAP(CPropertyPageImpl<PGWizTarget>)
	END_MSG_MAP()

	BEGIN_DDX_MAP(PGWizTarget)
		DDX_TEXT(IDC_TARGET, m_Target)
		DDX_CHECK(IDC_PREFIX, m_Prefix)
		DDX_CHECK(IDC_GENBASE, m_GenBase)
		DDX_CHECK(IDC_DROPS, m_DropS)
	END_DDX_MAP()

	// Ensure that what we have from the registry is put into the
	// window when it is initialised.
	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		DoDataExchange(FALSE);
		m_EditCtl.SubclassWindow(GetDlgItem(IDC_TARGET));
		return 1;
	}

	// This function is used to set the initial directory for the
	// Folder Browser window.
	static INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) 
	{
		TCHAR szDir[MAX_PATH];
		const char* pszInitDir = (const char*)pData;
		
		switch(uMsg) 
		{
		case BFFM_INITIALIZED:
			if (pszInitDir != 0 && *pszInitDir)
			{
				SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
			}
			else if (GetCurrentDirectory(sizeof(szDir)/sizeof(TCHAR), szDir))
			{
				SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
			}
			break;
			
		case BFFM_SELCHANGED: 
			if (SHGetPathFromIDList((LPITEMIDLIST)lp, szDir))
			{
				SendMessage(hwnd,BFFM_SETSTATUSTEXT, 0, (LPARAM)szDir);
			}
			break;
		}
		return 0;
	}

	// The method performs the browse operation and returns a string containing
	// the chosen folder. It returns an empty string if the user cancels.
	std::string getFolderPath(const char* szPrompt, const char* szStartFolder)
	{
		std::string		sPath;
		BROWSEINFO		bi;
		TCHAR			szDir[MAX_PATH];
		LPITEMIDLIST	pidl;
		LPMALLOC		pMalloc;

		if (SUCCEEDED(SHGetMalloc(&pMalloc)))
		{
			ZeroMemory(&bi, sizeof(bi));
			bi.hwndOwner = NULL;
			bi.pszDisplayName = 0;
			bi.pidlRoot = 0;
			bi.lpszTitle = szPrompt;
			bi.ulFlags = BIF_USENEWUI | BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT | BIF_VALIDATE;
			bi.lpfn = BrowseCallbackProc;
			bi.lParam = (LPARAM)szStartFolder;

			pidl = SHBrowseForFolder(&bi);
			if (pidl)
			{
				if (SHGetPathFromIDList(pidl, szDir))
				{
					sPath = szDir;
				}
				pMalloc->Free(pidl);
				pMalloc->Release();
			}
		}
		return sPath;
	}

	// Message handler for a click on the Browse button
	LRESULT OnBnClickedBrowse(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		DoDataExchange(TRUE);
		std::string	newPath = getFolderPath("Please choose the destination path for your classes:", m_Target);
		if (newPath != "")
		{
			m_Target = newPath.c_str();
			DoDataExchange(FALSE);
		}
		return 0;
	}

	// Message handler for a click on the "Generate base class" button
	LRESULT OnBnClickedGenBase(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CButton btn = GetDlgItem(IDC_GENBASE);
		GetDlgItem(IDC_DROPS).EnableWindow(btn.GetCheck() == BST_CHECKED);
		return 0;
	}

	int OnSetActive()
	{
		DoDataExchange(TRUE);
		if (m_Target == "")
		{
			char buf[MAX_PATH];
			GetCurrentDirectory(MAX_PATH, buf);
			m_Target = buf;
		}
		DoDataExchange(FALSE);
		return 1;
	}

	LRESULT OnWizardNext()
	{
		DoDataExchange(TRUE);

		GlobalData& gd = GlobalDataS::instance();
		gd.targetPath((const char*)m_Target);
		gd.usePrefix(m_Prefix);
		gd.genBase(m_GenBase);
		gd.dropTrailingS(m_DropS);

		return 0;
	}
};

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

_oti
Australia Australia
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160204.4 | Last Updated 18 Aug 2006
Article Copyright 2005 by _oti
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid