Skip to content
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

example of parser details #34

Open
sunshine69 opened this issue Jun 10, 2022 · 1 comment
Open

example of parser details #34

sunshine69 opened this issue Jun 10, 2022 · 1 comment

Comments

@sunshine69
Copy link

sunshine69 commented Jun 10, 2022

Hi there,

From the example we just print the parse out. How can I get - that is like type (create or alter etc, DDL type, ) table name, columns etc, all details?

the thing I could see is using WalkSubtree and parse them out however it is not obvious to new users like me without reading code and finding types etc..

So a more example to show the detailed portion of the parsed stmt would be great.

Thanks

@skywhat
Copy link

skywhat commented Jul 23, 2022

stmt, err := sqlparser.Parse("select hello, skywhat from user_items where user_id = 1 limit 10")
if err != nil {
	panic(err)
}
tableName := stmt.(*sqlparser.Select).From[0].(*sqlparser.AliasedTableExpr).Expr.(sqlparser.TableName).Name.String()

selectCol0 := stmt.(*sqlparser.Select).SelectExprs[0].(*sqlparser.AliasedExpr).Expr.(*sqlparser.ColName).Name.String()
selectCol1 := stmt.(*sqlparser.Select).SelectExprs[1].(*sqlparser.AliasedExpr).Expr.(*sqlparser.ColName).Name.String()

operator := stmt.(*sqlparser.Select).Where.Expr.(*sqlparser.ComparisonExpr).Operator

leftVal := stmt.(*sqlparser.Select).Where.Expr.(*sqlparser.ComparisonExpr).Left.(*sqlparser.ColName).Name.String()

rightVal := stmt.(*sqlparser.Select).Where.Expr.(*sqlparser.ComparisonExpr).Right.(*sqlparser.SQLVal).Val

limitNum := stmt.(*sqlparser.Select).Limit.Rowcount.(*sqlparser.SQLVal).Val
fmt.Printf("select %s, %s from %s where %s %s %s limit %s", selectCol0, selectCol1, tableName, leftVal, operator, string(rightVal), string(limitNum))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants