MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / parseFetchClause

Method parseFetchClause

pkg/sql/parser/select_clauses.go:675–731  ·  view source on GitHub ↗

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

()

Source from the content-addressed store, hash-verified

673// FETCH FIRST 10 PERCENT ROWS WITH TIES
674// FETCH NEXT 20 ROWS WITH TIES
675func (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

Callers 1

parseSelectStatementMethod · 0.95

Calls 5

advanceMethod · 0.95
isTypeMethod · 0.95
expectedErrorMethod · 0.95
isNumericLiteralMethod · 0.95
isAnyTypeMethod · 0.95

Tested by

no test coverage detected