parseUpdateCommand - Return ast.parseUpdateCommand created from tokens and validate the syntax Example of input parsable to the ast.parseUpdateCommand: UPDATE table SET col1 TO 'value' WHERE col2 EQUAL 10;
()
| 621 | // Example of input parsable to the ast.parseUpdateCommand: |
| 622 | // UPDATE table SET col1 TO 'value' WHERE col2 EQUAL 10; |
| 623 | func (parser *Parser) parseUpdateCommand() (ast.Command, error) { |
| 624 | // token.UPDATE already at current position in parser |
| 625 | updateCommand := &ast.UpdateCommand{Token: parser.currentToken} |
| 626 | |
| 627 | // Ignore token.UPDATE |
| 628 | parser.nextToken() |
| 629 | |
| 630 | // Get table name |
| 631 | err := validateToken(parser.currentToken.Type, []token.Type{token.IDENT}) |
| 632 | if err != nil { |
| 633 | return nil, err |
| 634 | } |
| 635 | updateCommand.Name = ast.Identifier{Token: parser.currentToken} |
| 636 | |
| 637 | // Ignore token.IDENT |
| 638 | parser.nextToken() |
| 639 | |
| 640 | err = validateTokenAndSkip(parser, []token.Type{token.SET}) |
| 641 | if err != nil { |
| 642 | return nil, err |
| 643 | } |
| 644 | |
| 645 | err = validateToken(parser.currentToken.Type, []token.Type{token.IDENT}) |
| 646 | if err != nil { |
| 647 | return nil, err |
| 648 | } |
| 649 | |
| 650 | updateCommand.Changes = make(map[token.Token]ast.Anonymitifier) |
| 651 | for parser.currentToken.Type == token.IDENT { |
| 652 | // Get column name |
| 653 | err := validateToken(parser.currentToken.Type, []token.Type{token.IDENT}) |
| 654 | if err != nil { |
| 655 | return nil, err |
| 656 | } |
| 657 | colKey := parser.currentToken |
| 658 | |
| 659 | // skip column name |
| 660 | parser.nextToken() |
| 661 | |
| 662 | err = validateToken(parser.currentToken.Type, []token.Type{token.TO}) |
| 663 | if err != nil { |
| 664 | return nil, err |
| 665 | } |
| 666 | // skip token.TO |
| 667 | parser.nextToken() |
| 668 | |
| 669 | startedWithApostrophe := parser.skipIfCurrentTokenIsApostrophe() |
| 670 | err = validateToken(parser.currentToken.Type, []token.Type{token.IDENT, token.LITERAL, token.NULL}) |
| 671 | if err != nil { |
| 672 | return nil, err |
| 673 | } |
| 674 | updateCommand.Changes[colKey] = ast.Anonymitifier{Token: parser.currentToken} |
| 675 | |
| 676 | // skip token.IDENT, token.LITERAL or token.NULL |
| 677 | parser.nextToken() |
| 678 | finishedWithApostrophe := parser.skipIfCurrentTokenIsApostrophe() |
| 679 | |
| 680 | err = validateApostropheWrapping(startedWithApostrophe, finishedWithApostrophe, updateCommand.Changes[colKey].GetToken()) |
no test coverage detected