(resp http.ResponseWriter, req *http.Request)
| 124 | } |
| 125 | |
| 126 | func (batch *BatchQueryHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { |
| 127 | timing := servertiming.FromContext(req.Context()) |
| 128 | |
| 129 | timingQueryParsed := timing.NewMetric("query_parsed"). |
| 130 | WithDesc("Query has been parsed"). |
| 131 | Start() |
| 132 | |
| 133 | requestPayload, err := parseQueryRequestPayload[BatchQueryRequestPayload](req) |
| 134 | if err != nil { |
| 135 | http.Error(resp, err.Error(), http.StatusBadRequest) |
| 136 | |
| 137 | return |
| 138 | } |
| 139 | |
| 140 | timingQueryParsed.Stop() |
| 141 | |
| 142 | response := make(map[string][]map[string]any, len(*requestPayload)) |
| 143 | |
| 144 | batches := make([]BatchExecute, 0, len(*requestPayload)) |
| 145 | |
| 146 | for key, query := range *requestPayload { |
| 147 | |
| 148 | timingQueryBuilt := timing.NewMetric("query_built_" + key). |
| 149 | WithDesc("The SQL query has been built"). |
| 150 | Start() |
| 151 | |
| 152 | sql, paramMap, err := query.generateSQL(req.Context(), batch.Database.Schema) |
| 153 | if err != nil { |
| 154 | http.Error(resp, err.Error(), http.StatusBadRequest) |
| 155 | |
| 156 | return |
| 157 | } |
| 158 | |
| 159 | timingQueryBuilt.Stop() |
| 160 | |
| 161 | var result []map[string]any |
| 162 | batches = append(batches, BatchExecute{ |
| 163 | ID: key, |
| 164 | SQL: sql, |
| 165 | Params: paramMap, |
| 166 | Result: &result, |
| 167 | }) |
| 168 | } |
| 169 | |
| 170 | timingQueryExecute := timing.NewMetric("sql_exec"). |
| 171 | WithDesc("SQL query execution time"). |
| 172 | Start() |
| 173 | |
| 174 | status := http.StatusOK |
| 175 | if err := batch.Database.ExecuteBatch(req.Context(), batches); err != nil { |
| 176 | status = http.StatusInternalServerError |
| 177 | |
| 178 | var merr *multierror.Error |
| 179 | if errors.As(err, &merr) { |
| 180 | for _, e := range merr.Errors { |
| 181 | resp.Header().Add("X-Query-Error", e.Error()) |
| 182 | } |
| 183 | } else { |
nothing calls this directly
no test coverage detected