/**
 * @file
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is part of The iWear Framework.
 * In particular this file is part of the iWear Database Access Library.
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.

 * 
 * The iWear Framework is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#ifndef __DBCLASSES_RECORDSET_H
#define __DBCLASSES_RECORDSET_H

#ifndef __IWEAR_UTILITY_H
#include <iwear/utility.h>
#endif
#include "boost/lexical_cast.hpp"

#ifndef __IWEAR_POLYGON_H
//#include <iwear/polygon.h>
#endif

#include <string>
#include <stdexcept>
using namespace std;

namespace iwear {
    namespace db {
/**
 * This is a base class for all recordsets that different database incarnations
 * might use.
 * @todo Create iterators
 */
class RecordSet
{
private:
protected:
public:
    virtual ~RecordSet();


    class Field
    {
    public:
       /**
	* @return Size of the field in Bytes
	*/
	virtual size_t size(void) const = 0;

	template<class T> T as( const T& Default) const;
	template<class T> T as() const;
	/**
	 * @return true if the database value is null
	 */ 
	virtual bool is_null() const = 0;

	/**
	 * Acces to the plain underlying \0 terminated string the data is
	 * delivered. This is the fastest way to access it.
	 */
	virtual const char * c_str( void ) const = 0;
	virtual ~Field();
    protected:
    private:
    };

    class Row
    {
    public:
	virtual size_t size(void) const = 0;
	virtual const Field& operator[]( size_t column ) const = 0;
	virtual ~Row();
    protected:
    private:
	Row& operator=(const Row&);
    };

    virtual size_t size(void) const = 0;
    virtual const Row& operator[]( size_t row ) const = 0;
};

//template<class T> T as( const T& Default) const;

template<class T> inline T RecordSet::Field::as(const T& Default) const
{
    if ( ! is_null() )
    {
	return as<T>();
    }
    else
    {
	return T(Default);
    }
}

template<> inline timeval RecordSet::Field::as() const
{
    return time_from_string(c_str());
}

template<> inline string RecordSet::Field::as() const
{
    if ( c_str() ) return c_str();
    return string();
}
/*
template<> inline iwear::math::Point2D<double> RecordSet::Field::as() const
{
    const char * str;
    if ( is_null() || (str = c_str()) == 0 || *str == '\0')
    {
	return iwear::math::Point2D<double>();
    }

    char * ol = strdup(setlocale(LC_NUMERIC,"C"));


    // A Point looks like (8,8) so sscanf should simply work...
    iwear::math::Point2D<double> ret;
    if( sscanf(str,"(%le,%le)",&ret.x,&ret.y) != 2 )
    {
	setlocale(LC_NUMERIC,ol);
	free(ol);
	THROW( iwruntime_error,("Could not Parse as Point"));
    }

    setlocale(LC_NUMERIC,ol);
    free(ol);
    return ret;
}

template<> inline iwear::math::Polygon2D<double> RecordSet::Field::as() const
{
    const char * str;
    if ( is_null() || (str = c_str()) == 0 || *str == '\0')
    {
	return iwear::math::Polygon2D<double>();
    }

    char * ol = strdup(setlocale(LC_NUMERIC,"C"));

    const char * ptr = str;

    uint32_t ccntr = 0;
    while (*ptr != '\0' )
    {
	if ( *ptr == '(' )
	    ++ccntr;
	++ptr;
    }
    if ( ccntr )
	--ccntr;
    // Now we know the number of polygons (if any)
    // skip first "("
    ptr = str;
    if ( *ptr != '(' )
    {
	setlocale(LC_NUMERIC,ol);
	free(ol);
	THROW( iwruntime_error,("Error Parsing Polygon2D, no ( at start"));
    }
    ++ptr;
    double x;
    double y;
    iwear::math::Polygon2D<double> ret;
    ret.resize(ccntr);
    uint32_t cnt = 0;

    while ( sscanf(ptr,"(%le,%le)",&x,&y) == 2 )
    {
	ret[cnt].x = x;
	ret[cnt].y = y;
	++cnt;
	while (*ptr != ')') ++ptr;

	if ( *ptr != ',' )
	    break;
    }
    if (*ptr != ')')
    {
	cout << "ENDS AT \"" << ptr << "\"" << endl;
	setlocale(LC_NUMERIC,ol);
	free(ol);
	THROW( iwruntime_error,("Error Parsing Polygon2D, no proper ending"));
    }
    setlocale(LC_NUMERIC,ol);
    free(ol);
    return ret;
}
*/
template<> inline const char * RecordSet::Field::as() const
{
    return c_str();
}

template<> inline bool RecordSet::Field::as() const
{
    return as<int>(0);
}

template<class T>
T RecordSet::Field::as() const
{
    if( c_str() == NULL ) throw logic_error("Cannot convert NULL value");

    return boost::lexical_cast<T>(c_str());
    /*
    char * ol = strdup(setlocale(LC_NUMERIC,"C"));

    const char* dummy;
    T t = (T)0;
    string onefmt(get_fmt_str(t));
    onefmt += "%s";
    if ( sscanf( c_str(), onefmt.c_str(),&t,&dummy) != 1 )
    {
	setlocale(LC_NUMERIC,ol);
	free(ol);
	throw logic_error(string(string("Cannot convert \"") + c_str()) + "\" via " + get_fmt_str(t));
    }

    setlocale(LC_NUMERIC,ol);
    free(ol);
    return t;*/
}

}
}
#endif
