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

/***************************************************************************
 GlobalData.h  -  Global data class. This is a singleton class, and serves
                  as a bit of a catchall for the stuff that I need to keep
				  around for the duration of the pages.

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

class GlobalData
{
	friend class Singleton<GlobalData>;

private:

	// Constructor loads some registry values if they are available.
	// Private constructor enforces the singleton pattern. I should
	// also privatise the copy constructor as well, but I can't be
	// bothered at this point.
	GlobalData(void)
		: _dbConn(0)
	{
		_targetPath = regParam("TargetPath");
		_usePrefix = (regParam("UseCPrefix") == "1");
		_genBase = (regParam("GenBaseClass") == "1");
		_dropTrailingS = (regParam("DropTrailingS") == "1");
	}

	// Destructor closes up the database connection if (a) it exists
	// and (b) it is actually connected.
	~GlobalData()
	{
		if (_dbConn)
		{
			if (_dbConn->is_open())
			{
				_dbConn->disconnect();
			}
			delete _dbConn;
		}
	}

private:
	pqxx::connection* _dbConn;	// connection to the database
	std::string _schema;		// name of the schema
	TABLEINFOMAP _tableInfo;	// name->table info map
	std::string _targetPath;	// where we want to put the code
	BOOL _usePrefix;			// should we use a "C" prefix on class names
	BOOL _genBase;				// should we generate separate base classes
	BOOL _dropTrailingS;		// should we drop trailing S when generating base class names

public:
	// Get and set methods for the connection
	pqxx::connection* dbConn() { return _dbConn; }
	void dbConn(const std::string& connStr)
	{
		if (_dbConn)
		{
			if (_dbConn->is_open())
			{
				_dbConn->disconnect();
			}
			delete _dbConn;
		}
		_dbConn = new pqxx::connection(connStr);
	}

	// Get and set methods for the schema
	const std::string& schema() { return _schema; }
	void schema(const std::string& s) { _schema = s; }

	// Get and set methods for the target path.
	const std::string& targetPath() { return _targetPath; }
	void targetPath(const std::string& s)
	{
		_targetPath = s;
		regParam("TargetPath", _targetPath.c_str());
	}

	// Get and set methods for the prefix flag
	BOOL usePrefix() const { return _usePrefix; }
	void usePrefix(BOOL b)
	{
		_usePrefix = b;
		regParam("UseCPrefix", _usePrefix ? "1" : "0");
	}

	// Get and set methods for the generate base flag
	BOOL genBase() const { return _genBase; }
	void genBase(BOOL b)
	{
		_genBase = b;
		regParam("GenBaseClass", _genBase ? "1" : "0");
	}

	// Get and set methods for the "drop trailing S" flag
	BOOL dropTrailingS() const { return _dropTrailingS; }
	void dropTrailingS(BOOL b)
	{
		_dropTrailingS = b;
		regParam("DropTrailingS", _dropTrailingS ? "1" : "0");
	}

	// Get method for the table info
	TABLEINFOMAP& tableInfo() { return _tableInfo; }

	// Utility function to load a named registry parameter
	std::string regParam(const char* name)
	{
		CRegKey rkSettings;
		CString itemName;
		itemName.LoadString(_Module.GetResourceInstance(), IDS_REGKEY_SETTINGS);
		LONG lRet = rkSettings.Open(HKEY_CURRENT_USER, itemName);
		if (lRet == ERROR_SUCCESS)
		{
			char buf[MAX_PATH] = { 0 };
			DWORD dwLen = MAX_PATH;

			rkSettings.QueryStringValue(name, buf, &dwLen);
			return std::string(buf);
		}
		return std::string("");
	}

	// Utility function to store a registry parameter
	void regParam(const char* name, const char* value)
	{
		CRegKey rkSettings;
		CString itemName;
		itemName.LoadString(_Module.GetResourceInstance(), IDS_REGKEY_SETTINGS);
		LONG lRet = rkSettings.Open(HKEY_CURRENT_USER, itemName);
		if (lRet != ERROR_SUCCESS)
		{
			lRet = rkSettings.Create(HKEY_CURRENT_USER, itemName);
		}
		if (lRet == ERROR_SUCCESS)
		{
			rkSettings.SetStringValue(name, value);
		}
	}
};

// Define a type that we can use to access the singleton
typedef Singleton<GlobalData> GlobalDataS;

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.150123.1 | Last Updated 18 Aug 2006
Article Copyright 2005 by _oti
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid