#include "dirdb.h"

int DIR_Sizeof(void)
{
  return sizeof(DIR);
}

DIR * DIR_New(void)
{
  return DIR_Init((DIR *) mymalloc(sizeof(DIR)));
}

DIR * DIR_Init(DIR *dir)
{
  dir->_iSize = DIR_Sizeof();
  dir->_iTablenr = DIR_TABLENR;
  dir->_pfnCmpPk = (PFNCMP) DIR_CmpPk;
  dir->_pfnCmpLk = (PFNCMP) DIR_CmpLk;
  INIT_FLong(dir->flIVal);
  INIT_FLong(dir->flDIR_Id);
  return dir;
}

MDIR * MDIR_Init(MDIR *mdir)
{
  ARRAY_Init((ARRAY*)mdir);
  mdir->_iTablenr = DIR_TABLENR;
  return mdir;
}


static char * _DIR_def = NULL;

char * DIR_Definition(void)
{
  if (_DIR_def != NULL) return _DIR_def;
  else {
    DIR dir;
    char sql[10000];

    sprintf(sql,"2000,%d"\
"|DIR_Id,4,4,%d"\
"|IVal,4,4,%d"\
, sizeof(DIR)
,(int) &dir.flDIR_Id - (int) &dir
,(int) &dir.flIVal - (int) &dir);
    _DIR_def = mystrdup(sql);
    return _DIR_def;
  }
}

int DIR_BFill(DIR *dir, 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("DIR_Fill: None or more than one resulttuple!\n");
      iRc = FALSE;
    } else {
      if (row == -1) row = 0;
      DBfillbinary(res, &dir->flDIR_Id, row, 0);
      DBfillbinary(res, &dir->flIVal, row, 1);
    }
  } else {
    Log_Msg("DIR_Fill: Status is not DB_TUPLES_OK!\n");
    iRc = FALSE;
  }
  return iRc;
}

char * DIR_Attribs(void)
{
  return("DIR_Id,\
IVal");
}

int DIR_IsValid(DBHANDLE *pDBHANDLE, DIR *dir)
{
  return TRUE;
}

char * DIR_Data(DBHANDLE * pDBHANDLE, DIR *dir)
{
  char sql[10000];

  Hold_ValueStrings();
  sprintf(sql, "\"%s\",\
\"%s\"" \
,Make_Reduce_NumString(Make_LongString(dir->flDIR_Id)) \
,Make_Reduce_NumString(Make_LongString(dir->flIVal)));
  Release_ValueStrings();
  Free_ValueStrings();
  return mystrdup(sql);
}

char * DIR_TB_Attribs(int kz_db)
{
  if (kz_db) {
    return("DIR_Id,"\
"IVal");
  } else {
    return("DIR_Id,"\
"IVal");
  }
}

int DIR_SelectPk(DIR *dir, DBHANDLE *pDBHANDLE)
{
  return FALSE;
}

int DIR_SelectLk(DIR *dir, DBHANDLE *pDBHANDLE)
{
  return FALSE;
}

int DIR_Insert(DIR *dir, 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_STD);
      DBquery_addtokname(DBTK_Name, "DIR");
      DBquery_addtokdata(DBTK_INTEGER,&dir->flDIR_Id,sizeof(int));
      DBquery_addtokdata(DBTK_INTEGER,&dir->flIVal,sizeof(int));
      iRc = dbbinexec1(pDBHANDLE->conn, &res, &status);
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int DIR_Load(DIR *dir, 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, "DIR");
    DBquery_addtokdata(DBTK_INTEGER,&dir->flDIR_Id,sizeof(int));
    DBquery_addtokdata(DBTK_INTEGER,&dir->flIVal,sizeof(int));
    iRc = dbbinexec1(pDBHANDLE->conn, &res, &status);
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int DIR_Update(DIR *dir, DBHANDLE *pDBHANDLE)
{
  Log_Msg("DIR_Update: No key!\n");
  Msglog_Exit(1);
  return FALSE;
}

int DIR_Delete(DIR *dir, DBHANDLE *pDBHANDLE)
{
  Log_Msg("DIR_Delete: No key!\n");
  Msglog_Exit(1);
}

