Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

example of parser details #34

Open
sunshine69 opened this issue Jun 10, 2022 · 3 comments
Open

example of parser details #34

sunshine69 opened this issue Jun 10, 2022 · 3 comments

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))

@WillianBR
Copy link

@skywhat It's amazing, but....

Your code is taking a fixed numbers of columns.
The example would be better if it dump everything!

  • All tables
  • All columns

And so on!

@skywhat
Copy link

skywhat commented Aug 26, 2024

@skywhat It's amazing, but....

Your code is taking a fixed numbers of columns. The example would be better if it dump everything!

  • All tables
  • All columns

And so on!

Try this code, it should work as expected.

package main

import (
	"fmt"

	"github.com/blastrain/vitess-sqlparser/sqlparser"
)

func main() {
	stmt, err := sqlparser.Parse("select hello, skywhat from user_items where user_id = 1 limit 10")
	if err != nil {
		panic(err)
	}

	// Get table name
	tableName := stmt.(*sqlparser.Select).From[0].(*sqlparser.AliasedTableExpr).Expr.(sqlparser.TableName).Name.String()

	// Traverse and collect all selected columns
	var selectCols []string
	for _, expr := range stmt.(*sqlparser.Select).SelectExprs {
		if aliasedExpr, ok := expr.(*sqlparser.AliasedExpr); ok {
			if colName, ok := aliasedExpr.Expr.(*sqlparser.ColName); ok {
				selectCols = append(selectCols, colName.Name.String())
			}
		}
	}

	// Get WHERE clause details
	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

	// Get LIMIT value
	limitNum := stmt.(*sqlparser.Select).Limit.Rowcount.(*sqlparser.SQLVal).Val

	// Print the result
	fmt.Printf("select %s from %s where %s %s %s limit %s",
		fmt.Sprintf("%s", selectCols), tableName, leftVal, operator, string(rightVal), string(limitNum))
}

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

No branches or pull requests

3 participants