-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.cpp
89 lines (76 loc) · 2.21 KB
/
insert.cpp
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
#include "catalog.h"
#include "query.h"
#include "index.h"
#include "datatypes.h"
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
/*
* Inserts a record into the specified relation
*
* Returns:
* OK on success
* an error code otherwise
*/
Status Updates::Insert(const string& relation, // Name of the relation
const int attrCnt, // Number of attributes specified in INSERT statement
const attrInfo attrList[]) // Value of attributes specified in INSERT statement
{
// Get the information about the attributes
int attrCntInfo;
AttrDesc* attrs;
Status res;
res = attrCat->getRelInfo(relation, attrCntInfo, attrs);
if (res != OK) {
return res;
}
// Compute record size
AttrDesc curAttr;
int size = 0;
for (int x=0;x<attrCntInfo;x++) {
curAttr = attrs[x];
size += curAttr.attrLen;
}
// Allocate record
Record record;
record.length = size;
record.data = malloc(size);
attrInfo curInfo;
for (int x=0;x<attrCnt;x++) {
curInfo = attrList[x];
// Get the description of this attribute
AttrDesc desc;
res = attrCat->getInfo(relation, curInfo.attrName, desc);
if (res != OK) {
return res;
}
// Copy the data into the record at the correct offset
memcpy(((char*)record.data)+desc.attrOffset, curInfo.attrValue, desc.attrLen);
}
// Find the heapfile and write the record
RID rid;
HeapFile heap = HeapFile(relation, res);
if (res != OK) {
return res;
}
heap.insertRecord(record, rid);
// Find which attrs are indexed
for (int x=0;x<attrCnt;x++) {
curInfo = attrList[x];
// Get the description of this attribute
AttrDesc desc;
res = attrCat->getInfo(relation, curInfo.attrName, desc);
if (res != OK) {
return res;
}
// Create/find the index and insert the RID
if (desc.indexed) {
Index index = Index(curInfo.relName, desc.attrOffset, desc.attrLen, (Datatype)desc.attrType, 1, res);
if (res != OK) {
return res;
}
index.insertEntry(((char*)record.data)+desc.attrOffset, rid);
}
}
return OK;
}