int DIR_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, "DIR");
    iRc = dbbinexec1(pDBHANDLE->conn, &res, &status);
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int DIR_Do(DIR *dir, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;

  switch (dir->iDB) {
    case SQL_NONE:
      break;
    case SQL_SELECTLK:
      iRc = DIR_SelectLk(dir, pDBHANDLE);
      break;
    case SQL_SELECTPK:
      iRc = DIR_SelectPk(dir, pDBHANDLE);
      break;
    case SQL_INSERT:
      iRc = DIR_Insert(dir, pDBHANDLE);
      break;
    case SQL_UPDATE:
      iRc = DIR_Update(dir, pDBHANDLE);
      break;
    case SQL_DELETE:
      iRc = DIR_Delete(dir, pDBHANDLE);
      break;
    default:
      Log_Msg("DIR_Do: 'iDB'-code unknown!\n");
      iRc = FALSE;
  }
  return iRc;
}

int DIR_CopyPk(DIR *dir1, DIR *dir2)
{
  Log_Msg("DIR_CopyPk: No primary key!\n");
  Msglog_Exit(1);
  return FALSE;
}

int DIR_CmpPk(TFSurrogat pkey, DIR *dir2)
{
  Log_Msg("DIR_CmpPk: No primary key!\n");
  Msglog_Exit(1);
  return FALSE;
}

int DIR_CmpLk(DIR *dir1, DIR *dir2)
{
  Log_Msg("DIR_CmpLk: No logical key!\n");
  Msglog_Exit(1);
  return FALSE;
}

