Help us shape the future of Go! Take the 2026 Go Developer Survey

querycontext

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendSQLComment

func AppendSQLComment(ctx context.Context, sql string) string

AppendSQLComment appends a query context SQL comment to the given SQL string if query context is present in the context. Returns the original SQL unchanged if no query context is set.

func AppendSQLLineComment added in v0.23.0

func AppendSQLLineComment(ctx context.Context, sql string) string

AppendSQLLineComment is AppendSQLComment with the context as a line comment (FormatAsSQLLineComment), for a statement whose parser rejects a trailing block comment. Prefer AppendSQLComment everywhere else: it is the form a query log reader looks for.

func WithQueryContext

func WithQueryContext(ctx context.Context, qc QueryContext) context.Context

WithQueryContext attaches query context metadata to a Go context.

Types

type QueryContext

type QueryContext map[string]string

QueryContext holds key-value metadata to attach to database queries. It is used to track query provenance (e.g., originating from SYNQ, associated monitor or model) across all supported data warehouses.

func GetQueryContext

func GetQueryContext(ctx context.Context) QueryContext

GetQueryContext retrieves query context from a Go context. Returns nil if not set.

func (QueryContext) FormatAsJSON

func (qc QueryContext) FormatAsJSON() string

FormatAsJSON returns the query context as a JSON string.

func (QueryContext) FormatAsSQLComment

func (qc QueryContext) FormatAsSQLComment() string

FormatAsSQLComment returns the query context as a SQL block comment suitable for appending to a query string: /* {"key":"value"} */

The JSON payload is sanitized to prevent SQL injection: any occurrence of the comment-close sequence "*/" is neutralised by replacing "/" with its JSON Unicode escape "\u002f". The result is still valid, parseable JSON but cannot prematurely close the SQL block comment.

func (QueryContext) FormatAsSQLLineComment added in v0.23.0

func (qc QueryContext) FormatAsSQLLineComment() string

FormatAsSQLLineComment returns the query context as a SQL line comment on a line of its own, for a parser that rejects a block comment after the statement: \n-- {"key":"value"}

The comment cannot end early: JSON escapes every line break in a key or a value, so the payload stays on one line.

Jump to

Keyboard shortcuts

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