base

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MixUserSystemTablesError = errors.Errorf("cannot access user and system tables at the same time")
)

Functions

func ConvertToSQLIsolation

func ConvertToSQLIsolation(level common.IsolationLevel) sql.IsolationLevel

ConvertToSQLIsolation converts our IsolationLevel to database/sql.IsolationLevel

func FirstDefaultChannelTokenPosition

func FirstDefaultChannelTokenPosition(tokens []antlr.Token) *common.ANTLRPosition

FirstDefaultChannelTokenPosition returns the first token position of the default channel. Both line and column are ZERO based.

func GenerateRestoreSQL

func GenerateRestoreSQL(ctx context.Context, engine storepb.Engine, rCtx RestoreContext, statement string, backupItem *storepb.PriorBackupDetail_Item) (string, error)

func GetDefaultChannelTokenType

func GetDefaultChannelTokenType(tokens []antlr.Token, base int, offset int) int

func GetLineOffset

func GetLineOffset(startPosition *storepb.Position) int

GetLineOffset returns the 0-based line offset from a StartPosition. This is useful for converting from the 1-based StartPosition to the 0-based offset needed for some calculations.

func GetOffsetLength

func GetOffsetLength(total int) int

func IsEmpty

func IsEmpty(tokens []antlr.Token, semi int) bool

func NewRange

func NewRange(statement, singleSQL string) *storepb.Range

NewRange creates a new Range with index range of singleSQL in statement.

func ParseTransactionConfig

func ParseTransactionConfig(script string) (common.TransactionConfig, string)

ParseTransactionConfig extracts both transaction mode and isolation level directives from the SQL script. It scans comment lines at the top of the file for directives: - -- txn-mode = on|off - -- txn-isolation = READ UNCOMMITTED|READ COMMITTED|REPEATABLE READ|SERIALIZABLE Directives can appear in any order within the top comment lines. Scanning stops at the first non-comment, non-empty line. Returns the transaction configuration and the SQL script without the directives.

func PrepareANTLRTokenStream

func PrepareANTLRTokenStream(statement string, createLexer func(antlr.CharStream) antlr.Lexer) *antlr.CommonTokenStream

PrepareANTLRTokenStream is a helper function to prepare an ANTLR token stream from a statement. It trims trailing whitespace and creates the necessary lexer and token stream.

Parameters:

  • statement: The SQL statement to process
  • createLexer: A function that creates an ANTLR lexer from an input stream

Returns:

  • A filled CommonTokenStream ready for processing

func RegisterCompleteFunc

func RegisterCompleteFunc(engine storepb.Engine, f CompletionFunc)

RegisterCompleteFunc registers the completion function for the engine.

func RegisterDiagnoseFunc

func RegisterDiagnoseFunc(engine storepb.Engine, f DiagnoseFunc)

RegisterDiagnoseFunc registers the diagnose function for the engine.

func RegisterExtractChangedResourcesFunc

func RegisterExtractChangedResourcesFunc(engine storepb.Engine, f ExtractChangedResourcesFunc)

func RegisterGenerateRestoreSQL

func RegisterGenerateRestoreSQL(engine storepb.Engine, f GenerateRestoreSQLFunc)

func RegisterGetQuerySpan

func RegisterGetQuerySpan(engine storepb.Engine, f GetQuerySpanFunc)

func RegisterParseFunc

func RegisterParseFunc(engine storepb.Engine, f ParseFunc)

func RegisterQueryValidator

func RegisterQueryValidator(engine storepb.Engine, f ValidateSQLForEditorFunc)

func RegisterSplitterFunc

func RegisterSplitterFunc(engine storepb.Engine, f SplitMultiSQLFunc)

func RegisterStatementRangesFunc

func RegisterStatementRangesFunc(engine storepb.Engine, f StatementRangeFunc)

func RegisterTransformDMLToSelect

func RegisterTransformDMLToSelect(engine storepb.Engine, f TransformDMLToSelectFunc)

RegisterTransformDMLToSelect registers the transformDMLToSelect function for the engine.

func TSQLRecognizeExplainType

func TSQLRecognizeExplainType(spans []*QuerySpan, stmt []string)

TSQLRecognizeExplainType walks the spans, and rewrite the select type to explain type if previous statement is SET SHOWPLAN_ALL.

func ValidateSQLForEditor

func ValidateSQLForEditor(engine storepb.Engine, statement string) (bool, bool, error)

ValidateSQLForEditor validates the SQL statement for editor. We support the following SQLs: 1. EXPLAIN statement, except EXPLAIN ANALYZE 2. SELECT statement We also support CTE with SELECT statements, but not with DML statements. The first bool indicates whether the query can run in read-only mode, and the second bool determines whether all queries return data.

Types

type ANTLRAST

type ANTLRAST struct {
	// StartPosition is the 1-based position where this statement starts.
	StartPosition *storepb.Position
	Tree          antlr.Tree
	Tokens        *antlr.CommonTokenStream
}

ANTLRAST is the AST implementation for ANTLR-based parsers. Supported engines: PostgreSQL, MySQL, MariaDB, OceanBase, MSSQL, Oracle, Redshift, Snowflake, BigQuery, Spanner, Doris, Cassandra, Trino, PartiQL, CosmosDB.

Parser packages can use this directly or embed it to add engine-specific fields.

func GetANTLRAST

func GetANTLRAST(a AST) (*ANTLRAST, bool)

GetANTLRAST extracts the ANTLRAST from an AST interface. Returns the ANTLRAST and true if it is an ANTLR-based AST, nil and false otherwise.

func (*ANTLRAST) ASTStartPosition

func (a *ANTLRAST) ASTStartPosition() *storepb.Position

ASTStartPosition implements AST interface.

type AST

type AST interface {
	// ASTStartPosition returns the 1-based position where this SQL statement starts
	// in the original multi-statement input. Used for error position reporting.
	// Returns nil if position is unknown.
	// Named to avoid collision with protobuf-generated GetStartPosition methods.
	ASTStartPosition() *storepb.Position
}

AST is the interface that all parser AST types must implement. Each parser package defines its own concrete AST type with parser-specific fields.

func Parse

func Parse(engine storepb.Engine, statement string) ([]AST, error)

Parse parses the SQL statement and returns an AST representation. Each parser is responsible for creating []AST instances directly.

type ArraySelector

type ArraySelector struct {
	Identifier string
	Index      int
	Next       SelectorNode
}

func NewArraySelector

func NewArraySelector(identifier string, index int) *ArraySelector

func (*ArraySelector) GetIdentifier

func (n *ArraySelector) GetIdentifier() string

func (*ArraySelector) GetNext

func (n *ArraySelector) GetNext() SelectorNode

func (*ArraySelector) SetNext

func (n *ArraySelector) SetNext(next SelectorNode)

type BackupStatement

type BackupStatement struct {
	Statement       string
	SourceSchema    string
	SourceTableName string
	TargetTableName string

	StartPosition *storebp.Position
	EndPosition   *storebp.Position
}

func TransformDMLToSelect

func TransformDMLToSelect(ctx context.Context, engine storepb.Engine, tCtx TransformContext, statement string, sourceDatabase string, targetDatabase string, tablePrefix string) ([]BackupStatement, error)

TransformDMLToSelect transforms the DML statement to SELECT statement.

type Candidate

type Candidate struct {
	Text       string
	Type       CandidateType
	Definition string
	Comment    string
	// The smaller the number, the higher the priority.
	Priority int
}

Candidate is the candidate for auto-completion.

func Completion

func Completion(ctx context.Context, engine storepb.Engine, cCtx CompletionContext, statement string, caretLine int, caretOffset int) ([]Candidate, error)

Completion returns the completion candidates for the statement.

func (Candidate) String

func (c Candidate) String() string

func (Candidate) TextWithPriority

func (c Candidate) TextWithPriority() string

type CandidateType

type CandidateType string

CandidateType is the type of candidate.

const (
	CandidateTypeNone             CandidateType = "NONE"
	CandidateTypeKeyword          CandidateType = "KEYWORD"
	CandidateTypeDatabase         CandidateType = "DATABASE"
	CandidateTypeSchema           CandidateType = "SCHEMA"
	CandidateTypeTable            CandidateType = "TABLE"
	CandidateTypeForeignTable     CandidateType = "FOREIGNTABLE"
	CandidateTypeRoutine          CandidateType = "ROUTINE"
	CandidateTypeFunction         CandidateType = "FUNCTION"
	CandidateTypeView             CandidateType = "VIEW"
	CandidateTypeMaterializedView CandidateType = "MATERIALIZEDVIEW"
	CandidateTypeColumn           CandidateType = "COLUMN"
	CandidateTypeOperator         CandidateType = "OPERATOR"
	CandidateTypeEngine           CandidateType = "ENGINE"
	CandidateTypeTrigger          CandidateType = "TRIGGER"
	CandidateTypeLogFileGroup     CandidateType = "LOGFILEGROUP"
	CandidateTypeUserVar          CandidateType = "USERVAR"
	CandidateTypeSystemVar        CandidateType = "SYSTEMVAR"
	CandidateTypeTableSpace       CandidateType = "TABLESPACE"
	CandidateTypeEvent            CandidateType = "EVENT"
	CandidateTypeIndex            CandidateType = "INDEX"
	CandidateTypeUser             CandidateType = "USER"
	CandidateTypeCharset          CandidateType = "CHARSET"
	CandidateTypeCollation        CandidateType = "COLLATION"
)

type CandidatesCollection

type CandidatesCollection struct {
	Tokens map[int][]int
	Rules  map[int][]*RuleContext
}

CandidatesCollection is the collection of candidates. There are two types of candidates: tokens and rules. Tokens are the tokens that can follow the caret position. Rules are the parser rules that can be reduced at the caret position.

type ChangeSummary

type ChangeSummary struct {
	ChangedResources *model.ChangedResources
	SampleDMLS       []string
	DMLCount         int
	InsertCount      int
}

func ExtractChangedResources

func ExtractChangedResources(engine storepb.Engine, currentDatabase string, currentSchema string, dbMetadata *model.DatabaseMetadata, asts []AST, statement string) (*ChangeSummary, error)

ExtractChangedResources extracts the changed resources from the SQL.

