//////////////////////////////////////////////////////
// Pinclass.cpp
//
// Source code for Wrapping C++ around Pinnacle
//
// David Brumbaugh, 1991
//////////////////////////////////////////////////////
#include <iostream.h>
#include <conio.h>
#include "PINCLAS.H"

Pfm_List::Pfm_List(char *database, char *table_name,
size_t mbs)
{
    db = DB_Open(database,"rw",0);
    if (db == NULL)
    {
                cerr << "Error opening database:"
                << database << "Error = "
                << DB_ErrorString() << "\n";
        cerr << "Strike a Key"; getch();
        return;
    }
    table = DB_Table(db,table_name);
    if (DB_Errno != DB_OK)
    {
                cerr << "Error opening table:"
                << table_name << "Error = "
                << DB_ErrorString() << "\n";
                cerr << "Strike a Key";
                getch();
        DB_Close(db);
        return;
    }
    DB_FirstRow(table);
    DB_NextRow(table,DBNEXT);
    is_at_top = true;
    is_at_bottom = false;
    needs_closed = true;
    default_key = NULL;
    default_dbsearch = NULL;
    max_buffer_size = mbs;
    buffer = new char[max_buffer_size];

}

Pfm_List::Pfm_List(DB &open_db, char *table_name,
size_t mbs)
{
    db = open_db;
    
    table = DB_Table(db,table_name);
    if (DB_Errno != DB_OK)
    {
        cerr << "Error opening table:"
        << table_name << "Error = "
        << DB_ErrorString() << "\n";
        cerr << "Strike a Key"; getch();
        return;
    }
    DB_FirstRow(table);
    DB_NextRow(table,DBNEXT);
    is_at_top = true;
    is_at_bottom = false;
    needs_closed = false;
    default_key = NULL;
    default_dbsearch = NULL;
    max_buffer_size = mbs;
    buffer = new char[max_buffer_size];

}

Pfm_List::Pfm_List(DB &open_db, DBTAB &db_table,
size_t mbs)
{
    db = open_db;
    table = db_table;
    DB_FirstRow(table);
    DB_NextRow(table,DBNEXT);
    is_at_top = true;
    is_at_bottom = false;
    needs_closed = false;
    default_key = NULL;
    default_dbsearch = NULL;
    max_buffer_size = mbs;
    buffer = new char[max_buffer_size];


}

Pfm_List::~Pfm_List()
{
    if (needs_closed)
        DB_Close(db);
    delete buffer;
}

Boolean Pfm_List::find(void *key, char *relation)
{
    if (default_key == NULL)
        return(false);
    default_dbsearch = DB_SearchObject(db,
    DB_GetType(default_key),key,relation);
    DB_FirstRow(table);
    return((Boolean) DB_FindNext(default_key,
    default_dbsearch,DBNEXT));

}

Boolean Pfm_List::find(char *col, char *relation,
void *key)
{
    default_key = DB_Column(table,col);
    return(find(key,relation));
}

Boolean Pfm_List::find(void *key)
{
    return(find(key,"=="));
}

void Pfm_List::prev()
{
    if (DB_NextRow(table,DBPREVIOUS) != DB_OK)
    {
        is_at_top = false;
    }
    else
    {
        is_at_top = true;
        if (total() > 1L)
        {
            is_at_bottom = false;
        }
    }
}

void Pfm_List::next()
{
    if (DB_NextRow(table,DBNEXT) != DB_OK)
    {
        is_at_bottom = false;
    }
    else
    {
        is_at_bottom = true;
        if (total() > 1L)
        {
            is_at_top = false;
        }
    }
}

void Pfm_List::top()
{
    DB_FirstRow(table);
    DB_NextRow(table,DBNEXT);
    is_at_top = true;
    if (total() > 1L)
        is_at_bottom = false;

}

void Pfm_List::end()
{
    DB_ForAllRows(table);
    DB_NextRow(table,DBPREVIOUS);
    is_at_bottom = true;
    if (total() > 1L)
        is_at_top = false;
}


void Pfm_List::add()
{
    DB_AddRow(table);
}


void Pfm_List::replace(char *field,
char *value)
{
    DBCOL col = DB_Column(table,field);
    DB_PutString(col,(unsigned char *) value);
}

void Pfm_List::replace(char *field,
long value)
{
    DBCOL col = DB_Column(table,field);
    DB_PutInteger(col, value);
}

void Pfm_List::replace(char *field, double value)
{
    DBCOL col = DB_Column(table,field);
    DB_PutReal(col, value);
}

void Pfm_List::remove()
{
    DB_DeleteRow(table);
    next();
}

long Pfm_List::tell()
{
    DBROWID thisrow, checkrow;
    long position = 0L;

    thisrow = DB_CurrentRow(table);
    top();
    do
    {
        checkrow = DB_CurrentRow(table);
        if (checkrow != thisrow)
        {
            ++position;
            DB_NextRow(table,DBNEXT);
        }
    } while(checkrow != thisrow);

    return(position);
}

Boolean Pfm_List::findnext()
{
  if (default_key == NULL || default_dbsearch == NULL)
      return(false);

  return((Boolean) DB_FindNext(default_key,
  default_dbsearch,DBNEXT));
}

Boolean Pfm_List::findprev()
{
  if (default_key == NULL || default_dbsearch == NULL)
      return(false);

  return((Boolean) DB_FindNext(default_key,
  default_dbsearch,DBPREVIOUS));
}

char *Pfm_List::get(char *field, char *value)
{
    DBCOL col = DB_Column(table, field);
    if (value != NULL)
    {
        strcpy(value,(char *)DB_GetString(col));
    }
    return(value);
}

long Pfm_List::get(char *field, long &value)
{
    DBCOL col = DB_Column(table, field);
    value = DB_GetInteger(col);
    return(value);
}

double Pfm_List::get(char *field, double &value)
{
    DBCOL col = DB_Column(table, field);
    value = DB_GetReal(col);
    return(value);
}

void *Pfm_List::get(char *field, void *value)
{

    DBCOL col = DB_Column(table,field);
    unsigned long type = DB_GetType(col);

    if (type == Integer)
    {
        DBINTEGER *dbint = (DBINTEGER *) value;
        get(field,*dbint);
    }
    else if (type == Real)
    {
        DBREAL *dbreal = (DBREAL*) value;
        get(field,*dbreal);
    }
    else if (type == String)
    {
        get(field,(DBSTRING) value);
    }
    else if (type == NBytes)
    {
            memcpy(value,DB_GetNBytes(col,NULL),
            DB_GetSize(col));
    }
    return(value);
}

