Documentation
¶
Index ¶
- func Format(input string, opts FormatOptions) (string, error)
- func FormatQuery(q *Query, opts FormatOptions) string
- func HighlightANSIText(input string) string
- func Lint(input string, cfg LintConfig) (*Query, []LintIssue)
- func OutputName(item SelectItem) string
- func Validate(q *Query, schema Schema) error
- type AggregateExpr
- type AggregateFunc
- type Analysis
- type CTE
- type CompareExpr
- type CompareOp
- type Entity
- type Expr
- type Field
- type FieldPolicy
- type FieldRequirement
- type FieldType
- type FieldUsage
- type FormatOptions
- type FormatStyle
- type HighlightStyle
- type Join
- type JoinRequirement
- type JoinType
- type LintConfig
- type LintIssue
- type LogicalExpr
- type LogicalOp
- type NotExpr
- type Operation
- type Order
- type ParseError
- type Policy
- type PolicyIssue
- type Query
- type RequirementSet
- type ResolvedSource
- type Row
- type Schema
- type SelectItem
- type Value
- type ValueType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Format ¶
func Format(input string, opts FormatOptions) (string, error)
Format parses input and returns canonical GuardSQL text.
func FormatQuery ¶
func FormatQuery(q *Query, opts FormatOptions) string
FormatQuery formats a parsed GuardSQL query.
func HighlightANSIText ¶
HighlightANSIText applies terminal ANSI colors to GuardSQL keywords and literals. It is best-effort and intended for display, not parsing.
func Lint ¶
func Lint(input string, cfg LintConfig) (*Query, []LintIssue)
Lint parses and validates a query, then applies static SaaS safety checks.
func OutputName ¶
func OutputName(item SelectItem) string
OutputName returns the result column name for a SELECT item.
Types ¶
type AggregateExpr ¶
type AggregateExpr struct {
Func AggregateFunc
Field string
Star bool
}
AggregateExpr is an aggregate function call in SELECT.
type AggregateFunc ¶
type AggregateFunc string
AggregateFunc identifies a supported aggregate function.
const ( AggCount AggregateFunc = "COUNT" AggSum AggregateFunc = "SUM" AggAvg AggregateFunc = "AVG" AggMin AggregateFunc = "MIN" AggMax AggregateFunc = "MAX" )
type Analysis ¶
type Analysis struct {
Operation Operation
Sources []ResolvedSource
Fields []FieldRequirement
Joins []JoinRequirement
Functions []AggregateFunc
CTEs []Analysis
NestedSources []Analysis
HasAggregate bool
}
Analysis is the resolved, read-only summary of a parsed query.
func Analyze ¶
Analyze resolves a parsed query against a schema and returns the resources, fields, functions, and joins the query requires. It does not mutate the query.
func (Analysis) Requirements ¶
func (a Analysis) Requirements() RequirementSet
Requirements returns a de-duplicated requirement set for authorization and policy adapters.
type CompareExpr ¶
CompareExpr compares one field to one or more literal values.
type Expr ¶
type Expr interface {
// contains filtered or unexported methods
}
Expr is implemented by all filter expression nodes.
type FieldPolicy ¶
type FieldPolicy struct {
Selectable bool
Filterable bool
Sortable bool
Groupable bool
Aggregatable bool
AggregateFuncs []AggregateFunc
Joinable bool
}
FieldPolicy controls how one field may be used in a query.
type FieldRequirement ¶
type FieldRequirement struct {
Entity string
Field string
Source string
Usage FieldUsage
Aggregate AggregateFunc
OutputName string
}
FieldRequirement describes a concrete field operation in a query.
type FieldUsage ¶
type FieldUsage string
FieldUsage describes how a query uses a field.
const ( FieldUsageSelect FieldUsage = "select" FieldUsageFilter FieldUsage = "filter" FieldUsageSort FieldUsage = "sort" FieldUsageGroup FieldUsage = "group" FieldUsageAggregate FieldUsage = "aggregate" FieldUsageJoin FieldUsage = "join" FieldUsageHaving FieldUsage = "having" )
type FormatOptions ¶
type FormatOptions struct {
Style FormatStyle
Indent string
Highlight HighlightStyle
}
FormatOptions configures GuardSQL formatting.
func DefaultFormatOptions ¶
func DefaultFormatOptions() FormatOptions
DefaultFormatOptions returns conservative multiline formatting options.
type FormatStyle ¶
type FormatStyle string
FormatStyle controls whether a query is emitted on one line or as a human-readable multiline statement.
const ( FormatMultiline FormatStyle = "multiline" FormatSingleLine FormatStyle = "singleline" )
type HighlightStyle ¶
type HighlightStyle string
HighlightStyle controls optional syntax highlighting.
const ( HighlightNone HighlightStyle = "none" HighlightANSI HighlightStyle = "ansi" )
type JoinRequirement ¶
type JoinRequirement struct {
Type JoinType
LeftEntity string
RightEntity string
RightSource string
}
JoinRequirement describes a source-to-source join used by a query.
type LintConfig ¶
type LintConfig struct {
Schema Schema
AllowedOps []Operation
MaxDepth int
MaxNodes int
MaxInValues int
RequireLimit bool
}
LintConfig defines static policy checks for user-authored GuardSQL.
type LintIssue ¶
type LintIssue struct {
Message string
}
LintIssue is a syntax, schema, or policy problem found in a query.
type LogicalExpr ¶
LogicalExpr combines two expressions with AND or OR.
type Operation ¶
type Operation string
Operation identifies the CRUD operation represented by a query.
type ParseError ¶
ParseError is returned for invalid syntax.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type Policy ¶
type Policy struct {
AllowedOps []Operation
AllowedEntities []string
AllowedJoinTypes []JoinType
AllowedFunctions []AggregateFunc
Fields map[string]map[string]FieldPolicy
AllowStar bool
AllowCTEs bool
AllowNestedSources bool
RequireLimit bool
MaxLimit int
MaxDepth int
MaxNodes int
MaxInValues int
MaxSelectItems int
MaxOrderFields int
MaxGroupFields int
MaxJoins int
MaxCTEs int
MaxSubqueryDepth int
}
Policy defines AST-level safety rules for a GuardSQL query. It is intended for structural query controls; application authorization systems can compile their decisions into this policy shape before execution.
func SafeAnalyticsPolicy ¶
SafeAnalyticsPolicy returns a deny-by-default read-only policy derived from a schema. Host applications should tighten limits and authorization further for each deployment.
type PolicyIssue ¶
type PolicyIssue struct {
Message string
}
PolicyIssue is a policy violation found in a parsed query.
func CheckAnalysisPolicy ¶
func CheckAnalysisPolicy(analysis Analysis, policy Policy) []PolicyIssue
CheckAnalysisPolicy applies policy to resolved analysis requirements. It is preferred for operational authorization because aliases, CTEs, and nested sources have already been resolved into concrete resource and field usage.
func CheckPolicy ¶
func CheckPolicy(q *Query, policy Policy) []PolicyIssue
CheckPolicy applies AST-level policy checks to a parsed GuardSQL query. It does not mutate the query.
type Query ¶
type Query struct {
Operation Operation
With []CTE
Select []SelectItem
From string
FromQuery *Query
FromAlias string
Joins []Join
Where Expr
GroupBy []string
Having Expr
Prioritize []string
OrderBy []Order
Limit int
}
Query is the parsed representation of a GuardSQL query.
type RequirementSet ¶
type RequirementSet struct {
Operations []Operation
Entities []string
Fields []FieldRequirement
Joins []JoinRequirement
Functions []AggregateFunc
}
RequirementSet is a compact authorization and policy input derived from Analysis.
type ResolvedSource ¶
type ResolvedSource struct {
Name string
Alias string
Entity string
Nested bool
CTE bool
JoinType JoinType
}
ResolvedSource identifies a query source after schema and CTE resolution.
type SelectItem ¶
type SelectItem struct {
Field string
Star bool
Aggregate *AggregateExpr
Alias string
}
SelectItem is a projected field. Star is true for SELECT *.