type CodeCompletionCore

type CodeCompletionCore struct {
	IgnoredTokens  map[int]bool
	PreferredRules map[int]bool

	// QueryRule and SelectItemAliasRule are used to determine the select item alias
	// for the completion candidates.
	// They can be used in GROUP BY, ORDER BY and HAVING clauses.
	QueryRule           int
	ShadowQueryRule     int
	SelectItemAliasRule int
	// CTERule is used to determine the CTE alias for the completion candidates.
	// Currently, it always uses in ShadowQueryRule for MySQL.
	CTERule int
	// contains filtered or unexported fields
}

CodeCompletionCore is the core of code completion. It only relies on the ANTLR runtime and does not depend on any specific language.

func NewCodeCompletionCore

func NewCodeCompletionCore(
	ctx context.Context,
	parser antlr.Parser,
	ignoredTokens,
	preferredRules map[int]bool,
	followSets *FollowSetsByState,
	queryRule int,
	shadowQueryRule int,
	selectItemAliasRule int,
	cteRule int,
) *CodeCompletionCore

NewCodeCompletionCore creates a new CodeCompletionCore.

func (*CodeCompletionCore) CollectCandidates

func (c *CodeCompletionCore) CollectCandidates(caretTokenIndex int, context antlr.ParserRuleContext) *CandidatesCollection

CollectCandidates collects the candidates.

type ColumnOptionType

type ColumnOptionType int
const (
	ColumnOptionTypeNone ColumnOptionType = iota
	ColumnOptionTypeNotNull
	ColumnOptionTypeDefault
)

type ColumnResource

type ColumnResource struct {
	// Server is the normalized server name, it's empty if the column comes from the connected server.
	Server string
	// Database is the normalized database name, it should not be empty.
	Database string
	// Schema is the normalized schema name, it should not be empty for the engines that support schema, and should be empty for the engines that don't support schema.
	Schema string
	// Table is the normalized table name, it should not be empty.
	Table string
	// Column is the normalized column name, it should not be empty.
	Column string
}

ColumnResource is the resource key for a column.

func (ColumnResource) String

func (c ColumnResource) String() string

String returns the string format of the column resource.

type CompletionContext

type CompletionContext struct {
	Scene             SceneType
	InstanceID        string
	DefaultDatabase   string
	DefaultSchema     string
	Metadata          GetDatabaseMetadataFunc
	ListDatabaseNames ListDatabaseNamesFunc
}

type CompletionFunc

type CompletionFunc func(ctx context.Context, cCtx CompletionContext, statement string, caretLine int, caretOffset int) ([]Candidate, error)

type DiagnoseContext

type DiagnoseContext struct {
}

DiagnosticContext is the context for diagnosing SQL statements.

type DiagnoseFunc

type DiagnoseFunc func(ctx context.Context, dCtx DiagnoseContext, statement string) ([]Diagnostic, error)

type Diagnostic

type Diagnostic = lsp.Diagnostic

func ConvertSyntaxErrorToDiagnostic

func ConvertSyntaxErrorToDiagnostic(err *SyntaxError, statement string) Diagnostic

func Diagnose

func Diagnose(ctx context.Context, dCtx DiagnoseContext, engine storepb.Engine, statement string) ([]Diagnostic, error)

Diagnose returns the diagnostics for the statement. The diagnostics never be nil, and may not be empty although the error is not nil.

type DiffContext

type DiffContext struct {
	IgnoreCaseSensitive bool
	StrictMode          bool
}

DiffContext is the context for diffing.

type Edge

type Edge struct {
	Start string
	End   string
}

type ExtractChangedResourcesFunc

type ExtractChangedResourcesFunc func(string, string, *model.DatabaseMetadata, []AST, string) (*ChangeSummary, error)

type FollowSetWithPath

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

type FollowSetsByState

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

FollowSetsByState is the map of follow sets by state. It is used to cache the follow sets. The FollowSetsByState is only dependent on the grammar, so that we can reuse it in multiple CodeCompletionCore calls for the same grammar. On the other hand, in single CodeCompletionCore call, the FollowSetsByState is also useful.

For thread safety and performance, we use RWMutex to protect the map. The map is read frequently and written rarely.

func NewFollowSetsByState

func NewFollowSetsByState() FollowSetsByState

NewFollowSetsByState creates a new FollowSetsByState.

func (*FollowSetsByState) CollectFollowSets

func (f *FollowSetsByState) CollectFollowSets(parser antlr.Parser, startState antlr.ATNState, ignoredTokens, _ map[int]bool)

CollectFollowSets collects the follow sets if needed.

func (*FollowSetsByState) Get

func (f *FollowSetsByState) Get(state int) FollowSetsHolder

Get thread safety gets the follow sets by state.

func (*FollowSetsByState) Set

func (f *FollowSetsByState) Set(state int, holder FollowSetsHolder)

Set thread safety sets the follow sets by state.

type FollowSetsHolder

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

FollowSetsHolder is the holder of follow sets.

type FollowSetsList

type FollowSetsList []FollowSetWithPath

func (*FollowSetsList) Append

func (l *FollowSetsList) Append(f FollowSetWithPath)

type GenerateRestoreSQLFunc

