#include "testdb.h"

int TEST_Sizeof(void)
{
  return sizeof(TEST);
}

TEST * TEST_New(void)
{
  return TEST_Init((TEST *) mymalloc(sizeof(TEST)));
}

TEST * TEST_Init(TEST *test)
{
  test->_iSize = TEST_Sizeof();
  test->_iTablenr = TEST_TABLENR;
  test->_pfnCmpPk = (PFNCMP) TEST_CmpPk;
  test->_pfnCmpLk = (PFNCMP) TEST_CmpLk;
  INIT_FDouble(test->fdDVal);
  INIT_FLong(test->flIVal);
  INIT_FChar_35(test->fszName);
  INIT_FSurrogat(test->fszTEST_Id);
  return test;
}

MTEST * MTEST_Init(MTEST *mtest)
{
  ARRAY_Init((ARRAY*)mtest);
  mtest->_iTablenr = TEST_TABLENR;
  return mtest;
}


static char * _TEST_def = NULL;

char * TEST_Definition(void)
{
  if (_TEST_def != NULL) return _TEST_def;
  else {
    TEST test;
    char sql[10000];

    sprintf(sql,"1000,%d"\
"|TEST_Id,11,9,%d"\
"|Name,11,36,%d"\
"|IVal,4,4,%d"\
"|DVal,9,8,%d"\
, sizeof(TEST)
,(int) &test.fszTEST_Id - (int) &test
,(int) &test.fszName - (int) &test
,(int) &test.flIVal - (int) &test
,(int) &test.fdDVal - (int) &test);
    _TEST_def = mystrdup(sql);
    return _TEST_def;
  }
}

int TEST_BFill(TEST *test, DBresult *res, int row, int status, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  int tuples;

  if (status == MYDB_TUPLES_OK) {
    tuples = DBntuples(res);
    if (row == -1 && tuples != 1) {
      if (tuples > 1)
        Log_Msg("TEST_Fill: None or more than one resulttuple!\n");
      iRc = FALSE;
    } else {
      if (row == -1) row = 0;
      DBfillbinary(res, test->fszTEST_Id, row, 0);
      DBfillbinary(res, test->fszName, row, 1);
      DBfillbinary(res, &test->flIVal, row, 2);
      DBfillbinary(res, &test->fdDVal, row, 3);
    }
  } else {
    Log_Msg("TEST_Fill: Status is not DB_TUPLES_OK!\n");
    iRc = FALSE;
  }
  return iRc;
}

char * TEST_Attribs(void)
{
  return("TEST_Id,\
Name,\
IVal,\
DVal");
}

int TEST_IsValid(DBHANDLE *pDBHANDLE, TEST *test)
{
  return TRUE;
}

char * TEST_Data(DBHANDLE * pDBHANDLE, TEST *test)
{
  char sql[10000];

  Hold_ValueStrings();
  sprintf(sql, "\"%s\",\
\"%s\",\
\"%s\",\
\"%s\"" \
,Make_Reduce_String(Make_ValueString(test->fszTEST_Id,8)) \
,Make_Reduce_String(Make_ValueString(test->fszName,35)) \
,Make_Reduce_NumString(Make_LongString(test->flIVal)) \
,Make_Reduce_NumString(Make_DoubleString(test->fdDVal)));
  Release_ValueStrings();
  Free_ValueStrings();
  return mystrdup(sql);
}

char * TEST_TB_Attribs(int kz_db)
{
  if (kz_db) {
    return("TEST_Id,"\
"Name,"\
"IVal,"\
"DVal");
  } else {
    return("TEST_Id,"\
"Name,"\
"IVal,"\
"DVal");
  }
}

