Click here to Skip to main content
15,881,172 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.5K   1.9K   37  
Automated generation of PostgreSQL data transfer classes.
/***************************************************************************
 pgpqtestView.h - WTL view class, based on a CListViewCtrl. This class
                  handles the list of records, and also implements the
				  database operations.

 begin     : August 2006
 copyright : (C) 2006 by Phil Cairns
 email     : oti169@hotmail.com
 $Id: pgpqtestView.cpp 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 "stdafx.h"
#include "resource.h"
#include ".\pgpqtestview.h"
#include "DataManager.h"
#include "articlesrs.h"
#include "ArticleEditDlg.h"

BOOL CPgpqtestView::PreTranslateMessage(MSG* pMsg)
{
	pMsg;
	return FALSE;
}

LRESULT CPgpqtestView::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
	bHandled = FALSE;

	// Add the columns to the view, with a dummy one to start so that we
	// can right-align the ID column.
	InsertColumn(0, "ID", LVCFMT_RIGHT, 30, 0);
	InsertColumn(1, "Author", LVCFMT_LEFT, 120, 1);
	InsertColumn(2, "Title", LVCFMT_LEFT, 250, 2);
	InsertColumn(3, "Submitted", LVCFMT_LEFT, 80, 3);
	InsertColumn(4, "Price", LVCFMT_LEFT, 70, 4);

	return 0;
}

LRESULT CPgpqtestView::OnDblClk(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)
{
	Edit();
	return 0;
}

void CPgpqtestView::Refresh()
{
	DeleteAllItems();

	Database::CArticlesRS ars;
	ars.order("author");
	dmwork TRX(DataManagerS::instance().dbConn(), "TRX002");
	pqxx::result dbr = TRX.exec(ars.query());
	pqxx::result::const_iterator rsit;
	int item = 0;
	for (rsit = dbr.begin(); rsit != dbr.end(); rsit++)
	{
		ars = rsit;
		item = InsertItem(item, pqxx::safe_value(ars.id).c_str());
		SetItemText(item, 1, ars.author.c_str());
		SetItemText(item, 2, ars.title.c_str());
		SetItemText(item, 3, ars.submitted.Format("%d-%b-%Y"));
		SetItemText(item, 4, ars.price.Format("%.2f"));
		SetItemData(item, ars.id);
	}
}

void CPgpqtestView::Edit()
{
	int item = GetNextItem(-1, LVNI_SELECTED);
	if (item == -1)
	{
		MessageBox("No item selected", "Edit Item", MB_ICONINFORMATION);
		return;
	}

	int articleId = GetItemData(item);
	CArticleEditDlg dlg;
	Database::CArticlesRS ars;
	// Need a block for the read transaction
	try
	{
		dmwork TRX(DataManagerS::instance().dbConn(), "TRX003");
		ars.load(TRX, articleId);
	}
	catch (pqxx::sql_error& xsql)
	{
		MessageBox(xsql.what(), "Edit Item", MB_ICONSTOP);
	}
	dlg.m_archived = (ars.archived == true);
	dlg.m_author = ars.author.c_str();
	dlg.m_content = ars.content.c_str();
	dlg.m_id.Format("%d", ars.id);
	dlg.m_words.Format("%d", ars.numwords);
	dlg.m_price = ars.price.Format("%.2f");
	dlg.m_submitted = ars.submitted.Format("%d-%b-%Y");
	dlg.m_title = ars.title.c_str();
	dlg.m_weight.Format("%.2f", ars.weight);
	if (IDOK == dlg.DoModal())
	{
		ars.archived = (dlg.m_archived == TRUE);
		ars.author = (const char*)dlg.m_author;
		ars.content = (const char*)dlg.m_content;
		ars.numwords = atoi(dlg.m_words);
		ars.price.ParseCurrency(dlg.m_price);
		ars.submitted.ParseDateTime(dlg.m_submitted);
		ars.title = (const char*)dlg.m_title;
		ars.weight = atof(dlg.m_weight);
		try
		{
			dmwork TRX(DataManagerS::instance().dbConn(), "TRX004");
			ars.save(TRX);
			TRX.commit();
			Refresh();
		}
		catch (pqxx::sql_error& xsql)
		{
			MessageBox(xsql.what(), "Edit Item", MB_ICONSTOP);
		}
	}
}

void CPgpqtestView::Delete()
{
	int item = GetNextItem(-1, LVNI_SELECTED);
	if (item == -1)
	{
		MessageBox("No item selected", "Delete Item", MB_ICONINFORMATION);
		return;
	}

	try
	{
		int articleId = GetItemData(item);
		Database::CArticlesRS ars;
		dmwork TRX(DataManagerS::instance().dbConn(), "TRX005");
		ars.load(TRX, articleId);
		ars.del(TRX);
		TRX.commit();
		Refresh();
	}
	catch (pqxx::sql_error& xsql)
	{
		MessageBox(xsql.what(), "Delete Item", MB_ICONSTOP);
	}
}

void CPgpqtestView::New()
{
	CArticleEditDlg dlg;
	dlg.m_id = "New";
	if (IDOK == dlg.DoModal())
	{
		dmwork TRX(DataManagerS::instance().dbConn(), "TRX006");
		Database::CArticlesRS ars;
		ars.archived = (dlg.m_archived == TRUE);
		ars.author = (const char*)dlg.m_author;
		ars.content = (const char*)dlg.m_content;
		ars.numwords = atoi(dlg.m_words);
		ars.price.ParseCurrency(dlg.m_price);
		ars.submitted.ParseDateTime(dlg.m_submitted);
		ars.title = (const char*)dlg.m_title;
		ars.weight = atof(dlg.m_weight);
		ars.save(TRX);
		ATLTRACE("Created a new record--the ID is %d\n", ars.id);
		TRX.commit();
		Refresh();
	}
}

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