Skip to content

Commit aca04fa

Browse files
kyleconroyclaude
andauthored
Write README with usage example (#22)
* Add README with usage example Document the teesql T-SQL parser with installation instructions and a complete code example showing parsing and JSON output. * Fix README: add SqlScriptDOM link and correct org name * Add encoding/json example, remove license section --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent e1f07e8 commit aca04fa

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# teesql
2+
3+
A T-SQL parser for Go that produces JSON AST output compatible with Microsoft's [SqlScriptDOM](https://learn.microsoft.com/en-us/dotnet/api/microsoft.sqlserver.transactsql.scriptdom).
4+
5+
## Installation
6+
7+
```bash
8+
go get github.com/sqlc-dev/teesql
9+
```
10+
11+
## Usage
12+
13+
```go
14+
package main
15+
16+
import (
17+
"context"
18+
"fmt"
19+
"strings"
20+
21+
"github.com/sqlc-dev/teesql/parser"
22+
)
23+
24+
func main() {
25+
sql := "SELECT id, name FROM users WHERE active = 1"
26+
27+
script, err := parser.Parse(context.Background(), strings.NewReader(sql))
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
json, err := parser.MarshalScript(script)
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
fmt.Println(string(json))
38+
}
39+
```
40+
41+
Output:
42+
43+
```json
44+
{
45+
"Batches": [
46+
{
47+
"Statements": [
48+
{
49+
"QueryExpression": {
50+
"SelectElements": [
51+
{"ColumnType": 2, "Identifier": {"Value": "id"}},
52+
{"ColumnType": 2, "Identifier": {"Value": "name"}}
53+
],
54+
"FromClause": {
55+
"TableReferences": [
56+
{"SchemaObject": {"BaseIdentifier": {"Value": "users"}}}
57+
]
58+
},
59+
"WhereClause": {
60+
"SearchCondition": {
61+
"FirstExpression": {"ColumnType": 2, "Identifier": {"Value": "active"}},
62+
"ComparisonType": 0,
63+
"SecondExpression": {"Value": "1"}
64+
}
65+
}
66+
}
67+
}
68+
]
69+
}
70+
]
71+
}
72+
```
73+
74+
## Using encoding/json
75+
76+
You can also use the standard library directly:
77+
78+
```go
79+
package main
80+
81+
import (
82+
"context"
83+
"encoding/json"
84+
"fmt"
85+
"strings"
86+
87+
"github.com/sqlc-dev/teesql/parser"
88+
)
89+
90+
func main() {
91+
sql := "SELECT 1"
92+
93+
script, err := parser.Parse(context.Background(), strings.NewReader(sql))
94+
if err != nil {
95+
panic(err)
96+
}
97+
98+
out, err := json.MarshalIndent(script, "", " ")
99+
if err != nil {
100+
panic(err)
101+
}
102+
103+
fmt.Println(string(out))
104+
}
105+
```

0 commit comments

Comments
 (0)