-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can I get the original text of a parser node? #55441
Comments
Try use func TestA(t *testing.T) {
statement := `
CREATE VIEW v1 AS SELECT * FROM t;
CREATE VIEW v2 AS SELECT 123123123123123;
`
p := parser.New()
// To support MySQL8 window function syntax.
// See https://github.com/bytebase/bytebase/issues/175.
p.EnableWindowFunc(true)
nodes, _, err := p.Parse(statement, "", "")
if err != nil {
panic(err)
}
var sb strings.Builder
for _, node := range nodes {
if node, ok := node.(*ast.CreateViewStmt); ok {
sb.Reset()
viewName := node.ViewName.Name.O
// selectText := node.Select.Text()
node.Select.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &sb))
fmt.Printf("%s: %s\n\n\n\n", viewName, sb.String())
}
}
} |
Hi @Defined2014 , thanks for your response. These are two requirements, when I need to get normalized SQL, I should use |
Ok, I think we didn't call |
Related docs: tidb/docs/design/2018-11-29-ast-to-sql-text.md Lines 11 to 25 in f272707
|
I get the output:
IMO, the original text should return
SELECT * FROM t;
ofv1
.The text was updated successfully, but these errors were encountered: