//
// PostgreSQLTypes.cpp
//
// $Id: //poco/1.4/Data/PostgreSQL/src/PostgreSQLTypes.cpp#1 $
//
// Library: Data
// Package: PostgreSQL
// Module:  PostgreSQLTypes
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// 
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//


#include "Poco/Data/PostgreSQL/PostgreSQLTypes.h"

namespace Poco {
namespace Data {
namespace PostgreSQL {

Poco::Data::MetaColumn::ColumnDataType oidToColumnDataType(const Oid anOID)
{
	Poco::Data::MetaColumn::ColumnDataType cdt = Poco::Data::MetaColumn::FDT_UNKNOWN;
	
	switch (anOID) {
		// bool
		case BOOLOID:
			cdt = Poco::Data::MetaColumn::FDT_BOOL;
			break;
			
		// integers
		case INT2OID:
			cdt = Poco::Data::MetaColumn::FDT_INT16;
			break;
		case INT4OID:
			cdt = Poco::Data::MetaColumn::FDT_INT32;
			break;
		case INT8OID:
			cdt = Poco::Data::MetaColumn::FDT_INT64;
			break;
			
		// floating point
		case FLOAT8OID:
			cdt = Poco::Data::MetaColumn::FDT_DOUBLE;
			break;
		case FLOAT4OID:
			// cdt = Poco::Data::MetaColumn::FDT_FLOAT;  This a bug in Poco::Data as a 4 byte "float" can't be cast/ugraded to an 8 byte "double"
			cdt = Poco::Data::MetaColumn::FDT_DOUBLE;
			break;
		case NUMERICOID:
			cdt = Poco::Data::MetaColumn::FDT_DOUBLE;
			break;
			
		// character strings
		case CHAROID:
			cdt = Poco::Data::MetaColumn::FDT_STRING;
			break;
		case BPCHAROID:
			cdt = Poco::Data::MetaColumn::FDT_STRING;
			break;
		case VARCHAROID:
			cdt = Poco::Data::MetaColumn::FDT_STRING;
			break;
				
		// BLOB, CLOB
		case BYTEAOID:
			cdt = Poco::Data::MetaColumn::FDT_BLOB;
			break;
		case TEXTOID:
			cdt = Poco::Data::MetaColumn::FDT_CLOB;
			break;
				
		// date
		case DATEOID:
			cdt = Poco::Data::MetaColumn::FDT_DATE;
			break;
				
		// time
		case TIMEOID:
			cdt = Poco::Data::MetaColumn::FDT_TIME;
			break;
		case TIMETZOID:
			cdt = Poco::Data::MetaColumn::FDT_TIME;
			break;
				
		//timestamp
		case TIMESTAMPOID:
			cdt = Poco::Data::MetaColumn::FDT_TIMESTAMP;
			break;
		case TIMESTAMPZOID:
			cdt = Poco::Data::MetaColumn::FDT_TIMESTAMP;
			break;
				
		// everything else is a string
		default:
			cdt = Poco::Data::MetaColumn::FDT_STRING;
			break;
	}

	return cdt;
}

} } } // namespace Poco::Data::PostgreSQL

