1: # include "../ingres.h" 2: # include "../aux.h" 3: # include "../access.h" 4: 5: /* 6: ** OPENR -- Open a relation into a descriptor 7: ** 8: ** Openr will open the named relation into the given descriptor 9: ** according to the mode specified. When searching for a name, 10: ** a relation owner by the current user will be searched for first. 11: ** If none is found then one owned by the DBA will be search for. 12: ** 13: ** There are several available modes for opening a relation. The 14: ** most common are 15: ** mode 0 -- open for reading 16: ** mode 2 -- open for writing (mode 1 works identically). 17: ** Other modes which can be used to optimize performance: 18: ** mode -1 -- get relation-relation tuple and tid only. 19: ** Does not open the relation. 20: ** mode -2 -- open relation for reading after a previous 21: ** call of mode -1. 22: ** mode -3 -- open relation for writing after a previous 23: ** call of mode -1. 24: ** mode -4 -- open relation for reading. Assumes that relation 25: ** was previously open (eg relation & attributed 26: ** have been filled) and file was closed by closer. 27: ** mode -5 -- open relation for writing. Same assumptions as 28: ** mode -4. 29: ** 30: ** Parameters: 31: ** dx - a pointer to a struct descriptor (defined in ingres.h) 32: ** mode - can be 2,0,-1,-2,-3,-4,-5 33: ** name - a null terminated name (only first 12 chars looked at) 34: ** 35: ** Returns: 36: ** 1 - relation does not exist 37: ** 0 - ok 38: ** <0 - error. Refer to the error codes in access.h 39: ** 40: ** Side Effects: 41: ** Opens the physical file if required. Fill the 42: ** descriptor structure. Initializes the access methods 43: ** if necessary. 44: ** 45: ** Requires: 46: ** access method routine: 47: ** acc_init() 48: ** clearkeys() 49: ** find() 50: ** 51: ** most the access methods + the unix open() function 52: ** 53: ** Called By: 54: ** everyone 55: ** 56: ** Trace Flags: 57: ** Uses trace flag 90 58: ** 59: ** Diagnostics: 60: ** none 61: ** 62: ** Syserrs: 63: ** openr:bd md -- call to openr with illegal mode. 64: ** 65: ** History: 66: ** 10-6-78 (rse) - documented 67: */ 68: 69: 70: openr(dx, mode, name) 71: struct descriptor *dx; 72: int mode; 73: char *name; 74: 75: { 76: register struct descriptor *d; 77: register int retval, filemode; 78: char filename[MAXNAME+3]; 79: 80: d = dx; 81: 82: # ifdef xATR1 83: if (tTf(90, -1)) 84: printf("openr:%.12s,%d\n", name, mode); 85: # endif 86: # ifdef xATM 87: if (tTf(76, 2)) 88: timtrace(21, 0); 89: # endif 90: 91: /* init admin */ 92: acc_init(); 93: 94: /* process according to mode */ 95: 96: filemode = 0; 97: switch (mode) 98: { 99: 100: case -1: 101: retval = get_reltuple(d, name); 102: break; 103: 104: case 1: 105: case 2: 106: filemode = 2; 107: 108: case 0: 109: if (retval = get_reltuple(d, name)) 110: break; 111: 112: case -2: 113: case -3: 114: if (retval = get_attuples(d)) 115: break; 116: 117: case -5: 118: if (mode == -3 || mode == -5) 119: filemode = 2; 120: 121: case -4: 122: clearkeys(d); 123: /* descriptor is filled. open file */ 124: ingresname(d->relid, d->relowner, filename); 125: /* can't open a view */ 126: if (d->relstat & S_VIEW) 127: { 128: retval = acc_err(AMOPNVIEW_ERR); /* view */ 129: break; 130: } 131: if ((d->relfp = open(filename, filemode)) < 0) 132: { 133: retval = acc_err(AMNOFILE_ERR); /* can't open file */ 134: break; 135: } 136: d->relopn = (d->relfp + 1) * 5; 137: if (filemode == 2) 138: d->relopn = -d->relopn; 139: d->reladds = 0; 140: retval = 0; 141: break; 142: 143: default: 144: syserr("openr:bd md=%d", mode); 145: } 146: 147: /* return */ 148: 149: # ifdef xATR1 150: if (tTf(90, 4) && mode != -1 && retval != 1) 151: printdesc(d); 152: if (tTf(90, -1)) 153: printf("openr rets %d\n", retval); 154: # endif 155: 156: # ifdef xATM 157: if (tTf(76, 2)) 158: timtrace(22, 0); 159: # endif 160: return (retval); 161: } 162: 163: 164: get_reltuple(dx, name) 165: struct descriptor *dx; 166: char *name; 167: 168: /* 169: ** Get the tuple for the relation specified by 'name' 170: ** and put it in the descriptor 'dx'. 171: ** 172: ** First a relation named 'name' owned 173: ** by the current user is searched for. If that fails, 174: ** then a relation owned by the dba is searched for. 175: */ 176: 177: { 178: register struct descriptor *d; 179: struct relation rel; 180: register int i; 181: char filename[MAXNAME+3]; 182: 183: d = dx; 184: 185: clearkeys(&Admin.adreld); 186: 187: /* make believe relation relation is read only for concurrency */ 188: Admin.adreld.relopn = abs(Admin.adreld.relopn); 189: 190: /* relation relation is open. Search for relation 'name' */ 191: setkey(&Admin.adreld, &rel, name, RELID); 192: setkey(&Admin.adreld, &rel, Usercode, RELOWNER); 193: 194: if ((i = getequal(&Admin.adreld, &rel, d, &d->reltid)) == 1) 195: { 196: /* not a user relation. try relation owner by dba */ 197: setkey(&Admin.adreld, &rel, Admin.adhdr.adowner, RELOWNER); 198: i = getequal(&Admin.adreld, &rel, d, &d->reltid); 199: } 200: 201: flush_rel(&Admin.adreld, TRUE); 202: 203: # ifdef xATR1 204: if (tTf(90, 1)) 205: printf("get_reltuple: %d\n", i); 206: # endif 207: 208: /* restore relation relation to read/write mode */ 209: Admin.adreld.relopn = -Admin.adreld.relopn; 210: return (i); 211: } 212: 213: 214: get_attuples(dx) 215: struct descriptor *dx; 216: 217: { 218: register struct descriptor *d; 219: struct attribute attr, attkey; 220: register int i, dom; 221: int numatts; 222: struct tup_id tid1, tid2; 223: 224: d = dx; 225: 226: clearkeys(&Admin.adattd); 227: 228: /* zero all format types */ 229: for (i = 0; i <= d->relatts; i++) 230: d->relfrmt[i] = 0; 231: 232: /* prepare to scan attribute relation */ 233: setkey(&Admin.adattd, &attkey, d->relid, ATTRELID); 234: setkey(&Admin.adattd, &attkey, d->relowner, ATTOWNER); 235: if (i = find(&Admin.adattd, EXACTKEY, &tid1, &tid2, &attkey)) 236: return (i); 237: 238: numatts = d->relatts; 239: 240: while (numatts && !get(&Admin.adattd, &tid1, &tid2, &attr, TRUE)) 241: { 242: 243: /* does this attribute belong? */ 244: if (bequal(&attr, &attkey, MAXNAME + 2)) 245: { 246: 247: /* this attribute belongs */ 248: dom = attr.attid; /* get domain number */ 249: 250: if (d->relfrmt[dom]) 251: break; /* duplicate attribute. force error */ 252: 253: numatts--; 254: d->reloff[dom] = attr.attoff; 255: d->relfrmt[dom] = attr.attfrmt; 256: d->relfrml[dom] = attr.attfrml; 257: d->relxtra[dom] = attr.attxtra; 258: } 259: } 260: 261: /* make sure all the atributes were there */ 262: for (dom = 1; dom <= d->relatts; dom++) 263: if (d->relfrmt[dom] == 0) 264: numatts = 1; /* force an error */ 265: if (numatts) 266: i = acc_err(AMNOATTS_ERR); 267: 268: flush_rel(&Admin.adattd, TRUE); 269: 270: # ifdef xATR1 271: if (tTf(90, 3)) 272: printf("get_attr ret %d\n", i); 273: # endif 274: 275: return (i); 276: }