addFetchNextClause adds a FETCH NEXT clause to a SELECT statement using AST parsing. This provides more precise placement of the limit clause compared to simple string wrapping.
(statement string, limitCount int)
| 223 | // addFetchNextClause adds a FETCH NEXT clause to a SELECT statement using AST parsing. |
| 224 | // This provides more precise placement of the limit clause compared to simple string wrapping. |
| 225 | func addFetchNextClause(statement string, limitCount int) (string, error) { |
| 226 | list, err := plsqlparser.ParsePLSQLOmni(statement) |
| 227 | if err != nil { |
| 228 | return "", err |
| 229 | } |
| 230 | if list == nil || len(list.Items) == 0 { |
| 231 | return "", errors.New("no parse results") |
| 232 | } |
| 233 | if len(list.Items) > 1 { |
| 234 | return "", errors.Errorf("expected single statement, got %d statements", len(list.Items)) |
| 235 | } |
| 236 | raw, ok := list.Items[0].(*oracleast.RawStmt) |
| 237 | if !ok { |
| 238 | return "", errors.Errorf("expected raw statement, got %T", list.Items[0]) |
| 239 | } |
| 240 | selectStmt, ok := raw.Stmt.(*oracleast.SelectStmt) |
| 241 | if !ok { |
| 242 | return statement, nil |
| 243 | } |
| 244 | |
| 245 | res, err := rewriteOracleSelectFetch(statement, selectStmt, limitCount) |
| 246 | if err != nil { |
| 247 | return "", err |
| 248 | } |
| 249 | // https://stackoverflow.com/questions/27987882/how-can-i-solve-ora-00911-invalid-character-error |
| 250 | res = strings.TrimRightFunc(res, utils.IsSpaceOrSemicolon) |
| 251 | |
| 252 | return res, nil |
| 253 | } |
| 254 | |
| 255 | func rewriteOracleSelectFetch(sql string, selectStmt *oracleast.SelectStmt, limitCount int) (string, error) { |
| 256 | target := rightmostOracleSetSelect(selectStmt) |