sqlref

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 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

Overview

Package sqlref extracts the tables, columns and equi-joins a statement reads and writes from the sqlglot AST (#538, #625). Extraction runs the fork's qualify pass so a reference is resolved to its physical source: a CTE, subquery or derived table is followed to the table it reads, and an unqualified table is stamped with the target's default schema.

It is best-effort: no engine round-trip is needed, and a parse or qualify failure yields an empty Reference, never an error the caller must treat as fatal.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Persist

func Persist(ctx context.Context, q Store, statementID, workspaceID, catalogID db.UUID, namespace, sql string) error

Persist replaces the stored references, columns and joins of one statement with the extractor's result (#538, #625). It is idempotent: the old rows are deleted first. The caller owns the transaction.

func PersistWithDialect

func PersistWithDialect(ctx context.Context, q Store, statementID, workspaceID, catalogID db.UUID, d dialect.Dialect, namespace, sql string) error

PersistWithDialect is Persist with the connection's dialect: table names are resolved against the target's default schema (namespace) and through CTEs, subqueries and derived tables by the fork's qualify pass.

Types

type Column

type Column struct {
	Namespace string
	Table     string
	Name      string
}

Column is one column reference, resolved to its physical table. Namespace and Table are both empty when the reference is unqualified and ambiguous or unresolved.

type Join

type Join struct {
	LeftAlias   string
	LeftColumn  string
	RightAlias  string
	RightColumn string
}

Join is an equi-join predicate between two aliases: left = right.

type Reference

type Reference struct {
	Tables  []Table
	Columns []Column
	Joins   []Join
}

Reference is what one statement reads and writes.

func Extract

func Extract(sql string) Reference

Extract returns references using Baleia's generic SQL dialect. Callers that know the connection dialect and its default schema should use ExtractResolved.

func ExtractResolved

func ExtractResolved(d dialect.Dialect, defaultSchema, sql string) Reference

ExtractResolved returns the tables, columns and equi-joins of one statement, parsed with d and resolved against defaultSchema (the target's default schema, stamped on unqualified table references). An unparsable statement yields an empty Reference.

func ExtractWithDialect

func ExtractWithDialect(d dialect.Dialect, sql string) Reference

ExtractWithDialect returns references parsed with the connection dialect but without a default schema; unqualified names stay unqualified.

type Store

type Store interface {
	DeleteStatementReferences(ctx context.Context, statementID db.UUID) error
	DeleteStatementJoins(ctx context.Context, statementID db.UUID) error
	DeleteStatementColumns(ctx context.Context, statementID db.UUID) error
	InsertStatementReference(ctx context.Context, arg db.InsertStatementReferenceParams) error
	InsertStatementJoin(ctx context.Context, arg db.InsertStatementJoinParams) error
	InsertStatementColumn(ctx context.Context, arg db.InsertStatementColumnParams) error
}

Store is the persistence of a statement's references, columns and joins; *repo.Repo implements it.

type Table

type Table struct {
	// Path is the resolved name, 1 to 3 parts (table, schema.table,
	// catalog.schema.table). The default schema is stamped on unqualified
	// names when ExtractResolved is given one.
	Path  []string
	Alias string
	// Kind is "read" for FROM/JOIN and "write" for UPDATE/INTO.
	Kind string
}

Table is one physical table named in FROM/JOIN/UPDATE/INTO.

Jump to

Keyboard shortcuts

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