type GenerateRestoreSQLFunc func(ctx context.Context, rCtx RestoreContext, statement string, backupItem *storepb.PriorBackupDetail_Item) (string, error)

type GetDatabaseMetadataFunc

type GetDatabaseMetadataFunc func(context.Context, string, string) (string, *model.DatabaseMetadata, error)

GetDatabaseMetadataFunc is the function to get database metadata.

type GetLinkedDatabaseMetadataFunc

type GetLinkedDatabaseMetadataFunc func(context.Context, string, string, string) (string, string, *model.DatabaseMetadata, error)

type GetQuerySpanContext

type GetQuerySpanContext struct {
	InstanceID                    string
	GetDatabaseMetadataFunc       GetDatabaseMetadataFunc
	ListDatabaseNamesFunc         ListDatabaseNamesFunc
	GetLinkedDatabaseMetadataFunc GetLinkedDatabaseMetadataFunc
	// TempTables is the temporary tables created in the query span.
	// It's used to store the temporary tables declared in one batch for SQL Server.
	TempTables map[string]*PhysicalTable

	// Adding the engine information here is a trade-off between the copy-pasted and shared code.
	// For engines with more different, we implement the getQuerySpan separately.
	// For some similar engines, we can share the same getQuerySpan implementation.
	// But they may have some differences, so we need to pass the engine information here.
	// No need to set this field when call GetQuerySpan, because the base.GetQuerySpan already has the engine information.
	// We'll deal this field in the base.GetQuerySpan.
	Engine storepb.Engine
}

type GetQuerySpanFunc

type GetQuerySpanFunc func(ctx context.Context, gCtx GetQuerySpanContext, statement, database, schema string, ignoreCaseSensitive bool) (*QuerySpan, error)

GetQuerySpanFunc is the interface of getting the query span for a query.

type Graph

type Graph struct {
	NodeMap  map[string]*Node
	EdgeList []*Edge
}

func NewGraph

func NewGraph() *Graph

func (*Graph) AddEdge

func (g *Graph) AddEdge(start, end string)

func (*Graph) AddNode

func (g *Graph) AddNode(id string)

func (*Graph) TopologicalSort

func (g *Graph) TopologicalSort() ([]string, error)

type ItemSelector

type ItemSelector struct {
	Identifier string
	Next       SelectorNode
}

func NewItemSelector

func NewItemSelector(identifier string) *ItemSelector

func (*ItemSelector) GetIdentifier

func (n *ItemSelector) GetIdentifier() string

func (*ItemSelector) GetNext

func (n *ItemSelector) GetNext() SelectorNode

func (*ItemSelector) SetNext

func (n *ItemSelector) SetNext(next SelectorNode)

type ListDatabaseNamesFunc

type ListDatabaseNamesFunc func(context.Context, string) ([]string, error)

ListDatabaseNamesFunc is the function to list database names.

type Node

type Node struct {
	ID string
}

type ParseErrorListener

type ParseErrorListener struct {
	// StartPosition is the 1-based position where this statement starts in the original multi-statement input.
	// Used to calculate error positions relative to the original script.
	StartPosition *storepb.Position
	Err           *SyntaxError
	Statement     string
}

ParseErrorListener is a custom error listener for PLSQL parser.

func (*ParseErrorListener) ReportAmbiguity

func (*ParseErrorListener) ReportAmbiguity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, exact bool, ambigAlts *antlr.BitSet, configs *antlr.ATNConfigSet)

ReportAmbiguity reports an ambiguity.

func (*ParseErrorListener) ReportAttemptingFullContext

func (*ParseErrorListener) ReportAttemptingFullContext(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, conflictingAlts *antlr.BitSet, configs *antlr.ATNConfigSet)

ReportAttemptingFullContext reports an attempting full context.

func (*ParseErrorListener) ReportContextSensitivity

func (*ParseErrorListener) ReportContextSensitivity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex, prediction int, configs *antlr.ATNConfigSet)

ReportContextSensitivity reports a context sensitivity.

func (*ParseErrorListener) SyntaxError

func (l *ParseErrorListener) SyntaxError(_ antlr.Recognizer, token any, line, column int, message string, _ antlr.RecognitionException)

SyntaxError returns the errors.

type ParseFunc

type ParseFunc func(statement string) ([]AST, error)

ParseFunc is the interface for parsing SQL statements and returning []AST. Each parser package is responsible for creating AST instances with the appropriate data. Parser packages can return *ANTLRAST for ANTLR-based parsers or their own concrete types.

type ParseResult

type ParseResult struct {
	Tree   antlr.Tree
	Tokens *antlr.CommonTokenStream
	// BaseLine stores the zero-based line offset where this SQL statement starts in the original multi-statement input.
	// When splitting a multi-statement SQL script (e.g., "SELECT 1;\nSELECT 2;"), each statement is parsed separately.
	// BaseLine tracks the original line number (minus 1) of the first token in this statement, allowing error messages
	// and positions to be correctly reported relative to the original script rather than the isolated statement.
	// Calculated from StartPosition.Line - 1 (since StartPosition uses 1-based line numbers, but BaseLine is 0-based for offset arithmetic).
	BaseLine int
}

