ExecuteScriptQuery runs a script and returns the result bytes.
(rq *http.Request, queriesPath string, script string)
| 58 | |
| 59 | // ExecuteScriptQuery runs a script and returns the result bytes. |
| 60 | func (h *ScriptHandler) ExecuteScriptQuery(rq *http.Request, queriesPath string, script string) ([]byte, error) { |
| 61 | h.db.SetDatabase(h.pgDB) |
| 62 | sqlPath, err := h.scripts.GetScript(rq.Method, queriesPath, script) |
| 63 | if err != nil { |
| 64 | err = fmt.Errorf("could not get script %s/%s, %v", queriesPath, script, err) |
| 65 | return nil, err |
| 66 | } |
| 67 | |
| 68 | templateData := make(map[string]interface{}) |
| 69 | extractHeaders(rq, templateData) |
| 70 | extractQueryParameters(rq, templateData) |
| 71 | |
| 72 | sql, values, err := h.scripts.ParseScript(sqlPath, templateData) |
| 73 | if err != nil { |
| 74 | err = fmt.Errorf("could not parse script %s/%s, %v", queriesPath, script, err) |
| 75 | return nil, err |
| 76 | } |
| 77 | |
| 78 | sc := h.executor.ExecuteScriptsCtx(rq.Context(), rq.Method, sql, values) |
| 79 | if sc.Err() != nil { |
| 80 | err = fmt.Errorf("could not execute sql, check your prest logs") |
| 81 | return nil, err |
| 82 | } |
| 83 | |
| 84 | return sc.Bytes(), nil |
| 85 | } |
| 86 | |
| 87 | // extractHeaders gets from the given request the headers and populate the provided templateData accordingly. |
| 88 | func extractHeaders(rq *http.Request, templateData map[string]interface{}) { |