#include "plandb.h"

int PLANS_Sizeof(void)
{
  return sizeof(PLANS);
}

PLANS * PLANS_New(void)
{
  return PLANS_Init((PLANS *) mymalloc(sizeof(PLANS)));
}

PLANS * PLANS_Init(PLANS *plans)
{
  plans->_iSize = PLANS_Sizeof();
  plans->_iTablenr = PLANS_TABLENR;
  plans->_pfnCmpPk = (PFNCMP) PLANS_CmpPk;
  plans->_pfnCmpLk = (PFNCMP) PLANS_CmpLk;
  INIT_FChar_80(plans->fszComment1);
  INIT_FSurrogat(plans->fszPLANS_Id);
  INIT_FDate(plans->fdaAt);
  INIT_FChar_80(plans->fszComment2);
  INIT_FChar_80(plans->fszContents);
  INIT_FChar_35(plans->fszTitle);
  INIT_FChar_1(plans->fcDone);
  return plans;
}

MPLANS * MPLANS_Init(MPLANS *mplans)
{
  ARRAY_Init((ARRAY*)mplans);
  mplans->_iTablenr = PLANS_TABLENR;
  return mplans;
}


static char * _PLANS_def = NULL;

char * PLANS_Definition(void)
{
  if (_PLANS_def != NULL) return _PLANS_def;
  else {
    PLANS plans;
    char sql[10000];

    sprintf(sql,"1000,%d"\
"|PLANS_Id,11,9,%d"\
"|At,12,4,%d"\
"|Title,11,36,%d"\
"|Contents,11,81,%d"\
"|Comment1,11,81,%d"\
"|Comment2,11,81,%d"\
"|Done,10,1,%d"\
, sizeof(PLANS)
,(int) &plans.fszPLANS_Id - (int) &plans
,(int) &plans.fdaAt - (int) &plans
,(int) &plans.fszTitle - (int) &plans
,(int) &plans.fszContents - (int) &plans
,(int) &plans.fszComment1 - (int) &plans
,(int) &plans.fszComment2 - (int) &plans
,(int) &plans.fcDone - (int) &plans);
    _PLANS_def = mystrdup(sql);
    return _PLANS_def;
  }
}

int PLANS_BFill(PLANS *plans, 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("PLANS_Fill: None or more than one resulttuple!\n");
      iRc = FALSE;
    } else {
      if (row == -1) row = 0;
      DBfillbinary(res, plans->fszPLANS_Id, row, 0);
      DBfillbinary(res, &plans->fdaAt, row, 1);
      DBfillbinary(res, plans->fszTitle, row, 2);
      DBfillbinary(res, plans->fszContents, row, 3);
      DBfillbinary(res, plans->fszComment1, row, 4);
      DBfillbinary(res, plans->fszComment2, row, 5);
      DBfillbinary(res, &plans->fcDone, row, 6);
    }
  } else {
    Log_Msg("PLANS_Fill: Status is not DB_TUPLES_OK!\n");
    iRc = FALSE;
  }
  return iRc;
}

char * PLANS_Attribs(void)
{
  return("PLANS_Id,\
At,\
Title,\
Contents,\
Comment1,\
Comment2,\
Done");
}

int PLANS_IsValid(DBHANDLE *pDBHANDLE, PLANS *plans)
{
  if (! VALID_FDate(plans->fdaAt)) return FALSE;
  return TRUE;
}

char * PLANS_Data(DBHANDLE * pDBHANDLE, PLANS *plans)
{
  char sql[10000];

  Hold_ValueStrings();
  sprintf(sql, "\"%s\",\
\"%s\",\
\"%s\",\
\"%s\",\
\"%s\",\
\"%s\",\
\"%s\"" \
,Make_Reduce_String(Make_ValueString(plans->fszPLANS_Id,8)) \
,Make_Reduce_String(Make_FEDateString(&(plans->fdaAt))) \
,Make_Reduce_String(Make_ValueString(plans->fszTitle,35)) \
,Make_Reduce_String(Make_ValueString(plans->fszContents,80)) \
,Make_Reduce_String(Make_ValueString(plans->fszComment1,80)) \
,Make_Reduce_String(Make_ValueString(plans->fszComment2,80)) \
,Make_Reduce_String(Make_CharString(plans->fcDone)));
  Release_ValueStrings();
  Free_ValueStrings();
  return mystrdup(sql);
}

