Skip to content
Snippets Groups Projects
convertQuery2.go 4.55 KiB
Newer Older
/*
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)
	ret += "\tRETURN x\n)"
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"
}

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 ">"
	}
}