-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.cpp
77 lines (62 loc) · 2.17 KB
/
select.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
#include "catalog.h"
#include "query.h"
#include "index.h"
/*
* Selects records from the specified relation.
*
* Returns:
* OK on success
* an error code otherwise
*/
Status Operators::Select(const string & result, // name of the output relation
const int projCnt, // number of attributes in the projection
const attrInfo projNames[], // the list of projection attributes
const attrInfo *attr, // attribute used inthe selection predicate
const Operator op, // predicate operation
const void *attrValue) // literal value in the predicate
{
Status status;
AttrDesc* attrs = NULL;
AttrDesc* projList = NULL;
int record_length;
bool conditional;
bool equality;
try {
projList = new AttrDesc[projCnt];
if(projList == NULL) throw INSUFMEM;
// For each projection attrInfo in projNames, store attrDesc in projList
for(int i = 0; i < projCnt; ++i) {
status = attrCat->getInfo(projNames[i].relName, projNames[i].attrName, projList[i]);
if(status != OK) throw status;
}
// get record length for Select functions
for(int i = 0; i < projCnt; ++i) {
record_length += projList[i].attrLen;
}
conditional = (attr == NULL) ? false : true;
equality = (op == EQ) ? true : false;
// If this is a conditional selection get the attribute info
if(conditional) {
attrs = new AttrDesc;
status = attrCat->getInfo(attr->relName, attr->attrName, *attrs);
if(status != OK) throw status;
}
// If it is a conditional selection, index exists on attribute in the predicate and equality predicate
// then use IndexSelect (indexselect.cpp) otherwise ScanSelect (scanselect.cpp)
if(conditional && attrs->indexed && equality) {
status = IndexSelect(result, projCnt, projList, attrs, op, attrValue, record_length);
if(status != OK) throw status;
} else {
status = ScanSelect(result, projCnt, projList, attrs, op, attrValue, record_length);
if(status != OK) throw status;
}
// no exceptions thrown, so status is OK
status = OK;
} catch (Status s) {
status = s;
}
// Free memory
if(attrs) delete attrs;
if(projList) delete[] projList;
return status;
}