1: # include   <ingres.h>
   2: # include   <tree.h>
   3: # include   <symbol.h>
   4: # include   <sccs.h>
   5: 
   6: SCCSID(@(#)tempvar.c	8.1	12/31/84)
   7: 
   8: 
   9: /*
  10: ** TEMPVAR
  11: **
  12: **	This file contains routines associated with redefining
  13: **	attribute numbers. This is needed when one variable sub queries
  14: **	or reduction change the positions of attributes in a relation.
  15: **	This file includes:
  16: **
  17: **	Tempvar -- Change the attribute numbers to new ones.
  18: **
  19: **	Origvar -- Restore attribute numbers back to their previous values.
  20: **
  21: **	Ckvar   -- Return the currently active VAR node
  22: **
  23: **	Trace Flags:
  24: **		51
  25: */
  26: /*
  27: ** Tempvar -- Replace a VAR attribute number with its new number.
  28: **
  29: **	Tempvar is given a list of subqueries which will potentially
  30: **	alter the attribute numbers of VARs they reference. An attno
  31: **	is changed by making the current VAR point to a new VAR node
  32: **	which has the updated attno.
  33: **
  34: **	The new attno is determined from the target list of the subquery
  35: **	for that VAR. The RESDOM number is the new attno and the VAR it
  36: **	points to is the old attno. For example:
  37: **		RESDOM/2 -> right = VAR 1/3
  38: **	The right subtree of result domain 2 is domain 3 of variable 1.
  39: **	Thus domain 3 should be renumbered to be domain 2.
  40: */
  41: 
  42: tempvar(node, sqlist, buf)
  43: register QTREE  *node;
  44: QTREE       *sqlist[];
  45: char        *buf;
  46: {
  47:     register QTREE  *v, *sq;
  48:     QTREE       *ckvar();
  49:     extern char *need(), *rangename();
  50: 
  51: 
  52:     if (node == NULL)
  53:         return;
  54: 
  55:     if (node->sym.type == VAR )
  56:     {
  57:         node = ckvar(node);
  58:         if (sq = sqlist[node->sym.value.sym_var.varno])
  59:         {
  60:             /* This var has a subquery on it */
  61: 
  62:             /* allocate a new VAR node */
  63:             if (buf)
  64:             {
  65:                 node->sym.value.sym_var.valptr = (ANYTYPE *) need(buf, QT_HDR_SIZ + sizeof(struct varnode));
  66:                 v = (QTREE *) node->sym.value.sym_var.valptr;
  67:                 bmove(&node->sym, &v->sym, SYM_HDR_SIZ + sizeof(struct varnode) - sizeof v);
  68:                 v->left = v->right = NULL;
  69:                 v->sym.value.sym_var.valptr = NULL;
  70:                 node->sym.value.sym_var.varno = -1;
  71:             }
  72:             else
  73:                 v = node;
  74: 
  75:             /* search for the new attno */
  76:             for (sq = sq->left; sq->sym.type != TREE; sq = sq->left)
  77:             {
  78:                 if (ckvar(sq->right)->sym.value.sym_var.attno == node->sym.value.sym_var.attno)
  79:                 {
  80: 
  81:                     v->sym.value.sym_var.attno = sq->sym.value.sym_resdom.resno;
  82: #					ifdef xDTR1
  83:                     if (tTf(51, 3))
  84:                     {
  85:                         printf("Tempvar:");
  86:                         nodepr(node);
  87:                     }
  88: #					endif
  89: 
  90:                     return;
  91:                 }
  92:             }
  93:             syserr("tempvar:dom %d of %s missing", node->sym.value.sym_var.attno, rangename(node->sym.value.sym_var.varno));
  94:         }
  95:         return;
  96:     }
  97: 
  98:     tempvar(node->left, sqlist, buf);
  99:     tempvar(node->right, sqlist, buf);
 100: }
 101: /*
 102: ** Origvar -- Restore VAR node to previous state.
 103: **
 104: **	Origvar undoes the effect of tempvar. All vars listed
 105: **	in the sqlist will have their most recent tempvar removed.
 106: */
 107: 
 108: origvar(t, sqlist)
 109: register QTREE  *t;
 110: QTREE       *sqlist[];
 111: {
 112:     register char   v;
 113:     register QTREE  *q;
 114: 
 115:     if (t == NULL)
 116:         return;
 117:     if (t->sym.type == VAR && t->sym.value.sym_var.varno < 0)
 118:     {
 119:         while ((v = (q = (QTREE *) (t->sym.value.sym_var.valptr))->sym.value.sym_var.varno) < 0)
 120:             t = q;
 121: 
 122:         if (sqlist[v])
 123:         {
 124:             t->sym.value.sym_var.varno = v;
 125:             t->sym.value.sym_var.valptr = NULL;
 126:         }
 127:         return;
 128:     }
 129:     origvar(t->left, sqlist);
 130:     origvar(t->right, sqlist);
 131: }
 132: /*
 133: ** Ckvar -- Return pointer to currently "active" VAR.
 134: **
 135: **	This routine guarantees that "t" will point to
 136: **	the most current definition of the VAR.
 137: */
 138: 
 139: QTREE *
 140: ckvar(t)
 141: register QTREE  *t;
 142: {
 143:     if (t->sym.type != VAR)
 144:     {
 145:         syserr("ckvar: not a VAR %d", t->sym.type);
 146:     }
 147: #	ifdef xDTR1
 148:     if (tTf(51, 2))
 149:     {
 150:         printf("ckvar: var %d.%d, type ",
 151:                 t->sym.value.sym_var.varno, t->sym.value.sym_var.attno);
 152:         xputchar(t->sym.value.sym_var.varfrmt);
 153:         printf("%3d\n", t->sym.value.sym_var.varfrml);
 154:     }
 155: #	endif
 156:     while (t != NULL && t->sym.value.sym_var.varno < 0)
 157:         t = (QTREE *) t->sym.value.sym_var.valptr;
 158: 
 159:     if (t == NULL)
 160:         syserr("ckvar null valptr");
 161:     return (t);
 162: }
Last modified: 1986-04-17
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1122
Valid CSS Valid XHTML 1.0 Strict