#include "phonedb.h"

int TELNR_Sizeof(void)
{
  return sizeof(TELNR);
}

TELNR * TELNR_New(void)
{
  return TELNR_Init((TELNR *) mymalloc(sizeof(TELNR)));
}

TELNR * TELNR_Init(TELNR *telnr)
{
  telnr->_iSize = TELNR_Sizeof();
  telnr->_iTablenr = TELNR_TABLENR;
  telnr->_pfnCmpPk = (PFNCMP) TELNR_CmpPk;
  telnr->_pfnCmpLk = (PFNCMP) TELNR_CmpLk;
  INIT_FChar_80(telnr->fszComment);
  INIT_FChar_25(telnr->fszOffice);
  INIT_FChar_25(telnr->fszPrivate);
  INIT_FChar_80(telnr->fszEmail);
  INIT_FSurrogat(telnr->fszTELNR_Id);
  INIT_FChar_35(telnr->fszName);
  INIT_FChar_25(telnr->fszHandy);
  return telnr;
}

MTELNR * MTELNR_Init(MTELNR *mtelnr)
{
  ARRAY_Init((ARRAY*)mtelnr);
  mtelnr->_iTablenr = TELNR_TABLENR;
  return mtelnr;
}


static char * _TELNR_def = NULL;

char * TELNR_Definition(void)
{
  if (_TELNR_def != NULL) return _TELNR_def;
  else {
    TELNR telnr;
    char sql[10000];

    sprintf(sql,"1000,%d"\
"|TELNR_Id,11,9,%d"\
"|Name,11,36,%d"\
"|Private,11,26,%d"\
"|Office,11,26,%d"\
"|Handy,11,26,%d"\
"|Email,11,81,%d"\
"|Comment,11,81,%d"\
, sizeof(TELNR)
,(int) &telnr.fszTELNR_Id - (int) &telnr
,(int) &telnr.fszName - (int) &telnr
,(int) &telnr.fszPrivate - (int) &telnr
,(int) &telnr.fszOffice - (int) &telnr
,(int) &telnr.fszHandy - (int) &telnr
,(int) &telnr.fszEmail - (int) &telnr
,(int) &telnr.fszComment - (int) &telnr);
    _TELNR_def = mystrdup(sql);
    return _TELNR_def;
  }
}

int TELNR_BFill(TELNR *telnr, 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("TELNR_Fill: None or more than one resulttuple!\n");
      iRc = FALSE;
    } else {
      if (row == -1) row = 0;
      DBfillbinary(res, telnr->fszTELNR_Id, row, 0);
      DBfillbinary(res, telnr->fszName, row, 1);
      DBfillbinary(res, telnr->fszPrivate, row, 2);
      DBfillbinary(res, telnr->fszOffice, row, 3);
      DBfillbinary(res, telnr->fszHandy, row, 4);
      DBfillbinary(res, telnr->fszEmail, row, 5);
      DBfillbinary(res, telnr->fszComment, row, 6);
    }
  } else {
    Log_Msg("TELNR_Fill: Status is not DB_TUPLES_OK!\n");
    iRc = FALSE;
  }
  return iRc;
}

char * TELNR_Attribs(void)
{
  return("TELNR_Id,\
Name,\
Private,\
Office,\
Handy,\
Email,\
Comment");
}

int TELNR_IsValid(DBHANDLE *pDBHANDLE, TELNR *telnr)
{
  return TRUE;
}

char * TELNR_Data(DBHANDLE * pDBHANDLE, TELNR *telnr)
{
  char sql[10000];

  Hold_ValueStrings();
  sprintf(sql, "\"%s\",\
\"%s\",\
\"%s\",\
\"%s\",\
\"%s\",\
\"%s\",\
\"%s\"" \
,Make_Reduce_String(Make_ValueString(telnr->fszTELNR_Id,8)) \
,Make_Reduce_String(Make_ValueString(telnr->fszName,35)) \
,Make_Reduce_String(Make_ValueString(telnr->fszPrivate,25)) \
,Make_Reduce_String(Make_ValueString(telnr->fszOffice,25)) \
,Make_Reduce_String(Make_ValueString(telnr->fszHandy,25)) \
,Make_Reduce_String(Make_ValueString(telnr->fszEmail,80)) \
,Make_Reduce_String(Make_ValueString(telnr->fszComment,80)));
  Release_ValueStrings();
  Free_ValueStrings();
  return mystrdup(sql);
}

