2016-04-01 20:58:15 +08:00
|
|
|
/*=============================================================================
|
2016-05-16 03:16:22 +08:00
|
|
|
# Filename: QueryTree.cpp
|
2016-04-01 20:58:15 +08:00
|
|
|
# Author: Jiaqi, Chen
|
2016-05-16 03:16:22 +08:00
|
|
|
# Mail: chenjiaqi93@163.com
|
2016-04-01 20:58:15 +08:00
|
|
|
# Last Modified: 2016-03-02 20:35
|
|
|
|
# Description: implement functions in QueryTree.h
|
|
|
|
=============================================================================*/
|
|
|
|
|
|
|
|
#include "QueryTree.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::GroupPattern::FilterTreeNode::getVarset(Varset &varset)
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
for (int i = 0; i < (int)this->child.size(); i++)
|
|
|
|
{
|
|
|
|
if (this->child[i].type == 's' && this->child[i].arg[0] == '?')
|
|
|
|
varset.addVar(this->child[i].arg);
|
|
|
|
if (this->child[i].type == 't')
|
|
|
|
this->child[i].node.getVarset(varset);
|
|
|
|
}
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::GroupPattern::FilterTreeNode::print(vector<GroupPattern> &exist_grouppatterns, int dep)
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Not_type) printf("!");
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Builtin_regex_type) printf("regex");
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Builtin_lang_type) printf("lang");
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Builtin_langmatches_type) printf("langmatches");
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Builtin_bound_type) printf("bound");
|
|
|
|
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Builtin_in_type)
|
|
|
|
{
|
|
|
|
if (this->child[0].type == 's') printf("%s", this->child[0].arg.c_str());
|
|
|
|
printf(" in (");
|
|
|
|
for (int i = 1; i < (int)this->child.size(); i++)
|
|
|
|
{
|
|
|
|
if (i != 1) printf(" , ");
|
|
|
|
if (this->child[i].type == 's') printf("%s", this->child[i].arg.c_str());
|
|
|
|
}
|
|
|
|
printf(")");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Builtin_exists_type)
|
|
|
|
{
|
|
|
|
printf("exists");
|
|
|
|
exist_grouppatterns[this->exists_grouppattern_id].print(dep);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("(");
|
|
|
|
|
|
|
|
if ((int)this->child.size() >= 1)
|
|
|
|
{
|
|
|
|
if (this->child[0].type == 's') printf("%s", this->child[0].arg.c_str());
|
|
|
|
if (this->child[0].type == 't') this->child[0].node.print(exist_grouppatterns, dep);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Or_type) printf(" || ");
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::And_type) printf(" && ");
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Equal_type) printf(" = ");
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::NotEqual_type) printf(" != ");
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Less_type) printf(" < ");
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::LessOrEqual_type) printf(" <= ");
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Greater_type) printf(" > ");
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::GreaterOrEqual_type) printf(" >= ");
|
|
|
|
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Builtin_regex_type || this->type == QueryTree::GroupPattern::FilterTreeNode::Builtin_langmatches_type) printf(", ");
|
|
|
|
|
|
|
|
if ((int)this->child.size() >= 2)
|
|
|
|
{
|
|
|
|
if (this->child[1].type == 's') printf("%s", this->child[1].arg.c_str());
|
|
|
|
if (this->child[1].type == 't') this->child[1].node.print(exist_grouppatterns, dep);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((int)this->child.size() >= 3)
|
|
|
|
{
|
|
|
|
if (this->type == QueryTree::GroupPattern::FilterTreeNode::Builtin_regex_type && this->child[2].type == 's')
|
|
|
|
printf(", %s", this->child[2].arg.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(")");
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void QueryTree::GroupPattern::addOnePattern(Pattern _pattern)
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
this->patterns.push_back(_pattern);
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::GroupPattern::addOneGroupUnion()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
this->unions.push_back(GroupPatternUnions((int)this->patterns.size() - 1));
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::GroupPattern::addOneUnion()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
int n = (int)this->unions.size();
|
|
|
|
this->unions[n - 1].grouppattern_vec.push_back(GroupPattern());
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
QueryTree::GroupPattern& QueryTree::GroupPattern::getLastUnion()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
int n = (int)this->unions.size();
|
|
|
|
int m = (int)this->unions[n - 1].grouppattern_vec.size();
|
|
|
|
return this->unions[n - 1].grouppattern_vec[m - 1];
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::GroupPattern::addOneOptionalOrMinus(char _type)
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
this->optionals.push_back(OptionalOrMinusGroupPattern((int)this->patterns.size() - 1, (int)this->unions.size() - 1, _type));
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
QueryTree::GroupPattern& QueryTree::GroupPattern::getLastOptionalOrMinus()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
int n = (int)this->optionals.size();
|
|
|
|
return this->optionals[n - 1].grouppattern;
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::GroupPattern::addOneFilterTree()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
this->filters.push_back(FilterTreeRoot());
|
|
|
|
this->filter_exists_grouppatterns.push_back(vector<GroupPattern>());
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
QueryTree::GroupPattern::FilterTreeNode& QueryTree::GroupPattern::getLastFilterTree()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
return this->filters[(int)(this->filters.size()) - 1].root;
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::GroupPattern::addOneExistsGroupPattern()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
int n = (int)this->filter_exists_grouppatterns.size();
|
|
|
|
this->filter_exists_grouppatterns[n - 1].push_back(GroupPattern());
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
QueryTree::GroupPattern& QueryTree::GroupPattern::getLastExistsGroupPattern()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
int n = (int)this->filter_exists_grouppatterns.size();
|
|
|
|
int m = (int)this->filter_exists_grouppatterns[n - 1].size();
|
|
|
|
return this->filter_exists_grouppatterns[n - 1][m - 1];
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::GroupPattern::getVarset()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
for (int i = 0; i < (int)this->patterns.size(); i++)
|
|
|
|
{
|
|
|
|
if (this->patterns[i].subject.value[0] == '?')
|
|
|
|
this->patterns[i].varset.addVar(this->patterns[i].subject.value);
|
|
|
|
if (this->patterns[i].object.value[0] == '?')
|
|
|
|
this->patterns[i].varset.addVar(this->patterns[i].object.value);
|
|
|
|
this->grouppattern_resultset_minimal_varset = this->grouppattern_resultset_minimal_varset + this->patterns[i].varset;
|
|
|
|
this->grouppattern_resultset_maximal_varset = this->grouppattern_resultset_maximal_varset + this->patterns[i].varset;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)this->unions.size(); i++)
|
|
|
|
{
|
|
|
|
Varset minimal_varset;
|
|
|
|
for (int j = 0; j < (int)this->unions[i].grouppattern_vec.size(); j++)
|
|
|
|
{
|
|
|
|
this->unions[i].grouppattern_vec[j].getVarset();
|
|
|
|
if (j == 0) minimal_varset = minimal_varset + this->unions[i].grouppattern_vec[j].grouppattern_resultset_minimal_varset;
|
|
|
|
else minimal_varset = minimal_varset * this->unions[i].grouppattern_vec[j].grouppattern_resultset_minimal_varset;
|
|
|
|
this->grouppattern_resultset_maximal_varset = this->grouppattern_resultset_maximal_varset + this->unions[i].grouppattern_vec[j].grouppattern_resultset_maximal_varset;
|
|
|
|
}
|
|
|
|
this->grouppattern_resultset_minimal_varset = this->grouppattern_resultset_minimal_varset + minimal_varset;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)this->optionals.size(); i++)
|
|
|
|
{
|
|
|
|
this->optionals[i].grouppattern.getVarset();
|
|
|
|
if (this->optionals[i].type == 'o')
|
|
|
|
this->grouppattern_resultset_maximal_varset = this->grouppattern_resultset_maximal_varset + this->optionals[i].grouppattern.grouppattern_resultset_maximal_varset;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)this->filters.size(); i++)
|
|
|
|
{
|
|
|
|
this->filters[i].root.getVarset(this->filters[i].varset);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < (int)this->filter_exists_grouppatterns.size(); i++)
|
|
|
|
for (int j = 0; j < (int)this->filter_exists_grouppatterns[i].size(); j++)
|
|
|
|
{
|
|
|
|
this->filter_exists_grouppatterns[i][j].getVarset();
|
|
|
|
}
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
bool QueryTree::GroupPattern::checkOnlyUnionOptionalFilterNoExists()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
for (int i = 0; i < (int)this->unions.size(); i++)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < (int)this->unions[i].grouppattern_vec.size(); j++)
|
|
|
|
if (!this->unions[i].grouppattern_vec[j].checkOnlyUnionOptionalFilterNoExists())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)this->optionals.size(); i++)
|
|
|
|
{
|
|
|
|
if (this->optionals[i].type != 'o')
|
|
|
|
return false;
|
|
|
|
if (!this->optionals[i].grouppattern.checkOnlyUnionOptionalFilterNoExists())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)this->filter_exists_grouppatterns.size(); i++)
|
|
|
|
if ((int)this->filter_exists_grouppatterns[i].size() != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
pair<Varset, Varset> QueryTree::GroupPattern::checkOptionalGroupPatternVarsAndSafeFilter(Varset occur , Varset ban, bool &check_condition)
|
|
|
|
//return occur varset and ban varset
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
if (!check_condition) return make_pair(Varset(), Varset());
|
|
|
|
|
|
|
|
Varset this_ban;
|
|
|
|
|
|
|
|
int lastpattern = -1, lastunions = -1, lastoptional = -1;
|
|
|
|
while (check_condition && (lastpattern + 1 < (int)this->patterns.size() || lastunions + 1 < (int)this->unions.size() || lastoptional + 1 < (int)this->optionals.size()))
|
|
|
|
{
|
|
|
|
if (lastoptional + 1 < (int)this->optionals.size() && this->optionals[lastoptional + 1].lastpattern == lastpattern && this->optionals[lastoptional + 1].lastunions == lastunions)
|
|
|
|
//optional
|
|
|
|
{
|
|
|
|
pair<Varset, Varset> sub_grouppattern_return_varset = this->optionals[lastoptional + 1].grouppattern.checkOptionalGroupPatternVarsAndSafeFilter(Varset(), ban, check_condition);
|
|
|
|
|
|
|
|
if (occur.hasCommonVar(sub_grouppattern_return_varset.second))
|
|
|
|
check_condition = false;
|
|
|
|
|
|
|
|
Varset out = this->optionals[lastoptional + 1].grouppattern.grouppattern_resultset_maximal_varset - occur;
|
|
|
|
occur = occur + sub_grouppattern_return_varset.first;
|
|
|
|
this_ban = this_ban + sub_grouppattern_return_varset.second;
|
|
|
|
this_ban = this_ban + out;
|
|
|
|
ban = ban + this_ban;
|
|
|
|
|
|
|
|
lastoptional++;
|
|
|
|
}
|
|
|
|
else if (lastunions + 1 < (int)this->unions.size() && this->unions[lastunions + 1].lastpattern == lastpattern)
|
|
|
|
//union
|
|
|
|
{
|
|
|
|
Varset sub_grouppattern_occur, sub_grouppattern_ban;
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)this->unions[lastunions + 1].grouppattern_vec.size(); i++)
|
|
|
|
{
|
|
|
|
pair<Varset, Varset> sub_grouppattern_result = this->unions[lastunions + 1].grouppattern_vec[i].checkOptionalGroupPatternVarsAndSafeFilter(occur, ban, check_condition);
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
sub_grouppattern_occur = sub_grouppattern_occur + sub_grouppattern_result.first;
|
|
|
|
else
|
|
|
|
sub_grouppattern_occur = sub_grouppattern_occur * sub_grouppattern_result.first;
|
|
|
|
sub_grouppattern_ban = sub_grouppattern_ban + sub_grouppattern_result.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
occur = occur + sub_grouppattern_occur;
|
|
|
|
this_ban = this_ban + sub_grouppattern_ban;
|
|
|
|
ban = ban + this_ban;
|
|
|
|
|
|
|
|
lastunions++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
//triple pattern
|
|
|
|
{
|
|
|
|
if (this->patterns[lastpattern + 1].varset.hasCommonVar(ban))
|
|
|
|
check_condition = false;
|
|
|
|
|
|
|
|
occur = occur + this->patterns[lastpattern + 1].varset;
|
|
|
|
|
|
|
|
lastpattern++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//filter
|
|
|
|
for (int i = 0; i < (int)this->filters.size(); i++)
|
|
|
|
if (!this->filters[i].varset.belongTo(occur))
|
|
|
|
{
|
|
|
|
check_condition = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_pair(occur, this_ban);
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::GroupPattern::initPatternBlockid()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
for (int i = 0; i < (int)this->patterns.size(); i++)
|
|
|
|
this->pattern_blockid.push_back(i);
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
int QueryTree::GroupPattern::getRootPatternBlockid(int x)
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
if (this->pattern_blockid[x] == x) return x;
|
|
|
|
this->pattern_blockid[x] = getRootPatternBlockid(this->pattern_blockid[x]);
|
|
|
|
return this->pattern_blockid[x];
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::GroupPattern::mergePatternBlockid(int x, int y)
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
int px = getRootPatternBlockid(x);
|
|
|
|
int py = getRootPatternBlockid(y);
|
|
|
|
this->pattern_blockid[px] = py;
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::GroupPattern::print(int dep)
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
for (int t = 0; t < dep; t++) printf("\t"); printf("{\n");
|
|
|
|
|
|
|
|
int lastpattern = -1, lastunions = -1, lastoptional = -1;
|
|
|
|
while (lastpattern + 1 < (int)this->patterns.size() || lastunions + 1 < (int)this->unions.size() || lastoptional + 1 < (int)this->optionals.size())
|
|
|
|
{
|
|
|
|
if (lastoptional + 1 < (int)this->optionals.size() && this->optionals[lastoptional + 1].lastpattern == lastpattern && this->optionals[lastoptional + 1].lastunions == lastunions)
|
|
|
|
//optional
|
|
|
|
{
|
|
|
|
for (int t = 0; t <= dep; t++) printf("\t");
|
|
|
|
if (this->optionals[lastoptional + 1].type == 'o') printf("OPTIONAL\n");
|
|
|
|
if (this->optionals[lastoptional + 1].type == 'm') printf("MINUS\n");
|
|
|
|
|
|
|
|
this->optionals[lastoptional + 1].grouppattern.print(dep + 1);
|
|
|
|
lastoptional++;
|
|
|
|
}
|
|
|
|
else if (lastunions + 1 < (int)this->unions.size() && this->unions[lastunions + 1].lastpattern == lastpattern)
|
|
|
|
//union
|
|
|
|
{
|
|
|
|
for (int i = 0; i < (int)this->unions[lastunions + 1].grouppattern_vec.size(); i++)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
for (int t = 0; t <= dep; t++) printf("\t"); printf("UNION\n");
|
|
|
|
}
|
|
|
|
this->unions[lastunions + 1].grouppattern_vec[i].print(dep + 1);
|
|
|
|
}
|
|
|
|
lastunions++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
//triple pattern
|
|
|
|
{
|
|
|
|
for (int t = 0; t <= dep; t++) printf("\t");
|
|
|
|
printf("%s\t%s\t%s.\n", this->patterns[lastpattern + 1].subject.value.c_str(), this->patterns[lastpattern + 1].predicate.value.c_str(), this->patterns[lastpattern + 1].object.value.c_str());
|
|
|
|
lastpattern++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//filter
|
|
|
|
for (int i = 0; i < (int)this->filters.size(); i++)
|
|
|
|
{
|
|
|
|
for (int t = 0; t <= dep; t++) printf("\t"); printf("FILTER\t");
|
|
|
|
this->filters[i].root.print(this->filter_exists_grouppatterns[i], dep + 1);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int t = 0; t < dep; t++) printf("\t"); printf("}\n");
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void QueryTree::setQueryForm(QueryForm _queryform)
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
this->query_form = _queryform;
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
QueryTree::QueryForm QueryTree::getQueryForm()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
return this->query_form;
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::setProjectionModifier(ProjectionModifier _projection_modifier)
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
projection_modifier = _projection_modifier;
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
QueryTree::ProjectionModifier QueryTree::getProjectionModifier()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
return this->projection_modifier;
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::addProjectionVar(string _projection)
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
this->projection.addVar(_projection);
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
int QueryTree::getProjectionNum()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
return (int)this->projection.varset.size();
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
Varset& QueryTree::getProjection()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
return this->projection;
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::setProjectionAsterisk()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
this->projection_asterisk = true;
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
bool QueryTree::checkProjectionAsterisk()
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
return this->projection_asterisk;
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
void QueryTree::addOrder(string &_var, bool _descending)
|
2016-04-01 20:58:15 +08:00
|
|
|
{
|
2016-05-16 03:16:22 +08:00
|
|
|
this->order.push_back(Order(_var, _descending));
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<QueryTree::Order>& QueryTree::getOrder()
|
|
|
|
{
|
|
|
|
return this->order;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueryTree::setOffset(int _offset)
|
|
|
|
{
|
|
|
|
this->offset = _offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
int QueryTree::getOffset()
|
|
|
|
{
|
|
|
|
return this->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueryTree::setLimit(int _limit)
|
|
|
|
{
|
|
|
|
this->limit = _limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
int QueryTree::getLimit()
|
|
|
|
{
|
|
|
|
return this->limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryTree::GroupPattern& QueryTree::getGroupPattern()
|
|
|
|
{
|
|
|
|
return this->grouppattern;
|
2016-04-01 20:58:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:16:22 +08:00
|
|
|
bool QueryTree::checkWellDesigned()
|
|
|
|
{
|
|
|
|
if (!this->getGroupPattern().checkOnlyUnionOptionalFilterNoExists())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool check_condition = true;
|
|
|
|
this->getGroupPattern().checkOptionalGroupPatternVarsAndSafeFilter(Varset(), Varset(), check_condition);
|
|
|
|
return check_condition;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueryTree::print()
|
|
|
|
{
|
|
|
|
for (int j = 0; j < 80; j++) printf("="); printf("\n");
|
|
|
|
|
|
|
|
if (this->getQueryForm() == QueryTree::Select_Query)
|
|
|
|
{
|
|
|
|
printf("select");
|
|
|
|
if (this->getProjectionModifier() == QueryTree::Modifier_Distinct)
|
|
|
|
printf(" distinct");
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("var is : \t");
|
|
|
|
vector <string> &varvec = this->getProjection().varset;
|
|
|
|
for (int i = 0; i < (int)varvec.size(); i++)
|
|
|
|
printf("%s\t", varvec[i].c_str());
|
|
|
|
if (this->checkProjectionAsterisk())
|
|
|
|
printf("*");
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
else printf("ask\n");
|
|
|
|
|
|
|
|
this->getGroupPattern().print(0);
|
|
|
|
|
|
|
|
if ((int)this->getOrder().size() > 0)
|
|
|
|
{
|
|
|
|
printf("order by : \t");
|
|
|
|
|
|
|
|
vector<QueryTree::Order>&order = this->getOrder();
|
|
|
|
for (int i = 0; i < (int)order.size(); i++)
|
|
|
|
{
|
|
|
|
if (!order[i].descending) printf("ASC(");
|
|
|
|
else printf("DESC(");
|
|
|
|
printf("%s) ", order[i].var.c_str());
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
if (this->getOffset() != 0)
|
|
|
|
printf("offset : %d\n", this->getOffset());
|
|
|
|
if (this->getLimit() != -1)
|
|
|
|
printf("limit : %d\n", this->getLimit());
|
|
|
|
|
|
|
|
for (int j = 0; j < 80; j++) printf("="); printf("\n");
|
|
|
|
}
|