6 Star 4 Fork 2

GPLme / SG-Database

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
jsCall.cpp 6.60 KB
一键复制 编辑 原始数据 按行查看 历史
ECIR 提交于 2021-03-19 17:43 . js可以操作find_in了
#include "tableManager.h"
#include "typeHelper.hpp"
#include "aggHelper.h"
#include <QtScript/QScriptEngine>
Q_DECLARE_METATYPE(QVector<QString>)
using namespace std;
TYPE strToType(const string& str){
if(str=="FLOAT"){
return FLOAT;
}
else if(str=="INT"){
return INT;
}
else if(str=="STR"){
return STR;
}
else if(str=="BOOL"){
return BOOL;
}
else if(str=="NULL"){
return _NULL;
}
throw string("type error");
}
IndexType strToIndType(const string& str){
if(str=="TV"){
return TV;
}
else if(str=="RB"){
return RB;
}
throw string("type error");
}
vector<string> jsvalueTostrVec(const QScriptValue& jsvalue){
vector<string> StrVec;
const QVector<QString>& strVec=qscriptvalue_cast<QVector<QString>>(jsvalue);
for(const QString& str:strVec){
StrVec.push_back(str.toStdString());
}
return StrVec;
}
tuple<vector<col*>,vector<IndexType>> strVecToColVec(const vector<string>& colStrVec){
vector<col*> colVec;
vector<IndexType> indTypes;
for(const string& str:colStrVec){
int count=0;
string colName;
TYPE dataType;
IndexType indType;
int lastLoc;
for(int i=0;i<str.length();++i){
if(i==str.length()-1){
indType=strToIndType(str.substr(lastLoc+1));
}
else if(str[i]==':'){
if(count==0){
colName=str.substr(0,i);
}
else if(count==1){
dataType=strToType(str.substr(lastLoc+1,i-(lastLoc+1)));
}
count++;
lastLoc=i;
}
}
colVec.push_back(new col (dataType,colName) );
indTypes.push_back(indType);
}
return make_tuple(colVec,indTypes);
}
vector<Basic*> jsvalueToBasicVec(const QScriptValue& str){
const vector<string>& tmp=jsvalueTostrVec(str);
vector<Basic*> basicVec;
for(int i=0;i<tmp.size();++i){
basicVec.push_back(typeHelper::typehelper->strToBasic(QString::fromStdString(tmp[i])));
}
return basicVec;
}
table* tableManager::createTable(const QString& ID,const QScriptValue& strVec){
const vector<string>& colStrVec=jsvalueTostrVec(strVec);
auto tp=strVecToColVec(colStrVec);
vector<col*> allCols=get<0>(tp);
vector<IndexType> indTypes=get<1>(tp);
return new table(ID.toStdString(),allCols,{},true,indTypes);
}
void tableManager::removeTable(const QString& ID){
const string& tableName=ID.toStdString();
auto iter=managedTable.find(tableName);
if(iter!=managedTable.end()){
delete managedTable[tableName];
managedTable.erase(tableName);
auto it=find(managedTableName.begin(),managedTableName.end(),tableName);
managedTableName.erase(it);
}
const string& path=IO::ID_to_path(ID.toStdString());
IO::del_table_blocks(path);
}
table* tableManager::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){
return tableJoin(newTableID,lTableName,jsvalueTostrVec(lColName),rTableName,jsvalueTostrVec(rColName),lKey,rKey,joinWay);
}
void table::setIndex(const QString &colName, const QString &indType){
IndexType indtp=strToIndType(indType.toStdString());
setIndex(colName.toStdString(),indtp);
}
table* table::genNewTable(const QScriptValue& colNames, jsCollection *tupSubList){
return this->genNewTable(jsvalueTostrVec(colNames),tupSubList->getintVec());
}
void table::add(const QScriptValue& tuple){
vector<Basic*> basicVec=jsvalueToBasicVec(tuple);
this->add(basicVec);
}
void table::mod(int opSub, const QScriptValue& tuple){
vector<Basic*> basicVec=jsvalueToBasicVec(tuple);
this->mod(opSub,basicVec);
}
void table::mod(jsCollection* opSubs, const QScriptValue& tuple){
vector<Basic*> basicVec=jsvalueToBasicVec(tuple);
for(int opSub:opSubs->getintVec()){
this->mod(opSub,basicVec);
}
}
void table::del(jsCollection *allOpSub){
this->del(allOpSub->getintVec());
}
jsCollection* table::find_in(const QString& colName, jsCollection *target_vec){
const vector<int>& tmp=this->find_in(colName.toStdString(),target_vec->getbasicVec());
return new jsCollection (tmp);
}
jsCollection* table::find_in(const QString& colName,const QScriptValue& target_vec){
const vector<int>& tmp=this->find_in(colName.toStdString(),jsvalueToBasicVec(target_vec));
return new jsCollection (tmp);
}
jsCollection* table::find_not_in(const QString& colName, jsCollection *target_vec){
const vector<int>& tmp=this->find_not_in(colName.toStdString(),target_vec->getbasicVec());
return new jsCollection (tmp);
}
jsCollection* table::find_not_in(const QString& colName,const QScriptValue& target_vec){
const vector<int>& tmp=this->find_not_in(colName.toStdString(),jsvalueToBasicVec(target_vec));
return new jsCollection (tmp);
}
jsCollection* table::find(const QScriptValue& allExp){
const vector<string>& StrVec=jsvalueTostrVec(allExp);
return new jsCollection(find(StrVec));
}
col* table::getCol(const QString &colName){
return getCol(this->findCol({colName.toStdString()})[0]);
}
int table::getColIndex(const QString &colName){
return this->getColIndex(colName.toStdString());
}
jsCollection* col::getData(jsCollection *filtered_index){
return new jsCollection(this->getData(filtered_index->getintVec()));
}
col* col::genNewCol(jsCollection *subList){
return this->genNewCol(subList->getintVec());
}
const jsCollection* col::getallData(){
return new jsCollection (this->allData);
}
Basic* typeHelper::strToBasic(const QString &val){
string tmp=val.toStdString();
TYPE tp=judgeType(tmp);
return strToBasic(tmp,tp);
}
jsCollection* aggHelper::distinct(jsCollection *data_vec){
return new jsCollection (this->distinct(data_vec->getbasicVec()));
}
Basic* aggHelper::avg(jsCollection* data_vec){
return this->avg(data_vec->getbasicVec());
}
Basic* aggHelper::count(jsCollection *data_vec){
return this->count(data_vec->getbasicVec());
}
Basic* aggHelper::first(jsCollection *data_vec){
return this->first(data_vec->getbasicVec());
}
Basic* aggHelper::last(jsCollection *data_vec){
return this->last(data_vec->getbasicVec());
}
Basic* aggHelper::max(jsCollection* data_vec){
return this->max(data_vec->getbasicVec());
}
Basic* aggHelper::min(jsCollection* data_vec){
return this->min(data_vec->getbasicVec());
}
Basic* aggHelper::sum(jsCollection *data_vec){
return this->sum(data_vec->getbasicVec());
}
C++
1
https://gitee.com/sg-first/SG-Database.git
git@gitee.com:sg-first/SG-Database.git
sg-first
SG-Database
SG-Database
master

搜索帮助