Newer
Older
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
/*
This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course.
© Copyright Utrecht University (Department of Information and Computing Sciences)
*/
package aql
import (
"errors"
"fmt"
"git.science.uu.nl/graphpolaris/query-conversion/entity"
)
// Version 1.13
/*
ConvertQuery converts an IncomingQueryJSON object into AQL
JSONQuery: *entity.IncomingQueryJSON, the query to be converted to AQL
Returns: *string, the AQL query and a possible error
*/
func (s *Service) ConvertQuery(JSONQuery *entity.IncomingQueryJSON) (*string, error) {
// Check to make sure all indexes exist
// The largest possible id for an entity
largestEntityID := len(JSONQuery.Entities) - 1
// The largest possible id for a relation
largestRelationID := len(JSONQuery.Relations) - 1
// Make sure no entity should be returned that is outside the range of that list
for _, e := range JSONQuery.Return.Entities {
// If this entity references an entity that is outside the range
if e > largestEntityID || e < 0 {
return nil, errors.New("non-existing entity referenced in return")
}
}
// Make sure that no relation mentions a non-existing entity
for _, r := range JSONQuery.Relations {
if r.EntityFrom > largestEntityID || r.EntityTo > largestEntityID {
return nil, errors.New("non-exisiting entity referenced in relation")
}
}
// Make sure no non-existing relation is tried to be returned
for _, r := range JSONQuery.Return.Relations {
if r > largestRelationID || r < 0 {
return nil, errors.New("non-existing relation referenced in return")
}
}
result := createQuery(JSONQuery)
return result, nil
}
/*
createQuery generates a query based on the json file provided
JSONQuery: *entity.IncomingQueryJSON, this is a parsedJSON struct holding all the data needed to form a query,
Return: *string, a string containing the corresponding AQL query and an error
*/
func createQuery(JSONQuery *entity.IncomingQueryJSON) *string {
query := ""
for list := range listoflists {
for index := range listoflists[list] {
element := listoflists[list][index]
switch element.typename {
case "entity":
entity := JSONQuery.Entities[element.pointer]
query += entityToQuery(entity, JSONQuery)
case "relation":
relation := JSONQuery.Relations[element.pointer]
query += relationToQuery(relation, JSONQuery)
case "function":
function := JSONQuery.GroupBys[element.pointer]
query += functionToQuery(function, JSONQuery)
case "filter":
filter := JSONQuery.Filters[element.pointer]
query += filterToQuery(filter, JSONQuery)
func entityToQuery(element entity.QueryEntityStruct, JSONQuery *entity.IncomingQueryJSON) string {
thisname := fmt.Sprintf("e%v", element.ID)
ret := createLetFor(thisname, element.Name)
func relationToQuery(element entity.QueryRelationStruct, JSONQuery *entity.IncomingQueryJSON) string {
thisname := fmt.Sprintf("r%v", element.ID)
ret := createLetFor(thisname, element.Name)
ret += fmt.Sprintf("\tFOR y in %v%v\n")
func functionToQuery(element entity.QueryGroupByStruct, JSONQuery *entity.IncomingQueryJSON) string {
thisname := fmt.Sprintf("g%v", element.ID)
ret := createLetFor(thisname)
return ret
}
func filterToQuery(element entity.QueryFilterStruct, JSONQuery *entity.IncomingQueryJSON) string {
thisname := fmt.Sprintf("f%v", element.ID)
ret := createLetFor(thisname, fmt.Sprintf("%v%v", typeToPrefix(element.FromType), element.FromID))
ret += fmt.Sprintf("\tFILTER x.%v %v %v\n", element.Attribute, wordsToLogicalSign((element.MatchType)), element.Value)
ret += "\tRETURN x\n)"
return ret
}
func createLetFor(variableName string, enumerableName string) string {
return "LET " + variableName + " = (\n\tFOR x IN " + enumerableName + "\n"
}
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
func typeToPrefix(pillType string) string {
switch pillType {
case "entity":
return "e"
case "relation":
return "r"
case "function":
return "g"
case "filter":
return "f"
default:
return ""
}
}
func tryGetFilter(toType string, toID int, JSONQuery *entity.IncomingQueryJSON) *entity.QueryFilterStruct {
for i := range JSONQuery.Filters {
filter := JSONQuery.Filters[i]
if filter.ToType == toType && filter.ToID == toID {
return &filter
}
}
return nil
}
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 if word == "NEQ" {
return "!="
} else {
return ">"
}
}