Click here to Skip to main content
15,867,308 members
Articles / Database Development / SQL Server

PostgreSQL/libpqxx Class Generator

Rate me:
Please Sign up or sign in to vote.
4.87/5 (15 votes)
18 Aug 200627 min read 82.2K   1.9K   37  
Automated generation of PostgreSQL data transfer classes.
#pragma once

#ifndef __ATLDDX_H__
	#error PWinDataExchange.h requires atlddx.h to be included first
#endif

#ifndef __ATLCOMTIME_H__
	#error PWinDataExchange.h requires atlcomtime.h to be included first
#endif

template<class T>
class CPWinDataExchange : public CWinDataExchange<T>
{
	typedef CWinDataExchange<T> baseClass;

public:

	// Text exchange for CURRENCY
	BOOL DDX_Text(UINT nID, CURRENCY& cy, int cbSize, BOOL bSave, BOOL bValidate = FALSE, int nLength = 0)
	{
		T* pT = static_cast<T*>(this);
		BOOL bSuccess = TRUE;
		const int cchBuff = 64;
		TCHAR szBuff[cchBuff] = { 0 };

		if (bSave)
		{
			pT->GetDlgItemText(nID, szBuff, cchBuff);
			double d = 0;
			if (baseClass::_AtlSimpleFloatParse(szBuff, d))
			{
				cy.int64 = (__int64)((d * 10000.0) + 0.5);
			}
			else
			{
				bSuccess = FALSE;
			}
		}
		else
		{
			sprintf(szBuff, "%I64d.%.2I64d", cy.int64 / 10000, (cy.int64 % 10000) / 100);
			bSuccess = pT->SetDlgItemText(nID, szBuff);
		}

		if (!bSuccess)
		{
			pT->OnDataExchangeError(nID, bSave);
		}
		return bSuccess;
	}

	// Text exchange for COleDateTime
	BOOL DDX_Text(UINT nID, COleDateTime& dt, int cbSize, BOOL bSave, BOOL bValidate = FALSE, int nLength = 0)
	{
		T* pT = static_cast<T*>(this);
		BOOL bSuccess = TRUE;
		const int cchBuff = 64;
		TCHAR szBuff[cchBuff] = { 0 };

		if (bSave)
		{
			pT->GetDlgItemText(nID, szBuff, cchBuff);
			dt.ParseDateTime(szBuff);
			bSuccess = (dt.GetStatus() == COleDateTime::valid || 0 == *szBuff);
		}
		else
		{
			bSuccess = pT->SetDlgItemText(nID, dt.Format(VAR_DATEVALUEONLY));
		}

		if (!bSuccess)
		{
			pT->OnDataExchangeError(nID, bSave);
		}
		return bSuccess;
	}

	// Ensure we have access to the parent class's versions of DDX_Text
	BOOL DDX_Text(UINT nID, LPTSTR lpstrText, int cbSize, BOOL bSave, BOOL bValidate = FALSE, int nLength = 0)
	{
		return baseClass::DDX_Text(nID, lpstrText, cbSize, bSave, bValidate, nLength);
	}
	BOOL DDX_Text(UINT nID, BSTR& bstrText, int cbSize, BOOL bSave, BOOL bValidate = FALSE, int nLength = 0)
	{
		return baseClass::DDX_Text(nID, bstrText, cbSize, bSave, bValidate, nLength);
	}
	BOOL DDX_Text(UINT nID, ATL::CComBSTR& bstrText, int cbSize, BOOL bSave, BOOL bValidate = FALSE, int nLength = 0)
	{
		return baseClass::DDX_Text(nID, bstrText, cbSize, bSave, bValidate, nLength);
	}
#if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
	BOOL DDX_Text(UINT nID, _CSTRING_NS::CString& strText, int cbSize, BOOL bSave, BOOL bValidate = FALSE, int nLength = 0)
	{
		return baseClass::DDX_Text(nID, strText, cbSize, bSave, bValidate, nLength);
	}
#endif //defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
};

#define DDX_CURR(nID, var) \
		if (nCtlID == (UINT)-1 || nCtlID == nID) \
		{ \
			if (!DDX_Text(nID, var, sizeof(var), bSaveAndValidate)) \
				return FALSE; \
		}

#define DDX_DATE(nID, var) \
		if(nCtlID == (UINT)-1 || nCtlID == nID) \
		{ \
			if (!DDX_Text(nID, var, sizeof(var), bSaveAndValidate)) \
				return FALSE; \
		}

#define DECLARE_DDX_MAP(thisClass) \
	BOOL DoDataExchange(BOOL bSaveAndValidate = FALSE, UINT nCtlID = (UINT)-1);

#define IMPLEMENT_DDX_MAP(thisClass) \
	BOOL thisClass::DoDataExchange(BOOL bSaveAndValidate, UINT nCtlID) \
	{ \
		bSaveAndValidate; \
		nCtlID;

#define IMPLEMENT_COMBOTEXTEXCH() \
	BOOL GetDlgItemText(int nID, ATL::CSimpleString& strText) \
	{ \
		CWindow ctrl = GetDlgItem(nID); \
		char buf[MAX_PATH]; \
		GetClassName(ctrl, buf, sizeof(buf)); \
		if (0 == strcmp(buf, "ComboBox")) \
		{ \
			WTL::CComboBox cmb = ctrl; \
			CString txt; \
			cmb.GetLBText(cmb.GetCurSel(), buf); \
			strText = buf; \
			return TRUE; \
		} \
		else \
		{ \
			return __super::GetDlgItemText(nID, strText); \
		} \
		return FALSE; \
	} \
	BOOL SetDlgItemText(int nID, LPCTSTR lpszString) \
	{ \
		CWindow ctrl = GetDlgItem(nID); \
		char buf[MAX_PATH]; \
		GetClassName(ctrl, buf, sizeof(buf)); \
		if (0 == strcmp(buf, "ComboBox")) \
		{ \
			WTL::CComboBox cmb = ctrl; \
			int selItem = cmb.FindString(0, lpszString); \
			if (selItem != CB_ERR) \
			{ \
				cmb.SetCurSel(selItem); \
				return TRUE; \
			} \
			else if (*lpszString == 0) \
			{ \
				cmb.SetCurSel(CB_ERR); \
				return TRUE; \
			} \
			else \
			{ \
				cmb.SetWindowText(lpszString); \
				return TRUE; \
			} \
		} \
		else \
		{ \
			return __super::SetDlgItemText(nID, lpszString); \
		} \
		return FALSE; \
	}

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
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions