aboutsummaryrefslogtreecommitdiff
path: root/query.cpp
blob: 5d2e3fdb768877ec4871e059050d2bc9c2f115f3 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "query.h"
#include <iostream>

using namespace std;


WhereClause::WhereClause(void):col(0){}
WhereClause::WhereClause(const int _c,const vector<RowItem> &_v):col(_c),values(_v){}

UpdateClause::UpdateClause(void):col(0){}
UpdateClause::UpdateClause(const int _c,const RowItem &_v):col(_c),value(_v){}


QueryResult::QueryResult(void):res(0){}
QueryResult::QueryResult(int _res):res(_res){}
QueryResult::QueryResult(int _res,const vector<Row> &_rows):res(_res),rows(_rows){}
QueryResult::QueryResult(int _res,const vector<Row> &_rows,const string &_m):res(_res),rows(_rows),msg(_m){}
QueryResult::QueryResult(int _res,const string &_m):res(_res),msg(_m){}


QueryResult Query::execute(map<string,Table> &tables){return QueryResult(-1);}
Query::~Query(void){}


CreateQuery::CreateQuery(void):nc(0),header(NULL){}

void CreateQuery::setHeader(const ColHeader *hd){
	header=new ColHeader[nc];
	memcpy(header,hd,nc*sizeof(ColHeader));
}

QueryResult CreateQuery::execute(map<string,Table> &tables){
	if(nc==0)return QueryResult(0,"zero nc (CQ:ex)");
	if(header==NULL)return QueryResult(0,"null header (CQ:ex)");
	const map<string,Table>::const_iterator tit=tables.find(tablename);
	if(tit!=tables.cend())return QueryResult(0,"existing table (CQ:ex)");
	tables.emplace(piecewise_construct,
	               forward_as_tuple(tablename),
	               forward_as_tuple(tablename,nc,header));
	return QueryResult(1);
}


FindQuery::FindQuery(void):limit(-1){};

pair<vector<map<string,Row>::const_iterator>,string> FindQuery::executeIterators(map<string,Table> &tables){
	const map<string,Table>::const_iterator tit=tables.find(tablename);
	if(tit==tables.cend())return {{},"non-existent table (FQ:eI)"};
	return executeIterators(tit->second);
}

pair<vector<map<string,Row>::const_iterator>,string> FindQuery::executeIterators(const Table &table){
	if(limit==0)return {{},"zero limit (FQ:eI)"};
	int i,j,k;

	for(i=0;i<(int)where.size();i++){
		if(where[i].col==0)break;
	}
	if(i!=(int)where.size()){
		for(const RowItem &wh : where[i].values){
			const map<string,Row>::const_iterator rit=table.rows.find(serialise(table.header[0],wh));
			if(rit==table.rows.end())return {{},""};
			const Row &row=rit->second;
			for(j=0;j<(int)where.size();j++){
				if(j==i)continue;
				const int col=where[j].col;
				if(col<0||col>=table.nc){
					return {{},"invalid col (DQ:eI)"};
				}
				for(k=0;k<where[j].values.size();k++){
					if(riCompare(table.header[col],row.items[col],where[j].values[k])==0)break;
				}
				if(k==where[j].values.size())return {{},""};
			}
			vector<map<string,Row>::const_iterator> v;
			v.push_back(rit);
			return {v,""};
		}
	}

	typedef map<string,Row>::const_iterator cit_t;

	cit_t end=table.rows.end();
	pair<vector<map<string,Row>::const_iterator>,string> ret;
	for(i=0;i<(int)where.size();i++){
		const int col=where[i].col;
		if(col<0||col>=table.nc){
			ret.second="invalid col (DQ:eI)";
			return ret;
		}
	}
	for(cit_t it=table.rows.begin();it!=end;it++){
		for(i=0;i<(int)where.size();i++){
			const int col=where[i].col;
			for(j=0;j<where[i].values.size();j++){
				if(riCompare(table.header[col],it->second.items[col],where[i].values[j])==0)break;
			}
			if(j==where[i].values.size())break;
		}
		if(i==(int)where.size()){
			ret.first.push_back(it);
			if(ret.first.size()==limit)return ret;
		}
	}
	return ret;
}

QueryResult FindQuery::execute(map<string,Table> &tables){
	pair<vector<map<string,Row>::const_iterator>,string> ret=executeIterators(tables);
	QueryResult qres;
	qres.res=ret.first.size();
	for(int i=0;i<qres.res;i++){
		qres.rows.push_back(ret.first[i]->second);
	}
	qres.msg=ret.second;
	return qres;
}


InsertQuery::InsertQuery(const Row &_r):row(_r){}
InsertQuery::InsertQuery(Row &&_r):row(move(_r)){}

QueryResult InsertQuery::execute(map <string,Table> &tables){
	map<string,Table>::iterator tit=tables.find(tablename);
	if(tit==tables.end())return QueryResult(0,"non-existent table (IQ:ex)");
	Table &table=tit->second;

	if(table.nc!=row.nc)return QueryResult(0,"non-conformant row width (IQ:ex)");
	const string primkey=serialise(table.header[0],row.items[0]);
	const map<string,Row>::const_iterator rit=table.rows.find(primkey);
	if(rit!=table.rows.cend())return QueryResult(0,"existing primary key (IQ:ex)");
	table.rows.insert(make_pair(primkey,row));
	return QueryResult(1);
}


UpdateQuery::UpdateQuery(void){};

QueryResult UpdateQuery::execute(map<string,Table> &tables){
	const map<string,Table>::iterator tit=tables.find(tablename);
	if(tit==tables.cend())return QueryResult(0,"non-existent table (UQ:ex)");
	Table &table=tit->second;

	FindQuery fqu;
	fqu.tablename=tablename;
	fqu.where=where;
	pair<vector<map<string,Row>::const_iterator>,string> ret=fqu.executeIterators(table);
	if(ret.second.size()!=0)return QueryResult(0,ret.second);
	map<string,Row>::const_iterator it;
	int i,j;
	const int numupdates=updates.size();
	for(i=0;i<numupdates;i++){
		const int col=updates[i].col;
		if(col<0||col>=table.nc)return QueryResult(0,"invalid col (UQ:ex)");
		if(col==0)return QueryResult(0,"update primary key (UQ:ex)");
	}
	for(i=0;i<(int)ret.first.size();i++){
		for(j=0;j<numupdates;j++){
			const int col=updates[j].col;
			ret.first[i]->second.items[col]=updates[j].value.copy(table.header[col]);
		}
	}
	return QueryResult(ret.first.size());
}


DeleteQuery::DeleteQuery(void){};

QueryResult DeleteQuery::execute(map<string,Table> &tables){
	const map<string,Table>::iterator tit=tables.find(tablename);
	if(tit==tables.cend())return QueryResult(0,"non-existent table (DQ:ex)");
	Table &table=tit->second;

	FindQuery fqu;
	fqu.tablename=tablename;
	fqu.where=where;
	pair<vector<map<string,Row>::const_iterator>,string> ret=fqu.executeIterators(table);
	if(ret.second.size()!=0)return QueryResult(0,ret.second);
	map<string,Row>::const_iterator it;
	for(int i=0;i<ret.first.size();i++){
		table.rows.erase(ret.first[i]);
	}
	return QueryResult(ret.first.size());
}


DropQuery::DropQuery(void){};

QueryResult DropQuery::execute(map<string,Table> &tables){
	const map<string,Table>::iterator tit=tables.find(tablename);
	if(tit==tables.cend())return QueryResult(0,"non-existent table (DrQ:ex)");
	tables.erase(tit);
	return QueryResult(1);
}