-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.C
74 lines (59 loc) · 1.87 KB
/
create.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "catalog.h"
#include <cstring>
const Status RelCatalog::createRel(const string & relation,
const int attrCnt,
const attrInfo attrList[])
{
Status status;
RelDesc rd;
AttrDesc ad;
if (relation.empty() || attrCnt < 1)
return BADCATPARM;
if (relation.length() >= sizeof rd.relName)
return NAMETOOLONG;
// make sure the relation doesn't already exist
status = getInfo(relation, rd);
if (status == OK)
return RELEXISTS;
if (status != RELNOTFOUND)
return status;
// make sure there are no duplicate attribute names
unsigned int tupleWidth = attrList[0].attrLen;
if (attrCnt > 1) {
for(int i = 1; i < attrCnt; i++) {
tupleWidth += attrList[i].attrLen;
for(int j = 0; j < i; j++)
if (strcmp(attrList[i].attrName, attrList[j].attrName) == 0)
return DUPLATTR;
}
}
if (tupleWidth > PAGESIZE) // should be more strict
return ATTRTOOLONG;
cout << "Creating relation " << relation << endl;
// insert information about relation
strcpy(rd.relName, relation.c_str());
rd.attrCnt = attrCnt;
if ((status = addInfo(rd)) != OK)
return status;
// insert information about attributes
strcpy(ad.relName, relation.c_str());
int offset = 0;
for(int i = 0; i < attrCnt; i++) {
if (strlen(attrList[i].attrName) >= sizeof ad.attrName)
return NAMETOOLONG;
strcpy(ad.attrName, attrList[i].attrName);
ad.attrOffset = offset;
ad.attrType = attrList[i].attrType;
ad.attrLen = attrList[i].attrLen;
if ((status = attrCat->addInfo(ad)) != OK)
{
cout << "got error return" << status << endl;
return status;
}
offset += ad.attrLen;
}
// now create the actual heapfile to hold the relation
status = createHeapFile (relation);
if (status != OK) return status;
return OK;
}