#ifndef RESULTSET_H
#define RESULTSET_H

#include <mysql.h>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>

#define EXIT_ON_ERROR // Define this to exit(1) on errors
//#define EXIT_ON_NULL // Define this to exit(1) on access of null field. Otherwise it returns "".

typedef unsigned char byte;

using namespace std;

class Variant{
 private:
  char* data;
 public:
  Variant();
  Variant(const char*);
  Variant(const Variant& other);
  ~Variant();
  Variant Variant::operator = (const Variant& other);
  //operator int();
  operator string();
  operator double();
  //operator const char*();
};

ostream& operator << (ostream& stream,Variant v);
double abs(double);

class ResultSet{
 protected:
  int numrows,currentrow; // Rows in result set
  MYSQL* conn; // Pointer to database
  MYSQL_RES result; // The result of the query
  MYSQL_ROW row; // The current row
  vector<string> names; // Field names
  vector<byte*> fields; // Field data from current row
  string thisquery;

 public:
  // Public Data
  ostringstream query;

  // Constructors/destructors
  ResultSet(); //
  ResultSet(MYSQL*); //
  ResultSet(string,MYSQL*); //
  ResultSet(const ResultSet&); //
  ResultSet operator = (const ResultSet&); //
  ~ResultSet(); //

  // Query functions
  void setConn(MYSQL*); //
  MYSQL* getConn() const; //
  void store(); //
  void storeQuery(string); //
  void storeNoRes(); // deprecated, calls storeQuery()
  void clear(); //
  string print(); //

  // Static functions
  static MYSQL makeConn(string,string,string,string); // Connect to a database, return the connection object.

  // Row access functions
  bool next(); //
  bool prev(); //
  bool seek(int); //
  void rewind(); //
  int tell() const; //
  int size() const; //
  
  // Field accessors
  Variant get(int) const; // Returns the contents of a field
  Variant get(string) const; //
  Variant operator[] (int) const; // Returns the contents of a field
  Variant operator[] (string) const; //
  int fieldNumber(string) const; // Returns the number associated with a field
};

#endif
