Exemplo n.º 1
0
func (api *Api) ServeV1Shape(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
	var ses graph.HttpSession
	switch params.ByName("query_lang") {
	case "gremlin":
		ses = gremlin.NewGremlinSession(api.ts, api.config.GremlinTimeout, false)
	case "mql":
		ses = mql.NewMqlSession(api.ts)
	default:
		return FormatJson400(w, "Need a query language.")
	}
	var err error
	bodyBytes, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return FormatJson400(w, err)
	}
	code := string(bodyBytes)
	result, err := ses.InputParses(code)
	switch result {
	case graph.Parsed:
		var output []byte
		var err error
		output, err = GetQueryShape(code, ses)
		if err != nil {
			return FormatJson400(w, err)
		}
		fmt.Fprint(w, string(output))
		return 200
	case graph.ParseFail:
		return FormatJson400(w, err)
	default:
		return FormatJsonError(w, 500, "Incomplete data?")
	}
	http.Error(w, "", http.StatusNotFound)
	return http.StatusNotFound
}
Exemplo n.º 2
0
func CayleyRepl(ts graph.TripleStore, queryLanguage string, config *cfg.CayleyConfig) {
	var ses graph.Session
	switch queryLanguage {
	case "sexp":
		ses = sexp.NewSexpSession(ts)
	case "mql":
		ses = mql.NewMqlSession(ts)
	case "gremlin":
		fallthrough
	default:
		ses = gremlin.NewGremlinSession(ts, config.GremlinTimeout, true)
	}
	inputBf := bufio.NewReader(os.Stdin)
	line := ""
	for {
		if line == "" {
			fmt.Print("cayley> ")
		} else {
			fmt.Print("...       ")
		}
		l, pre, err := inputBf.ReadLine()
		if err == io.EOF {
			if line != "" {
				line = ""
			} else {
				break
			}
		}
		if err != nil {
			line = ""
		}
		if pre {
			panic("Line too long")
		}
		line += string(l)
		if line == "" {
			continue
		}
		if strings.HasPrefix(line, ":debug") {
			ses.ToggleDebug()
			fmt.Println("Debug Toggled")
			line = ""
			continue
		}
		if strings.HasPrefix(line, ":a") {
			var tripleStmt = line[3:]
			triple := nquads.ParseLineToTriple(tripleStmt)
			if triple == nil {
				fmt.Println("Not a valid triple.")
				line = ""
				continue
			}
			ts.AddTriple(triple)
			line = ""
			continue
		}
		if strings.HasPrefix(line, ":d") {
			var tripleStmt = line[3:]
			triple := nquads.ParseLineToTriple(tripleStmt)
			if triple == nil {
				fmt.Println("Not a valid triple.")
				line = ""
				continue
			}
			ts.RemoveTriple(triple)
			line = ""
			continue
		}
		result, err := ses.InputParses(line)
		switch result {
		case graph.Parsed:
			RunQuery(line, ses)
			line = ""
		case graph.ParseFail:
			fmt.Println("Error: ", err)
			line = ""
		case graph.ParseMore:
		default:
		}
	}
}