ParseResult is the result of parsing SQL statements.

type PathAST

type PathAST struct {
	Root SelectorNode
}

func NewPathAST

func NewPathAST(selectorNode SelectorNode) *PathAST

func (*PathAST) String

func (p *PathAST) String() (string, error)

type PhysicalTable

type PhysicalTable struct {

	// Server is the normalized server name, it's empty if the column comes from the connected server.
	Server string
	// Database is the normalized database name, it should not be empty.
	Database string
	// Schema is the normalized schema name, it should not be empty for the engines that support schema, and should be empty for the engines that don't support schema.
	Schema string
	// Name is the normalized table name, it should not be empty.
	Name string
	// Columns are the columns of the table.
	Columns []string
	// contains filtered or unexported fields
}

PhysicalTable is the resource of a physical table.

func (*PhysicalTable) GetDatabaseName

func (p *PhysicalTable) GetDatabaseName() string

func (*PhysicalTable) GetQuerySpanResult

func (p *PhysicalTable) GetQuerySpanResult() []QuerySpanResult

func (*PhysicalTable) GetSchemaName

func (p *PhysicalTable) GetSchemaName() string

func (*PhysicalTable) GetServerName

func (p *PhysicalTable) GetServerName() string

func (*PhysicalTable) GetTableName

func (p *PhysicalTable) GetTableName() string

type PhysicalTableReference

type PhysicalTableReference struct {
	Database string
	Schema   string
	Table    string
	Alias    string
}

type PhysicalView

type PhysicalView struct {

	// Server is the normalized server name, it's empty if the column comes from the connected server.
	Server string
	// Database is the normalized database name, it should not be empty.
	Database string
	// Schema is the normalized schema name, it should not be empty for the engines that support schema, and should be empty for the engines that don't support schema.
	Schema string
	// Name is the normalized table name, it should not be empty.
	Name string
	// Columns are the columns of the table.
	Columns []QuerySpanResult
	// contains filtered or unexported fields
}

PhysicalView is the resource of a physical view, which can be refer with schema name, and its columns can refer to the columns of the underlying tables.

func (*PhysicalView) GetDatabaseName

func (p *PhysicalView) GetDatabaseName() string

func (*PhysicalView) GetQuerySpanResult

func (p *PhysicalView) GetQuerySpanResult() []QuerySpanResult

func (*PhysicalView) GetSchemaName

func (p *PhysicalView) GetSchemaName() string

func (*PhysicalView) GetServerName

func (p *PhysicalView) GetServerName() string

func (*PhysicalView) GetTableName

func (p *PhysicalView) GetTableName() string

type PipelineEntry

type PipelineEntry struct {
	State      antlr.ATNState
	TokenIndex int
}

PipelineEntry is the entry of the pipeline.

type PseudoTable

type PseudoTable struct {

	// Name is the normalized table name.
	Name string

	// Columns are the columns of the table.
	Columns []QuerySpanResult
	// contains filtered or unexported fields
}

PseudoTable is the resource of table, it's useful for some pseudo/temporary tables likes CTE, AS.

func NewPseudoTable

func NewPseudoTable(name string, columns []QuerySpanResult) *PseudoTable

func (*PseudoTable) GetDatabaseName

func (*PseudoTable) GetDatabaseName() string

func (*PseudoTable) GetQuerySpanResult

func (p *PseudoTable) GetQuerySpanResult() []QuerySpanResult

func (*PseudoTable) GetSchemaName

func (*PseudoTable) GetSchemaName() string

func (*PseudoTable) GetServerName

func (*PseudoTable) GetServerName() string

func (*PseudoTable) GetTableName

func (p *PseudoTable) GetTableName() string

type QuerySpan

type QuerySpan struct {
	Type QueryType
	// Results are the result columns of a query span.
	// Currently, SourceColumns in the QuerySpanResult are only for the fields in the Query.
	Results []QuerySpanResult
	// SourceColumns are the source columns contributing to the span.
	// SourceColumns here are the source columns for the whole query span, containing fields, where conditions, join conditions, etc.
	SourceColumns SourceColumnSet
	// PredicateColumns are the source columns contributing to the span.
	// PredicateColumns here are the source columns for the where conditions.
	PredicateColumns SourceColumnSet
	// PredicatePaths is predicateColumns used by Cosmos DB only, to store the path of the field, all the
	// paths should begin with the container name.
	PredicatePaths            map[string]*PathAST
	NotFoundError             error
	FunctionNotSupportedError error
}

QuerySpan is the span for a query.

func GetQuerySpan

func GetQuerySpan(ctx context.Context, gCtx GetQuerySpanContext, engine storepb.Engine, statement, database, schema string, ignoreCaseSensitive bool) ([]*QuerySpan, error)

GetQuerySpan gets the span of a query. The interface will return the query spans with non-critical errors, or return an error if the query is invalid.

func (*QuerySpan) ToYaml

func (s *QuerySpan) ToYaml() *YamlQuerySpan

type QuerySpanResult

