func RunQuery(query string, ses graph.Session) { nResults := 0 startTrace, startTime := trace("Elapsed time: %g ms\n\n") defer func() { if nResults > 0 { un(startTrace, startTime) } }() fmt.Printf("\n") c := make(chan interface{}, 5) go ses.ExecInput(query, c, 100) for res := range c { fmt.Print(ses.ToText(res)) nResults++ } if nResults > 0 { fmt.Printf("-----------\n%d Results\n", nResults) } }
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: } } }