/* ---------------------------------------------------- */
/* Header-file DBSYS 1.0, (C) Wabnig Harald, 15.12.2002 */
/* ---------------------------------------------------- */

#ifndef DBAPI_INCLUDE
#define DBAPI_INCLUDE

#include <speedy.h>
#include <speedydb.h>

#ifdef WIN32
#include <windows.h>
#endif

#define DBERROR_MAXSTRG   1000
extern char DB_Error_String[DBERROR_MAXSTRG];

/* -------------------------------------------------------------------------- */
/* Database-Access-Definitions                                                */
/* -------------------------------------------------------------------------- */

/* available database-engines */
typedef enum {
  MYDB_DBSQL
} MYDB_System;

/* possible resultcodes from database-commands */
typedef enum {
  MYDB_EMPTY_QUERY = 0,
  MYDB_COMMAND_OK,
  MYDB_TUPLES_OK,
  MYDB_BAD_RESPONSE,
  MYDB_NONFATAL_ERROR,
  MYDB_FATAL_ERROR,
  MYDB_NOTALL_TUPLES_OK
} MYDB_ExecStatus;

/* possible types of database-resultfields */
typedef enum {
  MYDB_UNKNOWN = 0,
  MYDB_CHARACTER,
  MYDB_SHORT,
  MYDB_INT,
  MYDB_DOUBLE,
  MYDB_TIME,
  MYDB_DATE,
  MYDB_TIMESTAMP
} MYDB_FieldType;

/* database-connection */
typedef struct {
  MYDB_System dbsystem;
  void * conn; /* DBconn */
} MYDB_Conn;

/* database-result */
typedef struct {
  MYDB_System dbsystem;
  void * res; /* DBresult */
} MYDB_Result;


/* -------------------------------------------------------------------------- */
/* Database-Load Acceleration Functions                                       */
/*                                                                            */
/* These functions are used to track which database-tables have been changed. */
/* If the same baseline shall be loaded again only the tables that have been  */
/* changed have to be loaded. This accelerates the speed of initializing the  */
/* database.
/* -------------------------------------------------------------------------- */

/* the maximum number of tables in the cache of changed tables */
#define MAX_DBCACHE    10000

/* the name of the file which handles the list of changed tables */
#define DBCACHE_FILENAME    ".dbcache"

/* this function reads the list of changed tables from the file */
extern int dbcache_read();

/* this function writes the list of changed tables to the file */
extern int dbcache_write();

/* this function defines the active baseline, if the baseline is changed,
   the changed tables cache is initialized */
extern int dbcache_set_baseline(char * baseline);

/* this function defines that the initialization of the baseline is started */
extern void dbcache_begin_init();

/* this function defines that the initialization of the baseline is finished */
extern void dbcache_end_init();

/* this function defines that a given table has changed */
extern int dbcache_set_changed(char * table);

/* this function checks whether a given table has been changed */
extern int dbcache_changed(char * table);


/* -------------------------------------------------------------------------- */
/* Database-Access Functions                                                  */
/* -------------------------------------------------------------------------- */

/* functions to connect to the database */

extern int dbopen(MYDB_Conn ** dbconn, char * dbname);
extern void dbclose (MYDB_Conn * dbconn);

/* functions for transactionslogic */

extern int dbbegin(MYDB_Conn * dbconn);
extern int dbend(MYDB_Conn * dbconn);
extern int dbcommit(MYDB_Conn * dbconn);
extern int dbrollback(MYDB_Conn * dbconn);

/* functions to execute cursor-selects */

extern int dbopencursor(MYDB_Conn * dbconn, char * cursor, char * command);
extern int dbclosecursor(MYDB_Conn * dbconn, char * cursor);

/* functions to execute given sql-commands */

extern int dbinsert(MYDB_Conn * dbconn, char * table, char * command,
                        int * status);
extern int dbupdate(MYDB_Conn * dbconn, char * table, char * command,
                        int * status);
extern int dbdelete(MYDB_Conn * dbconn, char * table, char * command,
                        int * status);
extern int dbdeleteall(MYDB_Conn * dbconn, char * table, int * status);

/* functions to execute general sql-commands */

extern int dbexec(MYDB_Conn * dbconn, char * cmd, char * parameters,
                  MYDB_Result * dbres, int * status);
extern int dbexec1(MYDB_Conn * conn, char * cmd, MYDB_Result * dbres,
                   int * status);
extern int dbbinexec1(MYDB_Conn * dbconn, MYDB_Result * dbres,
                      int * status);
/* only if print is TRUE messages are generated */
extern int dbbinexec2(MYDB_Conn * dbconn, MYDB_Result * dbres,
                      int * status, int print);

/* function can be called with dbres = Null; then results are printed; 
   cnt gives the number of affected rows */
extern int dbexec_sql(MYDB_Conn * dbconn, char * sql, MYDB_Result * dbres, int * cnt);

/* functions to execute special sql-select-commands */

extern int dbfill_res_count(MYDB_Conn * dbconn, MYDB_Result * dbres, 
                            int status, int * iCount);

extern int dbfill_count(MYDB_Conn * dbconn, MYDB_Result * dbres,
                            int status, int * iCount);
extern int dbfill_string(MYDB_Conn * dbconn, MYDB_Result * dbres,
                             int status, char * strg);

/* functions to access database-results */

extern MYDB_ExecStatus dbresultstatus(MYDB_Result * res);
extern void dbresfree (MYDB_Result * dbres);

extern char * dbfname(MYDB_Result * dbres, int ifield);
extern MYDB_FieldType dbftype(MYDB_Result * dbres, int ifield);
extern int dbgetlength(MYDB_Result * dbres, int ituple, int ifield);

extern int dbnfields(MYDB_Result * dbres);
extern int dbntuples(MYDB_Result * dbres);

extern char * dbgetvalue(MYDB_Result * dbres, int ituple, int ifield);
extern char * dbgetbinary(MYDB_Result * dbres, int ituple, int ifield);

#endif

