tsqlparser

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: GPL-3.0 Imports: 4 Imported by: 4

README

tsqlparser

A T-SQL parser written in Go. Parses Microsoft SQL Server T-SQL syntax into an abstract syntax tree (AST).

Features

  • Parses T-SQL DDL and DML statements
  • Supports stored procedures, functions, triggers, and views
  • Handles complex queries with CTEs, window functions, subqueries
  • Full support for JOINs, PIVOT/UNPIVOT, MERGE, and set operations
  • Control flow statements (IF/ELSE, WHILE, TRY/CATCH)
  • Transaction handling
  • XML and JSON operations
  • 96% pass rate on a corpus of 201 real-world T-SQL samples

Installation

go get github.com/ha1tch/tsqlparser

Usage

package main

import (
    "fmt"
    "github.com/ha1tch/tsqlparser"
)

func main() {
    input := `SELECT CustomerID, Name FROM Customers WHERE Status = 'Active'`
    
    program, errors := tsqlparser.Parse(input)
    if len(errors) > 0 {
        for _, err := range errors {
            fmt.Println("Error:", err)
        }
        return
    }
    
    fmt.Println(program.String())
}

Supported Statements

DML
  • SELECT (with all clauses, JOINs, subqueries, CTEs, window functions)
  • INSERT (single row, multi-row, SELECT, EXEC, DEFAULT VALUES)
  • UPDATE (with FROM, OUTPUT)
  • DELETE (with FROM, OUTPUT)
  • MERGE (all WHEN clauses)
  • TRUNCATE TABLE
DDL
  • CREATE/ALTER/DROP TABLE
  • CREATE/ALTER/DROP INDEX
  • CREATE/ALTER/DROP VIEW
  • CREATE/ALTER/DROP PROCEDURE
  • CREATE/ALTER/DROP FUNCTION
  • CREATE/ALTER/DROP TRIGGER
  • CREATE/ALTER/DROP DATABASE
  • CREATE/ALTER/DROP SCHEMA
Control Flow
  • IF/ELSE
  • WHILE
  • BEGIN/END
  • TRY/CATCH
  • GOTO
  • RETURN
  • BREAK/CONTINUE
  • WAITFOR
Transactions
  • BEGIN/COMMIT/ROLLBACK TRANSACTION
  • SAVE TRANSACTION
Other
  • DECLARE variables
  • SET statements
  • EXECUTE/EXEC
  • Cursors (DECLARE, OPEN, FETCH, CLOSE, DEALLOCATE)
  • GRANT/DENY/REVOKE
  • BACKUP/RESTORE
  • DBCC commands
  • BULK INSERT

Project Structure

tsqlparser/
├── token/          # Token types and keywords
├── lexer/          # Lexical analysis
├── ast/            # Abstract syntax tree nodes
├── parser/         # Recursive descent parser
├── testdata/       # 201 T-SQL sample files for integration testing
├── cmd/example/    # Example usage
├── tsqlparser.go   # Main API
└── go.mod

Testing

Run all tests:

go test ./...

Run integration tests against the T-SQL corpus:

go test ./parser -run TestCorpusIntegration -v

Run a specific sample file:

go test ./parser -run TestCorpusSamples/001_error_handler -v

Run benchmarks:

go test ./parser -bench=.

Run corpus benchmarks:

go test ./parser -bench=Corpus -benchtime=1s

Performance

  • Simple SELECT: ~23 μs
  • Complex query with JOINs: ~34 μs
  • Full corpus (201 files, 1.6 MB): ~27 MB/s throughput

License

Copyright (C) 2025 haitch

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.

See https://www.gnu.org/licenses/gpl-3.0.html for the full license text.

Contact

h@ual.fi

Documentation

Overview

Package tsqlparser provides a parser for Microsoft T-SQL.

This package can be used to parse T-SQL stored procedures and other T-SQL statements into an Abstract Syntax Tree (AST) that can be analyzed and interpreted in Go.

Example usage:

