#ifndef HS_IPCINCLUDED

/* @(#)hs_ipc.h 1.6 10/06/90 13:22:15 */
/*
** Function Codes
** Passed to historian and error handler routines.
** Passed to historian via mm_user1 (u16) in mailbox message.
** Passed to error handler via first argument (short) in PFH definition.
*/

/* Header file needed for prototype get_log_file */
#include <stdio.h>
/* Header file needed for size_t definition */
#include <string.h>

#define FNC_HS_OPEN     1 /* hs_open */
#define FNC_HS_CLOS     2 /* hs_close */
#define FNC_HS_COMM     3 /* hs_commit */
#define FNC_HS_ROLL     4 /* hs_rollback */
#define FNC_HS_DESC     5 /* hs_describe_table */
#define FNC_HS_DSQL     6 /* hs_describe_sql */
#define FNC_HS_UNIQ     7 /* hs_describe_unique_index */
#define FNC_HS_IMMD     8 /* hs_exec_immediate */
#define FNC_HS_CLST     9 /* hs_close_stmt */
#define FNC_HS_PREP    10 /* hs_prepare */
#define FNC_HS_EXEC    11 /* hs_exec */
#define FNC_HS_FTCH    12 /* hs_fetch */
#define FNC_HS_NXID    13 /* hs_next_serialid */
#define FNC_HS_RDID    14 /* hs_read_serialid */
#define FNC_HS_SUID    15 /* hs_setup_seialid */
#define FNC_HS_SHUT    16 /* Historian is shutting down */
#define FNC_HS_ESMP    17 /* hs_exec_sample */
#define FNC_HS_FSMP    18 /* hs_fetch_sample */
#define FNC_HS_MAXR    19 /* hs_maxrec */
#define FNC_HS_SCHM    20 /* hs_create_table */
#define FNC_OPER_EVENT 21 /* Graphic Object Security Event */
#define FNC_HS_FLTR    22 /* hs_register_filter */

/*
** Statement types returned by hs_prepare from historian in the
** message data.
*/
#define HS_DELTYPE 8 /* This statement is a DELETE statement */
#define HS_UPDTYPE 7 /* This statement is an UPDATE statement */
#define HS_INSTYPE 6 /* This statement is an INSERT statement */
#define HS_RPCTYPE 4 /* This is Stored Procedure "call" statement */
#define HS_INVTYPE 3 /* This statement is unsupported by historian */
#define HS_REGTYPE 2 /* This statement is a REGISTER statement */
#define HS_LOGTYPE 1 /* This statement is a LOG statement */
#define HS_NRMTYPE 0 /* This statement is none of the above */

/*
** Database Field type returned from an hs_describe_table and passed back 
** to historian in an hs_prepare statement
*/
#define HS_DBCHAR   50
#define HS_DBNUMBER 51
#define HS_DBDATE   52

/* Variable Types for SQL/RPC processing */
#define VAR_IGNORE 0x0000
#define VAR_OUTPUT 0x0001
#define VAR_INPUT  0x0002
#define VAR_INOUT  (VAR_OUTPUT + VAR_INPUT)
#define VAR_WHERE  0x0004

/*
** Historian Ids.  These macros identify a Historian for any
** session that has been opened.
*/

#define UNK_HISTORIAN (-1) /* Unknown Historian */
#define RDB_HISTORIAN  0   /* VAX RDB Historian */
#define ORA_HISTORIAN  1   /* Oracle Historian */
#define DBM_HISTORIAN  2   /* OS2 Database Manager Historian */
#define BIN_HISTORIAN  3   /* Binary (Dbase) Historian */
#define SYB_HISTORIAN  4   /* Sybase Historian */
#define ING_HISTORIAN  5   /* Ingres Historian */
#define ODBC_HISTORIAN 6   /* ODBC Historian ala ODBC SDK */
#define QE_HISTORIAN   7   /* Q+E Historian ala INTERSOLV Q+E LIB */
#define INF_HISTORIAN  8   /* Informix Historian */

