query

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package query implements the Raven query language parser and executor.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AncestorPredicate

type AncestorPredicate struct {
	Target   string // Specific target ID (mutually exclusive with SubQuery)
	SubQuery *Query // An object query (mutually exclusive with Target)
	// contains filtered or unexported fields
}

AncestorPredicate filters by any ancestor matching. Syntax: ancestor:{object:type ...}, ancestor:[[target]], ancestor:_

func (AncestorPredicate) Negated

func (b AncestorPredicate) Negated() bool

type ArrayQuantifierPredicate

type ArrayQuantifierPredicate struct {
	Quantifier  ArrayQuantifierType
	Field       string    // The array field to iterate
	ElementPred Predicate // Predicate to test against each element (uses _ as element reference)
	// contains filtered or unexported fields
}

ArrayQuantifierPredicate represents an array quantifier predicate. Syntax: any(.tags, _ == "urgent"), all(.tags, startswith(_, "feature-"))

func (ArrayQuantifierPredicate) Negated

func (b ArrayQuantifierPredicate) Negated() bool

type ArrayQuantifierType

type ArrayQuantifierType int

ArrayQuantifierType represents the type of array quantifier.

const (
	ArrayQuantifierAny  ArrayQuantifierType = iota // any(.field, predicate) - any element matches
	ArrayQuantifierAll                             // all(.field, predicate) - all elements match
	ArrayQuantifierNone                            // none(.field, predicate) - no element matches
)

func (ArrayQuantifierType) String

func (aq ArrayQuantifierType) String() string

type AtPredicate

type AtPredicate struct {
	Target   string // Specific trait ID (if referencing a known trait)
	SubQuery *Query // A trait query to match against
	// contains filtered or unexported fields
}

AtPredicate filters traits by co-location (same file:line). Syntax: at:{trait:name ...}, at:[[target]], at:_ For traits only - matches traits at the same file and line.

func (AtPredicate) Negated

func (b AtPredicate) Negated() bool

type ChildPredicate

type ChildPredicate struct {
	Target   string // Specific target ID (mutually exclusive with SubQuery)
	SubQuery *Query // An object query (mutually exclusive with Target)
	// contains filtered or unexported fields
}

ChildPredicate filters by having a direct child matching. Syntax: child:{object:type ...}, child:[[target]], child:_

func (ChildPredicate) Negated

func (b ChildPredicate) Negated() bool

type CompareOp

type CompareOp int

CompareOp represents a comparison operator.

const (
	CompareEq  CompareOp = iota // == (equals)
	CompareNeq                  // != (not equals)
	CompareLt                   // <
	CompareGt                   // >
	CompareLte                  // <=
	CompareGte                  // >=
)

func (CompareOp) String

func (op CompareOp) String() string

type ContainsPredicate

type ContainsPredicate struct {
	SubQuery *Query // A trait query
	// contains filtered or unexported fields
}

ContainsPredicate filters objects by whether they contain matching traits anywhere in their subtree (self or any descendant object). Syntax: encloses(trait:name ...)

func (ContainsPredicate) Negated

func (b ContainsPredicate) Negated() bool

type ContentPredicate

type ContentPredicate struct {
	SearchTerm string // The search term or phrase
	// contains filtered or unexported fields
}

ContentPredicate filters objects by full-text search on their content. Syntax: content("search terms"), content("exact phrase")

func (ContentPredicate) Negated

func (b ContentPredicate) Negated() bool

type DescendantPredicate

type DescendantPredicate struct {
	Target   string // Specific target ID (mutually exclusive with SubQuery)
	SubQuery *Query // An object query (mutually exclusive with Target)
	// contains filtered or unexported fields
}

DescendantPredicate filters by having any descendant matching (at any depth). Syntax: descendant:{object:type ...}, descendant:[[target]], descendant:_

func (DescendantPredicate) Negated

func (b DescendantPredicate) Negated() bool

type ElementEqualityPredicate

type ElementEqualityPredicate struct {
	Value      string
	CompareOp  CompareOp // == or !=
	IsRefValue bool      // true if the value came from a [[ref]] token
	// contains filtered or unexported fields
}

ElementEqualityPredicate represents _ == value or _ != value within array context. Syntax: _ == "urgent", _ != "deprecated"

func (ElementEqualityPredicate) Negated

func (b ElementEqualityPredicate) Negated() bool

type Executor

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

Executor executes queries against the database.

func NewExecutor

func NewExecutor(db *sql.DB) *Executor

NewExecutor creates a new query executor.

func (*Executor) Execute

func (e *Executor) Execute(queryStr string) (interface{}, error)

Execute parses and executes a query string, returning either object or trait results.

func (*Executor) ExecuteObjectQuery

func (e *Executor) ExecuteObjectQuery(q *Query) ([]model.Object, error)

ExecuteObjectQuery executes an object query and returns matching objects.

func (*Executor) ExecuteTraitQuery

func (e *Executor) ExecuteTraitQuery(q *Query) ([]model.Trait, error)

ExecuteTraitQuery executes a trait query and returns matching traits.

func (*Executor) SetDailyDirectory

func (e *Executor) SetDailyDirectory(dir string)

SetDailyDirectory sets the daily directory used when building a fallback resolver.

This matters for date shorthand references like [[2026-01-01]], which resolve to <dailyDirectory>/2026-01-01.

func (*Executor) SetResolver

func (e *Executor) SetResolver(r *resolver.Resolver)

SetResolver injects a resolver for target resolution.

This allows callers (CLI/MCP) to provide a canonical resolver that includes aliases and vault-specific settings like daily directory. If not set, the executor will fall back to building a resolver from the objects table.

func (*Executor) SetSchema

func (e *Executor) SetSchema(sch *schema.Schema)

SetSchema injects a schema for type-aware query semantics.

type FieldPredicate

type FieldPredicate struct {
	Field      string
	Value      string    // "*" means "exists"
	IsExists   bool      // true if Value is "*"
	CompareOp  CompareOp // comparison operator (==, !=, <, >, <=, >=)
	IsRefValue bool      // true if the value came from a [[ref]] token
	// contains filtered or unexported fields
}

FieldPredicate filters by object field value. Syntax: .field==value, .field>value, exists(.field) For string matching, use StringFuncPredicate (contains, startswith, endswith, matches).

func (FieldPredicate) Negated

func (b FieldPredicate) Negated() bool

type GroupPredicate

type GroupPredicate struct {
	Predicates []Predicate
	// contains filtered or unexported fields
}

GroupPredicate represents a parenthesized group of predicates. Used for explicit precedence control.

func (GroupPredicate) Negated

func (b GroupPredicate) Negated() bool

type HasPredicate

type HasPredicate struct {
	SubQuery *Query // A trait query
	// contains filtered or unexported fields
}

HasPredicate filters objects by whether they contain matching traits. Syntax: has(trait:name .value==...)

func (HasPredicate) Negated

func (b HasPredicate) Negated() bool

type Lexer

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

Lexer tokenizes a query string.

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer for the given input.

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

NextToken returns the next token from the input.

type NotPredicate

type NotPredicate struct {
	Inner Predicate
}

NotPredicate represents the negation of a predicate. Syntax: !(pred), !pred

func (NotPredicate) Negated

func (NotPredicate) Negated() bool

type OnPredicate

type OnPredicate struct {
	Target   string // Specific target ID (mutually exclusive with SubQuery)
	SubQuery *Query // An object query (mutually exclusive with Target)
	// contains filtered or unexported fields
}

OnPredicate filters traits by direct parent object. Syntax: on(object:type ...), on([[target]]), on(_)

func (OnPredicate) Negated

func (b OnPredicate) Negated() bool

type OrPredicate

type OrPredicate struct {
	Predicates []Predicate
	// contains filtered or unexported fields
}

OrPredicate represents an OR combination of two or more predicates. Syntax: (pred1 | pred2 | pred3)

func (OrPredicate) Negated

func (b OrPredicate) Negated() bool

type ParentPredicate

type ParentPredicate struct {
	Target   string // Specific target ID (mutually exclusive with SubQuery)
	SubQuery *Query // An object query (mutually exclusive with Target)
	// contains filtered or unexported fields
}

ParentPredicate filters by direct parent matching. Syntax: parent:{object:type ...}, parent:[[target]], parent:_

func (ParentPredicate) Negated

func (b ParentPredicate) Negated() bool

type Parser

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

Parser parses query strings into Query ASTs.

type Predicate

type Predicate interface {
	Negated() bool
	// contains filtered or unexported methods
}

Predicate represents a filter condition in a query.

type Query

type Query struct {
	Type      QueryType
	TypeName  string    // Object type or trait name
	Predicate Predicate // Filter to apply (may be nil)
}

Query represents a parsed query.

func Parse

func Parse(input string) (*Query, error)

Parse parses a query string and returns a Query AST.

type QueryType

type QueryType int

QueryType represents the type of query (object or trait).

const (
	QueryTypeObject QueryType = iota
	QueryTypeTrait
)

type RefdPredicate

type RefdPredicate struct {
	Target   string // Specific source ID
	SubQuery *Query // Query matching the sources that reference this
	// contains filtered or unexported fields
}

RefdPredicate filters objects/traits by what references them (inverse of refs:). Syntax: refd(object:type ...), refd(trait:name ...), refd([[target]]), refd(target)

func (RefdPredicate) Negated

func (b RefdPredicate) Negated() bool

type RefsPredicate

type RefsPredicate struct {
	Target   string // Specific target like "projects/website" (mutually exclusive with SubQuery)
	SubQuery *Query // Subquery to match targets (mutually exclusive with Target)
	// contains filtered or unexported fields
}

RefsPredicate filters objects by what they reference. Syntax: refs([[target]]), refs(target), refs(object:type ...)

func (RefsPredicate) Negated

func (b RefsPredicate) Negated() bool

type StringFuncPredicate

type StringFuncPredicate struct {
	FuncType      StringFuncType
	Field         string // Field name (without .) or "_" for array element
	Value         string // The string to match against
	CaseSensitive bool   // If true, match is case-sensitive (default: false)
	IsElementRef  bool   // True if Field is "_" (array element reference)
	// contains filtered or unexported fields
}

StringFuncPredicate represents a string function predicate. Syntax: contains(.field, "value"), startswith(.field, "value"), etc. Can also be used with _ as the field for array element context.

func (StringFuncPredicate) Negated

func (b StringFuncPredicate) Negated() bool

type StringFuncType

type StringFuncType int

StringFuncType represents the type of string function.

const (
	StringFuncIncludes   StringFuncType = iota // contains(.field, "str") - substring match
	StringFuncStartsWith                       // startswith(.field, "str")
	StringFuncEndsWith                         // endswith(.field, "str")
	StringFuncMatches                          // matches(.field, "pattern") - regex match
)

func (StringFuncType) String

func (sf StringFuncType) String() string

type Token

type Token struct {
	Type    TokenType
	Value   string
	Pos     int
	Literal string // original text for references
}

Token represents a lexer token.

type TokenType

type TokenType int

TokenType represents the type of a lexer token.

const (
	TokenEOF        TokenType = iota
	TokenIdent                // identifiers like "object", "trait", "project", "due"
	TokenColon                // :
	TokenDot                  // .
	TokenBang                 // !
	TokenPipe                 // |
	TokenPipeline             // |>
	TokenLParen               // (
	TokenRParen               // )
	TokenLBrace               // {
	TokenRBrace               // }
	TokenLBracket             // [
	TokenRBracket             // ]
	TokenStar                 // *
	TokenRef                  // [[...]] reference
	TokenString               // "quoted string" for content search
	TokenRegex                // /pattern/ for regex matching
	TokenUnderscore           // _ (result reference)
	TokenLt                   // <
	TokenGt                   // >
	TokenLte                  // <=
	TokenGte                  // >=
	TokenEq                   // =
	TokenEqEq                 // ==
	TokenBangEq               // !=
	TokenComma                // ,
	TokenError                // error token
)

type ValidationError

type ValidationError struct {
	Message    string
	Suggestion string
}

ValidationError represents a query validation error.

func (*ValidationError) Error

func (e *ValidationError) Error() string

type Validator

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

Validator validates queries against a schema.

func NewValidator

func NewValidator(sch *schema.Schema) *Validator

NewValidator creates a new query validator.

func (*Validator) Validate

func (v *Validator) Validate(q *Query) error

Validate checks a parsed query against the schema. Returns a ValidationError if the query references undefined types, traits, or fields.

type ValuePredicate deprecated

type ValuePredicate struct {
	Value     string
	CompareOp CompareOp // comparison operator (==, !=, <, >, <=, >=)
	// contains filtered or unexported fields
}

ValuePredicate filters traits by value.

Deprecated: Use FieldPredicate with Field="value" instead. The parser now creates FieldPredicate for .value== syntax. This type is retained for internal SQL generation helpers.

func (ValuePredicate) Negated

func (b ValuePredicate) Negated() bool

type WithinPredicate

type WithinPredicate struct {
	Target   string // Specific target ID (mutually exclusive with SubQuery)
	SubQuery *Query // An object query (mutually exclusive with Target)
	// contains filtered or unexported fields
}

WithinPredicate filters traits by any ancestor object. Syntax: within(object:type ...), within([[target]]), within(_)

func (WithinPredicate) Negated

func (b WithinPredicate) Negated() bool

Jump to

Keyboard shortcuts

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