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

PostgreSQL/libpqxx Class Generator

, 18 Aug 2006
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

Share

About the Author

_oti

Australia Australia
No Biography provided

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