/* Table class

        The table class crudely models the result set from an SQL query, ie a
        relational database table. It contains the data members:

        Schema - The table schema, eg column number, name, datatype etc
        tData  - The data (if any) returned by the SQL command
  
*/

#ifndef TABLE_H
#define TABLE_H

#include <windows.h>
#include <iostream>
#include <sql.h>
#include <sqlext.h>
#include <string>
#include <algorithm>
#include <vector>

#define max(a,b) (a > b ? a : b)

/*      Table Schema for a given column */
typedef struct
{ SQLUINTEGER colNum;              //column number
  string      colName;             //column name  
  SQLSMALLINT colNameLen;          //length of name
  SQLSMALLINT colType;             //column data type
  string      colTypeName;         //user friendly datatype name
  SQLUINTEGER colLength;           //length of column
  SQLSMALLINT colScale;            //column numeric precision
  SQLSMALLINT colNullable;         //indicates if column can hold null values
  char        *binding;            //data structure to which column is bound
  SQLINTEGER  bytesReturned;       //bytes returned by a get data operation on
} COLATTRIBUTES;                   //a column

//Table result data
typedef struct
{ char * text;
} tableData;

struct RESULTATTR
{ SDWORD cRows;
  SWORD  cColumns;
};

class Table
{ public:
   Table();
   ~Table();
   void          initTable(COLATTRIBUTES);
   static int    tableInstances;
   SQLRETURN     buildSchema(HSTMT &, int);
   char *        getSchema(int);
   RESULTATTR    getAttribs();
   void          setAttribs(int, int);
   void          setSchema(COLATTRIBUTES);
   SQLRETURN     bindColumns(HSTMT &);
   SQLRETURN     transferData(HSTMT &);
   char *        getField(int, int, int); //row, col number of colums
  private:
   COLATTRIBUTES          tab;
   vector <tableData>     tData;
   vector <COLATTRIBUTES> Schema;
   RESULTATTR             attribs;
};

#endif