sqlflags

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package sqlflags implements the shared CLI flag grammar for the SQL-verb commands (select, insert, update, delete, drop).

Each parser is a pure function. Each flag-registration helper takes a *cobra.Command and adds the flag with the documented metadata. The package is the single source of truth for:

  • --where comparison operators (==, ===, !=, !==, >=, <=, >, <)
  • --set YAML-inferred assignments
  • --unset comma-separated field removal list
  • --id collection/key targeting (single-record mode)
  • --from collection targeting (set mode)
  • --into collection targeting (insert only)
  • --all full-collection scope guard
  • --min-affected positive-integer count threshold
  • --order-by comma-separated, '-' prefix for descending
  • --fields '*', '$id', or comma-separated projection

Mode resolution (single-record vs set) is handled by ResolveMode. Applicability checks (which verb accepts which flag) are handled by the Reject* helpers. Authoritative spec: spec/features/shared-cli-flags/README.md

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MinAffectedFromCmd

func MinAffectedFromCmd(cmd *cobra.Command) (int, bool, error)

MinAffectedFromCmd reads --min-affected from cmd, returning the parsed value, a "supplied" boolean, and any error. When the flag is not supplied, returns (0, false, nil). When supplied with N >= 1, returns (N, true, nil). When supplied with N < 1, returns an error.

Verb commands should prefer this helper over GetInt because it distinguishes "not supplied" (no threshold check) from "explicit 0" (which is rejected by the spec).

func ParseFields

func ParseFields(s string) ([]string, error)

ParseFields parses --fields. Returns nil for "*" or empty (meaning "all fields"). Otherwise returns the trimmed comma-separated list, preserving order. Empty entries (stray commas) are rejected for consistency with ParseOrderBy and ParseUnset.

func ParseMinAffected

func ParseMinAffected(s string) (int, error)

ParseMinAffected parses --min-affected=N into a positive integer. N must be >= 1; zero and negative values are rejected.

func ParseUnset

func ParseUnset(s string) ([]string, error)

ParseUnset parses a comma-separated --unset field list. Each field must be non-empty, contain no '=', and contain no whitespace inside the name.

func RegisterAllFlag

func RegisterAllFlag(cmd *cobra.Command)

RegisterAllFlag adds --all. Used by update, delete.

func RegisterFieldsFlag

func RegisterFieldsFlag(cmd *cobra.Command)

RegisterFieldsFlag adds --fields -f. Used by select.

func RegisterFromFlag

func RegisterFromFlag(cmd *cobra.Command)

RegisterFromFlag adds --from. Used by select, update, delete.

func RegisterIDFlag

func RegisterIDFlag(cmd *cobra.Command)

RegisterIDFlag adds --id. Used by select, update, delete.

func RegisterIntoFlag

func RegisterIntoFlag(cmd *cobra.Command)

RegisterIntoFlag adds --into. Used by insert only.

func RegisterMinAffectedFlag

func RegisterMinAffectedFlag(cmd *cobra.Command)

RegisterMinAffectedFlag adds --min-affected. Used by select, update, delete.

func RegisterOrderByFlag

func RegisterOrderByFlag(cmd *cobra.Command)

RegisterOrderByFlag adds --order-by. Used by select.

func RegisterSetFlag

func RegisterSetFlag(cmd *cobra.Command)

RegisterSetFlag adds repeatable --set. Used by update.

func RegisterUnsetFlag

func RegisterUnsetFlag(cmd *cobra.Command)

RegisterUnsetFlag adds repeatable --unset. Used by update.

func RegisterWhereFlag

func RegisterWhereFlag(cmd *cobra.Command)

RegisterWhereFlag adds repeatable --where -w. Used by select, update, delete in set mode.

func RejectSetModeFlags

func RejectSetModeFlags(f SetModeFlags, mode Mode) error

RejectSetModeFlags enforces the cross-flag rules that depend on the resolved Mode.

In ModeID (single-record): --where, --all, and --min-affected MUST all be absent.

In ModeFrom (set): exactly one of --where or --all MUST be supplied; neither and both are rejected. --min-affected is unconstrained at this layer (it has its own validation in ParseMinAffected and its own applicability rule against ModeID).

func RejectSetUnsetSameField

func RejectSetUnsetSameField(sets []Assignment, unsets []string) error

RejectSetUnsetSameField enforces that no field name appears in both --set and --unset within the same invocation.

Types

type Assignment

type Assignment struct {
	Field string
	Value any
}

Assignment is one parsed --set expression.

func ParseSet

func ParseSet(s string) (Assignment, error)

ParseSet parses one --set expression: `field=value`. Comparison operators between field and value are rejected.

type Condition

type Condition struct {
	Field string
	Op    Operator
	Value any
}

Condition is the parsed form of one --where expression.

func ParseWhere

func ParseWhere(s string) (Condition, error)

ParseWhere parses one --where expression. The bare `=` operator is rejected (spec: req:comparison-operators).

type Mode

type Mode int

Mode is the verb operating mode.

const (
	ModeInvalid Mode = iota
	ModeID           // single-record (--id supplied)
	ModeFrom         // set (--from supplied)
)

func ResolveMode

func ResolveMode(idFlag, fromFlag string) (Mode, error)

ResolveMode returns the operating mode for a verb based on its --id and --from flag values. Empty string means "not supplied". Supplying both or neither is rejected.

type Operator

type Operator int

Operator identifies a --where comparison.

const (
	OpInvalid Operator = iota
	OpLooseEq
	OpStrictEq
	OpLooseNeq
	OpStrictNeq
	OpGt
	OpLt
	OpGte
	OpLte
)

func (Operator) IsStrict

func (o Operator) IsStrict() bool

IsStrict reports whether the operator preserves operand types (=== or !==).

type OrderTerm

type OrderTerm struct {
	Field      string
	Descending bool
}

OrderTerm is one parsed --order-by entry.

func ParseOrderBy

func ParseOrderBy(s string) ([]OrderTerm, error)

ParseOrderBy parses a comma-separated --order-by list. A leading '-' indicates descending order for that field. An empty input returns nil with no error.

type SetModeFlags

type SetModeFlags struct {
	WhereSupplied       bool
	AllSupplied         bool
	MinAffectedSupplied bool
}

SetModeFlags carries the boolean presence of set-mode-only flags for applicability checking. Verb-specific flags (--limit, --fields) remain the verb's concern; this helper covers only the shared shape governed by shared-cli-flags.

Jump to

Keyboard shortcuts

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