-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathparse.go
More file actions
64 lines (52 loc) · 1.25 KB
/
parse.go
File metadata and controls
64 lines (52 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package clickhouse
import (
"bytes"
"context"
"io"
"github.com/sqlc-dev/doubleclick/parser"
"github.com/sqlc-dev/sqlc/internal/source"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
)
func NewParser() *Parser {
return &Parser{}
}
type Parser struct{}
func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
blob, err := io.ReadAll(r)
if err != nil {
return nil, err
}
ctx := context.Background()
stmtNodes, err := parser.Parse(ctx, bytes.NewReader(blob))
if err != nil {
return nil, err
}
var stmts []ast.Statement
for _, stmt := range stmtNodes {
converter := &cc{}
out := converter.convert(stmt)
if _, ok := out.(*ast.TODO); ok {
continue
}
// Get position information from the statement
pos := stmt.Pos()
end := stmt.End()
stmtLen := end.Offset - pos.Offset
stmts = append(stmts, ast.Statement{
Raw: &ast.RawStmt{
Stmt: out,
StmtLocation: pos.Offset,
StmtLen: stmtLen,
},
})
}
return stmts, nil
}
// https://clickhouse.com/docs/en/sql-reference/syntax#comments
func (p *Parser) CommentSyntax() source.CommentSyntax {
return source.CommentSyntax{
Dash: true, // -- comment
SlashStar: true, // /* comment */
Hash: true, // # comment (ClickHouse supports this)
}
}