(resp http.ResponseWriter, req *http.Request)
| 39 | ) |
| 40 | |
| 41 | func (qh *QueryHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { |
| 42 | timing := servertiming.FromContext(req.Context()) |
| 43 | |
| 44 | timingQueryParsed := timing.NewMetric("query_parsed"). |
| 45 | WithDesc("Query has been parsed"). |
| 46 | Start() |
| 47 | |
| 48 | requestPayload, err := parseQueryRequestPayload[QueryRequestPayload](req) |
| 49 | if err != nil { |
| 50 | http.Error(resp, err.Error(), http.StatusBadRequest) |
| 51 | |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | timingQueryParsed.Stop() |
| 56 | |
| 57 | timingQueryBuilt := timing.NewMetric("query_built"). |
| 58 | WithDesc("The SQL query has been built"). |
| 59 | Start() |
| 60 | |
| 61 | query, paramMap, err := requestPayload.generateSQL(req.Context(), qh.Database.Schema) |
| 62 | if err != nil { |
| 63 | http.Error(resp, err.Error(), http.StatusBadRequest) |
| 64 | |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | timingQueryBuilt.Stop() |
| 69 | |
| 70 | timingQueryExecute := timing.NewMetric("sql_exec"). |
| 71 | WithDesc("SQL query execution time"). |
| 72 | Start() |
| 73 | |
| 74 | // actually execute the query against the database and collect the result |
| 75 | var result []map[string]interface{} |
| 76 | if err := qh.Database.Execute( |
| 77 | req.Context(), |
| 78 | query, |
| 79 | orm.WithNamedArgs(paramMap), |
| 80 | orm.WithResult(&result), |
| 81 | orm.WithSchema(*qh.Database.Schema), |
| 82 | ); err != nil { |
| 83 | http.Error(resp, failedQuery+err.Error(), http.StatusInternalServerError) |
| 84 | |
| 85 | return |
| 86 | } |
| 87 | timingQueryExecute.Stop() |
| 88 | |
| 89 | // send the HTTP status code |
| 90 | resp.WriteHeader(http.StatusOK) |
| 91 | |
| 92 | // prepare the result encoder. |
| 93 | enc := json.NewEncoder(resp) |
| 94 | enc.SetEscapeHTML(false) |
| 95 | enc.SetIndent("", " ") |
| 96 | |
| 97 | // prepare the result body that, in dev mode, contains |
| 98 | // some diagnostics data about the query |
nothing calls this directly
no test coverage detected