char * PLANS_TB_Attribs(int kz_db)
{
  if (kz_db) {
    return("PLANS_Id,"\
"At,"\
"Title,"\
"Contents,"\
"Comment1,"\
"Comment2,"\
"Done");
  } else {
    return("PLANS_Id,"\
"At,"\
"Title,"\
"Contents,"\
"Comment1,"\
"Comment2,"\
"Done");
  }
}

int PLANS_SelectPk(PLANS *plans, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;
  MYDB_Result res;

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

int PLANS_SelectLk(PLANS *plans, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;
  MYDB_Result res;

  if (! dbbegin(pDBHANDLE->conn)) Msglog_Exit(1);
  sprintf(sql, "* FROM plans WHERE "\
"Title='%s' AND \
At='%s'" \
,Make_DbValueString(plans->fszTitle, 35) \
,Make_DateString(&(plans->fdaAt)));
  iRc = dbexec(pDBHANDLE->conn,"SELECT",sql,&res,&status);
  if (iRc == TRUE)
    if (Is_DBSQL(pDBHANDLE)) {
      iRc = PLANS_BFill(plans,(DBresult*)res.res,-1,status,pDBHANDLE);
    } else {
      iRc = FALSE;
    }
  Free_ValueStrings(); dbresfree(&res);
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int PLANS_Insert(PLANS *plans, 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(&(plans->fszPLANS_Id));
      DBquery_init(); DBquery_addcmd(DB_INSERT_STD);
      DBquery_addtokname(DBTK_Name, "PLANS");
      DBquery_addtokstring(DBTK_CHARACTER,plans->fszPLANS_Id);
      DBquery_addtokdata(DBTK_DATE,&plans->fdaAt,sizeof(DBDATE));
      DBquery_addtokstring(DBTK_CHARACTER,plans->fszTitle);
      DBquery_addtokstring(DBTK_CHARACTER,plans->fszContents);
      DBquery_addtokstring(DBTK_CHARACTER,plans->fszComment1);
      DBquery_addtokstring(DBTK_CHARACTER,plans->fszComment2);
      DBquery_addtokdata(DBTK_CHARACTER,&plans->fcDone,1);
      iRc = dbbinexec2(pDBHANDLE->conn, &res, &status, FALSE);
    } while (iRc != TRUE && cnt++ < 100);
    if (iRc != TRUE) Log_Msg("PLANS_Insert: Insert not possible (100 tries)\n");
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int PLANS_Load(PLANS *plans, 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, "PLANS");
    DBquery_addtokstring(DBTK_CHARACTER,plans->fszPLANS_Id);
    DBquery_addtokdata(DBTK_DATE,&plans->fdaAt,sizeof(DBDATE));
    DBquery_addtokstring(DBTK_CHARACTER,plans->fszTitle);
    DBquery_addtokstring(DBTK_CHARACTER,plans->fszContents);
    DBquery_addtokstring(DBTK_CHARACTER,plans->fszComment1);
    DBquery_addtokstring(DBTK_CHARACTER,plans->fszComment2);
    DBquery_addtokdata(DBTK_CHARACTER,&plans->fcDone,1);
    iRc = dbbinexec1(pDBHANDLE->conn, &res, &status);
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int PLANS_Update(PLANS *plans, 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, "PLANS");
    DBquery_addtokstring(DBTK_CHARACTER,plans->fszPLANS_Id);
    DBquery_addtokdata(DBTK_DATE,&plans->fdaAt,sizeof(DBDATE));
    DBquery_addtokstring(DBTK_CHARACTER,plans->fszTitle);
    DBquery_addtokstring(DBTK_CHARACTER,plans->fszContents);
    DBquery_addtokstring(DBTK_CHARACTER,plans->fszComment1);
    DBquery_addtokstring(DBTK_CHARACTER,plans->fszComment2);
    DBquery_addtokdata(DBTK_CHARACTER,&plans->fcDone,1);
    sprintf(sql, "WHERE PLANS_Id='%s'"
,plans->fszPLANS_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 PLANS_Delete(PLANS *plans, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;

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

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

int PLANS_Do(PLANS *plans, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;

  switch (plans->iDB) {
    case SQL_NONE:
      break;
    case SQL_SELECTLK:
      iRc = PLANS_SelectLk(plans, pDBHANDLE);
      break;
    case SQL_SELECTPK:
      iRc = PLANS_SelectPk(plans, pDBHANDLE);
      break;
    case SQL_INSERT:
      iRc = PLANS_Insert(plans, pDBHANDLE);
      break;
    case SQL_UPDATE:
      iRc = PLANS_Update(plans, pDBHANDLE);
      break;
    case SQL_DELETE:
      iRc = PLANS_Delete(plans, pDBHANDLE);
      break;
    default:
      Log_Msg("PLANS_Do: 'iDB'-code unknown!\n");
      iRc = FALSE;
  }
  return iRc;
}

int PLANS_CopyPk(PLANS *plans1, PLANS *plans2)
{
  
  {

  COPY_FSurrogat (plans1->fszPLANS_Id, plans2->fszPLANS_Id);
  return TRUE;
}}

int PLANS_CmpPk(TFSurrogat pkey, PLANS *plans2)
{
  if (! CMP_FSurrogat(pkey, plans2->fszPLANS_Id)) return FALSE;
  return TRUE;
}

int PLANS_CmpLk(PLANS *plans1, PLANS *plans2)
{
  
  {
  int iRc;

  iRc = DATA_Compare (plans1->fszTitle, TYP_STRING, plans2->fszTitle, TYP_STRING);
  if (iRc != 0) return FALSE;
  iRc = DATA_Compare (&plans1->fdaAt, TYP_DATE, &plans2->fdaAt, TYP_DATE);
  if (iRc != 0) return FALSE;
  return TRUE;
}}

static int _PLANS_Select_cnt = 0;
static MPLANS _MPLANS_Select;

int PLANS_Select(PLANS *plans, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, "SELECT * FROM PLANS ");
  Free_ValueStrings();
  _PLANS_Select_cnt = 0;
  MPLANS_Init(&_MPLANS_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 = PLANS_BFill(plans, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MPLANS_Add(&_MPLANS_Select, plans);
      }
      dbresfree(&res);
    }
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int PLANS_Fetch(PLANS *plans, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;

  if (_PLANS_Select_cnt < _MPLANS_Select.iCount)
    PLANS_Assign(plans, MPLANS_GetPtr(&_MPLANS_Select, _PLANS_Select_cnt++));
  else {
    iRc = FALSE; MPLANS_Free(&_MPLANS_Select);
  }
  return iRc;
}

static int _PLANS_Select_Title_cnt = 0;
static MPLANS _MPLANS_PLANS_Select_Title;

int PLANS_Select_Title (PLANS * plans, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, 	"SELECT * FROM PLANS " \
	"WHERE  TITLE LIKE '%s' " \
	"ORDER BY AT, TITLE" \
,Make_DbValueString(plans->fszTitle, 35));
  Free_ValueStrings();
  _PLANS_Select_Title_cnt = 0;
  MPLANS_Init(&_MPLANS_PLANS_Select_Title);
  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 = PLANS_BFill(plans, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MPLANS_Add(&_MPLANS_PLANS_Select_Title, plans);
      }
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int PLANS_Fetch_Title (PLANS * plans, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;

  if (_PLANS_Select_Title_cnt < _MPLANS_PLANS_Select_Title.iCount)
    PLANS_Assign(plans, MPLANS_GetPtr(&_MPLANS_PLANS_Select_Title, _PLANS_Select_Title_cnt++));
  else {
    iRc = FALSE; MPLANS_Free(&_MPLANS_PLANS_Select_Title);
  }
  return iRc;
}

static int _PLANS_Select_Title_Date_cnt = 0;
static MPLANS _MPLANS_PLANS_Select_Title_Date;

int PLANS_Select_Title_Date (PLANS * plans, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, 	"SELECT * FROM PLANS " \
	"WHERE  TITLE LIKE '%s' " \
	"AND    (AT >= '%s' OR AT = '1800-01-01' OR DONE = 'n') " \
	"ORDER BY AT, TITLE" \
,Make_DbValueString(plans->fszTitle, 35) \
,Make_DateString(&(plans->fdaAt)));
  Free_ValueStrings();
  _PLANS_Select_Title_Date_cnt = 0;
  MPLANS_Init(&_MPLANS_PLANS_Select_Title_Date);
  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 = PLANS_BFill(plans, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MPLANS_Add(&_MPLANS_PLANS_Select_Title_Date, plans);
      }
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int PLANS_Fetch_Title_Date (PLANS * plans, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;

  if (_PLANS_Select_Title_Date_cnt < _MPLANS_PLANS_Select_Title_Date.iCount)
    PLANS_Assign(plans, MPLANS_GetPtr(&_MPLANS_PLANS_Select_Title_Date, _PLANS_Select_Title_Date_cnt++));
  else {
    iRc = FALSE; MPLANS_Free(&_MPLANS_PLANS_Select_Title_Date);
  }
  return iRc;
}

static int _PLANS_Select_Title_Date_Acc_cnt = 0;
static MPLANS _MPLANS_PLANS_Select_Title_Date_Acc;

int PLANS_Select_Title_Date_Acc (PLANS * plans, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, 	"SELECT * FROM PLANS " \
	"WHERE  TITLE LIKE '%s' " \
	"AND    AT = '%s' " \
	"ORDER BY AT, TITLE" \
,Make_DbValueString(plans->fszTitle, 35) \
,Make_DateString(&(plans->fdaAt)));
  Free_ValueStrings();
  _PLANS_Select_Title_Date_Acc_cnt = 0;
  MPLANS_Init(&_MPLANS_PLANS_Select_Title_Date_Acc);
  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 = PLANS_BFill(plans, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MPLANS_Add(&_MPLANS_PLANS_Select_Title_Date_Acc, plans);
      }
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int PLANS_Fetch_Title_Date_Acc (PLANS * plans, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;

  if (_PLANS_Select_Title_Date_Acc_cnt < _MPLANS_PLANS_Select_Title_Date_Acc.iCount)
    PLANS_Assign(plans, MPLANS_GetPtr(&_MPLANS_PLANS_Select_Title_Date_Acc, _PLANS_Select_Title_Date_Acc_cnt++));
  else {
    iRc = FALSE; MPLANS_Free(&_MPLANS_PLANS_Select_Title_Date_Acc);
  }
  return iRc;
}

int PLANS_Count (PLANS * plans, 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 COUNT(*) FROM PLANS"
);
  iRc = dbopencursor(pDBHANDLE->conn, "PLANS_Cur_Count", sql);
  iRc = dbexec(pDBHANDLE->conn, "FETCH ALL", "in PLANS_Cur_Count", &res, &status);
  if (iRc == TRUE) {
    iRc = dbfill_count(pDBHANDLE->conn, &res, status, iCount);
    dbclosecursor(pDBHANDLE->conn, "PLANS_Cur_Count");
  }
  dbresfree(&res);
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  Free_ValueStrings();
  return iRc;
}

int PASSWD_Sizeof(void)
{
  return sizeof(PASSWD);
}

PASSWD * PASSWD_New(void)
{
  return PASSWD_Init((PASSWD *) mymalloc(sizeof(PASSWD)));
}

PASSWD * PASSWD_Init(PASSWD *passwd)
{
  passwd->_iSize = PASSWD_Sizeof();
  passwd->_iTablenr = PASSWD_TABLENR;
  passwd->_pfnCmpPk = (PFNCMP) PASSWD_CmpPk;
  passwd->_pfnCmpLk = (PFNCMP) PASSWD_CmpLk;
  INIT_FTimestamp(passwd->fdtAt);
  INIT_FChar_35(passwd->fszPassword);
  INIT_FShort(passwd->fsSubnummer);
  INIT_FSurrogat(passwd->fszPASSWD_Id);
  return passwd;
}

MPASSWD * MPASSWD_Init(MPASSWD *mpasswd)
{
  ARRAY_Init((ARRAY*)mpasswd);
  mpasswd->_iTablenr = PASSWD_TABLENR;
  return mpasswd;
}


static char * _PASSWD_def = NULL;

char * PASSWD_Definition(void)
{
  if (_PASSWD_def != NULL) return _PASSWD_def;
  else {
    PASSWD passwd;
    char sql[10000];

    sprintf(sql,"2000,%d"\
"|PASSWD_Id,11,9,%d"\
"|Password,11,36,%d"\
"|Subnummer,2,2,%d"\
"|At,14,12,%d"\
, sizeof(PASSWD)
,(int) &passwd.fszPASSWD_Id - (int) &passwd
,(int) &passwd.fszPassword - (int) &passwd
,(int) &passwd.fsSubnummer - (int) &passwd
,(int) &passwd.fdtAt - (int) &passwd);
    _PASSWD_def = mystrdup(sql);
    return _PASSWD_def;
  }
}

int PASSWD_BFill(PASSWD *passwd, 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("PASSWD_Fill: None or more than one resulttuple!\n");
      iRc = FALSE;
    } else {
      if (row == -1) row = 0;
      DBfillbinary(res, passwd->fszPASSWD_Id, row, 0);
      DBfillbinary(res, passwd->fszPassword, row, 1);
      DBfillbinary(res, &passwd->fsSubnummer, row, 2);
      DBfillbinary(res, &passwd->fdtAt, row, 3);
    }
  } else {
    Log_Msg("PASSWD_Fill: Status is not DB_TUPLES_OK!\n");
    iRc = FALSE;
  }
  return iRc;
}

char * PASSWD_Attribs(void)
{
  return("PASSWD_Id,\
Password,\
Subnummer,\
At");
}

int PASSWD_IsValid(DBHANDLE *pDBHANDLE, PASSWD *passwd)
{
  if (! VALID_FTimestamp(passwd->fdtAt)) return FALSE;
  return TRUE;
}

char * PASSWD_Data(DBHANDLE * pDBHANDLE, PASSWD *passwd)
{
  char sql[10000];

  Hold_ValueStrings();
  sprintf(sql, "\"%s\",\
\"%s\",\
\"%s\",\
\"%s\"" \
,Make_Reduce_String(Make_ValueString(passwd->fszPASSWD_Id,8)) \
,Make_Reduce_String(Make_ValueString(passwd->fszPassword,35)) \
,Make_Reduce_NumString(Make_ShortString(passwd->fsSubnummer)) \
,Make_Reduce_String(Make_FETimestampString(&(passwd->fdtAt))));
  Release_ValueStrings();
  Free_ValueStrings();
  return mystrdup(sql);
}

char * PASSWD_TB_Attribs(int kz_db)
{
  if (kz_db) {
    return("PASSWD_Id,"\
"Password,"\
"Subnummer,"\
"At");
  } else {
    return("PASSWD_Id,"\
"Password,"\
"Subnummer,"\
"At");
  }
}

int PASSWD_SelectPk(PASSWD *passwd, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;
  MYDB_Result res;

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

int PASSWD_SelectLk(PASSWD *passwd, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;
  MYDB_Result res;

  if (! dbbegin(pDBHANDLE->conn)) Msglog_Exit(1);
  sprintf(sql, "* FROM passwd WHERE "\
"Subnummer=%d" \
,passwd->fsSubnummer);
  iRc = dbexec(pDBHANDLE->conn,"SELECT",sql,&res,&status);
  if (iRc == TRUE)
    if (Is_DBSQL(pDBHANDLE)) {
      iRc = PASSWD_BFill(passwd,(DBresult*)res.res,-1,status,pDBHANDLE);
    } else {
      iRc = FALSE;
    }
  Free_ValueStrings(); dbresfree(&res);
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int PASSWD_Insert(PASSWD *passwd, 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(&(passwd->fszPASSWD_Id));
      DBquery_init(); DBquery_addcmd(DB_INSERT_STD);
      DBquery_addtokname(DBTK_Name, "PASSWD");
      DBquery_addtokstring(DBTK_CHARACTER,passwd->fszPASSWD_Id);
      DBquery_addtokstring(DBTK_CHARACTER,passwd->fszPassword);
      DBquery_addtokdata(DBTK_SMALLINT,&passwd->fsSubnummer,sizeof(short));
      DBquery_addtokdata(DBTK_TIMESTAMP,&passwd->fdtAt,sizeof(DBDATI));
      iRc = dbbinexec2(pDBHANDLE->conn, &res, &status, FALSE);
    } while (iRc != TRUE && cnt++ < 100);
    if (iRc != TRUE) Log_Msg("PASSWD_Insert: Insert not possible (100 tries)\n");
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int PASSWD_Load(PASSWD *passwd, 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, "PASSWD");
    DBquery_addtokstring(DBTK_CHARACTER,passwd->fszPASSWD_Id);
    DBquery_addtokstring(DBTK_CHARACTER,passwd->fszPassword);
    DBquery_addtokdata(DBTK_SMALLINT,&passwd->fsSubnummer,sizeof(short));
    DBquery_addtokdata(DBTK_TIMESTAMP,&passwd->fdtAt,sizeof(DBDATI));
    iRc = dbbinexec1(pDBHANDLE->conn, &res, &status);
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int PASSWD_Update(PASSWD *passwd, 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, "PASSWD");
    DBquery_addtokstring(DBTK_CHARACTER,passwd->fszPASSWD_Id);
    DBquery_addtokstring(DBTK_CHARACTER,passwd->fszPassword);
    DBquery_addtokdata(DBTK_SMALLINT,&passwd->fsSubnummer,sizeof(short));
    DBquery_addtokdata(DBTK_TIMESTAMP,&passwd->fdtAt,sizeof(DBDATI));
    sprintf(sql, "WHERE PASSWD_Id='%s'"
,passwd->fszPASSWD_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 PASSWD_Delete(PASSWD *passwd, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;

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

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

int PASSWD_Do(PASSWD *passwd, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;

  switch (passwd->iDB) {
    case SQL_NONE:
      break;
    case SQL_SELECTLK:
      iRc = PASSWD_SelectLk(passwd, pDBHANDLE);
      break;
    case SQL_SELECTPK:
      iRc = PASSWD_SelectPk(passwd, pDBHANDLE);
      break;
    case SQL_INSERT:
      iRc = PASSWD_Insert(passwd, pDBHANDLE);
      break;
    case SQL_UPDATE:
      iRc = PASSWD_Update(passwd, pDBHANDLE);
      break;
    case SQL_DELETE:
      iRc = PASSWD_Delete(passwd, pDBHANDLE);
      break;
    default:
      Log_Msg("PASSWD_Do: 'iDB'-code unknown!\n");
      iRc = FALSE;
  }
  return iRc;
}

int PASSWD_CopyPk(PASSWD *passwd1, PASSWD *passwd2)
{
  
  {

  COPY_FSurrogat (passwd1->fszPASSWD_Id, passwd2->fszPASSWD_Id);
  return TRUE;
}}

int PASSWD_CmpPk(TFSurrogat pkey, PASSWD *passwd2)
{
  if (! CMP_FSurrogat(pkey, passwd2->fszPASSWD_Id)) return FALSE;
  return TRUE;
}

int PASSWD_CmpLk(PASSWD *passwd1, PASSWD *passwd2)
{
  
  {
  int iRc;

  iRc = DATA_Compare (&passwd1->fsSubnummer, TYP_SHORT, &passwd2->fsSubnummer, TYP_SHORT);
  if (iRc != 0) return FALSE;
  return TRUE;
}}

static int _PASSWD_Select_cnt = 0;
static MPASSWD _MPASSWD_Select;

int PASSWD_Select(PASSWD *passwd, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, "SELECT * FROM PASSWD ");
  Free_ValueStrings();
  _PASSWD_Select_cnt = 0;
  MPASSWD_Init(&_MPASSWD_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 = PASSWD_BFill(passwd, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MPASSWD_Add(&_MPASSWD_Select, passwd);
      }
      dbresfree(&res);
    }
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int PASSWD_Fetch(PASSWD *passwd, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;

  if (_PASSWD_Select_cnt < _MPASSWD_Select.iCount)
    PASSWD_Assign(passwd, MPASSWD_GetPtr(&_MPASSWD_Select, _PASSWD_Select_cnt++));
  else {
    iRc = FALSE; MPASSWD_Free(&_MPASSWD_Select);
  }
  return iRc;
}

static int _PASSWD_Select_Subnummer_cnt = 0;
static MPASSWD _MPASSWD_PASSWD_Select_Subnummer;

int PASSWD_Select_Subnummer (PASSWD * passwd, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, 	"SELECT * FROM PASSWD " \
	"WHERE SUBNUMMER = %d " \
	"ORDER BY SUBNUMMER" \
,passwd->fsSubnummer);
  Free_ValueStrings();
  _PASSWD_Select_Subnummer_cnt = 0;
  MPASSWD_Init(&_MPASSWD_PASSWD_Select_Subnummer);
  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 = PASSWD_BFill(passwd, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MPASSWD_Add(&_MPASSWD_PASSWD_Select_Subnummer, passwd);
      }
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int PASSWD_Fetch_Name (PASSWD * passwd, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;

  if (_PASSWD_Select_Subnummer_cnt < _MPASSWD_PASSWD_Select_Subnummer.iCount)
    PASSWD_Assign(passwd, MPASSWD_GetPtr(&_MPASSWD_PASSWD_Select_Subnummer, _PASSWD_Select_Subnummer_cnt++));
  else {
    iRc = FALSE; MPASSWD_Free(&_MPASSWD_PASSWD_Select_Subnummer);
  }
  return iRc;
}

int PASSWD_Delete_Wrong (PASSWD * passwd, int * iCount, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;
  MYDB_Result res;

  if (! dbbegin(pDBHANDLE->conn)) Msglog_Exit(1);
  sprintf(sql, 	"DELETE FROM PASSWD " \
	"WHERE SUBNUMMER > 1"
);
  iRc = dbexec1(pDBHANDLE->conn, sql, &res, &status);
  if (iRc == TRUE) {
    iRc = dbfill_res_count(pDBHANDLE->conn, &res, status, iCount);
  }
  dbresfree(&res);
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  Free_ValueStrings();
  return iRc;
}

int PASSWD_Update_Password (PASSWD * passwd, int * iCount, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;
  MYDB_Result res;

  if (! dbbegin(pDBHANDLE->conn)) Msglog_Exit(1);
  sprintf(sql, 	"UPDATE PASSWD SET PASSWORD = '%s' " \
	"WHERE SUBNUMMER = 1"
 \
,Make_DbValueString(passwd->fszPassword, 35));
  iRc = dbexec1(pDBHANDLE->conn, sql, &res, &status);
  if (iRc == TRUE) {
    iRc = dbfill_res_count(pDBHANDLE->conn, &res, status, iCount);
  }
  dbresfree(&res);
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  Free_ValueStrings();
  return iRc;
}

void Get_GRPs (_MGRP * pMGrp)
{
  _GRP Grp;

  _GRP_Init(&Grp);
  Grp.grpname = mystrdup("Plans"); Grp.grpid = 0;
  Grp.grpkuerzel = mystrdup("PLANSDAT");
  _MGRP_Add(pMGrp, &Grp);
  }

int GRP_Data_Save (DBHANDLE * pDBHANDLE, char * fn, char * kuerzel)
{
  int iRc = TRUE;
  char Table[20000];
  FILE * fp;

  if ((fp = File_Write_Open(fn)) == NULL) return FALSE;
  if (strcmp(kuerzel, "PLANSDAT") == 0) {
    iRc = iRc && TABLE_File_Save(pDBHANDLE,fp,PLANS_Definition(),
          (char*)PLANS_Init((PLANS*)&Table),PLANS_Select,PLANS_Fetch);
    iRc = iRc && TABLE_File_Save(pDBHANDLE,fp,PASSWD_Definition(),
          (char*)PASSWD_Init((PASSWD*)&Table),PASSWD_Select,PASSWD_Fetch);
  }
  fclose(fp);
  return iRc;
}

int GRP_Data_Load (DBHANDLE * pDBHANDLE, char * fn, char * kuerzel)
{
  int iRc = TRUE;
  FILE * fp;

  if ((fp = File_Read_Open(fn)) == NULL) return FALSE;
  if (strcmp(kuerzel, "PLANSDAT") == 0) {
    if (dbcache_changed("PLANS")) {
      PLANS_DeleteAll(pDBHANDLE);
      iRc = iRc && TABLE_File_Load(pDBHANDLE,fp,PLANS_Definition(),
            PLANS_Init, PLANS_Load);
    } else {
      iRc = iRc && TABLE_File_Skip(fp,PLANS_Definition());
    }
    if (dbcache_changed("PASSWD")) {
      PASSWD_DeleteAll(pDBHANDLE);
      iRc = iRc && TABLE_File_Load(pDBHANDLE,fp,PASSWD_Definition(),
            PASSWD_Init, PASSWD_Load);
    } else {
      iRc = iRc && TABLE_File_Skip(fp,PASSWD_Definition());
    }
  }
  fclose(fp);
  return iRc;
}

int GRP_Data_Delete (DBHANDLE * pDBHANDLE, char * kuerzel)
{
  int iRc = TRUE;

  if (strcmp(kuerzel, "PLANSDAT") == 0) {
    iRc = iRc && PLANS_DeleteAll(pDBHANDLE);
    iRc = iRc && PASSWD_DeleteAll(pDBHANDLE);
  }
  return iRc;
}

