-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbcreate.C
131 lines (103 loc) · 2.9 KB
/
dbcreate.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include "catalog.h"
#include "stdlib.h"
DB db;
BufMgr *bufMgr;
Error error;
RelCatalog *relCat;
AttrCatalog *attrCat;
#define CALL(c) {Status s;if((s=c)!=OK){error.print(s);exit(1);}}
int main(int argc, char *argv[])
{
if (argc < 2) {
cerr << "Usage: " << argv[0] << " dbname" << endl;
return 1;
}
// create database subdirectory and chdir there
if (mkdir(argv[1], S_IRUSR | S_IWUSR | S_IXUSR
| S_IRGRP | S_IWGRP | S_IXGRP) < 0) {
perror("mkdir");
exit(1);
}
if (chdir(argv[1]) < 0) {
perror("chdir");
exit(1);
}
// create buffer manager
bufMgr = new BufMgr(100);
Status status;
// create heapfiles to hold the relcat and attribute catalogs
status = createHeapFile("relcat");
if (status != OK) {
error.print(status);
exit(1);
}
status = createHeapFile("attrcat");
if (status != OK) {
error.print(status);
exit(1);
}
// open relation and attribute catalogs
relCat = new RelCatalog(status);
if (status == OK)
attrCat = new AttrCatalog(status);
if (status != OK) {
error.print(status);
exit(1);
}
// add tuples describing relcat and attrcat to relation catalog
// and attribute catalog
RelDesc rd;
AttrDesc ad;
strcpy(rd.relName, RELCATNAME);
rd.attrCnt = 2;
CALL(relCat->addInfo(rd));
strcpy(ad.relName, RELCATNAME);
strcpy(ad.attrName, "relName");
ad.attrOffset = 0;
ad.attrType = (int)STRING;
ad.attrLen = sizeof rd.relName;
CALL(attrCat->addInfo(ad));
strcpy(ad.attrName, "attrCnt");
ad.attrOffset += sizeof rd.relName;
ad.attrType = (int)INTEGER;
ad.attrLen = sizeof rd.attrCnt;
CALL(attrCat->addInfo(ad));
strcpy(rd.relName, ATTRCATNAME);
rd.attrCnt = 5;
CALL(relCat->addInfo(rd))
strcpy(ad.relName, ATTRCATNAME);
strcpy(ad.attrName, "relName");
ad.attrOffset = 0;
ad.attrType = (int)STRING;
ad.attrLen = sizeof ad.relName;
CALL(attrCat->addInfo(ad));
strcpy(ad.attrName, "attrName");
ad.attrOffset += sizeof ad.relName;
ad.attrType = (int)STRING;
ad.attrLen = sizeof ad.attrName;
CALL(attrCat->addInfo(ad));
strcpy(ad.attrName, "attrOffset");
ad.attrOffset += sizeof ad.attrName;
ad.attrType = (int)INTEGER;
ad.attrLen = sizeof ad.attrOffset;
CALL(attrCat->addInfo(ad));
strcpy(ad.attrName, "attrType");
ad.attrOffset += sizeof ad.attrOffset;
ad.attrType = (int)INTEGER;
ad.attrLen = sizeof ad.attrType;
CALL(attrCat->addInfo(ad));
strcpy(ad.attrName, "attrLen");
ad.attrOffset += sizeof ad.attrType;
ad.attrType = (int)INTEGER;
ad.attrLen = sizeof ad.attrLen;
CALL(attrCat->addInfo(ad));
delete relCat;
delete attrCat;
delete bufMgr;
cout << "Database " << argv[1] << " created" << endl;
return 0;
}