parseFetchClause parses the SQL-99 FETCH FIRST/NEXT clause (F861, F862). Syntax: FETCH {FIRST | NEXT} n [{ROW | ROWS}] [{PERCENT}] {ONLY | WITH TIES} Examples: FETCH FIRST 5 ROWS ONLY FETCH NEXT 10 ROWS ONLY FETCH FIRST 10 PERCENT ROWS WITH TIES FETCH NEXT 20 ROWS WITH TIES
()
| 673 | // FETCH FIRST 10 PERCENT ROWS WITH TIES |
| 674 | // FETCH NEXT 20 ROWS WITH TIES |
| 675 | func (p *Parser) parseFetchClause() (*ast.FetchClause, error) { |
| 676 | fetchClause := &ast.FetchClause{} |
| 677 | |
| 678 | // Consume FETCH keyword (already checked by caller) |
| 679 | p.advance() |
| 680 | |
| 681 | // Parse FIRST or NEXT |
| 682 | if p.isType(models.TokenTypeFirst) { |
| 683 | fetchClause.FetchType = "FIRST" |
| 684 | p.advance() |
| 685 | } else if p.isType(models.TokenTypeNext) { |
| 686 | fetchClause.FetchType = "NEXT" |
| 687 | p.advance() |
| 688 | } else { |
| 689 | return nil, p.expectedError("FIRST or NEXT after FETCH") |
| 690 | } |
| 691 | |
| 692 | // Parse the count value |
| 693 | if !p.isNumericLiteral() { |
| 694 | return nil, p.expectedError("integer for FETCH count") |
| 695 | } |
| 696 | |
| 697 | // Convert string to int64 |
| 698 | var fetchVal int64 |
| 699 | _, _ = fmt.Sscanf(p.currentToken.Token.Value, "%d", &fetchVal) |
| 700 | fetchClause.FetchValue = &fetchVal |
| 701 | p.advance() |
| 702 | |
| 703 | // Check for PERCENT (optional) |
| 704 | if p.isType(models.TokenTypePercent) { |
| 705 | fetchClause.IsPercent = true |
| 706 | p.advance() |
| 707 | } |
| 708 | |
| 709 | // Check for ROW/ROWS (optional) |
| 710 | if p.isAnyType(models.TokenTypeRow, models.TokenTypeRows) { |
| 711 | p.advance() // Consume ROW/ROWS |
| 712 | } |
| 713 | |
| 714 | // Parse ONLY or WITH TIES |
| 715 | if p.isType(models.TokenTypeOnly) { |
| 716 | fetchClause.WithTies = false |
| 717 | p.advance() |
| 718 | } else if p.isType(models.TokenTypeWith) { |
| 719 | p.advance() // Consume WITH |
| 720 | if !p.isType(models.TokenTypeTies) { |
| 721 | return nil, p.expectedError("TIES after WITH") |
| 722 | } |
| 723 | fetchClause.WithTies = true |
| 724 | p.advance() // Consume TIES |
| 725 | } else { |
| 726 | // If neither ONLY nor WITH TIES, default to ONLY behavior |
| 727 | fetchClause.WithTies = false |
| 728 | } |
| 729 | |
| 730 | return fetchClause, nil |
| 731 | } |
| 732 |
no test coverage detected