/*
** Session Types.  These macros define SQL Relational Database
** characteristics for any session that has been opened.
*/

#define FULL_SUPPORT  0x0000
/* No special processing needed.  It supports cursor stability and */
/* non-destructive commits.   */

#define READ_LOCKS     0x0001  
/* This characteristic of an SQL RDB defines that select statements by */
/* default are for update.  Therefore, the data will be locked when a select */
/* is executed.  To disable this every hs_exec() of a select statement must */
/* be preceded by an hs_exec() of a "SET TRANSACTION READ ONLY" statement. */
/* The statement "SET TRANSACTION READ ONLY" must be preceded with a call to */
/* hs_commit() since "SET TRANSACTION READ ONLY" must be the first executable */
/* statement within a transaction. */ 

#define DEST_COMMITS   0x0002
/* This characteristic of an SQL RDB defines a "COMMIT" as destructive. */
/* Therefore, once an hs_commit() is executed the cursor created within  */
/* hs_fetch() is closed.  For hs_fetch() to work again hs_exec() must be */
/* called again. */

#define AUTO_COMMIT    0x0004
/* This characteristic of an SQL RDB defines that all database modifications */
/* are not buffered.  All "UPDATE"s and "DELETE"s are immediately written to */
/* disk.  "COMMIT"s are a no-op and "ROLLBACK"s do not work.  The AUTO_COMMIT */
/* is true for the Binary and Sybase Historians. */

#define SINGLE_SELECT  0x0008
/* This characteristic of an SQL RDB defines that select statements are */
/* single threaded buffered operations and only one sql statement can be */
/* performed.  Therefore, the FactoryLink Task cannot perform an UPDATE */
/* and then continue fetching (hs_fetch) from a previously prepared */
/* (hs_prepare) and executed (hs_exec) statement.  The FactoryLink must */
/* rexecute (hs_exec) the select statement and fetch (hs_fetch) to retrieve */
/* any rows.  */

#define RPCEXECRESULTS 0x0010
/* The characteristic of this SQL Data Source is that when a stored procedure */
/* is executed the result set of stored procedure is returned at EXECUTE time */
/* not at FETCH time.  So an sv_fetch of this statement fails. */
/* Oracle7 RDBMS acts this way */

/*
** Historian Function Return Codes (hserror)
** Code returned by the historian task via mm_user1 
** in mailbox message.
** If hserror == ERROR then a factory link kernel error occurred.
** If hserror == GOOD then a function was successful.
** (HS_SUCCN, HS_SUCCY, HS_REGST) are mapped to GOOD.
** Return Codes (HSMAXOPENS - HSSTMTID) are generated locally in
**  clients process.
** Return Codes HSDBERROR and greater are return codes generated by
**  the historian and passed via mailbox as mm_user1.
*/
#define HS_SUCCN        0 /* Success with no data */
#define HS_SUCCY        1 /* Success with data	*/
#define HS_REGST        2 /* Registered data; mm_user2 is null 	*/
                          /* data is local stmt handle + row count + data */
#define HSMAXOPENS      3 /* Too many open sessions can have up to 10 */
#define HSMEMORY        4 /* Memory error malloc failed		*/
#define HSCONNECT       5 /* Failed to connect to historian	*/
#define HSPREPARE       6 /* Failed to prepare stmtid		*/
#define HSSTMTID        7 /* Invalid stmtid returned from historian */
#define HSENDOFETCH     8 /* Last row fetched, no more rows to fetch */
#define HSNOMATCH       9 /* Database alias has no matching entry in */
                          /* Historian information table */
#define HSTIMEDOUT     10 /* Maximum timeout value exceeded waiting */
                          /* for a historian task to reply to client */
#define HSINVOPEN      11 /* Historian client passed an Invalid Connect */
                          /* Handle to a historian task */
#define HSINVSTMT      12 /* Historian client passed an Invalid Statement */
                          /* Handle to a historian task */