type QuerySpanResult struct {
	// Name is the result name of a query.
	Name string
	// SourceColumns are the source columns contributing to the span result.
	SourceColumns SourceColumnSet
	// IsPlainField indicates whether the field is a plain column reference (true) or an expression (false).
	IsPlainField bool
	// SourceFieldPaths is used for Cosmos DB only, to store the path of the field, the root is the container name.
	SourceFieldPaths map[string]*PathAST
	// SelectAsterisk indicates whether the field is selected by asterisk, used by Cosmos DB.
	SelectAsterisk bool
}

QuerySpanResult is the result column of a query span.

type QueryType

type QueryType int

QueryType is the type of a query. The query type determines the permission to use.

const (
	// Type can not be recognized for now.
	QueryTypeUnknown QueryType = iota
	// The read-only select query.
	Select
	// The explain query.
	Explain
	// The read-only select query for reading information schema and system objects.
	SelectInfoSchema
	// The DDL query that changes schema.
	DDL
	// The DML query that changes table data.
	DML
)

type Range

type Range = lsp.Range

func GetANTLRStatementRangesUTF16Position

func GetANTLRStatementRangesUTF16Position(tokenStream *antlr.CommonTokenStream, eofTokenType, terminatorTokenType int) []Range

GetANTLRStatementRangesUTF16Position is a generic function to extract statement ranges from ANTLR token streams. It returns the ranges of statements in UTF-16 positions suitable for LSP protocol.

Parameters:

  • tokenStream: The ANTLR token stream to process
  • eofTokenType: The EOF token type (e.g., parser.SnowflakeParserEOF)
  • terminatorTokenType: The statement terminator token type (e.g., parser.SnowflakeParserSEMI)

The function handles:

  • UTF-16 surrogate pairs for characters outside the Basic Multilingual Plane (BMP)
  • Trimming leading whitespace from statements for more accurate ranges
  • Ignoring single terminator statements
  • Handling statements that don't end with a terminator

func GetStatementRanges

func GetStatementRanges(ctx context.Context, sCtx StatementRangeContext, engine storepb.Engine, statement string) ([]Range, error)

GetStatementRanges returns a list of ranges for the statement. Start is inclusive and end is exclusive. Character is 0-based UTF-16 code unit offset, Line is 0-based line number.

type RestoreContext

type RestoreContext struct {
	InstanceID              string
	GetDatabaseMetadataFunc GetDatabaseMetadataFunc
	ListDatabaseNamesFunc   ListDatabaseNamesFunc
	IsCaseSensitive         bool
}

type RuleContext

type RuleContext struct {
	ID int
	// SelectItemAliases is the map of token index of select item aliases.
	// Only the QueryRule has the SelectItemAliases.
	SelectItemAliases map[int]bool
	CTEList           []int
}

RuleContext is the context of a rule.

type RuleEndStatus

type RuleEndStatus map[int]bool

type RuleList

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

RuleList is the list of rules. Use a bitset to check existence of a rule in the list efficiently.

func NewRuleList

func NewRuleList() *RuleList

NewRuleList creates a new RuleList.

func (*RuleList) Append

func (l *RuleList) Append(r *RuleList)

Append appends the rules from the given RuleList.

func (*RuleList) Contains

func (l *RuleList) Contains(rule int) bool

Contains checks if the rule exists in the list. Use the bitset to check the existence efficiently.

func (*RuleList) Copy

func (l *RuleList) Copy() *RuleList

Copy copies the RuleList.

func (*RuleList) Pop

func (l *RuleList) Pop() *RuleContext

Pop pops the last rule from the list. HINT: Each Push should not push the existing rule, otherwise Pop will destroy the bitSet.

func (*RuleList) Push

func (l *RuleList) Push(rule *RuleContext)

Push appends the rule to the list.

type Scanner

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

Scanner is the wrapper of antlr.CommonTokenStream. It provides a common scanner for all parsers and more useful methods.

func NewScanner

func NewScanner(input *antlr.CommonTokenStream, fillInput bool) *Scanner

NewScanner creates a new Scanner.

func (*Scanner) Backward

func (s *Scanner) Backward(skipHidden bool) bool

Backward moves the index backward. If skipHidden is true, it will skip the hidden tokens.

func (*Scanner) Forward

func (s *Scanner) Forward(skipHidden bool) bool

Forward moves the index forward. If skipHidden is true, it will skip the hidden tokens.

func (*Scanner) GetFollowingText

func (s *Scanner) GetFollowingText() string

GetFollowingText returns the following text of the current token.

func (*Scanner) GetFollowingTextAfter

func (s *Scanner) GetFollowingTextAfter(pos int) string

GetFollowingTextAfter returns the following text after the given position.

func (*Scanner) GetIndex

func (s *Scanner) GetIndex() int

GetIndex returns the current index of the scanner.

func (*Scanner) GetPreviousTokenText

func (s *Scanner) GetPreviousTokenText(skipHidden bool) string

GetPreviousTokenText returns the text of the previous tok If skipHidden is true, it will skip the hidden tokens. It does not change the current index of the scanner.

func (*Scanner) GetPreviousTokenType

func (s *Scanner) GetPreviousTokenType(skipHidden bool) int

GetPreviousTokenType returns the type of the previous token. If skipHidden is true, it will skip the hidden tokens. It does not change the current index of the scanner.

func (*Scanner) GetTokenChannel