int DIR_Select_DIR_Id (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;
  MYDB_Result res;

  sprintf(sql, 	"SELECT * FROM DIR " \
	"WHERE DIR_ID = %d" \
,dir->flDIR_Id);
  Free_ValueStrings();
  if (Is_DBSQL(pDBHANDLE)) {
    iRc = dbexec1(pDBHANDLE->conn,sql,&res,&status);
    if (iRc == TRUE) {
      DBresult * dbres = (DBresult*)res.res;
      iRc = DIR_BFill(dir, dbres, -1, status, pDBHANDLE);
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int DIR_Selectcount_Id_Max (DIR * dir, int * iCount, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;
  MYDB_Result res;

  if (! dbbegin(pDBHANDLE->conn)) Msglog_Exit(1);
  sprintf(sql, 	"SELECT MAX(DIR_ID) FROM DIR"
);
  iRc = dbopencursor(pDBHANDLE->conn, "CUR_Id_Max", sql);
  iRc = dbexec(pDBHANDLE->conn, "FETCH ALL", "in CUR_Id_Max", &res, &status);
  if (iRc == TRUE) {
    iRc = dbfill_count(pDBHANDLE->conn, &res, status, iCount);
    dbclosecursor(pDBHANDLE->conn, "CUR_Id_Max");
  }
  dbresfree(&res);
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  Free_ValueStrings();
  return iRc;
}

static int _DIR_Select_IVal_cnt = 0;
static MDIR _MDIR_DIR_Select_IVal;

int DIR_Select_IVal (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, 	"SELECT * FROM DIR " \
	"WHERE IVAL = %d" \
,dir->flIVal);
  Free_ValueStrings();
  _DIR_Select_IVal_cnt = 0;
  MDIR_Init(&_MDIR_DIR_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 = DIR_BFill(dir, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MDIR_Add(&_MDIR_DIR_Select_IVal, dir);
      }
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int DIR_Fetch_IVal (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;

  if (_DIR_Select_IVal_cnt < _MDIR_DIR_Select_IVal.iCount)
    DIR_Assign(dir, MDIR_GetPtr(&_MDIR_DIR_Select_IVal, _DIR_Select_IVal_cnt++));
  else {
    iRc = FALSE; MDIR_Free(&_MDIR_DIR_Select_IVal);
  }
  return iRc;
}

static int _DIR_Select_IVal_G_cnt = 0;
static MDIR _MDIR_DIR_Select_IVal_G;

int DIR_Select_IVal_G (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, 	"SELECT * FROM DIR " \
	"WHERE IVAL > %d" \
,dir->flIVal);
  Free_ValueStrings();
  _DIR_Select_IVal_G_cnt = 0;
  MDIR_Init(&_MDIR_DIR_Select_IVal_G);
  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 = DIR_BFill(dir, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MDIR_Add(&_MDIR_DIR_Select_IVal_G, dir);
      }
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int DIR_Fetch_IVal_G (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;

  if (_DIR_Select_IVal_G_cnt < _MDIR_DIR_Select_IVal_G.iCount)
    DIR_Assign(dir, MDIR_GetPtr(&_MDIR_DIR_Select_IVal_G, _DIR_Select_IVal_G_cnt++));
  else {
    iRc = FALSE; MDIR_Free(&_MDIR_DIR_Select_IVal_G);
  }
  return iRc;
}

static int _DIR_Select_IVal_GE_cnt = 0;
static MDIR _MDIR_DIR_Select_IVal_GE;

int DIR_Select_IVal_GE (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, 	"SELECT * FROM DIR " \
	"WHERE IVAL >= %d" \
,dir->flIVal);
  Free_ValueStrings();
  _DIR_Select_IVal_GE_cnt = 0;
  MDIR_Init(&_MDIR_DIR_Select_IVal_GE);
  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 = DIR_BFill(dir, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MDIR_Add(&_MDIR_DIR_Select_IVal_GE, dir);
      }
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int DIR_Fetch_IVal_GE (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;

  if (_DIR_Select_IVal_GE_cnt < _MDIR_DIR_Select_IVal_GE.iCount)
    DIR_Assign(dir, MDIR_GetPtr(&_MDIR_DIR_Select_IVal_GE, _DIR_Select_IVal_GE_cnt++));
  else {
    iRc = FALSE; MDIR_Free(&_MDIR_DIR_Select_IVal_GE);
  }
  return iRc;
}

static int _DIR_Select_IVal_L_cnt = 0;
static MDIR _MDIR_DIR_Select_IVal_L;

int DIR_Select_IVal_L (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, 	"SELECT * FROM DIR " \
	"WHERE IVAL < %d" \
,dir->flIVal);
  Free_ValueStrings();
  _DIR_Select_IVal_L_cnt = 0;
  MDIR_Init(&_MDIR_DIR_Select_IVal_L);
  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 = DIR_BFill(dir, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MDIR_Add(&_MDIR_DIR_Select_IVal_L, dir);
      }
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int DIR_Fetch_IVal_L (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;

  if (_DIR_Select_IVal_L_cnt < _MDIR_DIR_Select_IVal_L.iCount)
    DIR_Assign(dir, MDIR_GetPtr(&_MDIR_DIR_Select_IVal_L, _DIR_Select_IVal_L_cnt++));
  else {
    iRc = FALSE; MDIR_Free(&_MDIR_DIR_Select_IVal_L);
  }
  return iRc;
}

static int _DIR_Select_IVal_LE_cnt = 0;
static MDIR _MDIR_DIR_Select_IVal_LE;

int DIR_Select_IVal_LE (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, 	"SELECT * FROM DIR " \
	"WHERE IVAL <= %d" \
,dir->flIVal);
  Free_ValueStrings();
  _DIR_Select_IVal_LE_cnt = 0;
  MDIR_Init(&_MDIR_DIR_Select_IVal_LE);
  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 = DIR_BFill(dir, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MDIR_Add(&_MDIR_DIR_Select_IVal_LE, dir);
      }
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int DIR_Fetch_IVal_LE (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;

  if (_DIR_Select_IVal_LE_cnt < _MDIR_DIR_Select_IVal_LE.iCount)
    DIR_Assign(dir, MDIR_GetPtr(&_MDIR_DIR_Select_IVal_LE, _DIR_Select_IVal_LE_cnt++));
  else {
    iRc = FALSE; MDIR_Free(&_MDIR_DIR_Select_IVal_LE);
  }
  return iRc;
}

static int _DIR_Select_IVal_BETWEEN_cnt = 0;
static MDIR _MDIR_DIR_Select_IVal_BETWEEN;

int DIR_Select_IVal_BETWEEN (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, 	"SELECT * FROM DIR " \
	"WHERE IVAL BETWEEN 0 AND 1000");
  Free_ValueStrings();
  _DIR_Select_IVal_BETWEEN_cnt = 0;
  MDIR_Init(&_MDIR_DIR_Select_IVal_BETWEEN);
  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 = DIR_BFill(dir, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MDIR_Add(&_MDIR_DIR_Select_IVal_BETWEEN, dir);
      }
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int DIR_Fetch_IVal_BETWEEN (DIR * dir, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;

  if (_DIR_Select_IVal_BETWEEN_cnt < _MDIR_DIR_Select_IVal_BETWEEN.iCount)
    DIR_Assign(dir, MDIR_GetPtr(&_MDIR_DIR_Select_IVal_BETWEEN, _DIR_Select_IVal_BETWEEN_cnt++));
  else {
    iRc = FALSE; MDIR_Free(&_MDIR_DIR_Select_IVal_BETWEEN);
  }
  return iRc;
}

