-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert.cpp
More file actions
executable file
·116 lines (85 loc) · 2.14 KB
/
insert.cpp
File metadata and controls
executable file
·116 lines (85 loc) · 2.14 KB
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
#include "catalog.h"
#include "query.h"
#include "index.h"
#include "utility.h"
#include <string.h>
#include <cstdlib>
/*
* 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
{
Status temp;
int RattrCnt;
AttrDesc *attr_ptr;
temp = attrCat->getRelInfo(relation, RattrCnt, attr_ptr);
// error checking: if failed to parse RattrCnt, attr_ptr
if (temp != OK)
{
return temp;
}
// error checking: if number of attributes doesn't match
if (RattrCnt != attrCnt)
{
return NOTUSED1;
}
Record result;
result.length = 0;
AttrDesc Rattr[attrCnt]; // real attribute
for (int i = 0; i < RattrCnt; i++)
{
temp=attrCat->getInfo(relation, attrList[i].attrName, Rattr[i]);
// error checking: if failed to parse Rattr
if (temp != OK)
{
return temp;
}
result.length += Rattr[i].attrLen;
}
result.data=malloc(result.length);
for (int i = 0; i < RattrCnt; i++)
{
memcpy ((static_cast<char*> (result.data))+Rattr[i].attrOffset, attrList[i].attrValue, Rattr[i].attrLen);
}
// insert into heapfile
RID current_rid;
HeapFile current_heap(relation, temp);
//error checking: if failed to create HeapFile
if (temp != OK)
{
return temp;
}
temp = current_heap.insertRecord(result, current_rid);
// error checking: if failed to insert Record
if (temp != OK)
{
return temp;
}
for (int i = 0; i < RattrCnt; i++)
{
if (Rattr[i].indexed)
{
Index current_index(Rattr[i].relName, Rattr[i].attrOffset, Rattr[i].attrLen, (Datatype)Rattr[i].attrType, 0, temp);
// error checking: if failed to parse index
if (temp != OK)
{
return temp;
}
temp = current_index.insertEntry(attrList[i].attrValue, current_rid);
// error checking: if failed to insert index entry
if (temp != OK)
{
return temp;
}
}
}
// free the memory
free(result.data);
return OK;
}