ParseTiDB parses the given SQL statement and returns the AST.
(sql string, charset string, collation string)
| 102 | |
| 103 | // ParseTiDB parses the given SQL statement and returns the AST. |
| 104 | func ParseTiDB(sql string, charset string, collation string) ([]ast.StmtNode, error) { |
| 105 | p := tidbparser.New() |
| 106 | |
| 107 | // To support MySQL8 window function syntax. |
| 108 | // See https://github.com/bytebase/bytebase/issues/175. |
| 109 | p.EnableWindowFunc(true) |
| 110 | mode, err := mysql.GetSQLMode(mysql.DefaultSQLMode) |
| 111 | if err != nil { |
| 112 | return nil, errors.Errorf("failed to get sql mode: %v", err) |
| 113 | } |
| 114 | mode = mysql.DelSQLMode(mode, mysql.ModeNoZeroDate) |
| 115 | mode = mysql.DelSQLMode(mode, mysql.ModeNoZeroInDate) |
| 116 | p.SetSQLMode(mode) |
| 117 | |
| 118 | nodes, _, err := p.Parse(sql, charset, collation) |
| 119 | if err != nil { |
| 120 | return nil, convertParserError(err) |
| 121 | } |
| 122 | return nodes, nil |
| 123 | } |
| 124 | |
| 125 | var ( |
| 126 | lineColumnRegex = regexp.MustCompile(`line (\d+) column (\d+)`) |