ParseBytes parses SQL from a []byte input without requiring a string conversion. This is especially useful when reading SQL from files via os.ReadFile. See issue #277.
(input []byte)
| 96 | // This is especially useful when reading SQL from files via os.ReadFile. |
| 97 | // See issue #277. |
| 98 | func ParseBytes(input []byte) (*ast.AST, error) { |
| 99 | tkz := tokenizer.GetTokenizer() |
| 100 | defer tokenizer.PutTokenizer(tkz) |
| 101 | |
| 102 | tokens, err := tkz.Tokenize(input) |
| 103 | if err != nil { |
| 104 | return nil, fmt.Errorf("tokenization error: %w", err) |
| 105 | } |
| 106 | |
| 107 | if len(tokens) == 0 { |
| 108 | return nil, goerrors.IncompleteStatementError(models.Location{}, "") |
| 109 | } |
| 110 | |
| 111 | p := GetParser() |
| 112 | defer PutParser(p) |
| 113 | |
| 114 | return p.ParseFromModelTokens(tokens) |
| 115 | } |
| 116 | |
| 117 | // ParseBytesWithTokens is like ParseBytes but also returns the preprocessed |
| 118 | // token slice ([]models.TokenWithSpan) for callers that need both. |