funcspec

package
v0.0.65 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExampleAuditHook

func ExampleAuditHook(ctx *HookContext) error

ExampleAuditHook logs all queries and results for audit purposes

func ExampleCacheHook

func ExampleCacheHook(ctx *HookContext) error

ExampleCacheHook implements simple response caching

func ExampleErrorHandlingHook

func ExampleErrorHandlingHook(ctx *HookContext) error

ExampleErrorHandlingHook provides custom error handling

func ExampleLoggingHook

func ExampleLoggingHook(ctx *HookContext) error

ExampleLoggingHook logs all SQL queries before execution

func ExampleQueryModificationHook

func ExampleQueryModificationHook(ctx *HookContext) error

ExampleQueryModificationHook modifies SQL queries to add user-specific filtering

func ExampleResultFilterHook

func ExampleResultFilterHook(ctx *HookContext) error

ExampleResultFilterHook filters results after query execution

func ExampleSecurityHook

func ExampleSecurityHook(ctx *HookContext) error

ExampleSecurityHook validates user permissions before executing queries

func IsNumeric

func IsNumeric(s string) bool

IsNumeric checks if a string contains only numeric characters

func ValidSQL

func ValidSQL(input, mode string) string

ValidSQL validates and sanitizes SQL input to prevent injection mode can be: "colname", "colvalue", "select"

Types

type FilterOperator

type FilterOperator struct {
	Operator string // eq, neq, gt, lt, gte, lte, like, ilike, in, between, etc.
	Value    string
	Logic    string // AND or OR
}

FilterOperator represents a filter with operator

type HTTPFuncType

type HTTPFuncType func(http.ResponseWriter, *http.Request)

HTTPFuncType is a function type for HTTP handlers

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler handles function-based SQL API requests

func NewHandler

func NewHandler(db common.Database) *Handler

NewHandler creates a new function API handler

func (*Handler) ApplyDistinct

func (h *Handler) ApplyDistinct(sqlQuery string, params *RequestParameters) string

ApplyDistinct adds DISTINCT to SQL query if requested

func (*Handler) ApplyFieldSelection

func (h *Handler) ApplyFieldSelection(sqlQuery string, params *RequestParameters) string

ApplyFieldSelection applies column selection to SQL query

func (*Handler) ApplyFilters

func (h *Handler) ApplyFilters(sqlQuery string, params *RequestParameters) string

ApplyFilters applies all filters to the SQL query

func (*Handler) GetDatabase added in v0.0.65

func (h *Handler) GetDatabase() common.Database

GetDatabase returns the underlying database connection Implements common.SpecHandler interface

func (*Handler) Hooks

func (h *Handler) Hooks() *HookRegistry

Hooks returns the hook registry for this handler Use this to register custom hooks for operations

func (*Handler) ParseParameters

func (h *Handler) ParseParameters(r *http.Request) *RequestParameters

ParseParameters parses all parameters from request headers and query string

func (*Handler) SqlQuery

func (h *Handler) SqlQuery(sqlquery string, pBlankparms bool) HTTPFuncType

SqlQuery creates an HTTP handler that executes a SQL query and returns a single record

func (*Handler) SqlQueryList

func (h *Handler) SqlQueryList(sqlquery string, pNoCount, pBlankparms, pAllowFilter bool) HTTPFuncType

SqlQueryList creates an HTTP handler that executes a SQL query and returns a list with pagination

type HookContext

