teesql

module
v0.0.0-...-2af7220 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT

README

teesql

Go Reference

A T-SQL parser for Go that produces JSON AST output compatible with Microsoft's SqlScriptDOM.

Installation

go get github.com/sqlc-dev/teesql

Usage

package main

import (
	"context"
	"fmt"
	"strings"

	"github.com/sqlc-dev/teesql/parser"
)

func main() {
	sql := "SELECT id, name FROM users WHERE active = 1"

	script, err := parser.Parse(context.Background(), strings.NewReader(sql))
	if err != nil {
		panic(err)
	}

	json, err := parser.MarshalScript(script)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(json))
}

Output:

{
  "Batches": [
    {
      "Statements": [
        {
          "QueryExpression": {
            "SelectElements": [
              {"ColumnType": 2, "Identifier": {"Value": "id"}},
              {"ColumnType": 2, "Identifier": {"Value": "name"}}
            ],
            "FromClause": {
              "TableReferences": [
                {"SchemaObject": {"BaseIdentifier": {"Value": "users"}}}
              ]
            },
            "WhereClause": {
              "SearchCondition": {
                "FirstExpression": {"ColumnType": 2, "Identifier": {"Value": "active"}},
                "ComparisonType": 0,
                "SecondExpression": {"Value": "1"}
              }
            }
          }
        }
      ]
    }
  ]
}

Using encoding/json

You can also use the standard library directly:

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"strings"

	"github.com/sqlc-dev/teesql/parser"
)

func main() {
	sql := "SELECT 1"

	script, err := parser.Parse(context.Background(), strings.NewReader(sql))
	if err != nil {
		panic(err)
	}

	out, err := json.MarshalIndent(script, "", "  ")
	if err != nil {
		panic(err)
	}

	fmt.Println(string(out))
}

Directories

Path Synopsis
Package ast provides AST types for T-SQL parsing.
Package ast provides AST types for T-SQL parsing.
cmd
next-test command
Package parser provides T-SQL parsing functionality.
Package parser provides T-SQL parsing functionality.

Jump to

Keyboard shortcuts

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