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
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
/*
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)
case "relation":
relation := JSONQuery.Relations[element.pointer]
query += relationToQuery(relation)
case "function":
function := JSONQuery.GroupBys[element.pointer]
query += functionToQuery(function)
case "filter":
filter := JSONQuery.Filters[element.pointer]
query += filterToQuery(filter)
}
}
}
}
func entityToQuery(element entity.QueryEntityStruct) string {
thisname := fmt.Sprintf("e%v", element.ID)
ret := createLetFor(thisname, element.Name)
return ret
}
func relationToQuery(element entity.QueryRelationStruct) string {
thisname := fmt.Sprintf("e%v", element.ID)
ret := createLetFor(thisname, element.Name)
return ret
}
func functionToQuery(element entity.QueryGroupByStruct) string {
thisname := fmt.Sprintf("e%v", element.ID)
ret := createLetFor(thisname)
return ret
}
func filterToQuery(element entity.QueryFilterStruct) string {
thisname := fmt.Sprintf("e%v", element.ID)
ret := createLetFor(thisname, fmt.Sprintf("e%v", element.FilteredID))
return ret
}
func createLetFor(variableName string, enumerableName string) string {
return "LET " + variableName + " = (\n\tFOR x IN " + enumerableName + "\n"
}