/
delete.C
68 lines (53 loc) · 1.6 KB
/
delete.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
#include "catalog.h"
#include "query.h"
/*
* Deletes records from a specified relation.
*
* Returns:
* OK on success
* an error code otherwise
*/
/* This function will delete all tuples in relation satisfying the predicate specified by attrName, op, and the constant attrValue.
* type denotes the type of the attribute. You can locate all the qualifying tuples using a filtered HeapFileScan. */
const Status QU_Delete(const string & relation,
const string & attrName,
const Operator op,
const Datatype type,
const char *attrValue)
{
Status status;
HeapFileScan *hfs;
AttrDesc record;
RID rid;
int tempInt;
float tempFloat;
hfs = new HeapFileScan(relation, status);
//Get the AttrDesc from the attrCat table
if (attrValue != NULL){
status = attrCat->getInfo(relation, attrName, record);
if (status != OK) return status;
}
//check type and cast accordingly
if(type == INTEGER){
tempInt = atoi(attrValue);
attrValue = (char *) &tempInt;
} else if(type == FLOAT){
tempFloat = atof(attrValue);
attrValue = (char *) &tempFloat;
}
//Start HFS on the relation table
status = hfs->startScan(record.attrOffset, record.attrLen, type, attrValue, op);
if(status != OK) return status;
while((status = hfs->scanNext(rid)) != FILEEOF){
if(status != OK) return status;
//Delete record if found in relation table
status = hfs->deleteRecord();
if(status != OK) return status;
}
//If end of file then return attribute not found
if (status == FILEEOF){
status = OK;
}
delete hfs;
return status;
}