/*	@(#)hs_clnt.h	1.7	09/26/90	12:52:31	*/
/*
**	hs_clnt.h 
**	Client/Historian API Header File
*/

#ifndef HS_CLNTINCLUDED

#ifndef EXPORT
#ifdef VMS
#define EXPORT globaldef
#define IMPORT globalref
#else
#define EXPORT
#define IMPORT extern
#endif
#endif

#include	<hs_ipc.h>

#define MAXLOGONS 10	/* Advised max number of sessions. */

IMPORT	int	Max_HsTrans;

/* Maximum number of outstanding async trxs THE MAX IS 2000 */
#define MAXHSTRANS	((Max_HsTrans > 2000 ? 2000 : Max_HsTrans))

/*
** PFH error handler:
**	arg1 = function code as defined previously
**	arg2 = return code (Hs_error) via historian reply message.
**	arg3 = historian handle either (HS_STMTID or HS_SESSIONID) 
**		depending upon function code.
*/

typedef	int (* PFH)(short, short, void *);
#define HS_SYNC 	((PFH)0) 	/* Synchronous operation */
#define HS_ASYNC 	((PFH)1)	/* Asynchronous operation */

extern long	Hs_server_error;	/* Server dependent return code	*/
extern char	Hs_server_text[256];	/* Server dependent error text string */
extern short	Hs_error;		/* Historian return code	*/

#ifdef __cplusplus
struct _HSD_SESSIONID;
struct _HSD_STMTID;
#endif

typedef struct _HS_TRANS
{
	union {
		struct _HSD_SESSIONID *sessid;
		struct _HSD_STMTID   *stmtid;
	} hdl;
	PFH	func;
	short	fcode;
} HS_TRANS;

typedef struct _HS_LSTMTID
{
	struct _HSD_STMTID	*stmtid;
	struct _HS_LSTMTID	*next;
	short	regst_rows;
} HS_LSTMTID;

typedef struct _HSD_SESSIONID
{
	void			*hsessid;
	struct _HSD_STMTID	*list;
	char			*dbalias;
	TAG			rmailbox;
	TAG			smailbox;
	id_t			taskid;
	id_t			histid;
	short			tail;
	short			logid;
	unsigned short		rdb_flags;
	struct _HSD_SESSIONID	*next;
	struct _HS_TRANS	trxhdl[1];
} HSD_SESSIONID, *HS_SESSIONID;


typedef struct _REGDATA
{
	struct	_REGDATA *next;
	short	datasize;
	char data[1];
} REGDATA;

typedef struct _HS_SAMPLE_DATA
{
	double	current;	/* Current value */
	double	incr;		/* Increment between samples */
	short	samples;	/* Total number of samples */
	short	key;		/* Index into SVDA of key field */
	short	index;		/* Current index */
	short	spare;		/* padding... */
} HS_SAMPLE_DATA;

typedef struct _HSD_STMTID
{
	void			*hstmtid;
	HS_DA			*params;
	HS_DA			*fld_data;
	void			*userptr;
	struct _HSD_STMTID	*next;
	HS_SESSIONID		session;
	struct _REGDATA		*rgdata;
	struct _HS_SAMPLE_DATA	*smpdata;
	short			stmt_type;
	short			reg_rows;
} HSD_STMTID, *HS_STMTID;

#define HS_HISTORIAN_ID(session) (session->rdb_flags >> 12)
#define HS_SESSION_TYPE(session) (session->rdb_flags & 0x0FFF)
#define HS_USER_STMT(stmt_id) (stmt_id->userptr)

typedef int (* PFDH)(HS_STMTID, short);

/***** Function prototypes *****/

#ifdef __cplusplus 
extern "C" {
#endif

extern HS_SESSIONID hs_open(id_t taskid, TAG rmailbox, TAG smailbox,
	char *dbname, char *mode);

extern HS_DA *hs_describe_unique_index(HS_SESSIONID session,
	char *table_name);

extern HS_DA *hs_describe_table(HS_SESSIONID session, char *table_name);

extern HS_STMTID hs_prepare(HS_SESSIONID session, char *sql_statement, 
	HS_DA *params, HS_DA *fld_data);

extern int hs_describe_sql(HS_SESSIONID session, char *sql_stmt,
	HS_DA **params, HS_DA **fld_data);
                      
extern int hs_exec(HS_STMTID stmtid, PFH func);

extern int hs_exec_results(int flag, short *row, short *hserr, long *dberr);

extern int hs_fetch(HS_STMTID stmtid, int rows);

extern int hs_exec_sample(HS_STMTID stmtid, HS_SAMPLE_DATA *sd, PFH func);

extern int hs_fetch_sample(HS_STMTID stmtid, int rows);

extern int hs_close_stmt(HS_STMTID stmtid, PFH func);

extern int hs_commit(HS_SESSIONID session, PFH func);

extern int hs_rollback(HS_SESSIONID session, PFH func);

extern int hs_close(HS_SESSIONID session);

extern int hs_maxrec(HS_SESSIONID session, char *table, u32 maxrec);

extern int hs_schema_create(HS_SESSIONID session, char *schema, char *table);

extern unsigned long hs_next_serialid(HS_SESSIONID session, char *ser_table,
	char *key_idname);
	
extern unsigned long hs_read_serialid(HS_SESSIONID session, char *ser_table,
	char *key_idname);
	
extern int hs_setup_serialid(HS_SESSIONID session, char *ser_table,
	char *key_idname,  unsigned long start_id, short incr);

extern int hs_register_count(HS_STMTID stmtid);

extern int hs_register_filter(HS_STMTID stmtid, HS_DA *filter);

extern int hs_create_hsdata(HS_DA *hsda, short nrows);

extern HS_DA *hs_create_hsda(int varcnt);

extern void hs_free_hsdata(HS_DA *hsda);

extern void hs_free_hsda(HS_DA *hsda);

extern char *hs_geterror(int ecode);

extern PFH hs_install_error_handler(PFH func);

extern PFDH hs_install_data_handler(PFDH func);

extern void hs_set_shutdown(int shutval);

extern int hs_get_shutdown(void);

extern void hs_set_syncshut(int syncval);

extern int hs_get_syncshut(void);

extern void hs_flush_mailbox(id_t taskid, TAG mbox);

extern void hs_process_message(id_t taskid, TAG mbox);

#ifdef __cplusplus
}
#endif

#define HS_CLNTINCLUDED
#endif
