/* ---------------------------------------------------- */
/* Source-file DBTEST 1.0, (C) Wabnig Harald, 25.5.2002 */
/* ---------------------------------------------------- */

#include "dbrand.h"
#include "basis.h"
#include "testdb.h"

static DBHANDLE * pDB;

/* ---------------------------------------------------------------- */

#define TABLE_TEST     "CREATE TABLE TEST (" \
                       "  TEST_ID CHARACTER(8)," \
                       "  NAME CHARACTER(35)," \
                       "  IVAL INTEGER," \
                       "  DVAL FLOAT" \
                       ");"
#define INDEX_XTEST01  "CREATE UNIQUE INDEX XTEST01 ON TEST (" \
                       "  TEST_ID" \
                       ");"
#define INDEX_XTEST02  "CREATE UNIQUE INDEX XTEST02 ON TEST ("\
                       "  NAME" \
                       ");"
#define INDEX_XTEST03  "CREATE INDEX XTEST03 ON TEST ("\
                       "  IVAL" \
                       ");"

/* ------------- */

int main (int argc, char *argv[])
{
  TEST Test, Test1;
  _MCMP MCmp;
  _CMP * pCmp;
  int iRc = TRUE;
  int i, iCount;

  TEST_Init(&Test);
  TEST_Init(&Test1);

  fprintf(stdout, "DB-Test 1.1, (C) Wabnig Harald, 12.7.2003\n");

#ifdef SPEEDY1
  if (! Speedy1_Init("", "TESTDB")) {
    fprintf(stderr, "Speedy1_Init error!\n");
    exit(1);
  }
  DB_Set_Error_String();
  if (! DBHANDLE_Init(&pDB, "TESTDB")) {
    DB_Print_Error_String();
    fprintf(stderr, "DBHANDLE_Init error!");
    exit(1);
  }
#else
  {
    char dbname[20];

    /* determine dbname from the commandline without default 'sdsys'
       if no dbname is given use 'test' */
    strcpy(dbname, "-");
    ARGV_GetDB_Name(argv, argc, dbname);
    if (strcmp(dbname, "") == 0) 
      strcpy(dbname, "test");

    if (! DBHANDLE_Init(&pDB, dbname)) {
      fprintf(stderr, "DBHANDLE_Init error!\n");
      exit(1);
    }
  }
#endif

  /* Create database-tables/indices if necessary */

  if (! DB_Exists_Table(pDB, "TEST")) {
    if (! DB_Create_Table(pDB, TABLE_TEST)) {
      DB_Print_Error_String();
      fprintf(stderr, "DB_Create_Table 'TEST' error!\n");
      exit(1);
    }
  }

  if (! DB_Exists_Index(pDB, "XTEST01")) {
    if (! DB_Create_Index(pDB, INDEX_XTEST01)) {
      DB_Print_Error_String();
      fprintf(stderr, "DB_Create_Index 'XTEST01' error!\n");
      exit(1);
    }
  }
  if (! DB_Exists_Index(pDB, "XTEST02")) {
    if (! DB_Create_Index(pDB, INDEX_XTEST02)) {
      DB_Print_Error_String();
      fprintf(stderr, "DB_Create_Index 'XTEST02' error!\n");
      exit(1);
    }
  }

  /* ----------------------- */

  /* cleanup the test-table */
  if (! TEST_DeleteAll(pDB)) {
    fprintf(stderr, "TEST_DeleteAll error!\n");
    exit(1);
  }
  COPY_FLong (Test.flIVal, 1000);
  COPY_FDouble (Test.fdDVal, 1000);
  /* without use of transactions each db-command 
     is done as one transaction */
  COPY_FChar_35 (Test.fszName, "Name1");
  if (! TEST_Insert(&Test, pDB)) {
    fprintf(stderr, "TEST_Insert error!\n");
    exit(1);
  }
  /* with use of transaction the database is blocked
     until DB_Commmit or DB_Rollback */
  if (! DB_Begin(pDB)) {
    fprintf(stderr, "DB_Begin error!\n");
    exit(1);
  }
  COPY_FChar_35 (Test.fszName, "Name2");
  if (! TEST_Insert(&Test, pDB)) {
    fprintf(stderr, "TEST_Insert error!\n");
    exit(1);
  }
  COPY_FChar_35 (Test.fszName, "Name3");
  if (! TEST_Insert(&Test, pDB)) {
    fprintf(stderr, "TEST_Insert error!\n");
    exit(1);
  }
  if (! DB_Commit(pDB)) {
    fprintf(stderr, "DB_Commit error!\n");
    exit(1);
  }
  /* this transaction uses DB_Rollback */
  if (! DB_Begin(pDB)) {
    fprintf(stderr, "DB_Begin error!\n");
    exit(1);
  }
  COPY_FChar_35 (Test.fszName, "Name4");
  if (! TEST_Insert(&Test, pDB)) {
    fprintf(stderr, "TEST_Insert error!\n");
    exit(1);
  }
  COPY_FChar_35 (Test.fszName, "Name5");
  if (! TEST_Insert(&Test, pDB)) {
    fprintf(stderr, "TEST_Insert error!\n");
    exit(1);
  }
  if (! DB_RollBack(pDB)) {
    fprintf(stderr, "DB_RollBack error!\n");
    exit(1);
  }
  /* read and print all items from the database */
  if (TEST_Select(&Test, pDB)) {
    fprintf(stdout, "%s\n", TEST_Attribs());
    while (TEST_Fetch(&Test, pDB)) {
      fprintf(stdout, "%s\n", TEST_Data(pDB, &Test));
    }
  }
  /* update-transaction */
  if (! DB_Begin(pDB)) {
    fprintf(stderr, "DB_Begin error!\n");
    exit(1);
  }
  COPY_FChar_35 (Test.fszName, "Name2");
  if (! TEST_SelectLk(&Test, pDB)) {
    fprintf(stderr, "TEST_SelectLk error!\n");
    exit(1);
  }
  COPY_FChar_35 (Test.fszName, "Name21");
  if (! TEST_Update(&Test, pDB)) {
    fprintf(stderr, "TEST_Update error!\n");
    exit(1);
  }
  if (! DB_Commit(pDB)) {
    fprintf(stderr, "DB_Commit error!\n");
    exit(1);
  }
  /* read and print all items from the database */
  if (TEST_Select(&Test, pDB)) {
    fprintf(stdout, "%s\n", TEST_Attribs());
    while (TEST_Fetch(&Test, pDB)) {
      fprintf(stdout, "%s\n", TEST_Data(pDB, &Test));
    }
  }

  /* compare database-rows */
  _MCMP_Init(&MCmp);
  COPY_FChar_35 (Test.fszName, "Name1");
  if (! TEST_SelectLk(&Test, pDB)) {
    fprintf(stderr, "TEST_SelectLk error!\n");
    exit(1);
  }
  COPY_FChar_35 (Test1.fszName, "Name21");
  if (! TEST_SelectLk(&Test1, pDB)) {
    fprintf(stderr, "TEST_SelectLk error!\n");
    exit(1);
  }

  iCount = TEST_Compare(pDB, &Test, &Test1, &MCmp);

  /* print all differences */
  if (iCount == 0) {
    fprintf(stdout, "No differences!\n");
  } else {
    fprintf(stdout, "%d differences\n", iCount);
    fprintf(stdout, "%s\n", _CMP_Attribs());
    for (i = 0; i < MCmp.iCount; i++) {
      pCmp = _MCMP_GetPtr(&MCmp, i);
      fprintf(stdout, "%s\n", _CMP_Data(pDB, pCmp));
    }
  }
  _MCMP_Free(&MCmp);

  exit(0);
}
