-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbInterface.cc
95 lines (82 loc) · 2.36 KB
/
dbInterface.cc
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include "dbInterface.hh"
vector<DataUnit *> DbInterface::get_query_result(vector<DataUnit *> data_units, std::string query)
{
char *query_ptr = &query[0]; //query_ptr used to access the characters
result.clear();
if (get_result(data_units, query_ptr) == -1)
result.clear();
return result;
}
int DbInterface::get_result(vector<DataUnit *> data_units, char *query_ptr)
{
std::string name = "";
std::string attr = "";
int query_end = 0;
while (*query_ptr != '\0' && *query_ptr != '.' && *query_ptr != '(') //get name value
{
name += *query_ptr;
query_ptr++;
}
if (*query_ptr == '(')
{
query_ptr++;
if (*query_ptr == '\0')
{
std::cout << "!!!query syntax error" << std::endl;
return -1;
}
while (*query_ptr != '\0' && *query_ptr != '.' && *query_ptr != ')') //get attribute value
{
attr += *query_ptr;
query_ptr++;
}
if (*query_ptr != ')')
{
std::cout << "!!!query syntax error" << std::endl;
return -1;
}
}
if (*query_ptr == ')')
{
query_ptr++;
}
if (*query_ptr == '.')
{
query_ptr++;
}
if (*query_ptr == '\0')
{
query_end = 1;
}
for (auto i = data_units.begin(); i != data_units.end(); ++i) //iterate over all children
{
if ((*i)->name == name && (attr == "" || (*i)->attr->attr_val == attr))
{
if (query_end)
(result).push_back(*i);
else
if (get_result((*i)->data_units, query_ptr) == -1) //recursive call to search query in the children vector
break;
}
}
return 0;
}
void DbInterface::print_query_result(vector<DataUnit*> data_units)
{
int data_count = data_units.size();
if (data_count == 0)
{
std::cout<<"no data found"<<std::endl;
return;
}
if ((*data_units.begin())->unit_type == complex)
{
std::cout<<"can't print result(Complex type)"<<std::endl;
return;
}
for (auto i = data_units.begin(); i != data_units.end(); ++i) //if results are simple type then print the values
{
std::cout<<"["<<(*i)->data<<"]"<<std::endl;
}
}