#define HSDISABLED     14 /* Historian Connection has been disabled */

#define HSNOTSUPPORTED 16 /* AN SQL STATEMENT THAT IS NOT SUPPORTED */
                          /* BY THE dBASE IV HISTORIAN */
#define HSDBERROR      17 /* Historian f() returned database error */
                          /* A general error code for historian	*/
                          /* Error string may contain more info	*/
#define HSUNKNOWN      18 /* Unknown function request sent to historian*/
#define HSDUPLICATE    19 /* Tried to insert a duplicate row	*/
#define HSTBLEXISTS    20 /* Table exists, tried to create a table */
                          /* that already exists			*/
#define HSFLDEXISTS    21 /* Field exists, tried to add a field */
                          /* that already exists in table		*/
#define HSNOTABLE      22 /* Table does not exist.  Tried to access */
                          /* a table that does not exist in database */
#define HSNOFIELD      23 /* Field does not exist.  Tried to access */
                          /* a field that does not exist in table */
#define HSNDXEXISTS    24 /* Index exists, tried to create an index */
                          /* that already exists */
#define HSSCHNOMATCH   25 /* Schema name not found */
#define HSUNSUPPTYPE   26 /* An unsupported data type is being used */
#define HSMAXCODE      HSUNSUPPTYPE+1 /* If more added make HSMAXCODE be */
                                      /* defined to greatest return code */
typedef struct _HS_VAR
{
	short data_type;
	short data_size;
	short db_data_type;
	short db_data_size;
	void  *data_location;
	short *data_null;
	char  *col_name;
} HS_VAR;

typedef struct _HS_DA
{
	short  max_rows;
	short  data_rows;
	short  var_count;
	HS_VAR var_array[1];
} HS_DA;

typedef struct _EXECSTATUS
{
	short nrow;
	short hserr;
	long  dberr;
} EXECSTATUS;

#define FIRSTERROR  0
#define NEXTERROR   1
#define FOUNDSTATS  2
#define NOMORESTATS 3
#define NOSTATS     4

typedef enum {WORKBUF0, WORKBUF1, WORKBUF2, WORKBUF3, WORKBUF4,
 WORKBUF5, WORKBUF6, WORKBUF7, WORKBUF8, WORKBUF9} WORKBUFIDS;

#ifdef __cplusplus
extern "C" {
#endif

/* Prototypes of functions in hsipc.c */

extern int hs_ipcdebug(int state);
extern int hs_set_timeout(int seconds);

/* Prototype of functions in hslog.c */

extern int open_log_file(char *prefix, char *app_dir);
extern int dberror(char *errstr);
extern void close_log_file(void);
extern void purge_log_files(char *prefix);
extern FILE *get_log_file(void);
extern char *get_timeofday(void);
extern int get_stmt_type(char *sqlstmt);

/* Prototypes of functions in parsecmd.c */

extern void hs_set_cmdargs(int argc, char **argv);
extern void hs_parse_cmdline(char *cmdline);
extern char *hs_getsqlop(int ecode);
extern int hs_get_args(char **parameter);
extern char *hs_get_argv(char value);

/* Prototypes of functions in strbuff.c */

extern void *Add2Buffer(WORKBUFIDS bufid, void *data, size_t dsize);
extern char *Append2Buffer(WORKBUFIDS bufid, char *string);
extern void DestroyWorkBuffers( void );
extern char *GetWorkBuffer( WORKBUFIDS bufid );
extern size_t GetWorkBufferBytes(WORKBUFIDS bufid);
extern size_t GetWorkBufferSize(WORKBUFIDS bufid);
extern void *InitWorkBuffer(WORKBUFIDS bufid, size_t bufsize);
extern void SetWorkBufferSize(WORKBUFIDS bufid);
extern char *strip_trblanks(char *paddedstring);
extern char *strip_bgblanks(char *paddedstring);

#ifdef __cplusplus
}
#endif

#define HS_IPCINCLUDED
#endif
