Click here to Skip to main content
15,884,628 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.
#pragma once

/***************************************************************************
 TableInfo.h  -  Contains class definitions for ColumnInfo and TableInfo.
                 These are in the one file because it didn't seem to make
				 sense to separate them.

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

// A ColumnInfo object contains a definition for a column.
// These are stored in a COLUMNMAP, keyed on database
// column name.
struct ColumnInfo
{
	ColumnInfo() : isIndex(false) {}

	static std::string guessMemberType(const char* sz)
	{
		std::string ret;
		if ((0 == strncmp(sz, "int", 3) && isdigit(sz[3]))
			|| 0 == strcmp(sz, "oid"))
		{
			ret = "int";
		}
		else if (0 == strcmp(sz, "bool"))
		{
			ret = "bool";
		}
		else if (0 == strncmp(sz, "float", 5)
			|| 0 == strncmp(sz, "numeric", 7))
		{
			ret = "double";
		}
		else if (0 == strcmp(sz, "date")
			|| 0 == strcmp(sz, "time")
			|| 0 == strcmp(sz, "timestamp"))
		{
			ret = "COleDateTime";
		}
		else
		{
			ret = "std::string";
		}
		return ret;
	}
	std::string colPGType;
	std::string memberName;
	std::string memberType;
	bool isIndex;
	bool generate;
};
typedef std::map<std::string, ColumnInfo> COLUMNMAP;

// TableInfo contains the name of the file into which the
// class definition for the table will be put, the name the
// new class will have, and column map. These are stored in
// a TABLEINFOMAP, keyed on the database table name.
struct TableInfo
{
	TableInfo() {}
	TableInfo(const std::string& cName, const std::string& fName, const std::string& bName)
		: fileName(fName)
		, baseClass(bName)
		, className(cName)
		, generate(true)
	{
	}

	std::string fileName;
	std::string baseClass;
	std::string className;
	bool generate;
	COLUMNMAP columns;
};

typedef std::map<std::string, TableInfo> TABLEINFOMAP;

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