program, errors := tsqlparser.Parse(tsqlCode)
if len(errors) > 0 {
    // handle errors
}
// work with program.Statements

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(input string) (*ast.Program, []string)

Parse parses T-SQL code and returns the AST and any errors.

func Tokenize

func Tokenize(input string) []token.Token

Tokenize returns all tokens from the input.

func Walk

func Walk(v Visitor, node ast.Node)

Walk traverses an AST in depth-first order.

Types

type AlterFunctionStatement

type AlterFunctionStatement = ast.AlterFunctionStatement

Helper types

type AlterIndexStatement

type AlterIndexStatement = ast.AlterIndexStatement

ALTER INDEX / BULK INSERT

type AlterProcedureStatement

type AlterProcedureStatement = ast.AlterProcedureStatement

Helper types

type AlterTableAction

type AlterTableAction = ast.AlterTableAction

Helper types

type AlterTableStatement

type AlterTableStatement = ast.AlterTableStatement

Statement types

type AlterTriggerStatement

type AlterTriggerStatement = ast.AlterTriggerStatement

Helper types

type AlterViewStatement

type AlterViewStatement = ast.AlterViewStatement

Helper types

type BeginEndBlock

type BeginEndBlock = ast.BeginEndBlock

Statement types

type BeginTransactionStatement

type BeginTransactionStatement = ast.BeginTransactionStatement

Statement types

type BetweenExpression

type BetweenExpression = ast.BetweenExpression

Expression types

type BinaryLiteral

type BinaryLiteral = ast.BinaryLiteral

Expression types

type BreakStatement

type BreakStatement = ast.BreakStatement

Statement types

type BulkInsertStatement

type BulkInsertStatement = ast.BulkInsertStatement

Helper types

type CTEDef

type CTEDef = ast.CTEDef

Helper types

type CaseExpression

type CaseExpression = ast.CaseExpression

Expression types

type CastExpression

type CastExpression = ast.CastExpression

Type conversion

type CloseCursorStatement

type CloseCursorStatement = ast.CloseCursorStatement

Statement types

type CollateExpression

type CollateExpression = ast.CollateExpression

COLLATE and OPTION

type ColumnConstraint

type ColumnConstraint = ast.ColumnConstraint

Helper types

type ColumnDefinition

type ColumnDefinition = ast.ColumnDefinition

Stage 1: Table Infrastructure

type CommitTransactionStatement

type CommitTransactionStatement = ast.CommitTransactionStatement

Statement types

type ContinueStatement

type ContinueStatement = ast.ContinueStatement

Statement types

type ConvertExpression

type ConvertExpression = ast.ConvertExpression

Helper types

type CreateFunctionStatement

type CreateFunctionStatement = ast.CreateFunctionStatement

Helper types

type CreateIndexStatement

type CreateIndexStatement = ast.CreateIndexStatement

Helper types

type CreateProcedureStatement

type CreateProcedureStatement = ast.CreateProcedureStatement

Statement types

type CreateTableStatement

type CreateTableStatement = ast.CreateTableStatement

Stage 1: Table Infrastructure

type CreateTriggerStatement

type CreateTriggerStatement = ast.CreateTriggerStatement

Helper types

type CreateViewStatement

type CreateViewStatement = ast.CreateViewStatement

Stage 5: DDL Completion

type CursorOptions

type CursorOptions = ast.CursorOptions

Stage 2: Cursor Support

type DataType

type DataType = ast.DataType

Helper types

type DeallocateCursorStatement

type DeallocateCursorStatement = ast.DeallocateCursorStatement

Statement types

type DeclareCursorStatement

type DeclareCursorStatement = ast.DeclareCursorStatement

Stage 2: Cursor Support

type DeclareStatement

type DeclareStatement = ast.DeclareStatement

Statement types

type DeleteStatement

type DeleteStatement = ast.DeleteStatement

Statement types

type DerivedTable

type DerivedTable = ast.DerivedTable

Helper types

type DropIndexStatement

type DropIndexStatement = ast.DropIndexStatement

Helper types

type DropObjectStatement

type DropObjectStatement = ast.DropObjectStatement

