Click here to Skip to main content
15,885,546 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.6K   1.9K   37  
Automated generation of PostgreSQL data transfer classes.
#pragma once

/***************************************************************************
 PGWizGenerate.h  -  This is the generate page. Given the table definitions
                     from PGWizTables, it will generate the a file in given
					 location for each table that is supposed to have table
					 definitions generated.

 begin     : December 2004
 copyright : (C) 2004 by Phil Cairns
 email     : oti169@hotmail.com
 $Id: PGWizGenerate.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"

#define WMA_DONEXTITEM (WM_APP + 1)

class PGWizGenerate
	: public CPropertyPageImpl<PGWizGenerate>
{
private:
	std::vector<std::string> _tableNames;

public:
	enum { IDD = IDD_GENERATE};

	PGWizGenerate(void)
	{
	}

	CString m_Target;

	BEGIN_MSG_MAP(PGWizGenerate)
		MESSAGE_HANDLER(WMA_DONEXTITEM, OnDoNextItem)
		CHAIN_MSG_MAP(CPropertyPageImpl<PGWizGenerate>)
	END_MSG_MAP()

	// When the page is activated, the list of table names that we have to
	// process is cleared, and then restocked with the names from the the
	// global table info map. I put them in a vector so that I could
	// iterate through them using the incremental message passing
	// implemented via the WMA_DONEXTITEM message. Note that this method
	// kicks off the file generation process by sending the first
	// WMA_DONEXTITEM message (WPARAM=0).
	int OnSetActive()
	{
		_tableNames.clear();

		TABLEINFOMAP& tableInfo = GlobalDataS::instance().tableInfo();
		TABLEINFOMAP::iterator timit = tableInfo.begin();
		while (timit != tableInfo.end())
		{
			_tableNames.push_back(timit->first);
			++timit;
		}

		GetDlgItem(IDC_GENLOG).SetWindowText("Starting class file generation");
		PostMessage(WMA_DONEXTITEM, 0);
		return 1;
	}

	// This method is called every time a WMA__DONEXTITEM message is sent to the
	// page. I could have made a thread to do it, but I didn't. I figured that
	// this would allow other window processing, and not get mucked around by
	// the requirement of mutexes and events.
	// Note that as long as we haven't processed all of the table names in the
	// _tablesNames vector, we're going to resend this message with an
	// incremented item number.
	LRESULT OnDoNextItem(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		TABLEINFOMAP& tableInfo = GlobalDataS::instance().tableInfo();
		size_t nextItem = (int)wParam;

		if (nextItem >= _tableNames.size())
		{
			CEdit logWnd = GetDlgItem(IDC_GENLOG);
			int textLen = logWnd.GetWindowTextLength();
			SetWizardButtons(PSWIZB_FINISH);
			logWnd.SetSel(textLen, textLen, FALSE);
			logWnd.ReplaceSel("\r\nClass file generation is complete.");
			CancelToClose();
		}
		else
		{
			TABLEINFOMAP::iterator timit = tableInfo.find(_tableNames[nextItem]);
			if (timit != tableInfo.end())
			{
				if (timit->second.generate)
				{
					if (generateFile(timit))
					{
						PostMessage(WMA_DONEXTITEM, (WPARAM)(nextItem + 1));
					}
				}
				else
				{
					PostMessage(WMA_DONEXTITEM, (WPARAM)(nextItem + 1));
				}
			}
		}
		return 1;
	}

	// This method generates a file given a table info map iterator.
	bool generateFile(TABLEINFOMAP::iterator timit)
	{
		GlobalData& gd = GlobalDataS::instance();
		CEdit logWnd = GetDlgItem(IDC_GENLOG);
		int textLen = logWnd.GetWindowTextLength();
		TableInfo& ti = timit->second;
		CString temp;
		int genColumns = 0;

		temp.Format("\r\nPutting %s into %s ... ", ti.className.c_str(), ti.fileName.c_str());
		logWnd.SetSel(textLen, textLen, FALSE);
		logWnd.ReplaceSel(temp);

		std::string fileName = gd.targetPath() + "\\" + ti.fileName;
		FILE* classFile = fopen(fileName.c_str(), "wt");
		if (classFile == 0)
		{
			temp.Format("Failed to open the destination file (%s) for writing", fileName.c_str());
			MessageBox(temp, "Generation Failure", MB_ICONSTOP);
			return false;
		}

		// Start printing class info to the file
		if (gd.genBase())
		{
			fprintf(classFile, "// $Id: PGWizGenerate.h 2080 2006-08-18 01:53:19Z phil $\n// %s: Declaration of the %s and %s classes\n\n",
				ti.fileName.c_str(),
				ti.baseClass.c_str(),
				ti.className.c_str());
		}
		else
		{
			fprintf(classFile, "// $Id: PGWizGenerate.h 2080 2006-08-18 01:53:19Z phil $\n// %s: Declaration of the %s class\n\n",
				ti.fileName.c_str(),
				ti.className.c_str());
		}
		fprintf(classFile, "// Generated by pgpqgen %s\n\n",
			(const char*)COleDateTime::GetCurrentTime().Format());
		fprintf(classFile,
			"#pragma once\n\n#include \"pqxxTransfer.h\"\n\n");
		fprintf(classFile, "namespace Database\n{\n");
		if (gd.genBase())
		{
			fprintf(classFile, "\tclass %s\n\t{\n", ti.baseClass.c_str());
		}
		else
		{
			fprintf(classFile, "\tclass %s : public CPqxxTransfer<%s>\n",
				ti.className.c_str(),
				ti.className.c_str());
			fprintf(classFile, "\t{\n\t\ttypedef CPqxxTransfer<%s> baseClass;\n\n",
				ti.className.c_str());
		}

		// Constructor (include member initialisation)
		if (gd.genBase())
		{
			fprintf(classFile, "\tpublic:\n\t\t%s()\n", ti.baseClass.c_str());
		}
		else
		{
			fprintf(classFile,
				"\tpublic:\n\t\t%s() : baseClass(\"%s\")\n",
				ti.className.c_str(),
				timit->first.c_str());
		}
		COLUMNMAP::iterator cmit = ti.columns.begin();
		bool firstMember = true;
		while (cmit != ti.columns.end())
		{
			temp = "";
			if (cmit->second.memberType == "int")
			{
				temp.Format("%s(NULLINT)\n", cmit->second.memberName.c_str());
			}
			else if (cmit->second.memberType == "double")
			{
				temp.Format("%s(NULLDBL)\n", cmit->second.memberName.c_str());
			}
			else if (cmit->second.memberType == "bool")
			{
				temp.Format("%s(false)\n", cmit->second.memberName.c_str());
			}
			if (temp != "")
			{
				fprintf(classFile, "\t\t\t%s%s",
					firstMember ? ": " : ", ",
					(const char*)temp);
				firstMember = false;
			}
			++cmit;
		}
		fprintf(classFile, "\t\t{\n");
		if (!gd.genBase())
		{
			fprintf(classFile, "\t\t\tinitColDescs();\n");
		}
		for (cmit = ti.columns.begin(); cmit != ti.columns.end(); cmit++)
		{
			if (cmit->second.memberType == "COleDateTime" && cmit->second.generate)
			{
				fprintf(classFile, "\t\t\t%s.SetStatus(COleDateTime::invalid);\n",
					cmit->second.memberName.c_str());
			}
		}
		fprintf(classFile, "\t\t}\n\n");

		// Comparison operator (only if base class is separate)
		if (gd.genBase())
		{
			fprintf(classFile, "\t\tbool operator==(const %s& other)\n\t\t{\n", ti.baseClass.c_str());
			temp = "\t\t\treturn ";
			cmit = ti.columns.begin();
			while (cmit != ti.columns.end())
			{
				std::string endOfLine = "";
				if (cmit->second.generate)
				{
					temp += (cmit->second.memberName + " == other." + cmit->second.memberName).c_str();
					fprintf(classFile, "%s", (const char*)temp);
					temp = "\t\t\t\t&& ";
					endOfLine = "\n";
				}
				++cmit;
				fprintf(classFile, "%s%s", (cmit == ti.columns.end()) ? ";" : "", endOfLine.c_str());
			}
			fprintf(classFile, "\t\t}\n\n");

			// Data member declarations
			fprintf(classFile, "\tpublic:\n");
			cmit = ti.columns.begin();
			while (cmit != ti.columns.end())
			{
				if (cmit->second.generate)
				{
					fprintf(classFile,
						"\t\t%s %s;\n",
						cmit->second.memberType.c_str(),
						cmit->second.memberName.c_str());
					++genColumns;
				}
				++cmit;
			}

			// Start the recordset class type
			fprintf(classFile, "\n\t};\n\n\tclass %s\n\t\t: public CPqxxTransfer<%s>\n",
				ti.className.c_str(),
				ti.className.c_str());
			fprintf(classFile, "\t\t, public %s\n", ti.baseClass.c_str());
			fprintf(classFile, "\t{\n\t\ttypedef CPqxxTransfer<%s> baseClass;\n\n",
				ti.className.c_str());
			fprintf(classFile,
				"\tpublic:\n\t\t%s() : baseClass(\"%s\") { initColDescs(); }\n",
				ti.className.c_str(),
				timit->first.c_str());
			fprintf(classFile,
				"\t\t%s(const pqxx::result::const_iterator& src) : baseClass(\"%s\") { initColDescs(); xferValues(src); }\n\n",
				ti.className.c_str(),
				timit->first.c_str());
		}


		// Default query string
		fprintf(classFile, "\t\tstatic const std::string defaultQuery()\n\t\t{\n");
		fprintf(classFile, "\t\t\treturn \"SELECT \"\n");
		bool includeComma = false;
		cmit = ti.columns.begin();
		while (cmit != ti.columns.end())
		{
			if (cmit->second.generate)
			{
				temp = "\t\t\t\t\"";
				temp += includeComma ? ", " : "";
				includeComma = true;
				temp += cmit->first.c_str();
				temp += "\"";
				fprintf(classFile, "%s\n", (const char*)temp);
			}
			++cmit;
		}
		fprintf(classFile,
			"\t\t\t\t\" FROM %s%s%s\";\n\t\t}\n",
			gd.schema().c_str(),
			(gd.schema().length() > 0) ? "." : "",
			timit->first.c_str());

		if (!gd.genBase())
		{
			// Data member declarations
			fprintf(classFile, "\n\tpublic:\n");
			cmit = ti.columns.begin();
			while (cmit != ti.columns.end())
			{
				if (cmit->second.generate)
				{
					fprintf(classFile,
						"\t\t%s %s;\n",
						cmit->second.memberType.c_str(),
						cmit->second.memberName.c_str());
				}
				++cmit;
			}
		}
		else
		{
			// Assignment operator, assigning base to RS class
			fprintf(classFile,
				"\n\t\t%s& operator=(const %s& other)\n\t\t{\n",
				ti.className.c_str(),
				ti.baseClass.c_str());
			fprintf(classFile,
				"\t\t\tinitColDescs();\n\n",
				timit->first.c_str());
			cmit = ti.columns.begin();
			while (cmit != ti.columns.end())
			{
				if (cmit->second.generate && !cmit->second.isIndex)
				{
					fprintf(classFile,
						"\t\t\t%s = other.%s;\n",
						cmit->second.memberName.c_str(),
						cmit->second.memberName.c_str());
				}
				++cmit;
			}
			fprintf(classFile, "\n\t\t\treturn *this;\n\t\t}\n");
		}

		// Column transfer map
		fprintf(classFile,
			"\n\t\tBEGIN_DB_MAP(%s, %d)\n",
			ti.className.c_str(),
			genColumns);
		cmit = ti.columns.begin();
		int ctr = 0;
		while (cmit != ti.columns.end())
		{
			if (cmit->second.generate)
			{
				std::string line = "\t\t\t";
				if (cmit->second.memberType == "int")
				{
					line += "DB_INT_";
				}
				else if (cmit->second.memberType == "std::string")
				{
					line += "DB_STRING_";
				}
				else if (cmit->second.memberType == "bool")
				{
					line += "DB_BOOL_";
				}
				else if (cmit->second.memberType == "double")
				{
					line += "DB_DOUBLE_";
				}
				else if (cmit->second.memberType == "COleDateTime")
				{
					line += "DB_DATE_";
				}
				else if (cmit->second.memberType == "COleCurrency")
				{
					line += "DB_CURR_";
				}
				fprintf(classFile,
					"%s%s(%d, %s)\n",
					line.c_str(),
					cmit->second.isIndex ? "INDEX" : "ENTRY",
					ctr,
					cmit->second.memberName.c_str());
				++ctr;
			}
			++cmit;
		}
		fprintf(classFile, "\t\tEND_DB_MAP()\n");

		// Finish off the class and namespace block
		fprintf(classFile, "\t};\n");
		fprintf(classFile, "}\n");
		fclose(classFile);

		logWnd.ReplaceSel("OK");

		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


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