/* ------------------------------------------------------- */
/* Header-file SPEEDY 2.0.0, (C) Wabnig Harald, 15.12.2002 */
/* ------------------------------------------------------- */

#ifndef SPEEDY_INCLUDE
#define SPEEDY_INCLUDE

#include <stdio.h>

#include "spdycomm.h"
#include "speedydb.h"

#ifndef TRUE
#define TRUE  1
#define FALSE 0
#endif

#ifdef SPEEDY1

/* Initialisation of the database */
extern int Speedy1_Init(char * storage, char * dbname);

/* Internal result-string from database server */
extern char * squery_data;
extern int squery_size;

#endif

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

typedef struct {
  short sYear;
  unsigned char sMonth, sDay;
} DBDATE;
#define SIZE_DBDATE     10

typedef struct {
  unsigned char sHour, sMin, sSec;
} DBTIME;
#define SIZE_DBTIME     8

typedef struct DBDATI {
  short sYear;
  unsigned char sMonth, sDay, sHour, sMin, sSec;
  long lMicro;
} DBDATI;
#define SIZE_DBDATI     26

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

typedef enum {
  DB_CONN_OK,
  DB_CONN_BAD
} DB_ConnStatus;

typedef enum {
  DB_EMPTY_QUERY = 0,
  DB_COMMAND_OK,
  DB_TUPLES_OK,
  DB_NOTALL_TUPLES_OK,
  DB_BAD_RESPONSE,
  DB_NONFATAL_ERROR,
  DB_FATAL_ERROR
} DB_ExecStatus;

typedef enum {
  DB_CHARACTER,
  DB_BINARY,
  DB_SHORT,
  DB_INT,
  DB_DOUBLE,
  DB_TIME,
  DB_DATE,
  DB_TIMESTAMP
} DB_FieldType;

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

/* Memory allocation strategy for the DB-query result storage */
#define DB_QUERY_MIN         100000
#define DB_QUERY_MORE        1000000

extern void DBquery_init(void);

extern void DBquery_add(char * data, int size);
extern void DBquery_addcmd(char cmd);
extern void DBquery_addtoken(char token);
extern void DBquery_addint(int ival);
extern void DBquery_addstring(char * strg);
extern void DBquery_addbinary(char * strg);
extern void DBquery_addldata(char * data, int size);
extern void DBquery_addtokname(char token, char * data);
extern void DBquery_addtokint(char token, int ival);
extern void DBquery_addtokdouble(char token, double dval);
extern void DBquery_addtokstring(char token, char * strg);
extern void DBquery_addtokdata(char token, void * data, short size);
extern void DBquery_addtokldata(char token, char * data, int size);
extern void DBquery_addok(void);
extern void DBquery_addokdata(void);
extern void DBquery_addoktuples(int icnt);
extern void DBquery_addnotalldata(void);
extern void DBquery_adderrmsg(char * estrg);

extern int DBquery_addwhere(char * wstrg, char * errmsg);

extern int DBget_token(int ptr, char * sql, int size, char * token, char * estrg);
extern int DBget_short(int ptr, char * sql, int size, short * sval, char * estrg);
extern int DBget_int(int ptr, char * sql, int size, int * ival, char * estrg);
extern int DBget_double(int ptr, char * sql, int size, double * dval, char * estrg);
extern int DBget_name(int ptr, char * sql, int size, char ** name, char * estrg);
extern int DBget_string(int ptr, char * sql, int size, char ** strg, char * estrg);
extern int DBget_data(int ptr, char * sql, int size, char ** data, 
                                                     short * len, char * estrg);
extern int DBget_ldata(int ptr, char * sql, int size, char ** data, 
                                                      int * len, char * estrg);

extern int DBmatch_token(int ptr, char * sql, int size, char token, 
                         char * estrg);
extern int DBmatch_tokshort(int ptr, char * sql, int size, 
                            short * sval, char * estrg);
extern int DBmatch_tokint(int ptr, char * sql, int size, 
                          int * ival, char * estrg);
extern int DBmatch_tokdouble(int ptr, char * sql, int size, 
                             double * dval, char * estrg);
extern int DBmatch_tokname(int ptr, char * sql, int size, 
                           char ** name, char * estrg);
extern int DBmatch_tokstrg(int ptr, char * sql, int size, 
                           char ** strg, char * estrg);
extern int DBmatch_errmsg(int ptr, char * sql, int size, char ** strg);

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

typedef struct db_conn DBconn;

extern int DBsend_data(DBconn * conn, char * strg, int size);
extern int DBrecv_data(DBconn * conn, char * data, int max, 
                       int * size, char ** pdata, int * psize);

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

extern DBconn * DBcreatedb(const char * dbhost, const char * dbport,
                           const char * dboptions, char * dbname);
extern DBconn * DBsetdb(const char * dbhost, const char * dbport,
                        const char * dboptions, char * dbname);

extern int DBstopdb(const char * dbhost, const char * dbport, const char * dboptions);

extern int DBdropdb(DBconn * conn1);

extern DB_ConnStatus DBstatus(DBconn * conn);

/* disconnect actual connection to database */
extern void DBdisconnect(DBconn * conn1);
/* disconnect and immediately reconnect */
extern int DBreset(DBconn * conn1);
/* free all communication resources (afterwards no connect to a database is possible) */
extern void DBfinish(DBconn * conn);

typedef struct db_result DBresult;

extern const char * DBerrorMessage(DBconn * conn1);

/* ----- */

extern DBresult * DBexec(DBconn * conn1, const char * query);
extern DBresult * DBbinexec(DBconn * conn1);

extern DB_ExecStatus DBresultStatus(DBresult * res1);
extern const char * DBresultErrorMessage(DBresult * res1);

extern int DBntuples(DBresult * res1);
extern int DBnfields(DBresult * res1);

extern const char * DBfname(DBresult * res1, int field_num);
extern int DBfsize(DBresult * res1, int field_num);
extern DB_FieldType DBftype(DBresult * res1, int field_num);

extern char * DBgetvalue(DBresult * res1, int tup_num, int field_num);
extern char * DBgetbinary(DBresult * res1, int tup_num, int field_num);

extern void DBfillbinary(DBresult * res1, void * data, int tup_num, int field_num);

extern void DBclear(DBresult * res);

#endif