int TEST_SelectPk(TEST *test, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;
  MYDB_Result res;

  if (! dbbegin(pDBHANDLE->conn)) Msglog_Exit(1);
  sprintf(sql, "* FROM test WHERE TEST_Id='%s'" \
,Make_DbValueString(test->fszTEST_Id, 8));
  iRc = dbexec(pDBHANDLE->conn,"SELECT",sql,&res,&status);
  if (iRc == TRUE)
    if (Is_DBSQL(pDBHANDLE)) {
      iRc = TEST_BFill(test,(DBresult*)res.res,-1,status,pDBHANDLE);
    } else {
      iRc = FALSE;
    }
  Free_ValueStrings(); dbresfree(&res);
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int TEST_SelectLk(TEST *test, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;
  MYDB_Result res;

  if (! dbbegin(pDBHANDLE->conn)) Msglog_Exit(1);
  sprintf(sql, "* FROM test WHERE "\
"Name='%s'" \
,Make_DbValueString(test->fszName, 35));
  iRc = dbexec(pDBHANDLE->conn,"SELECT",sql,&res,&status);
  if (iRc == TRUE)
    if (Is_DBSQL(pDBHANDLE)) {
      iRc = TEST_BFill(test,(DBresult*)res.res,-1,status,pDBHANDLE);
    } else {
      iRc = FALSE;
    }
  Free_ValueStrings(); dbresfree(&res);
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int TEST_Insert(TEST *test, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  int status;

  if (! dbbegin(pDBHANDLE->conn)) Msglog_Exit(1);
  if (Is_DBSQL(pDBHANDLE)) {
    MYDB_Result res;
    int cnt = 0;

    do {
      Make_Surrogat(&(test->fszTEST_Id));
      DBquery_init(); DBquery_addcmd(DB_INSERT_STD);
      DBquery_addtokname(DBTK_Name, "TEST");
      DBquery_addtokstring(DBTK_CHARACTER,test->fszTEST_Id);
      DBquery_addtokstring(DBTK_CHARACTER,test->fszName);
      DBquery_addtokdata(DBTK_INTEGER,&test->flIVal,sizeof(int));
      DBquery_addtokdata(DBTK_FLOAT,&test->fdDVal,sizeof(double));
      iRc = dbbinexec2(pDBHANDLE->conn, &res, &status, FALSE);
    } while (iRc != TRUE && cnt++ < 100);
    if (iRc != TRUE) Log_Msg("TEST_Insert: Insert not possible (100 tries)\n");
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int TEST_Load(TEST *test, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  int status;

  if (! dbbegin(pDBHANDLE->conn)) Msglog_Exit(1);
  if (Is_DBSQL(pDBHANDLE)) {
    MYDB_Result res;
    DBquery_init(); DBquery_addcmd(DB_INSERT_LOAD);
    DBquery_addtokname(DBTK_Name, "TEST");
    DBquery_addtokstring(DBTK_CHARACTER,test->fszTEST_Id);
    DBquery_addtokstring(DBTK_CHARACTER,test->fszName);
    DBquery_addtokdata(DBTK_INTEGER,&test->flIVal,sizeof(int));
    DBquery_addtokdata(DBTK_FLOAT,&test->fdDVal,sizeof(double));
    iRc = dbbinexec1(pDBHANDLE->conn, &res, &status);
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int TEST_Update(TEST *test, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;

  if (! dbbegin(pDBHANDLE->conn)) Msglog_Exit(1);
  if (Is_DBSQL(pDBHANDLE)) {
    MYDB_Result res;
    DBquery_init(); DBquery_addcmd(DB_UPDATE_STD);
    DBquery_addtokname(DBTK_Name, "TEST");
    DBquery_addtokstring(DBTK_CHARACTER,test->fszTEST_Id);
    DBquery_addtokstring(DBTK_CHARACTER,test->fszName);
    DBquery_addtokdata(DBTK_INTEGER,&test->flIVal,sizeof(int));
    DBquery_addtokdata(DBTK_FLOAT,&test->fdDVal,sizeof(double));
    sprintf(sql, "WHERE TEST_Id='%s'"
,test->fszTEST_Id);
    iRc = DBquery_addwhere(sql, NULL);
    if (iRc == TRUE) {
      iRc = dbbinexec1(pDBHANDLE->conn, &res, &status);
      dbresfree(&res);
    }
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int TEST_Delete(TEST *test, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;

  if (! dbbegin(pDBHANDLE->conn)) Msglog_Exit(1);
  sprintf(sql, "TEST_Id='%s'"
,test->fszTEST_Id);
  iRc = dbdelete(pDBHANDLE->conn, "test", sql, &status);
  Free_ValueStrings();
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int TEST_DeleteAll(DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  int status;

  if (! dbbegin(pDBHANDLE->conn)) Msglog_Exit(1);
  if (Is_DBSQL(pDBHANDLE)) {
    MYDB_Result res;
    DBquery_init(); DBquery_addcmd(DB_DELETE_STD);
    DBquery_addtokname(DBTK_Name, "TEST");
    iRc = dbbinexec1(pDBHANDLE->conn, &res, &status);
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int TEST_Do(TEST *test, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;

  switch (test->iDB) {
    case SQL_NONE:
      break;
    case SQL_SELECTLK:
      iRc = TEST_SelectLk(test, pDBHANDLE);
      break;
    case SQL_SELECTPK:
      iRc = TEST_SelectPk(test, pDBHANDLE);
      break;
    case SQL_INSERT:
      iRc = TEST_Insert(test, pDBHANDLE);
      break;
    case SQL_UPDATE:
      iRc = TEST_Update(test, pDBHANDLE);
      break;
    case SQL_DELETE:
      iRc = TEST_Delete(test, pDBHANDLE);
      break;
    default:
      Log_Msg("TEST_Do: 'iDB'-code unknown!\n");
      iRc = FALSE;
  }
  return iRc;
}

int TEST_CopyPk(TEST *test1, TEST *test2)
{
  
  {

  COPY_FSurrogat (test1->fszTEST_Id, test2->fszTEST_Id);
  return TRUE;
}}

int TEST_CmpPk(TFSurrogat pkey, TEST *test2)
{
  if (! CMP_FSurrogat(pkey, test2->fszTEST_Id)) return FALSE;
  return TRUE;
}

int TEST_CmpLk(TEST *test1, TEST *test2)
{
  
  {
  int iRc;

  iRc = DATA_Compare (test1->fszName, TYP_STRING, test2->fszName, TYP_STRING);
  if (iRc != 0) return FALSE;
  return TRUE;
}}

static int _TEST_Select_cnt = 0;
static MTEST _MTEST_Select;

int TEST_Select(TEST *test, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, "SELECT * FROM TEST ");
  Free_ValueStrings();
  _TEST_Select_cnt = 0;
  MTEST_Init(&_MTEST_Select);
  if (Is_DBSQL(pDBHANDLE)) {
    iRc = dbexec1(pDBHANDLE->conn,sql,&res,&status);
    if (iRc == TRUE) {
      DBresult * dbres = (DBresult*)res.res;
      int i;
      for (i = 0; i < DBntuples(dbres); i++) {
        iRc = TEST_BFill(test, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MTEST_Add(&_MTEST_Select, test);
      }
      dbresfree(&res);
    }
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int TEST_Fetch(TEST *test, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;

  if (_TEST_Select_cnt < _MTEST_Select.iCount)
    TEST_Assign(test, MTEST_GetPtr(&_MTEST_Select, _TEST_Select_cnt++));
  else {
    iRc = FALSE; MTEST_Free(&_MTEST_Select);
  }
  return iRc;
}

int TEST_Compare(DBHANDLE * pDBHANDLE, TEST * test1, TEST * test2, _MCMP * mcmp) {
  int iError = 0;

  Hold_ValueStrings();
  if (! CMP_FChar_35(test1->fszName, test2->fszName)) { iError++;
    if (mcmp != NULL) _AddCmpMultiple(pDBHANDLE, mcmp, mystrdup("TEST"), mystrdup("Name"),
Reduce_String(Make_ValueString(test1->fszName,35)),
Reduce_String(Make_ValueString(test2->fszName,35)));
  }
  if (! CMP_FLong(test1->flIVal, test2->flIVal)) { iError++;
    if (mcmp != NULL) _AddCmpMultiple(pDBHANDLE, mcmp, mystrdup("TEST"), mystrdup("IVal"),
Reduce_NumString(Make_LongString(test1->flIVal)),
Reduce_NumString(Make_LongString(test2->flIVal)));
  }
    if (DATA_CmpDouble (test1->fdDVal, 
                        test2->fdDVal, 0.0000001) != 0)
 { iError++;
    if (mcmp != NULL) _AddCmpMultiple(pDBHANDLE, mcmp, mystrdup("TEST"), mystrdup("DVal"),
Reduce_NumString(Make_DoubleString(test1->fdDVal)),
Reduce_NumString(Make_DoubleString(test2->fdDVal)));
  }
  Release_ValueStrings();
  Free_ValueStrings();
  return iError;
}

static int _TEST_Select_IVal_cnt = 0;
static MTEST _MTEST_TEST_Select_IVal;

int TEST_Select_IVal (TEST * test, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, 	"SELECT * FROM TEST " \
	"WHERE  IVAL = %d" \
,test->flIVal);
  Free_ValueStrings();
  _TEST_Select_IVal_cnt = 0;
  MTEST_Init(&_MTEST_TEST_Select_IVal);
  if (Is_DBSQL(pDBHANDLE)) {
    iRc = dbexec1(pDBHANDLE->conn,sql,&res,&status);
    if (iRc == TRUE) {
      DBresult * dbres = (DBresult*)res.res;
      int i;
      for (i = 0; i < DBntuples(dbres); i++) {
        iRc = TEST_BFill(test, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MTEST_Add(&_MTEST_TEST_Select_IVal, test);
      }
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int TEST_Fetch_IVal (TEST * test, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;

  if (_TEST_Select_IVal_cnt < _MTEST_TEST_Select_IVal.iCount)
    TEST_Assign(test, MTEST_GetPtr(&_MTEST_TEST_Select_IVal, _TEST_Select_IVal_cnt++));
  else {
    iRc = FALSE; MTEST_Free(&_MTEST_TEST_Select_IVal);
  }
  return iRc;
}

