/*
 ******************************************************************************
 *  Copyright 1984-1993 United States Data Corporation. All Rights Reserved.
 ******************************************************************************
 *                                - NOTICE -
 *
 *  The information contained herein is confidential information of United
 *  States Data Corporation, a Delaware corporation, and is protected by
 *  United States copyright and trade secret law and international treaties.
 *  This information is not to be disclosed, used or copied by or transferred
 *  to any individual, corporation, company or other person without the
 *  express written permission of United States Data Corporation.
 ******************************************************************************
 *
 * Module:	HDT_DBT.H
 *
 *	Header file for database table API.
 *
 * History:
 *		bjc 04/30/93	Created. 
 */


#ifndef _DBT_INCLUDED
#define _DBT_INCLUDED

#include <hdt_base.h>		/* Base Definitions for the API */
#include <hdt_cmap.h>
#include <hdt_dbs.h>	

/*
 ************************************************************************
 *                              Constants                               *       
 ************************************************************************
 */

/*
 *  Supported table field types
 */

#define DBT_SMALLINT	1
#define DBT_FLOAT	2
#define DBT_CHAR	3
#define DBT_INT		4
#define DBT_DATE	5


/*
 *  Index flags
 */

#define DBT_NDX_UNIQUE	0x0001		/* create unique index */


/*
 ************************************************************************
 *                              Structures                              * 
 ************************************************************************
 */

/* 
 * DBTCOL - DataBase Table Field structure
 *
 *  Desribes an individual column of a table.  This includes its name,
 *  type, and size.
 */

typedef struct
{
	short 	data_type;		/* data type of field */
	short 	data_size;		/* byte width of field */
	char	*col_name;		/* name of the field */
	void	*reserved;		/* for future expansion */
} DBTCOL;

/* 
 * DBTNDX - DataBase Table Index structure
 *
 * A database table index (DBTNDX) structure describes an index file
 * for a database table.  The index expression must be a string in the
 * format of "(<field>, <field>, ...)".  This string is appended directly
 * into the "CREATE INDEX" SQL statement.  When creating the index, the
 * index suffix appended to the table name to foem the index name.
 */

typedef struct
{
	char 	*suffix;		/* index name suffix */
	short	num_col;		/* num of cols in index */
	short	*cols;			/* index columns offsets */
	u16	flags;			/* index description flags */
	void	*reserved;		/* for future expansion */
} DBTNDX;


/* 
 * DBT - DataBase Table structure
 *
 * A database table (DBT) structure describes the schema and indexes
 * for a database table.  When filling in the HS_VAR to describe the 
 * fields of the table, only the 'col_name', 'date_type', and 'data_size'
 * members of the structure are significant to the DBT API.
 */

typedef struct
{
	short	col_cnt;		/* number of fields */	
	short	ndx_cnt;		/* number of indexes */	
	DBTCOL	*columns;		/* table schema */
	DBTNDX	*indexes;		/* index definitions */
	void	*reserved;		/* for future expansion */
} DBT;


/*
 *  DBT_FDESC - DataBase Table Field Description
 *
 *  Record which describes how a given historian treats a given DBT data
 *  type (DBT_INT, DBT_CHAR, ...).  
 */

typedef struct
{
	char	*sqltype_name;	/* SQL type string */
	short	fl_type;	/* Equiv FL type */
	short	db_type;	/* Database type */
	short	fl_size;	/* Equiv FL size (for non-message types) */
} DBT_FDESC;


/*
 ************************************************************************
 *                              Macros                                  *       
 ************************************************************************
 */

#define dbt_get_col_cnt(dbt)	((dbt)->col_cnt)
#define dbt_get_ndx_cnt(dbt)	((dbt)->ndx_cnt)
#define dbt_get_columns(dbt)	((dbt)->columns)
#define dbt_get_indexes(dbt)	((dbt)->indexes)

#define dbt_get_col_n(dbt, n)	(&((dbt)->columns[n]))
#define dbt_get_ndx_n(dbt, n)	(&((dbt)->indexes[n]))

#define dbtcol_get_name(dbtcol)	((dbtcol)->col_name)
#define dbtcol_get_type(dbtcol)	((dbtcol)->data_type)
#define dbtcol_get_size(dbtcol)	((dbtcol)->data_size)

#define dbtndx_get_suffix(dbtndx)	((dbtndx)->suffix)
#define dbtndx_get_num_col(dbtndx)	((dbtndx)->num_col)
#define dbtndx_get_cols(dbtndx)		((dbtndx)->cols)


/*
 ************************************************************************
 *                         Function Prototypes                          * 
 ************************************************************************
 */


#ifdef __cplusplus
extern "C" {
#endif

extern DBT*	dbt_create(short num_col, short num_ndx);
extern void	dbt_destroy(DBT *dbt);
extern int	dbt_set_col_n(DBT* dbt, short n, char *name, 
		              short type, short size);
extern int	dbt_set_ndx_n(DBT* dbt, short n, char *suffix, 
		              short num_col, short *cols, u16 flags);

extern int	dbt_init(DBS *dbs, DBT *dbt, char *tblname);
extern int	dbt_init_table(DBS *dbs, char *tblname, 
		               int num_col, DBTCOL *cols);
extern int	dbt_init_index(DBS *dbs, char *tblname, 
		               char *ndxname, char *expr, int unique);

extern char*	dbt_init_temp(DBS *dbs, DBT *dbt, short mkindex);
extern int	dbt_pack_table(DBS *dbs, char *tblname);

extern int	dbt_drop(DBS *dbs, DBT *dbt, char *tblname);
extern int	dbt_drop_table(DBS *dbs, char *tblname);
extern int	dbt_drop_index(DBS *dbs, char *ndxname);

extern int	dbt_describe(DBS *dbs, char *tblname, DBT **dbt);
extern int	dbt_access(DBS *dbs, char *tblname);
extern int	dbt_validate(DBS *dbs, DBT *dbt, char *tblname, int exact);
extern int	dbt_alter(DBS *dbs, DBT *dbt, char *tblname);
extern int	dbt_copy(DBS *tgt_dbs, char *tgtname, DBT *tgt_dbt,
		         DBS *src_dbs, char *srcname);
extern int	dbt_docopy(DBC *tgt, DBC *src, CMAP *cmap);

extern DBTCOL*	dbt_get_coldef(DBT *dbt, char *col_name);
extern short	dbt_get_colnum(DBT *dbt, char *col_name);

extern int	dbt_delin_flds(DBT *dbt, short num_col, short *cols, 
		               char *delin, char *outstr, int maxlen);
extern int	dbt_bld_regq(DBT *dbt, char *tblname, char *query, int maxlen);
extern int 	dbt_bld_addq(DBT *dbt, char *tblname, int add_type,
		             char *query, int maxlen);

extern int	dbt_set_col2var(HS_DA *dest, short var_ndx, short hist_id,
		                DBTCOL *src, int dup_name);
extern HS_DA*	dbt_create_hsda(DBT *dbt, short hist_id, short cache_size, 
		                short num_col, short *cols);

extern DBT_FDESC* dbt_desc_ftype(short dbt_ftype, short hist_id);

#ifdef __cplusplus
}
#endif

#endif

