func (s *Server) v1QueryGet(w http.ResponseWriter, r *http.Request) { ctx := r.Context() values := r.URL.Query() pretty := getPretty(r.URL.Query()["pretty"]) explainMode := getExplain(r.URL.Query()["explain"]) qStrs := values["q"] if len(qStrs) == 0 { handleErrorf(w, 400, "missing query parameter 'q'") return } qStr := qStrs[len(qStrs)-1] txn, err := s.store.NewTransaction(ctx) if err != nil { handleErrorAuto(w, err) return } defer s.store.Close(ctx, txn) compiler := s.Compiler() query, err := ast.ParseBody(qStr) if err != nil { handleCompileError(w, err) return } compiled, err := compiler.QueryCompiler().Compile(query) if err != nil { handleCompileError(w, err) return } results, err := s.execQuery(ctx, compiler, txn, compiled, explainMode) if err != nil { handleErrorAuto(w, err) return } handleResponseJSON(w, 200, results, pretty) }
func (s *Server) indexGet(w http.ResponseWriter, r *http.Request) { renderHeader(w) renderBanner(w) renderVersion(w) values := r.URL.Query() qStrs := values["q"] explainMode := getExplain(r.URL.Query()["explain"]) ctx := r.Context() renderQueryForm(w, qStrs, explainMode) if len(qStrs) > 0 { qStr := qStrs[len(qStrs)-1] t0 := time.Now() var results interface{} txn, err := s.store.NewTransaction(ctx) if err == nil { var query ast.Body query, err = ast.ParseBody(qStr) if err == nil { compiler := s.Compiler() query, err = compiler.QueryCompiler().Compile(query) if err == nil { results, err = s.execQuery(ctx, compiler, txn, query, explainMode) } } s.store.Close(ctx, txn) } dt := time.Since(t0) renderQueryResult(w, results, err, dt) } renderFooter(w) }
func ExampleQueryCompiler_Compile() { // Define an input module that will be compiled. exampleModule := ` package opa.example import data.foo import request.bar p[x] :- foo[x], not bar[x], x >= min_x min_x = 100 ` // Parse the input module to obtain the AST representation. mod, err := ast.ParseModule("my_module", exampleModule) if err != nil { fmt.Println("Parse error:", err) } // Create a new compiler instance and compile the module. c := ast.NewCompiler() mods := map[string]*ast.Module{ "my_module": mod, } if c.Compile(mods); c.Failed() { fmt.Println("Compile error:", c.Errors) } // Obtain the QueryCompiler from the compiler instance. Note, we will // compile this query within the context of the opa.example package and // declare that a query input named "queryinput" must be supplied. qc := c.QueryCompiler(). WithContext( ast.NewQueryContext( // Note, the ast.MustParse<X> functions are meant for test // purposes only. They will panic if an error occurs. Prefer the // ast.Parse<X> functions that return meaningful error messages // instead. ast.MustParsePackage("package opa.example"), ast.MustParseImports("import request.queryinput"), )) // Parse the input query to obtain the AST representation. query, err := ast.ParseBody("p[x], x < queryinput") if err != nil { fmt.Println("Parse error:", err) } compiled, err := qc.Compile(query) if err != nil { fmt.Println("Compile error:", err) } fmt.Println("Compiled:", compiled) // Output: // // Compiled: data.opa.example.p[x], lt(x, request.queryinput) }