func (s *Scanner) GetTokenChannel() int

GetTokenChannel returns the channel of the current token.

func (*Scanner) GetTokenText

func (s *Scanner) GetTokenText() string

GetTokenText returns the text of the current token.

func (*Scanner) GetTokenType

func (s *Scanner) GetTokenType() int

GetTokenType returns the type of the current token.

func (*Scanner) IsTokenType

func (s *Scanner) IsTokenType(tokenType int) bool

IsTokenType returns whether the current token type is the given token type.

func (*Scanner) PopAndRestore

func (s *Scanner) PopAndRestore() bool

PopAndRestore pops the index from the stack and restores the index.

func (*Scanner) Push

func (s *Scanner) Push()

Push pushes the current index to the stack.

func (*Scanner) SeekIndex

func (s *Scanner) SeekIndex(index int)

SeekIndex seeks the index of the scanner. If the index is out of range, it will do nothing.

func (*Scanner) SeekPosition

func (s *Scanner) SeekPosition(line, column int) bool

SeekPosition seeks the position of the scanner.

  1. If the position is before the first token, it will do nothing and return false.

The following cases will move the index to the token and return true:

  1. If the position is in one of the tokens, it will move the index to the token.
  2. If the position is between two tokens, it will move the index to the previous token.
  3. If the position is after the last token, it will move the index to the last token.

func (*Scanner) SkipTokenSequence

func (s *Scanner) SkipTokenSequence(list []int) bool

SkipTokenSequence skips the token sequence. This method will ignore the hidden tokens for scanner, but not check for given token list. If the given token list is not fully matched:

  1. It will skip the common prefix of the given token list and the current token list in the scanner.
  2. Return false.

If the given token list is fully matched:

  1. It will skip the matched tokens in the scanner.
  2. Return true.

type SceneType

type SceneType string
const (
	SceneTypeAll   SceneType = "ALL"
	SceneTypeQuery SceneType = "QUERY"
)

type SchemaResource

type SchemaResource struct {
	Database string
	Schema   string
	Table    string

	// LinkedServer is the special resource for MSSQL, which can be used to specify the linked server.
	LinkedServer string
}

SchemaResource is the resource of the schema.

func (SchemaResource) Pretty

func (r SchemaResource) Pretty() string

Pretty returns the pretty string of the resource.

func (SchemaResource) String

func (r SchemaResource) String() string

String implements fmt.Stringer interface.

type SelectorNode

type SelectorNode interface {
	GetIdentifier() string
	SetNext(SelectorNode)
	GetNext() SelectorNode
	// contains filtered or unexported methods
}

type Sequence

type Sequence struct {

	// Server is the normalized server name, it's empty if the column comes from the connected server.
	Server string
	// Database is the normalized database name, it should not be empty.
	Database string
	// Schema is the normalized schema name, it should not be empty for the engines that support schema, and should be empty for the engines that don't support schema.
	Schema string
	// Name is the normalized sequence name, it should not be empty.
	Name string
	// Columns are the columns of the sequence.
	Columns []string
	// contains filtered or unexported fields
}

Sequence is the resource of a sequence.

func (*Sequence) GetDatabaseName

func (p *Sequence) GetDatabaseName() string

func (*Sequence) GetQuerySpanResult

func (p *Sequence) GetQuerySpanResult() []QuerySpanResult

func (*Sequence) GetSchemaName

func (p *Sequence) GetSchemaName() string

func (*Sequence) GetServerName

func (p *Sequence) GetServerName() string

func (*Sequence) GetTableName

func (p *Sequence) GetTableName() string

type SingleSQL

type SingleSQL struct {
	Text string
	// BaseLine is the line number of the first line of the SQL in the original SQL.
	// HINT: ZERO based.
	BaseLine int
	// The inclusive start position of the SQL starts in the original SQL.
	Start *storepb.Position
	// The inclusive end position of the SQL ends in the original SQL.
	End *storepb.Position
	// The sql is empty, such as `/* comments */;` or just `;`.
	Empty bool

	// ByteOffsetStart is the start position of the sql.
	// This field may not be present for every engine.
	// ByteOffsetStart is intended for sql execution log display. It may not represent the actual sql that is sent to the database.
	ByteOffsetStart int
	// ByteOffsetEnd is the end position of the sql.
	// This field may not be present for every engine.
	// ByteOffsetEnd is intended for sql execution log display. It may not represent the actual sql that is sent to the database.
	ByteOffsetEnd int
}

SingleSQL is a separate SQL split from multi-SQL.

func FilterEmptySQL

func FilterEmptySQL(list []SingleSQL) []SingleSQL

func FilterEmptySQLWithIndexes

func FilterEmptySQLWithIndexes(list []SingleSQL) ([]SingleSQL, []int32)

func SplitMultiSQL

func SplitMultiSQL(engine storepb.Engine, statement string) ([]SingleSQL, error)

SplitMultiSQL splits statement into a slice of the single SQL.

func SplitSQLByLexer

func SplitSQLByLexer(stream *antlr.CommonTokenStream, semiTokenType int, statement string) ([]SingleSQL, error)

SplitSQLByLexer is a grammar-free helper function that splits SQL statements using an ANTLR lexer. It works with any ANTLR-based lexer by accepting the token stream and semicolon token type as parameters.

