ParseSQLWithDialect parses a SQL string using dialect-specific tokenization and parsing rules. This enables correct handling of dialect-specific syntax such as SQL Server TOP, MySQL backtick identifiers, and Snowflake QUALIFY. Use this when the input SQL uses dialect-specific constructs that the ge
(sql string, dialect keywords.SQLDialect)
| 256 | // |
| 257 | // tree, err := transform.ParseSQLWithDialect("SELECT TOP 10 * FROM users", keywords.DialectSQLServer) |
| 258 | func ParseSQLWithDialect(sql string, dialect keywords.SQLDialect) (*ast.AST, error) { |
| 259 | tkz := tokenizer.GetTokenizer() |
| 260 | defer tokenizer.PutTokenizer(tkz) |
| 261 | |
| 262 | if dialect != "" { |
| 263 | tkz.SetDialect(dialect) |
| 264 | } |
| 265 | |
| 266 | tokens, err := tkz.Tokenize([]byte(sql)) |
| 267 | if err != nil { |
| 268 | return nil, fmt.Errorf("tokenize: %w", err) |
| 269 | } |
| 270 | |
| 271 | p := parser.NewParser(parser.WithDialect(string(dialect))) |
| 272 | defer p.Release() |
| 273 | |
| 274 | tree, err := p.ParseFromModelTokens(tokens) |
| 275 | if err != nil { |
| 276 | return nil, fmt.Errorf("parse: %w", err) |
| 277 | } |
| 278 | |
| 279 | return tree, nil |
| 280 | } |