type HookContext struct {
	Context context.Context
	Handler *Handler // Reference to the handler for accessing database
	Request *http.Request
	Writer  http.ResponseWriter

	// SQL query and variables
	SQLQuery  string                 // The SQL query being executed (can be modified by hooks)
	Variables map[string]interface{} // Variables extracted from request
	InputVars []string               // Input variable placeholders found in query
	MetaInfo  map[string]interface{} // Metadata about the request
	PropQry   map[string]string      // Property query parameters

	// User context
	UserContext *security.UserContext

	// Pagination and filtering (for list queries)
	SortColumns string
	Limit       int
	Offset      int

	// Results
	Result      interface{} // Query result (single record or list)
	Total       int64       // Total count (for list queries)
	Error       error       // Error if operation failed
	ComplexAPI  bool        // Whether complex API response format is requested
	NoCount     bool        // Whether count query should be skipped
	BlankParams bool        // Whether blank parameters should be removed
	AllowFilter bool        // Whether filtering is allowed

	// Allow hooks to abort the operation
	Abort        bool   // If set to true, the operation will be aborted
	AbortMessage string // Message to return if aborted
	AbortCode    int    // HTTP status code if aborted
}

HookContext contains all the data available to a hook

type HookFunc

type HookFunc func(*HookContext) error

HookFunc is the signature for hook functions It receives a HookContext and can modify it or return an error If an error is returned, the operation will be aborted

type HookRegistry

type HookRegistry struct {
	// contains filtered or unexported fields
}

HookRegistry manages all registered hooks

func NewHookRegistry

func NewHookRegistry() *HookRegistry

NewHookRegistry creates a new hook registry

func (*HookRegistry) Clear

func (r *HookRegistry) Clear(hookType HookType)

Clear removes all hooks for the specified type

func (*HookRegistry) ClearAll

func (r *HookRegistry) ClearAll()

ClearAll removes all registered hooks

func (*HookRegistry) Count

func (r *HookRegistry) Count(hookType HookType) int

Count returns the number of hooks registered for a specific type

func (*HookRegistry) Execute

func (r *HookRegistry) Execute(hookType HookType, ctx *HookContext) error

Execute runs all hooks for the specified type in order If any hook returns an error, execution stops and the error is returned

func (*HookRegistry) GetAllHookTypes

func (r *HookRegistry) GetAllHookTypes() []HookType

GetAllHookTypes returns all hook types that have registered hooks

func (*HookRegistry) HasHooks

func (r *HookRegistry) HasHooks(hookType HookType) bool

HasHooks returns true if there are any hooks registered for the specified type

func (*HookRegistry) Register

func (r *HookRegistry) Register(hookType HookType, hook HookFunc)

Register adds a new hook for the specified hook type

func (*HookRegistry) RegisterMultiple

func (r *HookRegistry) RegisterMultiple(hookTypes []HookType, hook HookFunc)

RegisterMultiple registers a hook for multiple hook types

type HookType

type HookType string

HookType defines the type of hook to execute

const (
	// Query operation hooks (for SqlQuery - single record)
	BeforeQuery HookType = "before_query"
	AfterQuery  HookType = "after_query"

	// Query list operation hooks (for SqlQueryList - multiple records)
	BeforeQueryList HookType = "before_query_list"
	AfterQueryList  HookType = "after_query_list"

	// SQL execution hooks (just before SQL is executed)
	BeforeSQLExec HookType = "before_sql_exec"
	AfterSQLExec  HookType = "after_sql_exec"

	// Response hooks (before response is sent)
	BeforeResponse HookType = "before_response"
)

type RequestParameters

type RequestParameters struct {
	// Field selection
	SelectFields    []string
	NotSelectFields []string
	Distinct        bool

	// Filtering
	FieldFilters   map[string]string         // column -> value (exact match)
	SearchFilters  map[string]string         // column -> value (ILIKE)
	SearchOps      map[string]FilterOperator // column -> {operator, value, logic}
	CustomSQLWhere string
	CustomSQLOr    string

	// Sorting & Pagination
	SortColumns string
	Limit       int
	Offset      int

	// Advanced features
	SkipCount bool
	SkipCache bool

	// Response format
	ResponseFormat string // "simple", "detail", "syncfusion"
	ComplexAPI     bool   // true if NOT simple API
}

RequestParameters holds parsed parameters from headers and query string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL