mysql

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 29 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDatabaseRequired indicates that a connected request cannot establish a
	// deterministic session database.
	ErrDatabaseRequired = errors.New("MySQL database is required")
	// ErrInvalidDatabaseName indicates an unsafe or invalid database identifier.
	ErrInvalidDatabaseName = errors.New("invalid MySQL database name")
	// ErrDatabaseSwitchFailed indicates that USE failed on the pinned connection.
	ErrDatabaseSwitchFailed = errors.New("switch MySQL database failed")
	// ErrExplainRelationAmbiguous identifies non-fatal native plan ambiguity.
	ErrExplainRelationAmbiguous = explain.ErrExplainRelationAmbiguous
)
View Source
var ErrAgentClosed = errors.New("MySQL Agent is closed")

ErrAgentClosed means the Agent no longer accepts requests.

View Source
var ErrConnectionNotConfigured = errors.New("MySQL connection is not configured")

ErrConnectionNotConfigured means no online connection was configured.

Functions

func WithRequestBuiltinRuleSet

func WithRequestBuiltinRuleSet(name string) analysis.RequestOption

WithRequestBuiltinRuleSet selects a named builtin rule set for one Analyze or AnalyzeStatic call. Unknown names fail before analysis starts.

func WithRequestDatabase

func WithRequestDatabase(database string) analysis.RequestOption

WithRequestDatabase overrides the default database for unqualified physical relations in one request. Empty values do not clear the Agent default.

func WithRequestLanguage

func WithRequestLanguage(lang string) analysis.RequestOption

WithRequestLanguage sets the response language for a single request.

func WithRequestRuleFilter

func WithRequestRuleFilter(f optimizer.RuleFilter) analysis.RequestOption

WithRequestRuleFilter expands a filter against the current catalog for one request.

func WithRequestRuleSet

func WithRequestRuleSet(rs *optimizer.RuleSet) analysis.RequestOption

WithRequestRuleSet overrides the Agent default rule set for one request. Passing a non-nil empty RuleSet explicitly selects zero rules; passing nil clears any prior request-level selection so the Agent default applies.

Types

type Agent

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

Agent is the main entry point for SQL analysis

func NewAgent

func NewAgent(ctx context.Context, dsn string, opts ...Option) (*Agent, error)

NewAgent validates and freezes local configuration without opening a connection. An empty DSN enables offline use. Nonempty malformed DSNs fail immediately; connectivity and authentication failures are reported by online requests. If WithLocaleCatalog is not provided, it builds an agent-scoped catalog that loads built-in common/mysql locales to avoid cross-dialect collisions.

func (*Agent) Analyze

func (a *Agent) Analyze(ctx context.Context, sql string, opts ...RequestOption) (*analysis.AnalyzeResult, error)

Analyze performs connected SQL analysis using AST rules, read-only metadata, native EXPLAIN, and internal whole-query what-if planning. It never executes the supplied statement or generated DDL/DML suggestions.

The report keeps the native plan separate from internally modeled cost comparisons. Callers must inspect each evaluation's status and the declared model identity before using its estimates.

func (*Agent) AnalyzeStatic

func (a *Agent) AnalyzeStatic(ctx context.Context, sql string, opts ...RequestOption) (*analysis.AnalyzeResult, error)

AnalyzeStatic analyzes SQL text without querying database metadata or executing EXPLAIN. Its report contains AST-provable issues and no native plan or modeled whole-query evaluation. The context is accepted for symmetry with connected entry points but is not consumed by the synchronous parser.

func (*Agent) Capabilities

func (a *Agent) Capabilities() []analysis.CapabilityStatus

Capabilities reports supported engine facilities without claiming that every SQL shape is modeled by what-if.

func (*Agent) Close

func (a *Agent) Close()

Close rejects new requests, waits for admitted requests, and releases resources. It closes the connection pool, drops in-memory metadata and plan caches, and deletes the optimization-cache file when PersistencePath is set.

func (*Agent) Engine

func (*Agent) Engine() analysis.Engine

Engine identifies the implementation without querying the server.

func (*Agent) Explain

func (a *Agent) Explain(ctx context.Context, sql string, opts ...RequestOption) (*analysis.ExplainResult, error)

Explain returns only native plan evidence and never invokes analysis rules.

func (*Agent) InvalidateTableCacheAll

func (a *Agent) InvalidateTableCacheAll(schemaName, tableName string)

InvalidateTableCacheAll removes all cached metadata levels for one table.

func (*Agent) InvalidateTableInfoCache

func (a *Agent) InvalidateTableInfoCache(schemaName, tableName string)

InvalidateTableInfoCache removes the sampled metadata entry for one table.

func (*Agent) ListRules

func (a *Agent) ListRules() []optimizer.RuleDescriptor

func (*Agent) RenderMessage

func (a *Agent) RenderMessage(ref *analysis.MessageRef, lang string) string

RenderMessage renders one persisted public message reference.

func (*Agent) RenderResult

func (a *Agent) RenderResult(result *analysis.AnalyzeResult, lang string) *analysis.AnalyzeResult

RenderResult re-renders a persisted public report without running analysis.

func (*Agent) ServerVersion

func (a *Agent) ServerVersion() analysis.ServerVersion

ServerVersion returns the version observed at initialization, or unknown for an offline Agent.

func (*Agent) UseDatabase

func (a *Agent) UseDatabase(ctx context.Context, database string) error

UseDatabase validates access on a pinned connection and atomically changes the default observed by requests that start after it succeeds.

func (*Agent) VersionProfile

func (a *Agent) VersionProfile() analysis.VersionProfile

VersionProfile identifies the observed server generation and cost model.

type AgentConfig

type AgentConfig struct {
	DiagnosticPolicy   analysis.DiagnosticPolicy
	RulePolicy         optimizer.RulePolicy
	OptimizationConfig optimizer.OptimizationConfig
}

AgentConfig groups the immutable policies used by a MySQL Agent. NewAgent resolves defaults, validates every component, and freezes a defensive copy before opening a database connection.

func DefaultAgentConfig

func DefaultAgentConfig() AgentConfig

DefaultAgentConfig returns a fresh MySQL Agent configuration.

func (AgentConfig) Clone

func (config AgentConfig) Clone() AgentConfig

Clone returns a defensive deep copy suitable for caller mutation.

func (AgentConfig) Resolve

func (config AgentConfig) Resolve() (AgentConfig, error)

Resolve validates all policy groups and fills unspecified values from their respective defaults.

func (AgentConfig) Validate

func (config AgentConfig) Validate() error

Validate checks each immutable configuration group independently.

type Option

type Option func(*agentOptions)

Option is a function that configures the Agent

func WithAgentConfig

func WithAgentConfig(config *optimizer.AgentConfig) Option

WithAgentConfig sets all MySQL diagnostic, rule, and optimization policies. A nil pointer selects defaults. The input is copied so later caller mutations do not change an already-created Agent.

func WithBuiltinRuleSet

func WithBuiltinRuleSet(name string) Option

WithBuiltinRuleSet sets the Agent default rule set to a named builtin profile. Common names are "all", "static", and "dynamic"; see optimizer.BuiltinRuleSet for the full list. Unknown names fail in NewAgent.

func WithCharset

func WithCharset(charset string) Option

WithCharset sets the charset

func WithCollation

func WithCollation(collation string) Option

WithCollation sets the collation

func WithDefaultDatabase

func WithDefaultDatabase(database string) Option

WithDefaultDatabase sets the Agent default database. An empty value leaves DSN fallback enabled during construction.

func WithDefaultLanguage

func WithDefaultLanguage(lang string) Option

WithDefaultLanguage sets the default language for messages.

func WithLanguage deprecated

func WithLanguage(lang string) Option

WithLanguage is kept as a backward-compatible alias.

Deprecated: use WithDefaultLanguage instead.

func WithLocaleCatalog

func WithLocaleCatalog(catalog *sqli18n.Catalog) Option

WithLocaleCatalog sets a prebuilt caller-owned locale catalog for the agent. When non-nil, it takes precedence over WithTranslationFiles so the caller's catalog is never mutated by the agent.

func WithOptimizationCacheConfig

func WithOptimizationCacheConfig(cfg *cost.OptimizationCacheConfig) Option

WithOptimizationCacheConfig enables a shared whole-query what-if plan cache. PersistencePath is optional. When set, a new Agent loads that file if it exists. Close drops the in-memory entries and deletes the file.

func WithResolvedAgentConfig

func WithResolvedAgentConfig(config AgentConfig) Option

WithResolvedAgentConfig sets a MySQL Agent configuration using the Agent package's value shape. It is useful when a caller already resolved defaults.

func WithRuleSet

func WithRuleSet(rs *optimizer.RuleSet) Option

WithRuleSet sets the Agent default rule set. Pass optimizer.BuiltinRuleSet or a custom NewRuleSet result; nil restores running every registered rule.

func WithSchema

func WithSchema(schema string) Option

WithSchema is a backward-compatible alias for WithDefaultDatabase.

func WithTableInfoCacheConfig

func WithTableInfoCacheConfig(cfg *db.TableInfoCacheConfig) Option

WithTableInfoCacheConfig overrides the Agent's default table cache configuration. Passing nil disables the table cache.

func WithTranslationFiles

func WithTranslationFiles(files ...string) Option

WithTranslationFiles loads translation files into an agent-scoped catalog. Files are loaded after built-in common/mysql locale files and can override them. WithTranslationFiles loads additional TOML translation files into the agent-scoped catalog. Ignored when WithLocaleCatalog supplies an explicit caller-owned catalog.

type RequestOption

type RequestOption = analysis.RequestOption

RequestOption configures request-scoped behavior for Analyze / Explain. It is shared with the engine-neutral analysis contract.

Directories

Path Synopsis
internal
result_model.go defines the public boundary between native EXPLAIN facts and internal whole-query what-if predictions.
result_model.go defines the public boundary between native EXPLAIN facts and internal whole-query what-if predictions.
Package parser converts TiDB parser nodes into sql-gunyu's MySQL AST.
Package parser converts TiDB parser nodes into sql-gunyu's MySQL AST.
Package relation defines canonical MySQL physical-relation identities.
Package relation defines canonical MySQL physical-relation identities.

Jump to

Keyboard shortcuts

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