-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDoSeletct.cpp
88 lines (73 loc) · 3 KB
/
DoSeletct.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
77
78
79
80
81
//
// DoSeletct.cpp
// SQL parser and interpreter
//
// Created by 王璇 on 17/4/8.
// Copyright © 2017年 Xuan Wang. All rights reserved.
//
#include <stdio.h>
#include "MyNode.h"
#include "SelectNode.h"
#include <cmath>
void DoSelect(struct SelectNode *env, int currRel, int hasWhere){
if (currRel < env->NRels)
{ /* -----------------------------------------------------
We have not yet reached the last relation in the list
----------------------------------------------------- */
//reset file relation[currRel].fd to beginning of file;
env->Rel[currRel]->Open(env->Rel[currRel]->true_Relname);
while ( env->Rel[currRel]->GetNext() != 0 )
{
//read one tuple from relation[currRel].fd
if(env->Rel[currRel]->buf[env->Rel[currRel]->record_size] == 'N')
continue;
DoSelect(env, currRel+1, hasWhere); // Recurse...
}
env->Rel[currRel]->Close();
}
else
{
if ( hasWhere == 1 && env->where->eval_bool() == true )
{
/* ================================================
Print the SELECT clause
================================================ */
for (int i = 0; i < env->NAttrsExp; i++)
{
//print result of Expression i in SELECT-clause;
if(env->selectExp[i]->node_type == STRING_CONST || env->selectExp[i]->node_type == CHAR_ATTR){
printf("%10s ",env->selectExp[i]->eval_string());
}
else{
if(env->selectExp[i]->isFloat == true)
printf("%10.4f ",env->selectExp[i]->eval_num());
else{
printf("%10d ",(int)env->selectExp[i]->eval_num());
}
}
//(Note: how you print depends on the type of result.
//If it is INT, use printf("%10d".., for FLOAT, use printf("%10.4f", for CHAR, use printf("%20s"...) )
}
printf("\n");
}
else if(hasWhere == 0){
for (int i = 0; i < env->NAttrsExp; i++)
{
//print result of Expression i in SELECT-clause;
if(env->selectExp[i]->node_type == STRING_CONST || env->selectExp[i]->node_type == CHAR_ATTR){
printf("%10s ",env->selectExp[i]->eval_string());
}
else{
if(env->selectExp[i]->isFloat == true)
printf("%10.4f ",env->selectExp[i]->eval_num());
else{
printf("%10d ",(int)env->selectExp[i]->eval_num());
}
}
//(Note: how you print depends on the type of result.
//If it is INT, use printf("%10d".., for FLOAT, use printf("%10.4f", for CHAR, use printf("%20s"...) )
}
printf("\n");
}
}
}