Skip to content
Snippets Groups Projects
Commit 18679055 authored by Geurtjens,D. (Douwe Geurtjens)'s avatar Geurtjens,D. (Douwe Geurtjens)
Browse files

ADDED: Joes's changes pushed to the proper repository

parent 1bef7859
No related branches found
No related tags found
1 merge request!1Big merge
...@@ -8,6 +8,7 @@ package aql ...@@ -8,6 +8,7 @@ package aql
import ( import (
"errors" "errors"
"fmt" "fmt"
"strconv"
"git.science.uu.nl/graphpolaris/query-conversion/entity" "git.science.uu.nl/graphpolaris/query-conversion/entity"
) )
...@@ -49,7 +50,13 @@ func (s *Service) ConvertQuery(JSONQuery *entity.IncomingQueryJSON) (*string, er ...@@ -49,7 +50,13 @@ func (s *Service) ConvertQuery(JSONQuery *entity.IncomingQueryJSON) (*string, er
} }
} }
result := createQuery(JSONQuery) var result *string
if len(JSONQuery.Functions) == 0 {
result = createQuery(JSONQuery)
} else {
result = createQueryWithFunctions(JSONQuery)
}
return result, nil return result, nil
} }
...@@ -336,3 +343,68 @@ func createRelationLetWithOnlyToEntity(relation *entity.QueryRelationStruct, nam ...@@ -336,3 +343,68 @@ func createRelationLetWithOnlyToEntity(relation *entity.QueryRelationStruct, nam
ret := header + forStatement + optionStmtn + relationFilterStmnt + footer ret := header + forStatement + optionStmtn + relationFilterStmnt + footer
return &ret return &ret
} }
type variableNameGeneratorToken struct {
token int
}
func newVariableNameGeneratorToken() *variableNameGeneratorToken {
v := variableNameGeneratorToken{token: 0}
return &v
}
func variableNameGenerator(vngt *variableNameGeneratorToken) string {
result := "variable_" + strconv.Itoa(vngt.token)
vngt.token++
return result
}
func createQueryWithFunctions(JSONQuery *entity.IncomingQueryJSON) *string {
result := ""
v := newVariableNameGeneratorToken()
for i := 0; i < len(JSONQuery.Functions); i++ {
if JSONQuery.Functions[i].Type == "groupBy" {
a := variableNameGenerator(v)
b := variableNameGenerator(v)
c := variableNameGenerator(v)
d := variableNameGenerator(v)
e := variableNameGenerator(v)
f := variableNameGenerator(v)
g := variableNameGenerator(v)
h := variableNameGenerator(v)
result += "LET " + a + " = (FOR r IN " +
JSONQuery.Relations[i].Type + " LET " + b +
" = (FOR c IN " + JSONQuery.Entities[JSONQuery.Functions[i].GroupID].Type +
" FILTER c._id == r._to return c." + JSONQuery.Functions[i].GroupAttribute + ") " +
"LET " + c + " = " + b + "[0] LET " + d + " = (" +
"FOR p in " + JSONQuery.Entities[JSONQuery.Functions[i].ByID].Type +
" FILTER p._id == r._from return p." + JSONQuery.Functions[i].ByAttribute + ") " +
"LET " + e + " = " + d + "[0] RETURN {\"" + f + "\" : " + c + ", " +
"\"" + g + "\" : " + e + "}) " +
"FOR r in " + a + " COLLECT c = r." + f + " INTO groups = r." + g + " " +
"LET " + h + " = " + JSONQuery.Functions[i].AppliedModifier + "(groups) "
if len(JSONQuery.Functions[i].Constraints) > 0 {
result += "FILTER " + h + " " + wordsToLogicalSign(JSONQuery.Functions[i].Constraints[0].MatchType) + " " + JSONQuery.Functions[i].Constraints[0].Value + " "
}
result += "RETURN {" + JSONQuery.Functions[i].GroupAttribute + " : c, " +
JSONQuery.Functions[i].AppliedModifier + "_" + JSONQuery.Functions[i].ByAttribute + " : " + h + "}"
}
}
return &result
}
func wordsToLogicalSign(word string) string {
if word == "LT" {
return "<"
} else if word == "LTE" {
return "<="
} else if word == "EQ" {
return "=="
} else if word == "GTE" {
return ">="
} else {
return ">"
}
}
...@@ -6,6 +6,7 @@ type IncomingQueryJSON struct { ...@@ -6,6 +6,7 @@ type IncomingQueryJSON struct {
Return QueryReturnStruct Return QueryReturnStruct
Entities []QueryEntityStruct Entities []QueryEntityStruct
Relations []QueryRelationStruct Relations []QueryRelationStruct
Functions []QueryFunctionStruct
// Limit is for limiting the amount of paths AQL will return in a relation let statement // Limit is for limiting the amount of paths AQL will return in a relation let statement
Limit int Limit int
Modifiers []QueryModifierStruct Modifiers []QueryModifierStruct
...@@ -33,6 +34,19 @@ type QueryRelationStruct struct { ...@@ -33,6 +34,19 @@ type QueryRelationStruct struct {
Constraints []QueryConstraintStruct Constraints []QueryConstraintStruct
} }
type QueryFunctionStruct struct {
Type string
TypeID int
GroupType string
GroupID int
GroupAttribute string
ByType string
ByID int
ByAttribute string
AppliedModifier string
Constraints []QueryConstraintStruct
}
// QueryModifierStruct encapsulates a single modifier with its corresponding constraints // QueryModifierStruct encapsulates a single modifier with its corresponding constraints
type QueryModifierStruct struct { type QueryModifierStruct struct {
Type string // SUM COUNT AVG Type string // SUM COUNT AVG
...@@ -50,11 +64,12 @@ type QuerySearchDepthStruct struct { ...@@ -50,11 +64,12 @@ type QuerySearchDepthStruct struct {
// QueryConstraintStruct holds the information of the constraint // QueryConstraintStruct holds the information of the constraint
// Constraint datatypes // Constraint datatypes
// string MatchTypes: exact/contains/startswith/endswith // string MatchTypes: exact/contains/startswith/endswith
// int MatchTypes: GT/LT/EQ // int MatchTypes: GT/LT/EQ/
// bool MatchTypes: EQ/NEQ // bool MatchTypes: EQ/NEQ
type QueryConstraintStruct struct { type QueryConstraintStruct struct {
Attribute string Attribute string
Value string Value string
DataType string DataType string
MatchType string MatchType string
FunctionPointer int
} }
...@@ -20,63 +20,73 @@ func main() { ...@@ -20,63 +20,73 @@ func main() {
queryservice := aql.NewService() queryservice := aql.NewService()
js := []byte(`{ js := []byte(`{
"databaseName": "test", "return": {
"return": { "entities": [
"entities": [
0,
1,
2,
3
],
"relations": [
0, 0,
1 1
]
},
"entities": [
{
"type": "kamerleden",
"constraints": []
},
{
"type": "partijen",
"constraints": []
}
,
{
"type": "kamerleden",
"constraints": []
},
{
"type": "commissies",
"constraints": []
}
], ],
"relations": [ "relations": [
{ 0
"type": "lid_van", ]
},
"entities": [
{
"type": "parliament",
"constraints": []
},
{
"type": "commissions",
"constraints": []
}
],
"relations": [
{
"type": "part_of",
"depth": { "depth": {
"min": 1, "min": 1,
"max": 1 "max": 1
}, },
"entityFrom": 0, "entityFrom": 0,
"entityTo": 1, "entityTo": 1,
"constraints": [] "constraints": [],
}, "functionPointer": {
{ "from": -1,
"type": "onderdeel_van", "to": -1
"depth": { }
"min": 1,
"max": 1 }
}, ],
"entityFrom": 2, "functions": [
"entityTo": 3, {
"constraints": [] "type": "groupBy",
} "typeID": 0,
], "groupType": "entity",
"limit": 5000, "groupID": 1,
"modifiers": [] "groupAttribute": "name",
}`) "byType": "entity",
"byID": 0,
"byAttribute": "age",
"appliedModifier": "AVG",
"constraints": [
{
"attribute": "age",
"value": "45",
"dataType": "number",
"matchType": "GT",
"functionPointer": {
"from": -1,
"to": -1
}
}
]
}
],
"limit": 5000,
"modifiers": [],
"databaseName": "TweedeKamer"
}
`)
var inc entity.IncomingQueryJSON var inc entity.IncomingQueryJSON
json.Unmarshal(js, &inc) json.Unmarshal(js, &inc)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment