Files

393 lines
8.6 KiB
C
Raw Permalink Normal View History

2026-01-23 17:03:45 +08:00
/* Sccsid: @(#)print.c 9.1.1.18 12/13/95 10:56:16 */
/* generate flat files for data load */
#include <stdio.h>
#ifndef VMS
#include <sys/types.h>
#endif
#if defined(SUN)
#include <unistd.h>
#endif
#include <math.h>
#include "dss.h"
#include "dsstypes.h"
/*
* Function Prototypes
*/
FILE *print_prep PROTO((int table, int update));
int pr_drange PROTO((int tbl, long min, long cnt, long num));
/*
* Extensions to dbgen for generation of skewed data.
* Surajit Chaudhuri, Vivek Narasayya.
* (Jan '99)
*/
extern long NumLineitemsGenerated;
FILE *
print_prep(int table, int update)
{
char upath[128];
FILE *res;
if (updates)
{
if (update > 0) /* updates */
{
sprintf(upath, "%s%c%s.u%d",
env_config(PATH_TAG, PATH_DFLT),
PATH_SEP, tdefs[table].name, update);
}
else /* deletes */
{
sprintf(upath, "%s%cdelete.%d",
env_config(PATH_TAG, PATH_DFLT), PATH_SEP, -update);
}
return(fopen(upath, "w"));
}
res = tbl_open(table, "w");
OPEN_CHECK(res, tdefs[table].name);
return(res);
}
int
pr_cust(customer_t *c, int mode)
{
static FILE *fp = NULL;
static int last_mode = 0;
if (fp == NULL)
fp = print_prep(CUST, 0);
PR_STRT(fp);
PR_INT(fp, c->custkey);
PR_VSTR(fp, c->name, C_NAME_LEN);
PR_VSTR(fp, c->address,
(columnar)?(long)(ceil(C_ADDR_LEN * V_STR_HGH)):c->alen);
PR_INT(fp, c->nation_code);
PR_STR(fp, c->phone, PHONE_LEN);
PR_MONEY(fp, c->acctbal);
PR_STR(fp, c->mktsegment, C_MSEG_LEN);
PR_VSTR(fp, c->comment,
(columnar)?(long)(ceil(C_CMNT_LEN * V_STR_HGH)):c->clen);
PR_END(fp);
return(0);
}
/*
* print the numbered order
*/
int
pr_order(order_t *o, int mode)
{
static FILE *fp_o = NULL;
static int last_mode = 0;
if (fp_o == NULL || mode != last_mode)
{
if (fp_o)
fclose(fp_o);
fp_o = print_prep(ORDER, mode);
last_mode = mode;
}
PR_STRT(fp_o);
PR_HUGE(fp_o, o->okey);
PR_INT(fp_o, o->custkey);
PR_CHR(fp_o, o->orderstatus);
PR_MONEY(fp_o, o->totalprice);
PR_STR(fp_o, o->odate, DATE_LEN);
PR_STR(fp_o, o->opriority, O_OPRIO_LEN);
PR_STR(fp_o, o->clerk, O_CLRK_LEN);
PR_INT(fp_o, o->spriority);
PR_VSTR(fp_o, o->comment,
(columnar)?(long)(ceil(O_CMNT_LEN * V_STR_HGH)):o->clen);
PR_END(fp_o);
return(0);
}
/*
* print an order's lineitems
*/
int
pr_line(order_t *o, int mode)
{
static FILE *fp_l = NULL;
static int last_mode = 0;
long i;
if(NumLineitemsGenerated >= L_LINE_SIZE)
return (0);
if (fp_l == NULL || mode != last_mode)
{
if (fp_l)
fclose(fp_l);
fp_l = print_prep(LINE, mode);
last_mode = mode;
}
for (i = 0; i < o->lines; i++)
{
PR_STRT(fp_l);
PR_HUGE(fp_l, o->l[i].okey);
PR_INT(fp_l, o->l[i].partkey);
PR_INT(fp_l, o->l[i].suppkey);
PR_INT(fp_l, o->l[i].lcnt);
PR_INT(fp_l, o->l[i].quantity);
PR_MONEY(fp_l, o->l[i].eprice);
PR_MONEY(fp_l, o->l[i].discount);
PR_MONEY(fp_l, o->l[i].tax);
PR_CHR(fp_l, o->l[i].rflag[0]);
PR_CHR(fp_l, o->l[i].lstatus[0]);
PR_STR(fp_l, o->l[i].sdate, DATE_LEN);
PR_STR(fp_l, o->l[i].cdate, DATE_LEN);
PR_STR(fp_l, o->l[i].rdate, DATE_LEN);
PR_STR(fp_l, o->l[i].shipinstruct, L_INST_LEN);
PR_STR(fp_l, o->l[i].shipmode, L_SMODE_LEN);
PR_VSTR(fp_l, o->l[i].comment,
(columnar)?(long)(ceil(L_CMNT_LEN * V_STR_HGH)):o->l[i].clen);
PR_END(fp_l);
}
return(0);
}
/*
* print the numbered order *and* its associated lineitems
*/
int
pr_order_line(order_t *o, int mode)
{
tdefs[ORDER].name = tdefs[ORDER_LINE].name;
pr_order(o, mode);
if(NumLineitemsGenerated < L_LINE_SIZE)
pr_line(o, mode);
return(0);
}
/*
* print the given part
*/
int
pr_part(part_t *part, int mode)
{
static FILE *p_fp = NULL;
if (p_fp == NULL)
p_fp = print_prep(PART, 0);
PR_STRT(p_fp);
PR_INT(p_fp, part->partkey);
PR_VSTR(p_fp, part->name,
(columnar)?(long)P_NAME_LEN:part->nlen);
PR_STR(p_fp, part->mfgr, P_MFG_LEN);
PR_STR(p_fp, part->brand, P_BRND_LEN);
PR_VSTR(p_fp, part->type,
(columnar)?(long)P_TYPE_LEN:part->tlen);
PR_INT(p_fp, part->size);
PR_STR(p_fp, part->container, P_CNTR_LEN);
PR_MONEY(p_fp, part->retailprice);
PR_VSTR(p_fp, part->comment,
(columnar)?(long)(ceil(P_CMNT_LEN * V_STR_HGH)):part->clen);
PR_END(p_fp);
return(0);
}
/*
* print the given part's suppliers
*/
int
pr_psupp(part_t *part, int mode)
{
static FILE *ps_fp = NULL;
long i;
if (ps_fp == NULL)
ps_fp = print_prep(PSUPP, mode);
for (i = 0; i < SUPP_PER_PART; i++)
{
PR_STRT(ps_fp);
PR_INT(ps_fp, part->s[i].partkey);
PR_INT(ps_fp, part->s[i].suppkey);
PR_INT(ps_fp, part->s[i].qty);
PR_MONEY(ps_fp, part->s[i].scost);
PR_VSTR(ps_fp, part->s[i].comment,
(columnar)?(long)(ceil(PS_CMNT_LEN * V_STR_HGH)):part->s[i].clen);
PR_END(ps_fp);
}
return(0);
}
/*
* print the given part *and* its suppliers
*/
int
pr_part_psupp(part_t *part, int mode)
{
tdefs[PART].name = tdefs[PART_PSUPP].name;
pr_part(part, mode);
pr_psupp(part, mode);
return(0);
}
int
pr_supp(supplier_t *supp, int mode)
{
static FILE *fp = NULL;
if (fp == NULL)
fp = print_prep(SUPP, mode);
PR_STRT(fp);
PR_INT(fp, supp->suppkey);
PR_STR(fp, supp->name, S_NAME_LEN);
PR_VSTR(fp, supp->address,
(columnar)?(long)(ceil(S_ADDR_LEN * V_STR_HGH)):supp->alen);
PR_INT(fp, supp->nation_code);
PR_STR(fp, supp->phone, PHONE_LEN);
PR_MONEY(fp, supp->acctbal);
PR_VSTR(fp, supp->comment,
(columnar)?(long)(ceil(S_CMNT_LEN * V_STR_HGH)):supp->clen);
PR_END(fp);
return(0);
}
int
pr_time(dss_time_t *t, int mode)
{
static FILE *fp = NULL;
if (fp == NULL)
fp = print_prep(TIME, mode);
PR_STRT(fp);
PR_INT(fp, t->timekey);
PR_STR(fp, t->alpha, T_ALPHA_LEN);
PR_INT(fp, t->year);
PR_INT(fp, t->month);
PR_INT(fp, t->week);
PR_INT(fp, t->day);
PR_END(fp);
return(0);
}
int
pr_nation(code_t *c, int mode)
{
static FILE *fp = NULL;
if (fp == NULL)
fp = print_prep(NATION, mode);
PR_STRT(fp);
PR_INT(fp, c->code);
PR_STR(fp, c->text, NATION_LEN);
PR_INT(fp, c->join);
PR_VSTR(fp, c->comment,
(columnar)?(long)(ceil(N_CMNT_LEN * V_STR_HGH)):c->clen);
PR_END(fp);
return(0);
}
int
pr_region(code_t *c, int mode)
{
static FILE *fp = NULL;
if (fp == NULL)
fp = print_prep(REGION, mode);
PR_STRT(fp);
PR_INT(fp, c->code);
PR_STR(fp, c->text, REGION_LEN);
PR_VSTR(fp, c->comment,
(columnar)?(long)(ceil(R_CMNT_LEN * V_STR_HGH)):c->clen);
PR_END(fp);
return(0);
}
/*
* NOTE: this routine does NOT use the BCD2_* routines. As a result,
* it WILL fail if the keys being deleted exceed 32 bits. Since this
* would require ~660 update iterations, this seems an acceptable
* oversight
*/
int
pr_drange(int tbl, long min, long cnt, long num)
{
static int last_num = 0;
static FILE *dfp = NULL;
int child = -1;
long start, last, new;
if (last_num != num)
{
if (dfp)
fclose(dfp);
dfp = print_prep(tbl, -num);
if (dfp == NULL)
return(-1);
last_num = num;
}
start = MK_SPARSE(min, num/ (10000 / refresh));
last = start - 1;
for (child=min; cnt > 0; child++, cnt--)
{
new = MK_SPARSE(child, num/ (10000 / refresh));
if (gen_rng == 1 && new - last == 1)
{
last = new;
continue;
}
if (gen_sql)
{
fprintf(dfp,
"delete from %s where %s between %ld and %ld;\n",
tdefs[ORDER].name, "o_orderkey", start, last);
fprintf(dfp,
"delete from %s where %s between %ld and %ld;\n",
tdefs[LINE].name, "l_orderkey", start, last);
fprintf(dfp, "commit work;\n");
}
else
if (gen_rng)
{
PR_STRT(dfp);
PR_INT(dfp, start);
PR_INT(dfp, last);
PR_END(dfp);
}
else
{
PR_STRT(dfp);
PR_KEY(dfp, new);
PR_END(dfp);
}
start = new;
last = new;
}
if (gen_rng)
{
PR_STRT(dfp);
PR_INT(dfp, start);
PR_INT(dfp, last);
PR_END(dfp);
}
return(0);
}