char * TELNR_TB_Attribs(int kz_db)
{
  if (kz_db) {
    return("TELNR_Id,"\
"Name,"\
"Private,"\
"Office,"\
"Handy,"\
"Email,"\
"Comment");
  } else {
    return("TELNR_Id,"\
"Name,"\
"Private,"\
"Office,"\
"Handy,"\
"Email,"\
"Comment");
  }
}

int TELNR_SelectPk(TELNR *telnr, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;
  MYDB_Result res;

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

int TELNR_SelectLk(TELNR *telnr, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;
  MYDB_Result res;

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

int TELNR_Insert(TELNR *telnr, 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(&(telnr->fszTELNR_Id));
      DBquery_init(); DBquery_addcmd(DB_INSERT_STD);
      DBquery_addtokname(DBTK_Name, "TELNR");
      DBquery_addtokstring(DBTK_CHARACTER,telnr->fszTELNR_Id);
      DBquery_addtokstring(DBTK_CHARACTER,telnr->fszName);
      DBquery_addtokstring(DBTK_CHARACTER,telnr->fszPrivate);
      DBquery_addtokstring(DBTK_CHARACTER,telnr->fszOffice);
      DBquery_addtokstring(DBTK_CHARACTER,telnr->fszHandy);
      DBquery_addtokstring(DBTK_CHARACTER,telnr->fszEmail);
      DBquery_addtokstring(DBTK_CHARACTER,telnr->fszComment);
      iRc = dbbinexec2(pDBHANDLE->conn, &res, &status, FALSE);
    } while (iRc != TRUE && cnt++ < 100);
    if (iRc != TRUE) Log_Msg("TELNR_Insert: Insert not possible (100 tries)\n");
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int TELNR_Load(TELNR *telnr, 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, "TELNR");
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszTELNR_Id);
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszName);
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszPrivate);
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszOffice);
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszHandy);
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszEmail);
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszComment);
    iRc = dbbinexec1(pDBHANDLE->conn, &res, &status);
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  if (! dbend(pDBHANDLE->conn)) Msglog_Exit(1);
  return iRc;
}

