-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.C
More file actions
61 lines (53 loc) · 1.63 KB
/
delete.C
File metadata and controls
61 lines (53 loc) · 1.63 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
/*
Purpose: implement a DELETE query
@authors: Kohei Tagai (9084551077), Levi Cameron (9081118565),
Shourya Agrawal (9081614613)
*/
#include "catalog.h"
#include "query.h"
/*
* Deletes records from a specified relation.
*
* Returns:
* OK on success
* an error code otherwise
*/
const Status QU_Delete(const string & relation,
const string & attrName,
const Operator op,
const Datatype type,
const char *attrValue) // relname.attr
{
Status status;
HeapFileScan scan(relation, status);
if(status != OK) return status;
// Check if input argument is null
if(attrName[0] == '\0'){
status = scan.startScan(0,0,STRING,NULL,op);
}
else{ // if input is not null get description of the attribute
AttrDesc attrDesc;
attrCat->getInfo(relation,attrName,attrDesc);
if(type == FLOAT){// cast attrValue to type float
const float filter = atof(attrValue);
const float *fp = &filter;
memcpy(&attrValue,&fp, sizeof(attrValue));
status = scan.startScan(attrDesc.attrOffset,attrDesc.attrLen,type,attrValue,op);
}else if (type == INTEGER){// cast attrValue to type int
const int filter = atoi(attrValue);
const int *fp = &filter;
memcpy(&attrValue,&fp, sizeof(attrValue));
status = scan.startScan(attrDesc.attrOffset,attrDesc.attrLen,type,attrValue,op);
}else{//attrValue is type string
status = scan.startScan(attrDesc.attrOffset,attrDesc.attrLen,type,attrValue,op);
}
}
RID tmpRID;
// go through the heap file deleting records that are a match
while (scan.scanNext(tmpRID) == OK)
{
status = scan.deleteRecord();
assert(status == OK);
}
return OK;
}