-
Notifications
You must be signed in to change notification settings - Fork 0
/
tableManager.h
222 lines (195 loc) · 7.33 KB
/
tableManager.h
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#pragma once
#include <string>
#include <map>
#include "table.hpp"
#include <QtScript/QScriptEngine>
using namespace std;
static vector<ruleExp*> constructQuery(const int& size,const int& loc,Basic* tmpCopy){
vector<ruleExp*> queryVec(size,nullptr);
queryVec[loc]=new ruleExp(EQU,tmpCopy);
return queryVec;
}
static tuple<vector<int>,vector<int>> inner(table* lTable,table* rTable,const QString& lKey,const QString& rKey){
col* lCol=lTable->getCol(lKey);
col* rCol=rTable->getCol(rKey);
if(lCol->getType()!=rCol->getType()){
throw string("Wrong data type");
}
vector<int> lIndex;
vector<int> rIndex;
for(int i=0;i<lCol->getAllData().size();++i){
Basic* tmpCopy=typeHelper::typehelper->copy(lCol->getAllData()[i]);
int size=rTable->getAllCol().size();
int loc=rTable->findCol({rKey.toStdString()})[0];
vector<int> tmpVec=rTable->doFind(constructQuery(size,loc,tmpCopy));
if(tmpVec.empty()==true){
continue;
}
lIndex.insert(lIndex.end(),tmpVec.size(),i);
rIndex.insert(rIndex.end(),tmpVec.begin(),tmpVec.end());
}
return make_tuple(lIndex,rIndex);
}
static tuple<vector<int>,vector<int>> devition(table* lTable,table* rTable,const QString& lKey,const QString& rKey){
col* lCol=lTable->getCol(lKey);
col* rCol=rTable->getCol(rKey);
if(lCol->getType()!=rCol->getType()){
throw string("Wrong data type");
}
vector<int> lIndex;
vector<int> rIndex;
for(int i=0;i<lCol->getAllData().size();++i){
Basic* tmpCopy=typeHelper::typehelper->copy(lCol->getAllData()[i]);
int size=rTable->getAllCol().size();
int loc=rTable->findCol({rKey.toStdString()})[0];
vector<int> tmpVec=rTable->doFind(constructQuery(size,loc,tmpCopy));
if(tmpVec.empty()==true){
lIndex.insert(lIndex.end(),i);
rIndex.insert(rIndex.end(),-1);
}
else{
lIndex.insert(lIndex.end(),tmpVec.size(),i);
rIndex.insert(rIndex.end(),tmpVec.begin(),tmpVec.end());
}
}
return make_tuple(lIndex,rIndex);
}
static tuple<vector<int>,vector<int>> outer(table* lTable,table* rTable,const QString& lKey,const QString& rKey){
col* lCol=lTable->getCol(lKey);
col* rCol=rTable->getCol(rKey);
if(lCol->getType()!=rCol->getType()){
throw string("Wrong data type");
}
vector<int> lIndex;
vector<int> rIndex;
vector<bool> mark(rCol->getAllData().size(),true);
for(int i=0;i<lCol->getAllData().size();++i){
Basic* tmpCopy=typeHelper::typehelper->copy(lCol->getAllData()[i]);
int size=rTable->getAllCol().size();
int loc=rTable->findCol({rKey.toStdString()})[0];
vector<int> tmpVec=rTable->doFind(constructQuery(size,loc,tmpCopy));
if(tmpVec.empty()==true){
lIndex.insert(lIndex.end(),i);
rIndex.insert(rIndex.end(),-1);
}
else{
lIndex.insert(lIndex.end(),tmpVec.size(),i);
rIndex.insert(rIndex.end(),tmpVec.begin(),tmpVec.end());
}
for(int j=0;j<tmpVec.size();++j){
mark[tmpVec[j]]=false;
}
}
for(int i=0;i<mark.size();++i){
if(mark[i]){
rIndex.insert(rIndex.end(),i);
lIndex.insert(lIndex.end(),-1);
}
}
return make_tuple(lIndex,rIndex);
}
class tableManager: public QObject
{
private:
Q_OBJECT
const int maxManaged;
vector<string> managedTableName;
map<string,table*> managedTable;
tableManager(int maxNum):maxManaged(maxNum) {}
static string curOperatUser;
static table* jurisdictionTable;
public:
static tableManager* tablemanager;
static tableManager* getTableManager(int maxNum=10){
if(tablemanager==nullptr){
tablemanager=new tableManager(maxNum);
}
return tablemanager;
}
static void setcurOperatUser(const string& user){
curOperatUser=user;
}
static string getcurOperatUser(){
return curOperatUser;
}
static void setJurisdiction(table* table)
{
table->setSystemManage();
jurisdictionTable=table;
}
Q_INVOKABLE table* loadTable(QString tName)
{
string tableName=tName.toStdString();/*
if(jurisdictionTable->find({"(x=='"+curOperatUser+"')","(x=='"+tableName+"')"}).empty()) {
return nullptr;
}*/
if(managedTable.find(tableName)==managedTable.end())
{
table* t=table::loadFile(tableName);
t->setSystemManage();
managedTable[tableName] = t;
}
else
{
auto iter=find(managedTableName.begin(),managedTableName.end(),tableName);
managedTableName.erase(iter);
}
managedTableName.insert(managedTableName.begin(),tableName);
return managedTable[tableName];
}
void doManage()
{
for(auto iter=managedTable.begin();iter!=managedTable.end();iter++){
iter->second->updateFile();
}
while(managedTableName.size()>this->maxManaged){
string delTableName=managedTableName.back();
managedTableName.pop_back();
delete managedTable[delTableName];
managedTable.erase(delTableName);
}
}
void deliManage(table* newTable){
string tableName=newTable->ID;
if(managedTable.find(tableName)==managedTable.end()){
managedTable[tableName]=newTable;
managedTableName.insert(managedTableName.begin(),tableName);
}
}
void saveAllTable(){
for(auto iter=managedTable.begin();iter!=managedTable.end();iter++){
iter->second->saveFile();
}
}
Q_INVOKABLE table* createTable(const QString& ID,const QScriptValue& strVec);
Q_INVOKABLE void removeTable(const QString& ID);
table* tableJoin(const QString &newTableID, const QString &lTableName,const vector<string>& lColName,const QString &rTableName,const vector<string>& rColName,const QString &lKey, const QString &rKey, const QString &joinWay="innerjoin"){
table* lTable=tableManager::tablemanager->loadTable(lTableName);
table* rTable=tableManager::tablemanager->loadTable(rTableName);
tuple<vector<int>,vector<int>> resultIndex;
if(joinWay=="innerjoin"){
resultIndex=inner(lTable,rTable,lKey,rKey);
}
else if(joinWay=="leftjoin"){
resultIndex=devition(lTable,rTable,lKey,rKey);
}
else if(joinWay=="rightjoin"){
resultIndex=devition(rTable,lTable,lKey,rKey);
}
else if(joinWay=="outerjoin"){
resultIndex=outer(lTable,rTable,lKey,rKey);
}
else{
throw string("The wrong way to join");
}
table* lNewTable=lTable->genNewTable(lColName,get<0>(resultIndex));
table* rNewTable=rTable->genNewTable(rColName,get<1>(resultIndex));
table* result=table::doJoin(newTableID.toStdString(),lNewTable,rNewTable);
lNewTable->abandonOwnShip();
rNewTable->abandonOwnShip();
delete lNewTable;
delete rNewTable;
return result;
}
Q_INVOKABLE table* tableJoin(const QString &newTableID, const QString &lTableName,const QScriptValue& lColName,const QString &rTableName,const QScriptValue& rColName,const QString &lKey, const QString &rKey, const QString &joinWay="innerjoin");
};