access

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package access reads which database objects SQL statements read, write and call. ReadTSQL reads a whole T-SQL file -- its anonymous statements and the routines it creates, each body nested in the IF, WHILE and TRY that guard it; ReadStatement reads one statement of any dialect sqlglot parses.

Positions are per statement: sqlglot expressions carry no offsets, so every access a statement makes carries the line the statement starts on.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Access

type Access struct {
	Kind   Kind   `json:"kind"`
	Object Object `json:"object"`
	Line   int    `json:"line"`
}

Access is one read, write or call, on the line its statement starts on.

func Collect

func Collect(nodes []Node) []Access

Collect is every access of nodes and the nodes they nest, in the order they are written.

func ReadStatement

func ReadStatement(text string, opts StatementOptions) ([]Access, error)

ReadStatement reads one statement: the writes it makes, then the reads and calls in the order they are met. Dynamic SQL is not followed: an EXEC of a string is an UnsupportedSQLError, as it is for ReadTSQL only that reads it as statements of their own.

type Diagnostic

type Diagnostic struct {
	Line      int    `json:"line"`
	Statement string `json:"statement"`
	Message   string `json:"message"`
}

Diagnostic is one statement whose accesses could not be read: the 1-based line it starts on, its text, and why.

type File

type File struct {
	// Routines holds each CREATE (OR ALTER) / ALTER PROCEDURE or FUNCTION, in
	// the order they are read: a routine the string literal of an EXEC
	// defines is read where that EXEC is, before the routine holding it.
	Routines []Routine `json:"routines,omitempty"`
	// Script is what the file's anonymous statements, across GO batches, do
	// when it runs. It is nil when every batch defines a routine, or is
	// setup that accesses nothing, beside one that does.
	Script *Script `json:"script,omitempty"`
	// Diagnostics are every statement, in a routine or not, whose accesses
	// could not be read. None is ever dropped.
	Diagnostics []Diagnostic `json:"diagnostics,omitempty"`
}

File is what one T-SQL file does.

func ReadTSQL

func ReadTSQL(src string) (File, error)

ReadTSQL reads what a T-SQL file does: the routines it defines and its anonymous statements. A statement that cannot be read is a diagnostic, and reading goes on; only a file that does not split into GO batches is an error.

type Kind

type Kind string

Kind is what a statement does to an object.

const (
	Read  Kind = "read"
	Write Kind = "write"
	Call  Kind = "call"
)

type Node

type Node struct {
	Kind NodeKind `json:"kind"`
	// Line is the 1-based line the statement starts on; Text is the statement
	// as written, comments blanked. An IF's or WHILE's Text is its whole
	// statement, condition included.
	Line int    `json:"line"`
	Text string `json:"text,omitempty"`
	// Accesses are what the statement, or an IF's or WHILE's condition,
	// does: its writes first, then its reads and calls in the order they are
	// met.
	Accesses []Access `json:"accesses,omitempty"`
	// Dynamic is what the string literal a statement's EXEC or
	// sp_executesql runs does, as statements of their own; they run after
	// Accesses.
	Dynamic   []Node `json:"dynamic,omitempty"`
	Condition string `json:"condition,omitempty"`
	// Then is an IF's branch, a WHILE's body or a TRY's block; Else is an
	// IF's ELSE branch and Catch a TRY's CATCH block. HasElse and HasCatch
	// say the branch was written, since one that accesses nothing is empty.
	Then     []Node `json:"then,omitempty"`
	Else     []Node `json:"else,omitempty"`
	Catch    []Node `json:"catch,omitempty"`
	HasElse  bool   `json:"hasElse,omitempty"`
	HasCatch bool   `json:"hasCatch,omitempty"`
}

Node is one statement of a T-SQL body, or the IF, WHILE or TRY around the statements it governs. A statement that accesses nothing is no node.

type NodeKind

type NodeKind string

NodeKind is what a Node of a T-SQL body is.

const (
	// StatementNode is one statement.
	StatementNode NodeKind = "statement"
	// IfNode runs Then when Condition holds, else Else.
	IfNode NodeKind = "if"
	// WhileNode runs Then while Condition holds.
	WhileNode NodeKind = "while"
	// TryNode runs Then, and Catch when it fails.
	TryNode NodeKind = "try"
)

type Object

type Object struct {
	Database string `json:"database,omitempty"`
	Schema   string `json:"schema"`
	Name     string `json:"name"`
}

Object is a table, view or routine as a statement names it, with the default schema filled in. Database is set only for a name in another database.

func (Object) String

func (o Object) String() string

String is the object's dotted name, its database first when it has one.

type Routine

type Routine struct {
	// Object is the routine's name, in dbo when no schema is written.
	Object Object `json:"object"`
	// Definition is the routine's signature, read off its header.
	Definition sqlglot.Routine `json:"-"`
	// Line is the line its CREATE or ALTER starts on.
	Line        int          `json:"line"`
	Body        []Node       `json:"body,omitempty"`
	Diagnostics []Diagnostic `json:"diagnostics,omitempty"`
}

Routine is one routine a file creates or alters, and what its body does when it runs.

type Script

type Script struct {
	Line int    `json:"line"`
	Body []Node `json:"body,omitempty"`
}

Script is a file's anonymous statements: the line the first starts on, and what they do.

type StatementOptions

type StatementOptions struct {
	// Dialect is the sqlglot dialect the statement is written in.
	Dialect string
	// DefaultSchema is the schema an unqualified name is in: dbo in T-SQL,
	// public in PostgreSQL.
	DefaultSchema string
	// IntoVariables reads SELECT … INTO as assigning variables, as plpgsql
	// does, rather than as creating a table.
	IntoVariables bool
	// Line is the line the statement starts on, given to each access.
	Line int
}

StatementOptions say how ReadStatement reads one statement.

type UnsupportedSQLError

type UnsupportedSQLError struct{ Class string }

UnsupportedSQLError reports a statement that parsed but whose data access cannot be accounted for: an opaque Command, dynamic SQL in a variable, or a node no rule reads. Class names the sqlglot node class.

func (*UnsupportedSQLError) Error

func (e *UnsupportedSQLError) Error() string

Jump to

Keyboard shortcuts

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