/**
 * @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_PGRECORDSET_H
#define __DBCLASSES_PGRECORDSET_H

#ifndef __DBCLASSES_RECORDSET_H
#include <dbclasses/recordset.h>
#endif

#include <pqxx/result>

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

namespace iwear {
    namespace db {
/**
 * This is a base class for all recordsets that different database incarnations
 * might use.
 * @todo The Fields might not be needed to be allocated dynamically, this would
 * save some time, and eventually some data...
 */
class PGRecordSet : public RecordSet
{
private:

   /**
    * This result contains the real data we will access.
    */
    pqxx::result* res;

protected:
    
public:
    PGRecordSet( pqxx::result* r);
    virtual ~PGRecordSet();


    class PGField : public RecordSet::Field
    {
    public:
//	PGField( const pqxx::result::field* r );
	const PGField& operator=( const pqxx::result::field* r ) 
	{ 
	    delete res;
	    res = r; 
	    return *this; 
	}
	PGField( ) : res(NULL) { }
	~PGField( ) { delete res; }
       /**
	* @return Size of the field in Bytes
	*/
	virtual size_t size(void) const;

	/**
	 * @return true if the database value is null
	 */ 
	virtual bool is_null(void) const;

	/**
	 * 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;
//    protected:
	/**
	 * Reference to the real data
	 */
	const pqxx::result::field* res;
    private:
    };

    class PGRow : public RecordSet::Row
    {
    public:
	PGRow( pqxx::result& r, size_t _row );

	/**
	 * @return the size in columns of this row.
	 */
	virtual size_t size(void) const;

	/**
	 * @return a const reference to the column th Field of this PGRow. 
	 * @warning There is no bound check done in the first place, e.g.
	 * theres always a Field returned, but it might point to some pgresult
	 * data thats not available. This means that only in the case you
	 * really access a field you got, an out of bounds exception will be
	 * thrown.
	 */
	virtual const RecordSet::Field& operator[]( size_t column ) const;
	virtual ~PGRow();
    protected:
	/**
	 * Referring to the real data
	 */
	pqxx::result& res;

	/**
	 * This Row is No...
	 */
	size_t my_row;

	size_t max_col;
	PGField* fields;
    private:
	/**
	 * Since we return references to Fields we need to keep track of them.
	 * To avoid allocating uneeded data, we do it only on access time and
	 * store it here.
	 * @note We could allocate all at once, which would lead to the problem
	 * that we would need to iterate through all the data, which in some
	 * PGSQL operation modes is not already there.
	 */
	mutable map<size_t,PGField*> FieldPool;
    };
    friend class PGRow;
    friend class PGField;

    /**
     * @return the count of the rows we got. In theory, but in reality this
     * might not always be known, so this may also return 0, when there are an
     * unknown amount waiting...
     */
    virtual size_t size(void) const;
    /**
     * Gets the row th Row of the Current RecordSet.
     */
    virtual const Row& operator[]( size_t row ) const;

    /**
     * In some cases we need to assign record Sets, so copy the whole data here !!
     */
    PGRecordSet& operator=(const PGRecordSet&);
private:

    /**
     * The Pool of Rows.
     */
    mutable map<size_t,PGRow*> RowPool;
};

}
}
#endif

