From 41cf477881998fba716bbb9e8b83be6e69b1dbf7 Mon Sep 17 00:00:00 2001 From: Kieran van Gaalen <kieran.van.gaalen@casema.nl> Date: Fri, 12 Nov 2021 16:01:59 +0100 Subject: [PATCH] indentation and query fixes --- aql/convertQuery.go | 71 ++++++++++++++++++++++++++++++++++----------- realtest.json | 10 +++---- 2 files changed, 59 insertions(+), 22 deletions(-) diff --git a/aql/convertQuery.go b/aql/convertQuery.go index e6d5368..e749289 100644 --- a/aql/convertQuery.go +++ b/aql/convertQuery.go @@ -76,39 +76,42 @@ createQuery generates a query based on the json file provided Return: *string, a string containing the corresponding AQL query and an error */ func createQuery(JSONQuery *entity.IncomingQueryJSON, tree []entity.Tree, topNode entity.QueryEntityStruct) *string { - output := createLetFor("result", fmt.Sprintf("e_%v", topNode.ID), topNode.Name) + output := createLetFor("result", fmt.Sprintf("e_%v", topNode.ID), topNode.Name, 0) for constraint := range topNode.Constraints { output += createFilter(topNode.Constraints[constraint], fmt.Sprintf("e_%v", topNode.ID)) } - subQuery, subName := createQueryRecurse(JSONQuery, tree, 0, topNode) + subQuery, subName := createQueryRecurse(JSONQuery, tree, 0, topNode, 1) subNames := []string{subName} output += subQuery output += createZeroFilter(append(subNames, fmt.Sprintf("e_%v", topNode.ID))) output += createReturn(fmt.Sprintf("e_%v", topNode.ID), "", subNames) + output += ")\n" output += "let nodes = union_distinct(flatten(result[**].nodes),[])\nlet edges = union_distinct(flatten(result[**].rel),[])\nreturn {\"vertices\":nodes,\"edges\":edges}" return &output } -func createQueryRecurse(JSONQuery *entity.IncomingQueryJSON, tree []entity.Tree, currentindex int, topNode entity.QueryEntityStruct) (string, string) { +func createQueryRecurse(JSONQuery *entity.IncomingQueryJSON, tree []entity.Tree, currentindex int, topNode entity.QueryEntityStruct, indentindex int) (string, string) { currentTree := tree[currentindex] newNode := getTreeNewNode(currentTree, tree, topNode) - output := createLetFor(fmt.Sprintf("e%v", newNode.ID), fmt.Sprintf("e_%v", newNode.ID), newNode.Name) - output += fmt.Sprintf("FOR r%v IN %v", currentTree.Self.Rel.ID, currentTree.Self.Rel.Name) + output := "" + output += fixIndent(createLetFor(fmt.Sprintf("e%v", newNode.ID), fmt.Sprintf("e_%v", newNode.ID), newNode.Name, indentindex), indentindex) + output += fixIndent(fmt.Sprintf("\tFOR r%v IN %v\n", currentTree.Self.Rel.ID, currentTree.Self.Rel.Name), indentindex) for constraint := range newNode.Constraints { - output += createFilter(newNode.Constraints[constraint], fmt.Sprintf("e_%v", newNode.ID)) + output += fixIndent(createFilter(newNode.Constraints[constraint], fmt.Sprintf("e_%v", newNode.ID)), indentindex) } for constraint := range currentTree.Self.Rel.QueryConstraintStruct { - output += createFilter(currentTree.Self.Rel.QueryConstraintStruct[constraint], fmt.Sprintf("r%v", currentTree.Self.Rel.ID)) + output += fixIndent(createFilter(currentTree.Self.Rel.QueryConstraintStruct[constraint], fmt.Sprintf("r%v", currentTree.Self.Rel.ID)), indentindex) } - output += fmt.Sprintf("FILTER r%v._from == e_%v._id AND r%v._to == e_%v._id", currentTree.Self.Rel.ID, currentTree.Self.FromNode.ID, currentTree.Self.Rel.ID, currentTree.Self.ToNode.ID) + output += fixIndent(fmt.Sprintf("\tFILTER r%v._from == e_%v._id AND r%v._to == e_%v._id\n", currentTree.Self.Rel.ID, currentTree.Self.FromNode.ID, currentTree.Self.Rel.ID, currentTree.Self.ToNode.ID), indentindex) var subNames []string for i := range currentTree.Children { - subQuery, subName := createQueryRecurse(JSONQuery, tree, currentTree.Children[i], topNode) + subQuery, subName := createQueryRecurse(JSONQuery, tree, currentTree.Children[i], topNode, indentindex+1) output += subQuery subNames = append(subNames, subName) } - output += createZeroFilter(append(subNames, fmt.Sprintf("e_%v", newNode.ID), fmt.Sprintf("r%v", currentTree.Self.Rel.ID))) - output += createReturn(fmt.Sprintf("e_%v", newNode.ID), fmt.Sprintf("r%v", currentTree.Self.Rel.ID), subNames) + output += fixIndent(createZeroFilter(append(subNames, fmt.Sprintf("e_%v", newNode.ID), fmt.Sprintf("r%v", currentTree.Self.Rel.ID))), indentindex) + output += fixIndent(createReturn(fmt.Sprintf("e_%v", newNode.ID), fmt.Sprintf("r%v", currentTree.Self.Rel.ID), subNames), indentindex) + output += fixIndent(")\n", indentindex) return output, fmt.Sprintf("e%v", newNode.ID) } @@ -126,27 +129,52 @@ func getTreeNewNode(currentTree entity.Tree, tree []entity.Tree, topNode entity. } } -func createLetFor(variableName string, forName string, enumerableName string) string { - return "LET " + variableName + " = (\n\tFOR " + forName + " IN " + enumerableName + "\n" +func createLetFor(variableName string, forName string, enumerableName string, indentindex int) string { + output := "LET " + variableName + " = (\n" + for i := 0; i < indentindex; i++ { + output += "\t" + } + output += "\tFOR " + forName + " IN " + enumerableName + "\n" + return output } func createFilter(constraint entity.QueryConstraintStruct, filtered string) string { - return "\tFILTER + " + filtered + constraint.Attribute + " " + wordsToLogicalSign(constraint) + " " + constraint.Value + " \n" + output := "\tFILTER " + filtered + "." + constraint.Attribute + " " + wordsToLogicalSign(constraint) + if constraint.DataType == "string" { + output += " \"" + } else { + output += " " + } + if constraint.MatchType == "contains" { + output += "%" + } + output += constraint.Value + if constraint.MatchType == "contains" { + output += "%" + } + if constraint.DataType == "string" { + output += "\" " + } else { + output += " " + } + output += " \n" + return output } func createZeroFilter(subNames []string) string { - output := "FILTER" + output := "\tFILTER" for i := range subNames { output += fmt.Sprintf(" length(%v) != 0", subNames[i]) if i < len(subNames)-1 { output += " AND" } } + output += "\n" return output } func createReturn(nodeName string, relName string, subNames []string) string { - output := "RETURN {\"nodes\":union_distinct(" + output := "\tRETURN {\"nodes\":union_distinct(" for i := range subNames { output += fmt.Sprintf("flatten(%v[**].nodes), ", subNames[i]) } @@ -162,7 +190,7 @@ func createReturn(nodeName string, relName string, subNames []string) string { if len(subNames) == 0 { output += ", []" } - output += ")}\n)\n" + output += ")}\n" return output } @@ -205,3 +233,12 @@ func wordsToLogicalSign(element entity.QueryConstraintStruct) string { } return match } + +func fixIndent(input string, indentCount int) string { + output := "" + for i := 0; i < indentCount; i++ { + output += "\t" + } + output += input + return output +} diff --git a/realtest.json b/realtest.json index 46028cc..bfb7485 100644 --- a/realtest.json +++ b/realtest.json @@ -22,8 +22,8 @@ { "attribute": "name", "value": "Geert", - "dataType": "text", - "matchType": "like" + "dataType": "string", + "matchType": "contains" } ] }, @@ -44,7 +44,7 @@ { "attribute": "seats", "value": "10", - "dataType": "number", + "dataType": "int", "matchType": "LT" } ] @@ -56,8 +56,8 @@ { "attribute": "date", "value": "mei", - "dataType": "text", - "matchType": "like" + "dataType": "string", + "matchType": "contains" } ] } -- GitLab