Better handling for variables callback
Some checks are pending
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Waiting to run
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Waiting to run
Build , Vet Test, and Lint / Lint Code (push) Waiting to run
Build , Vet Test, and Lint / Build (push) Waiting to run
Tests / Unit Tests (push) Waiting to run
Tests / Integration Tests (push) Waiting to run

This commit is contained in:
Hein 2025-12-11 15:57:01 +02:00
parent c50eeac5bf
commit 7ef1d6424a

View File

@ -20,15 +20,23 @@ import (
// Handler handles function-based SQL API requests // Handler handles function-based SQL API requests
type Handler struct { type Handler struct {
db common.Database db common.Database
hooks *HookRegistry hooks *HookRegistry
variablesCallback func(r *http.Request) map[string]interface{}
} }
type SqlQueryOptions struct { type SqlQueryOptions struct {
GetVariablesCallback func(w http.ResponseWriter, r *http.Request) map[string]interface{} NoCount bool
NoCount bool BlankParams bool
BlankParams bool AllowFilter bool
AllowFilter bool }
func NewSqlQueryOptions() SqlQueryOptions {
return SqlQueryOptions{
NoCount: false,
BlankParams: true,
AllowFilter: true,
}
} }
// NewHandler creates a new function API handler // NewHandler creates a new function API handler
@ -45,6 +53,14 @@ func (h *Handler) GetDatabase() common.Database {
return h.db return h.db
} }
func (h *Handler) SetVariablesCallback(callback func(r *http.Request) map[string]interface{}) {
h.variablesCallback = callback
}
func (h *Handler) GetVariablesCallback() func(r *http.Request) map[string]interface{} {
return h.variablesCallback
}
// Hooks returns the hook registry for this handler // Hooks returns the hook registry for this handler
// Use this to register custom hooks for operations // Use this to register custom hooks for operations
func (h *Handler) Hooks() *HookRegistry { func (h *Handler) Hooks() *HookRegistry {
@ -77,9 +93,7 @@ func (h *Handler) SqlQueryList(sqlquery string, options SqlQueryOptions) HTTPFun
inputvars := make([]string, 0) inputvars := make([]string, 0)
metainfo := make(map[string]interface{}) metainfo := make(map[string]interface{})
variables := make(map[string]interface{}) variables := make(map[string]interface{})
if options.GetVariablesCallback != nil {
variables = options.GetVariablesCallback(w, r)
}
complexAPI := false complexAPI := false
// Get user context from security package // Get user context from security package
@ -416,9 +430,7 @@ func (h *Handler) SqlQuery(sqlquery string, options SqlQueryOptions) HTTPFuncTyp
inputvars := make([]string, 0) inputvars := make([]string, 0)
metainfo := make(map[string]interface{}) metainfo := make(map[string]interface{})
variables := make(map[string]interface{}) variables := make(map[string]interface{})
if options.GetVariablesCallback != nil {
variables = options.GetVariablesCallback(w, r)
}
dbobj := make(map[string]interface{}) dbobj := make(map[string]interface{})
complexAPI := false complexAPI := false
@ -644,8 +656,18 @@ func (h *Handler) extractInputVariables(sqlquery string, inputvars *[]string) st
// mergePathParams merges URL path parameters into the SQL query // mergePathParams merges URL path parameters into the SQL query
func (h *Handler) mergePathParams(r *http.Request, sqlquery string, variables map[string]interface{}) string { func (h *Handler) mergePathParams(r *http.Request, sqlquery string, variables map[string]interface{}) string {
// Note: Path parameters would typically come from a router like gorilla/mux
// For now, this is a placeholder for path parameter extraction if h.GetVariablesCallback() != nil {
pathVars := h.GetVariablesCallback()(r)
for k, v := range pathVars {
kword := fmt.Sprintf("[%s]", k)
if strings.Contains(sqlquery, kword) {
sqlquery = strings.ReplaceAll(sqlquery, kword, fmt.Sprintf("%v", v))
}
variables[k] = v
}
}
return sqlquery return sqlquery
} }