Parameters:

  • stream: The ANTLR token stream (must be already filled with stream.Fill())
  • semiTokenType: The token type value for semicolon in the specific grammar
  • statement: The original SQL statement string (used for position conversion)

Returns:

  • A slice of SingleSQL, each representing one statement with its text, position, and metadata

Example usage:

lexer := parser.NewSnowflakeLexer(antlr.NewInputStream(statement))
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
stream.Fill()
return SplitSQLByLexer(stream, parser.SnowflakeLexerSEMI, statement)

type SourceColumnSet

type SourceColumnSet map[ColumnResource]bool

func MergeSourceColumnSet

func MergeSourceColumnSet(m, n SourceColumnSet) (SourceColumnSet, bool)

MergeSourceColumnSet merges two source column maps, returns true if there is difference.

type SplitMultiSQLFunc

type SplitMultiSQLFunc func(string) ([]SingleSQL, error)

type StatementRangeContext

type StatementRangeContext struct{}

type StatementRangeFunc

type StatementRangeFunc func(ctx context.Context, sCtx StatementRangeContext, statement string) ([]Range, error)

type StringsManipulatorAction

type StringsManipulatorAction interface {
	GetType() StringsManipulatorActionType
	GetSchemaName() string
	GetTopLevelNaming() string
	GetSecondLevelNaming() string
}

type StringsManipulatorActionBase

type StringsManipulatorActionBase struct {
	Type       StringsManipulatorActionType
	SchemaName string
}

func (*StringsManipulatorActionBase) GetSchemaName

func (s *StringsManipulatorActionBase) GetSchemaName() string

func (*StringsManipulatorActionBase) GetType

type StringsManipulatorActionType

type StringsManipulatorActionType int
const (
	StringsManipulatorActionTypeNone StringsManipulatorActionType = iota
	StringsManipulatorActionTypeDropTable
	StringsManipulatorActionTypeAddTable
	StringsManipulatorActionTypeDropColumn
	StringsManipulatorActionTypeAddColumn
	StringsManipulatorActionTypeModifyColumnType
	StringsManipulatorActionTypeDropColumnOption
	StringsManipulatorActionTypeAddColumnOption
	StringsManipulatorActionTypeModifyColumnOption
	StringsManipulatorActionTypeDropTableConstraint
	StringsManipulatorActionTypeModifyTableConstraint
	StringsManipulatorActionTypeAddTableConstraint
	StringsManipulatorActionTypeDropTableOption
	StringsManipulatorActionTypeModifyTableOption
	StringsManipulatorActionTypeAddTableOption
	StringsManipulatorActionTypeDropIndex
	StringsManipulatorActionTypeAddIndex
	StringsManipulatorActionTypeModifyIndex
)

type SyntaxError

type SyntaxError struct {
	Position   *storepb.Position
	Message    string
	RawMessage string
}

SyntaxError is a syntax error.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

Error returns the error message.

type TableConstraintType

type TableConstraintType int
const (
	TableConstraintTypeNone TableConstraintType = iota
	TableConstraintTypePrimaryKey
	TableConstraintTypeUnique
)

type TableReference

type TableReference interface {
	// contains filtered or unexported methods
}

type TableSource

type TableSource interface {
	GetTableName() string
	GetSchemaName() string
	GetDatabaseName() string
	GetServerName() string
	// GetQuerySpanResult returns the query span result of the table, it's callers' responsibility
	// to make a copy of the result if they want to modify it.
	GetQuerySpanResult() []QuerySpanResult
	// contains filtered or unexported methods
}

type Token

type Token struct {
	Type          int
	StartPosition int
}

type TransformContext

type TransformContext struct {
	Version                 any
	InstanceID              string
	GetDatabaseMetadataFunc GetDatabaseMetadataFunc
	ListDatabaseNamesFunc   ListDatabaseNamesFunc
	IsCaseSensitive         bool
	DatabaseName            string
}

type TransformDMLToSelectFunc

type TransformDMLToSelectFunc func(ctx context.Context, tCtx TransformContext, statement string, sourceDatabase string, targetDatabase string, tablePrefix string) ([]BackupStatement, error)

TransformDMLToSelectFunc is the interface of transforming DML statements to SELECT statements.

type ValidateSQLForEditorFunc

type ValidateSQLForEditorFunc func(string) (bool, bool, error)

type VirtualTableReference

type VirtualTableReference struct {
	Table   string
	Columns []string
}

type YamlQuerySpan

type YamlQuerySpan struct {
	Type             QueryType
	Results          []YamlQuerySpanResult
	SourceColumns    []ColumnResource
	PredicateColumns []ColumnResource
}

type YamlQuerySpanResult

type YamlQuerySpanResult struct {
	Name             string
	SourceColumns    []ColumnResource
	IsPlainField     bool
	SourceFieldPaths []YamlQuerySpanResultSourceFieldPaths
	SelectAsterisk   bool
}

type YamlQuerySpanResultSourceFieldPaths

type YamlQuerySpanResultSourceFieldPaths struct {
	Name string
	Path string
}

Jump to

Keyboard shortcuts

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