Helper types

type DropTableStatement

type DropTableStatement = ast.DropTableStatement

Statement types

type ExecParameter

type ExecParameter = ast.ExecParameter

Helper types

type ExecStatement

type ExecStatement = ast.ExecStatement

Statement types

type ExistsExpression

type ExistsExpression = ast.ExistsExpression

Expression types

type Expression

type Expression = ast.Expression

Re-export types for convenience

type FetchStatement

type FetchStatement = ast.FetchStatement

Statement types

type FloatLiteral

type FloatLiteral = ast.FloatLiteral

Expression types

type ForClause

type ForClause = ast.ForClause

FOR XML/JSON

type FrameBound

type FrameBound = ast.FrameBound

Helper types

type FromClause

type FromClause = ast.FromClause

Helper types

type FunctionCall

type FunctionCall = ast.FunctionCall

Expression types

type FunctionType

type FunctionType = ast.FunctionType

Helper types

type GoStatement

type GoStatement = ast.GoStatement

Statement types

type GotoStatement

type GotoStatement = ast.GotoStatement

Helper types

type Identifier

type Identifier = ast.Identifier

Expression types

type IdentitySpec

type IdentitySpec = ast.IdentitySpec

Helper types

type IfStatement

type IfStatement = ast.IfStatement

Statement types

type InExpression

type InExpression = ast.InExpression

Expression types

type IndexColumn

type IndexColumn = ast.IndexColumn

Helper types

type InfixExpression

type InfixExpression = ast.InfixExpression

Expression types

type InsertStatement

type InsertStatement = ast.InsertStatement

Statement types

type Inspector

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

Inspector provides a convenient way to inspect AST nodes.

func NewInspector

func NewInspector(program *ast.Program) *Inspector

NewInspector creates a new Inspector for the given program.

func (*Inspector) FindFunctionCalls

func (insp *Inspector) FindFunctionCalls() []*ast.FunctionCall

FindFunctionCalls returns all function calls in the AST.

func (*Inspector) FindSelectStatements

func (insp *Inspector) FindSelectStatements() []*ast.SelectStatement

FindSelectStatements returns all SELECT statements in the AST.

func (*Inspector) FindVariables

func (insp *Inspector) FindVariables() []*ast.Variable

FindVariables returns all variable references in the AST.

type IntegerLiteral

type IntegerLiteral = ast.IntegerLiteral

Expression types

type IsNullExpression

type IsNullExpression = ast.IsNullExpression

Expression types

type JoinClause

type JoinClause = ast.JoinClause

Helper types

type LabelStatement

type LabelStatement = ast.LabelStatement

Helper types

type LikeExpression

type LikeExpression = ast.LikeExpression

Expression types

type MergeActionType

type MergeActionType = ast.MergeActionType

Helper types

type MergeStatement

type MergeStatement = ast.MergeStatement

Stage 4b: MERGE Statement

type MergeWhenClause

type MergeWhenClause = ast.MergeWhenClause

Helper types

type MergeWhenType

type MergeWhenType = ast.MergeWhenType

Helper types

type NextValueForExpression

type NextValueForExpression = ast.NextValueForExpression

Stage 10 additions

type NullLiteral

type NullLiteral = ast.NullLiteral

Expression types

type OpenCursorStatement

type OpenCursorStatement = ast.OpenCursorStatement

Statement types

type OrderByItem

type OrderByItem = ast.OrderByItem

Helper types

type OutputClause

type OutputClause = ast.OutputClause

Stage 4a: OUTPUT and DML Enhancements

type OverClause

type OverClause = ast.OverClause

Helper types

type ParameterDef

type ParameterDef = ast.ParameterDef

Helper types

type ParseExpression

type ParseExpression = ast.ParseExpression

Helper types

type PivotTable

type PivotTable = ast.PivotTable

PIVOT/UNPIVOT

type PrefixExpression

type PrefixExpression = ast.PrefixExpression

Expression types

type PrintStatement

type PrintStatement = ast.PrintStatement

Statement types

type Program

type Program = ast.Program

