parseLimitOffsetClause parses optional LIMIT and/or OFFSET clauses. Supports standard "LIMIT n OFFSET m", MySQL "LIMIT offset, count", and SQL-99 "OFFSET n ROWS" (ROW/ROWS consumed but value stored). Returns (limit, offset, error); either or both pointers may be nil.
()
| 607 | // SQL-99 "OFFSET n ROWS" (ROW/ROWS consumed but value stored). |
| 608 | // Returns (limit, offset, error); either or both pointers may be nil. |
| 609 | func (p *Parser) parseLimitOffsetClause() (limit *int, offset *int, err error) { |
| 610 | // LIMIT clause |
| 611 | if p.isType(models.TokenTypeLimit) { |
| 612 | // Reject LIMIT in SQL Server and Oracle - these dialects use TOP/OFFSET-FETCH or ROWNUM/FETCH FIRST. |
| 613 | if p.dialect == string(keywords.DialectSQLServer) || p.dialect == string(keywords.DialectOracle) { |
| 614 | msg := "LIMIT clause is not supported in SQL Server; use TOP or OFFSET/FETCH NEXT instead" |
| 615 | if p.dialect == string(keywords.DialectOracle) { |
| 616 | msg = "LIMIT clause is not supported in Oracle; use ROWNUM or FETCH FIRST … ROWS ONLY instead" |
| 617 | } |
| 618 | return nil, nil, fmt.Errorf("%s", msg) |
| 619 | } |
| 620 | p.advance() // Consume LIMIT |
| 621 | |
| 622 | if !p.isNumericLiteral() { |
| 623 | return nil, nil, p.expectedError("integer for LIMIT") |
| 624 | } |
| 625 | firstVal := 0 |
| 626 | _, _ = fmt.Sscanf(p.currentToken.Token.Value, "%d", &firstVal) |
| 627 | p.advance() |
| 628 | |
| 629 | // MySQL: LIMIT offset, count |
| 630 | if p.dialect == "mysql" && p.isType(models.TokenTypeComma) { |
| 631 | p.advance() |
| 632 | if !p.isNumericLiteral() { |
| 633 | return nil, nil, p.expectedError("integer for LIMIT count") |
| 634 | } |
| 635 | secondVal := 0 |
| 636 | _, _ = fmt.Sscanf(p.currentToken.Token.Value, "%d", &secondVal) |
| 637 | p.advance() |
| 638 | offset = &firstVal |
| 639 | limit = &secondVal |
| 640 | } else { |
| 641 | limit = &firstVal |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | // OFFSET clause |
| 646 | if p.isType(models.TokenTypeOffset) { |
| 647 | p.advance() |
| 648 | |
| 649 | if !p.isNumericLiteral() { |
| 650 | return nil, nil, p.expectedError("integer for OFFSET") |
| 651 | } |
| 652 | offsetVal := 0 |
| 653 | _, _ = fmt.Sscanf(p.currentToken.Token.Value, "%d", &offsetVal) |
| 654 | offset = &offsetVal |
| 655 | p.advance() |
| 656 | |
| 657 | // SQL-99: OFFSET n ROWS |
| 658 | if p.isAnyType(models.TokenTypeRow, models.TokenTypeRows) { |
| 659 | p.advance() |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | return limit, offset, nil |
| 664 | } |
| 665 | |
| 666 | // parseFetchClause parses the SQL-99 FETCH FIRST/NEXT clause (F861, F862). |
no test coverage detected