parseUpdateStatement parses an UPDATE statement
()
| 26 | |
| 27 | // parseUpdateStatement parses an UPDATE statement |
| 28 | func (p *Parser) parseUpdateStatement() (ast.Statement, error) { |
| 29 | // We've already consumed the UPDATE token in matchType |
| 30 | |
| 31 | // Parse table name (supports schema.table qualification and double-quoted identifiers) |
| 32 | tableName, err := p.parseQualifiedName() |
| 33 | if err != nil { |
| 34 | return nil, p.expectedError("table name") |
| 35 | } |
| 36 | |
| 37 | // Parse SET |
| 38 | if !p.isType(models.TokenTypeSet) { |
| 39 | return nil, p.expectedError("SET") |
| 40 | } |
| 41 | p.advance() // Consume SET |
| 42 | |
| 43 | // Parse assignments |
| 44 | updates := make([]ast.UpdateExpression, 0) |
| 45 | for { |
| 46 | // Parse column name (supports double-quoted identifiers) |
| 47 | if !p.isIdentifier() { |
| 48 | return nil, p.expectedError("column name") |
| 49 | } |
| 50 | columnName := p.currentToken.Token.Value |
| 51 | p.advance() |
| 52 | |
| 53 | if !p.isType(models.TokenTypeEq) { |
| 54 | return nil, p.expectedError("=") |
| 55 | } |
| 56 | p.advance() // Consume = |
| 57 | |
| 58 | // Parse value expression |
| 59 | var expr ast.Expression |
| 60 | if p.isStringLiteral() { |
| 61 | expr = &ast.LiteralValue{Value: p.currentToken.Token.Value, Type: "string"} |
| 62 | p.advance() |
| 63 | } else if p.isNumericLiteral() { |
| 64 | litType := "int" |
| 65 | if strings.ContainsAny(p.currentToken.Token.Value, ".eE") { |
| 66 | litType = "float" |
| 67 | } |
| 68 | expr = &ast.LiteralValue{Value: p.currentToken.Token.Value, Type: litType} |
| 69 | p.advance() |
| 70 | } else if p.isBooleanLiteral() { |
| 71 | expr = &ast.LiteralValue{Value: p.currentToken.Token.Value, Type: "bool"} |
| 72 | p.advance() |
| 73 | } else { |
| 74 | var err error |
| 75 | expr, err = p.parseExpression() |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Create update expression |
| 82 | columnExpr := &ast.Identifier{Name: columnName} |
| 83 | updateExpr := ast.UpdateExpression{ |
| 84 | Column: columnExpr, |
| 85 | Value: expr, |
no test coverage detected