diff --git a/internal/usecases/convertquery/aql.go b/internal/usecases/convertquery/aql.go index bc33f4aae307bcb01ead940889e184cc6417e99c..4bb9f00d0b7e7e24816c53b671b81b1a15266de7 100644 --- a/internal/usecases/convertquery/aql.go +++ b/internal/usecases/convertquery/aql.go @@ -595,3 +595,304 @@ func convertJSONToStruct(jsonMsg *[]byte) (*parsedJSON, error) { // toPrint := createQueryConstraint(*con) // fmt.Println(*toPrint) // } + +func createQuery(jsQuery *parsedJSON) *string { + // GROTE SIDENOTE: + // Vrij zeker dat een query waar alléén edges worden opgevraagd (#4) + // niet wordt gesupport door zowel de result parser als de frontend reciever + + jsonQuery := *jsQuery + + // TODO: + // NODES + // EDGES (als ze er zijn) + // RETURN STATEMENT + +} + +func createNodeLet(node *entityStruct, name *string) *string { + header := fmt.Sprintf("LET %s = (\n\tFOR x IN %s \n", name, node.Type) + footer := "\nLIMIT 100\n\tRETURN x\n)\n" + + constraints := *createConstraints(&node.Constraints, false) + + ret := header + constraints + footer + return &ret +} + +func createEdgeLet(edge *relationStruct, name *string) *string { + var ( + header string + forEdge string + forSecondNode string + ) + + footer := "\n\tLIMIT 100\n\tRETURN { vertices: p.vertices[*], edges: p.edges[*] }\n)" + + // WICKED SWITCHES LETSAGO + if edge.EntityFrom != -1 { + // # 1 (2) + header = fmt.Sprintf("LET %s = (\n\tFOR x IN n%s \n", name, edge.EntityFrom) + forEdge = fmt.Sprintf("\tFOR v, e, p IN %v..%v OUTBOUND x %s \n", edge.Depth.Min, edge.Depth.Max, edge.Type) + if edge.EntityTo != -1 { + // # 2 + forSecondNode = fmt.Sprintf("\tFILTER v IN n%v \n", edge.EntityTo) + } + } else { + if edge.EntityTo != -1 { + // # 3 + header = fmt.Sprintf("LET %s = (\n\tFOR x IN n%v \n", name, edge.EntityTo) + forEdge = fmt.Sprintf("\tFOR v, e, p IN %v..%v INBOUND x %s \n", edge.Depth.Min, edge.Depth.Max, edge.Type) + } else { + // # 4 + header = fmt.Sprintf("LET %s = (\n\tFOR x IN %v \n", name, edge.Type) + footer = "\n\tLIMIT 100\n\tRETURN x\n)" + } + } + + ret := header + forEdge + forSecondNode + footer + return &ret +} + +func createConstraints(constraints *[]constraintStruct, isRelation bool) *string { + s := "" + if len(*constraints) == 0 { + return &s + } + + newLineStatement := "\tFILTER" + + for _, v := range *constraints { + s += fmt.Sprintf("%v %v \n", newLineStatement, *createConstraintLine(&v, isRelation)) + newLineStatement = "\tAND" + } + + return &s +} + +func createConstraintLine(constraint *constraintStruct, isRelation bool) *string { + var ( + match string + value string + line string + ) + + //Wicked switches letsgo + switch constraint.DataType { + case "text": + value = fmt.Sprintf("\"%s\"", constraint.Value) + switch constraint.MatchType { + case "contains": + match = "IN" + case "startswith": + match = "LIKE" + value = fmt.Sprintf("\"%s%%\"", constraint.Value) + case "endswith": + match = "LIKE" + value = fmt.Sprintf("\"_%s\"", constraint.Value) + default: //exact + match = "==" + } + case "number": + value = constraint.Value + switch constraint.MatchType { + case "GT": + match = ">" + case "LT": + match = "<" + case "GET": + match = ">=" + case "LET": + match = "<=" + default: //EQ + match = "==" + } + default: /*bool*/ + value = constraint.Value + switch constraint.MatchType { + case "NEQ": + match = "!=" + default: //EQ + match = "==" + } + } + + if isRelation { + line = fmt.Sprintf("p.edges[*].%s ALL %s %s", constraint.Attribute, match, value) + } else { + line = fmt.Sprintf("x.%s %s %s", constraint.Attribute, match, value) + } + return &line +} + +/* +#1 +{ + "Return": { + "Entities": [ + 0, + 1 + ], + "Relations": [ + 0 + ] + }, + "Entities": [ + { + "Type": "airports", + "Constraints": [] + }, + { + "Type": "airports", + "Constraints": [] + } + ], + "Relations": [ + { + "Type": "flights", + "Depth": { + "min": 1, + "max": 1 + }, + "EntityFrom": 0, + "EntityTo": 1, + "Constraints": [] + } + ] +} +*/ + +/* +#2 + +{ + "Return": { + "Entities": [ + 0 + ], + "Relations": [ + 0 + ] + }, + "Entities": [ + { + "Type": "airports", + "Constraints": [] + } + ], + "Relations": [ + { + "Type": "flights", + "Depth": { + "min": 1, + "max": 1 + }, + "EntityFrom": 0, + "EntityTo": -1, + "Constraints": [] + } + ] +} +*/ + +/* +#3 +{ + "Return": { + "Entities": [ + 0 + ], + "Relations": [ + 0 + ] + }, + "Entities": [ + { + "Type": "airports", + "Constraints": [] + } + ], + "Relations": [ + { + "Type": "flights", + "Depth": { + "min": 1, + "max": 1 + }, + "EntityFrom": -1, + "EntityTo": 0, + "Constraints": [] + } + ] +} +*/ + +/* +#4 +{ + "Return": { + "Entities": [], + "Relations": [ + 0 + ] + }, + "Entities": [], + "Relations": [ + { + "Type": "flights", + "Depth": { + "min": 1, + "max": 1 + }, + "EntityFrom": -1, + "EntityTo": -1, + "Constraints": [ + { + "Attribute": "Month", + "Value": "1", + "DataType": "number", + "MatchType": "EQ" + }, + { + "Attribute": "Day", + "Value": "15", + "DataType": "number", + "MatchType": "EQ" + } + ] + } + ] +} +*/ + +/* +#5 +{ + "Return": { + "Entities": [ + 0 + ], + "Relations": [] + }, + "Entities": [ + { + "Type": "airports", + "Constraints": [ + { + "Attribute": "city", + "Value": "New York", + "DataType": "text", + "MatchType": "exact" + }, + { + "Attribute": "country", + "Value": "USA", + "DataType": "text", + "MatchType": "exact" + } + ] + } + ], + "Relations": [] +} +*/