-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.cpp
65 lines (57 loc) · 1.71 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
#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 = OK;
// Compute recLen
int reclen = 0;
for(int i = 0; i < projCnt; i++)
{
reclen += projNames[i].attrLen;
}
// Create projNames attrDescs
AttrDesc names[projCnt];
for(int i = 0; i < projCnt; i++)
{
AttrDesc retAttr;
status = attrCat->getInfo(projNames[i].relName, projNames[i].attrName, retAttr);
if(status != OK) return status;
names[i] = retAttr;
}
// If a predicate exists
if(attr)
{
AttrDesc attrDesc;
status = attrCat->getInfo(attr->relName, attr->attrName, attrDesc);
if(status != OK) return status;
//If index exists on attribute in the predicate AND an equality, call IndexSelect
if(op == EQ && attrDesc.indexed)
{
status = Operators::IndexSelect(result, projCnt, names, &attrDesc, op, attrValue, reclen);
if(status != OK) return status;
}
else
{
status = Operators::ScanSelect(result, projCnt, names, &attrDesc, op, attrValue, reclen);
if(status != OK) return status;
}
}
else // predicate does NOT exist
{
status = Operators::ScanSelect(result, projCnt, names, NULL, op, attrValue, reclen);
}
return status;
}