-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_result.cpp
170 lines (150 loc) · 6.17 KB
/
query_result.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "logger.h"
#include "query_result.h"
/*PUBLIC*/
query_result::query_result(){
nr_of_columns=0;
}
query_result::~query_result(){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"destructing query result");
}
std::multimap<unsigned int,field>::const_iterator query_result::row_at_position(unsigned int rowid) const{
return raw_result_set.find(rowid);
}
const std::string* query_result::field_value_at_row_position(unsigned int rowid, const std::string& field_name) const{
std::multimap<unsigned int,field>::const_iterator position;
if(raw_result_set.empty()==false){
for(position=raw_result_set.find(rowid);position!=raw_result_set.end();++position){
if(position->second.field_name==field_name) break;
}
if(position!=raw_result_set.end()) return &position->second.field_value;
else return NULL;
}
return NULL;
}
const std::multimap<unsigned int,field>& query_result::result_set() const{
return raw_result_set;
}
unsigned int query_result::nr_of_result_rows() const{
unsigned int size=0;
if(raw_result_set.empty()==false){
size=raw_result_set.size()/nr_of_columns;
}
return size;
}
void query_result::insert(const std::pair<unsigned int, field>& row){
//insert() ensures that the field values for each field of the same row id are unqiue in the table
unsigned int nr_of_inserted_columns=0;
std::set<std::string> fields_inserted;
std::multimap<unsigned int,field>::iterator upper_bound;
std::set<std::pair<std::string,std::string> > field_set;
if(raw_result_set.empty()==false){
//check to avoid inserting a value for an existing field with the same name with the same row id
if(field_value_at_row_position(row.first,row.second.field_name)==NULL){
row_buffer.insert(row);
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"inserting with rowid "+std::to_string(row.first)+":"+row.second.field_name+"="+row.second.field_value+" into row buffer");
}
}
else{
row_buffer.insert(row);
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"inserting with rowid "+std::to_string(row.first)+":"+row.second.field_name+"="+row.second.field_value+" into row buffer");
}
nr_of_inserted_columns=row_buffer.count(row.first);
if(nr_of_inserted_columns==nr_of_columns){
for(std::multimap<unsigned int,field>::const_iterator i=row_buffer.lower_bound(row.first),
upper_bound=row_buffer.upper_bound(row.first);
i!=upper_bound;++i){
if(fields.find(i->second.field_name)!=fields.end()){
fields_inserted.insert(i->second.field_name);
field_set.insert(std::make_pair(i->second.field_name,i->second.field_value));
}
}
if(fields_inserted!=fields){
throw std::runtime_error("Field names to be inserted and that of the table structure do not match.");
}
if(row_set.insert(field_set).second==true){
for(std::multimap<unsigned int,field>::const_iterator i=row_buffer.lower_bound(row.first),
upper_bound=row_buffer.upper_bound(row.first);
i!=upper_bound;++i){
raw_result_set.insert(*i);
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"inserting with rowid "+std::to_string(i->first)+":"+i->second.field_name+"="+i->second.field_value+" from row buffer into result set");
}
row_buffer.erase(row.first);
}
else{
row_buffer.erase(row.first);
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"Another entry with the same content already exists in result set for row id "+std::to_string(row.first));
}
if(row_buffer.find(row.first)!=row_buffer.end()){
throw std::runtime_error("Exiting, row_buffer not cleared up.");
}
}
else if(nr_of_inserted_columns>nr_of_columns){
throw std::runtime_error("Exiting, row_buffer is inconsistent");
}
}
void query_result::append(const std::pair<unsigned int, field>& field){
if(row_set.empty()==true){
insert(std::make_pair(0,field.second));
}
else{
insert(std::make_pair(raw_result_set.rbegin()->first+1,field.second));
}
}
const std::pair<const unsigned int,field>* query_result::first_value_for_field_name_found(const std::string& field_name, const std::string& field_value) const{
std::multimap<unsigned int,field>::const_iterator position;
const std::pair<const unsigned int,field> *field_found=NULL;
if(raw_result_set.empty()==false){
for(position=raw_result_set.begin();position!=raw_result_set.end();++position){
if(position->second.field_name==field_name&&position->second.field_value==field_value){
if(field_found==NULL){
field_found=&(*position);
break;
}
}
}
}
return field_found;
}
const std::pair<const unsigned int,field>* query_result::value_for_field_name_found_after_row_position(const unsigned int row_id, const std::string& field_name, const std::string& field_value) const{
std::multimap<unsigned int,field>::const_iterator position;
const std::pair<const unsigned int,field> *field_found=NULL;
if(raw_result_set.empty()==false){
for(position=row_at_position(row_id+1);position!=raw_result_set.end();++position){
if(position->second.field_name==field_name&&position->second.field_value==field_value){
if(field_found==NULL){
field_found=&(*position);
break;
}
}
}
}
return field_found;
}
void query_result::keep(const std::set<unsigned int>& rowids){
std::multimap<unsigned int,field>::const_iterator position,upper_bound;
std::set<std::pair<std::string,std::string> > field_set;
for(position=raw_result_set.begin();position!=raw_result_set.end();++position){
if(rowids.find(position->first)==rowids.end()){
for(auto i=raw_result_set.lower_bound(position->first),
upper_bound=raw_result_set.upper_bound(position->first);
i!=upper_bound;++i){
field_set.insert(std::make_pair(i->second.field_name,i->second.field_value));
}
raw_result_set.erase(position->first);
row_set.erase(field_set);
}
}
}
void query_result::append(const query_result& appending_entries){
for(std::multimap<unsigned int,field>::const_iterator field=appending_entries.result_set().begin();field!=appending_entries.result_set().end();++field){
append(*field);
}
}
void query_result::set_metadata(const unsigned int colnr, const char** field_names){
if(nr_of_columns==0) nr_of_columns=colnr;
if(fields.empty()==true){
for(unsigned int i=0;i<nr_of_columns;++i){
fields.insert(field_names[i]);
}
}
}