int TELNR_Update(TELNR *telnr, 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, "TELNR");
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszTELNR_Id);
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszName);
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszPrivate);
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszOffice);
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszHandy);
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszEmail);
    DBquery_addtokstring(DBTK_CHARACTER,telnr->fszComment);
    sprintf(sql, "WHERE TELNR_Id='%s'"
,telnr->fszTELNR_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 TELNR_Delete(TELNR *telnr, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  char sql[10000];
  int status;

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

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

int TELNR_Do(TELNR *telnr, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;

  switch (telnr->iDB) {
    case SQL_NONE:
      break;
    case SQL_SELECTLK:
      iRc = TELNR_SelectLk(telnr, pDBHANDLE);
      break;
    case SQL_SELECTPK:
      iRc = TELNR_SelectPk(telnr, pDBHANDLE);
      break;
    case SQL_INSERT:
      iRc = TELNR_Insert(telnr, pDBHANDLE);
      break;
    case SQL_UPDATE:
      iRc = TELNR_Update(telnr, pDBHANDLE);
      break;
    case SQL_DELETE:
      iRc = TELNR_Delete(telnr, pDBHANDLE);
      break;
    default:
      Log_Msg("TELNR_Do: 'iDB'-code unknown!\n");
      iRc = FALSE;
  }
  return iRc;
}

int TELNR_CopyPk(TELNR *telnr1, TELNR *telnr2)
{
  
  {

  COPY_FSurrogat (telnr1->fszTELNR_Id, telnr2->fszTELNR_Id);
  return TRUE;
}}

int TELNR_CmpPk(TFSurrogat pkey, TELNR *telnr2)
{
  if (! CMP_FSurrogat(pkey, telnr2->fszTELNR_Id)) return FALSE;
  return TRUE;
}

int TELNR_CmpLk(TELNR *telnr1, TELNR *telnr2)
{
  
  {
  int iRc;

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

static int _TELNR_Select_cnt = 0;
static MTELNR _MTELNR_Select;

int TELNR_Select(TELNR *telnr, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, "SELECT * FROM TELNR ");
  Free_ValueStrings();
  _TELNR_Select_cnt = 0;
  MTELNR_Init(&_MTELNR_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 = TELNR_BFill(telnr, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MTELNR_Add(&_MTELNR_Select, telnr);
      }
      dbresfree(&res);
    }
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int TELNR_Fetch(TELNR *telnr, DBHANDLE *pDBHANDLE)
{
  int iRc = TRUE;

  if (_TELNR_Select_cnt < _MTELNR_Select.iCount)
    TELNR_Assign(telnr, MTELNR_GetPtr(&_MTELNR_Select, _TELNR_Select_cnt++));
  else {
    iRc = FALSE; MTELNR_Free(&_MTELNR_Select);
  }
  return iRc;
}

static int _TELNR_Select_Name_cnt = 0;
static MTELNR _MTELNR_TELNR_Select_Name;

int TELNR_Select_Name (TELNR * telnr, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;
  int status;
  MYDB_Result res;
  char sql[10000];

  sprintf(sql, 	"SELECT * FROM TELNR " \
	"WHERE NAME LIKE '%s' " \
	"ORDER BY NAME" \
,Make_DbValueString(telnr->fszName, 35));
  Free_ValueStrings();
  _TELNR_Select_Name_cnt = 0;
  MTELNR_Init(&_MTELNR_TELNR_Select_Name);
  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 = TELNR_BFill(telnr, dbres, i, status, pDBHANDLE);
        if (iRc != TRUE) break;
        else MTELNR_Add(&_MTELNR_TELNR_Select_Name, telnr);
      }
    }
    dbresfree(&res);
  } else {
    iRc = FALSE;
  }
  return iRc;
}

int TELNR_Fetch_Name (TELNR * telnr, DBHANDLE * pDBHANDLE)
{
  int iRc = TRUE;

  if (_TELNR_Select_Name_cnt < _MTELNR_TELNR_Select_Name.iCount)
    TELNR_Assign(telnr, MTELNR_GetPtr(&_MTELNR_TELNR_Select_Name, _TELNR_Select_Name_cnt++));
  else {
    iRc = FALSE; MTELNR_Free(&_MTELNR_TELNR_Select_Name);
  }
  return iRc;
}

int TELNR_Count (TELNR * telnr, 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 TELNR"
);
  iRc = dbopencursor(pDBHANDLE->conn, "TELNR_Cur_Count", sql);
  iRc = dbexec(pDBHANDLE->conn, "FETCH ALL", "in TELNR_Cur_Count", &res, &status);
  if (iRc == TRUE) {
    iRc = dbfill_count(pDBHANDLE->conn, &res, status, iCount);
    dbclosecursor(pDBHANDLE->conn, "TELNR_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("Telefonnumbers"); Grp.grpid = 0;
  Grp.grpkuerzel = mystrdup("TELNRDAT");
  _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, "TELNRDAT") == 0) {
    iRc = iRc && TABLE_File_Save(pDBHANDLE,fp,TELNR_Definition(),
          (char*)TELNR_Init((TELNR*)&Table),TELNR_Select,TELNR_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, "TELNRDAT") == 0) {
    if (dbcache_changed("TELNR")) {
      TELNR_DeleteAll(pDBHANDLE);
      iRc = iRc && TABLE_File_Load(pDBHANDLE,fp,TELNR_Definition(),
            TELNR_Init, TELNR_Load);
    } else {
      iRc = iRc && TABLE_File_Skip(fp,TELNR_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, "TELNRDAT") == 0) {
    iRc = iRc && TELNR_DeleteAll(pDBHANDLE);
    iRc = iRc && PASSWD_DeleteAll(pDBHANDLE);
  }
  return iRc;
}