Re-export types for convenience

type QualifiedIdentifier

type QualifiedIdentifier = ast.QualifiedIdentifier

Expression types

type QueryOption

type QueryOption = ast.QueryOption

Helper types

type RaiserrorStatement

type RaiserrorStatement = ast.RaiserrorStatement

Statement types

type ReturnStatement

type ReturnStatement = ast.ReturnStatement

Statement types

type RollbackTransactionStatement

type RollbackTransactionStatement = ast.RollbackTransactionStatement

Statement types

type SaveTransactionStatement

type SaveTransactionStatement = ast.SaveTransactionStatement

Helper types

type SelectColumn

type SelectColumn = ast.SelectColumn

Helper types

type SelectStatement

type SelectStatement = ast.SelectStatement

Statement types

type SetClause

type SetClause = ast.SetClause

Helper types

type SetOptionStatement

type SetOptionStatement = ast.SetOptionStatement

Helper types

type SetStatement

type SetStatement = ast.SetStatement

Statement types

type SetTransactionIsolationStatement

type SetTransactionIsolationStatement = ast.SetTransactionIsolationStatement

Helper types

type Statement

type Statement = ast.Statement

Re-export types for convenience

type StringLiteral

type StringLiteral = ast.StringLiteral

Expression types

type SubqueryExpression

type SubqueryExpression = ast.SubqueryExpression

Expression types

type TableConstraint

type TableConstraint = ast.TableConstraint

Helper types

type TableName

type TableName = ast.TableName

Helper types

type TableTypeDefinition

type TableTypeDefinition = ast.TableTypeDefinition

Helper types

type TableValuedFunction

type TableValuedFunction = ast.TableValuedFunction

Helper types

type ThrowStatement

type ThrowStatement = ast.ThrowStatement

Statement types

type Token

type Token = token.Token

Re-export types for convenience

type TopClause

type TopClause = ast.TopClause

Helper types

type TriggerTiming

type TriggerTiming = ast.TriggerTiming

Helper types

type TruncateTableStatement

type TruncateTableStatement = ast.TruncateTableStatement

Statement types

type TryCatchStatement

type TryCatchStatement = ast.TryCatchStatement

Statement types

type TupleExpression

type TupleExpression = ast.TupleExpression

Helper types

type UnionClause

type UnionClause = ast.UnionClause

Stage 6: Set Operations & Grouping

type UnpivotTable

type UnpivotTable = ast.UnpivotTable

Helper types

type UpdateStatement

type UpdateStatement = ast.UpdateStatement

Statement types

type UseStatement

type UseStatement = ast.UseStatement

Stage 7a: Quick Wins

type ValuesTable

type ValuesTable = ast.ValuesTable

Helper types

type Variable

type Variable = ast.Variable

Expression types

type VariableDef

type VariableDef = ast.VariableDef

Helper types

type Visitor

type Visitor interface {
	Visit(node ast.Node) Visitor
}

Visitor defines an interface for AST visitors.

type WaitforStatement

type WaitforStatement = ast.WaitforStatement

Helper types

type WhenClause

type WhenClause = ast.WhenClause

Helper types

type WhileStatement

type WhileStatement = ast.WhileStatement

Statement types

type WindowFrame

type WindowFrame = ast.WindowFrame

Stage 3: Query Enhancements

type WithStatement

type WithStatement = ast.WithStatement

Statement types

Directories

Path Synopsis
Package ast defines the Abstract Syntax Tree nodes for T-SQL.
Package ast defines the Abstract Syntax Tree nodes for T-SQL.
cmd
example command
Example: Parsing and analyzing T-SQL stored procedures
Example: Parsing and analyzing T-SQL stored procedures
Package lexer implements a lexical scanner for T-SQL.
Package lexer implements a lexical scanner for T-SQL.
Package parser implements a parser for T-SQL.
Package parser implements a parser for T-SQL.
Package token defines constants representing the lexical tokens of T-SQL.
Package token defines constants representing the lexical tokens of T-SQL.

Jump to

Keyboard shortcuts

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