1: # include <ingres.h> 2: # include <aux.h> 3: # include <access.h> 4: # include <symbol.h> 5: # include <catalog.h> 6: # include <sccs.h> 7: 8: SCCSID(@(#)openr.c 8.4 1/22/85) 9: 10: /* 11: ** OPENR -- Open a relation into a descriptor 12: ** 13: ** Openr will open the named relation into the given descriptor 14: ** according to the mode specified. When searching for a name, 15: ** a relation owner by the current user will be searched for first. 16: ** If none is found then one owned by the DBA will be search for. 17: ** 18: ** There are several available modes for opening a relation. The 19: ** most common are 20: ** mode OR_READ -- open for reading 21: ** mode OR_WRITE -- open for writing. 22: ** Other modes which can be used to optimize performance: 23: ** mode OR_RELTID -- get relation-relation tuple and tid only. 24: ** Does not open the relation. 25: ** mode OR_AREAD -- open relation for reading after a previous 26: ** call of mode OR_RELTID. 27: ** mode OR_AWRITE -- open relation for writing after a previous 28: ** call of mode OR_RELTID. 29: ** mode OR_REREAD -- open relation for reading. Assumes that relation 30: ** was previously open (eg relation & attributed 31: ** have been filled) and file was closed by closer. 32: ** mode OR_REWRITE -- open relation for writing. Same assumptions as 33: ** mode OR_REREAD. 34: ** 35: ** Parameters: 36: ** dx - a pointer to a struct descriptor (defined in ingres.h) 37: ** mode - can be OR_READ -> OR_REWRITE 38: ** name - a null terminated name (only first 12 chars looked at) 39: ** 40: ** Returns: 41: ** 1 - relation does not exist 42: ** 0 - ok 43: ** <0 - error. Refer to the error codes in access.h 44: ** 45: ** Side Effects: 46: ** Opens the physical file if required. Fill the 47: ** descriptor structure. Initializes the access methods 48: ** if necessary. 49: ** 50: ** Trace Flags: 51: ** 90 52: */ 53: 54: 55: openr(d, mode, name) 56: register DESC *d; 57: int mode; 58: char *name; 59: { 60: int i; 61: register int retval, filemode; 62: char filename[MAXNAME+3]; 63: char btree[MAXNAME]; 64: char btreefile[MAXNAME + 3]; 65: # ifdef xATR1 66: if (tTf(21, 0)) 67: printf("openr:%.12s,%d\n", name, mode); 68: # endif 69: 70: /* init admin */ 71: acc_init(); 72: 73: /* process according to mode */ 74: 75: filemode = O_RDONLY; 76: 77: if (mode >= 0) 78: d->relbtree = NULL; 79: 80: switch (mode) 81: { 82: 83: case OR_RELTID: 84: retval = get_reltup(d, name); 85: break; 86: 87: case OR_WRITE: 88: filemode = O_RDWR; 89: 90: case OR_READ: 91: if (retval = get_reltup(d, name)) 92: break; 93: 94: case OR_AREAD: 95: case OR_AWRITE: 96: if (retval = get_attuples(d)) 97: break; 98: 99: case OR_REWRITE: 100: if (mode == OR_AWRITE || mode == OR_REWRITE) 101: filemode = O_RDWR; 102: 103: case OR_REREAD: 104: clearkeys(d); 105: /* descriptor is filled. open file */ 106: ingresname(d->reldum.relid, d->reldum.relowner, filename); 107: /* can't open a view */ 108: if (d->reldum.relstat & S_VIEW) 109: { 110: retval = acc_err(AMOPNVIEW_ERR); /* view */ 111: break; 112: } 113: if ((d->relfp = open(filename, filemode)) < 0) 114: { 115: retval = acc_err(AMNOFILE_ERR); /* can't open file */ 116: break; 117: } 118: d->relopn = (d->relfp + 1) * 5; 119: if (filemode == O_RDWR) 120: d->relopn = -d->relopn; 121: d->reladds = 0; 122: retval = 0; 123: break; 124: 125: default: 126: syserr("openr:bd md=%d", mode); 127: } 128: 129: if (mode == OR_RELTID && d->reldum.reldim > 0 && !retval) 130: { 131: /* open btreesec relation */ 132: capital(d->reldum.relid, btree); 133: if ((d->relbtree = (DESC *) calloc(1, sizeof(DESC))) == NULL) 134: syserr("calloc error in openr"); 135: if (i = openr(d->relbtree, OR_RELTID, btree)) 136: syserr("opening Btreesec %s %d\n", btree, i); 137: } 138: 139: if (retval == 0 && d->reldum.reldim > 0 && mode != OR_RELTID) 140: { 141: capital(d->reldum.relid, btree); 142: if (d->relbtree == NULL) 143: { 144: if ((d->relbtree = (DESC *) calloc(1, sizeof(DESC))) == NULL) 145: syserr("calloc error in openr"); 146: } 147: if (i = openr(d->relbtree, mode, btree)) 148: syserr("opening Btreesec %s %d\n", btree, i); 149: ingresname(d->reldum.relid, d->reldum.relowner, filename); 150: btreename(filename, btreefile); 151: if ((d->btree_fd = open(btreefile, O_RDWR)) < 0) 152: syserr("openr: can't open %s", btreefile); 153: } 154: 155: /* return */ 156: 157: # ifdef xATR1 158: if (tTf(21, 4) && mode != OR_RELTID && retval != 1) 159: printdesc(d); 160: if (tTf(21, 0)) 161: printf("openr rets %d\n", retval); 162: # endif 163: 164: return (retval); 165: } 166: /* 167: ** GET_ATTUPLES -- get tuples from attribute relation for this relation 168: */ 169: 170: get_attuples(d) 171: register DESC *d; 172: { 173: struct attribute attr, attkey; 174: register int i, dom; 175: int numatts; 176: TID tid1, tid2; 177: 178: clearkeys(&Admin.adattd); 179: 180: /* zero all format types */ 181: for (i = 0; i <= d->reldum.relatts; i++) 182: d->relfrmt[i] = 0; 183: 184: /* prepare to scan attribute relation */ 185: setkey(&Admin.adattd, (char *) &attkey, d->reldum.relid, ATTRELID); 186: setkey(&Admin.adattd, (char *) &attkey, d->reldum.relowner, ATTOWNER); 187: if (i = find(&Admin.adattd, EXACTKEY, &tid1, &tid2, &attkey)) 188: return (i); 189: 190: numatts = d->reldum.relatts; 191: 192: while (numatts && !get(&Admin.adattd, &tid1, &tid2, &attr, TRUE)) 193: { 194: 195: /* does this attribute belong? */ 196: if (bequal(&attr, &attkey, MAXNAME + 2)) 197: { 198: 199: /* this attribute belongs */ 200: dom = attr.attid; /* get domain number */ 201: 202: if (d->relfrmt[dom]) 203: break; /* duplicate attribute. force error */ 204: 205: numatts--; 206: d->reloff[dom] = attr.attoff; 207: d->relfrmt[dom] = attr.attfrmt; 208: d->relfrml[dom] = attr.attfrml; 209: d->relxtra[dom] = attr.attxtra; 210: } 211: } 212: 213: d->relfrmt[0] = INT; 214: d->relfrml[0] = 4; 215: /* make sure all the atributes were there */ 216: for (dom = 1; dom <= d->reldum.relatts; dom++) 217: if (d->relfrmt[dom] == 0) 218: numatts = 1; /* force an error */ 219: if (numatts) 220: i = acc_err(AMNOATTS_ERR); 221: 222: flush_rel(&Admin.adattd, TRUE); 223: 224: # ifdef xATR1 225: if (tTf(21, 3)) 226: printf("get_attr ret %d\n", i); 227: # endif 228: 229: return (i); 230: }