parseMergeStatement parses a MERGE statement (SQL:2003 F312) Syntax: MERGE INTO target [AS alias] USING source [AS alias] ON condition WHEN MATCHED [AND condition] THEN UPDATE/DELETE WHEN NOT MATCHED [AND condition] THEN INSERT WHEN NOT MATCHED BY SOURCE [AND condition] THEN UPDATE/DELETE
()
| 34 | // WHEN NOT MATCHED [AND condition] THEN INSERT |
| 35 | // WHEN NOT MATCHED BY SOURCE [AND condition] THEN UPDATE/DELETE |
| 36 | func (p *Parser) parseMergeStatement() (ast.Statement, error) { |
| 37 | stmt := &ast.MergeStatement{} |
| 38 | |
| 39 | // Parse INTO (optional) |
| 40 | if p.isType(models.TokenTypeInto) { |
| 41 | p.advance() // Consume INTO |
| 42 | } |
| 43 | |
| 44 | // Parse target table |
| 45 | tableRef, err := p.parseTableReference() |
| 46 | if err != nil { |
| 47 | return nil, goerrors.WrapError(goerrors.ErrCodeInvalidSyntax, "error parsing MERGE target table", models.Location{}, "", err) |
| 48 | } |
| 49 | stmt.TargetTable = *tableRef |
| 50 | |
| 51 | // Parse optional target alias (AS alias or just alias) |
| 52 | if p.isType(models.TokenTypeAs) { |
| 53 | p.advance() // Consume AS |
| 54 | if !p.isIdentifier() && !p.isNonReservedKeyword() { |
| 55 | return nil, p.expectedError("target alias after AS") |
| 56 | } |
| 57 | stmt.TargetAlias = p.currentToken.Token.Value |
| 58 | p.advance() |
| 59 | } else if p.canBeAlias() && !p.isType(models.TokenTypeUsing) && p.currentToken.Token.Value != "USING" { |
| 60 | stmt.TargetAlias = p.currentToken.Token.Value |
| 61 | p.advance() |
| 62 | } |
| 63 | |
| 64 | // Parse USING |
| 65 | if !p.isType(models.TokenTypeUsing) && p.currentToken.Token.Value != "USING" { |
| 66 | return nil, p.expectedError("USING") |
| 67 | } |
| 68 | p.advance() // Consume USING |
| 69 | |
| 70 | // Parse source table (could be a table or subquery) |
| 71 | sourceRef, err := p.parseTableReference() |
| 72 | if err != nil { |
| 73 | return nil, goerrors.WrapError(goerrors.ErrCodeInvalidSyntax, "error parsing MERGE source", models.Location{}, "", err) |
| 74 | } |
| 75 | stmt.SourceTable = *sourceRef |
| 76 | |
| 77 | // Parse optional source alias |
| 78 | if p.isType(models.TokenTypeAs) { |
| 79 | p.advance() // Consume AS |
| 80 | if !p.isIdentifier() && !p.isNonReservedKeyword() { |
| 81 | return nil, p.expectedError("source alias after AS") |
| 82 | } |
| 83 | stmt.SourceAlias = p.currentToken.Token.Value |
| 84 | p.advance() |
| 85 | } else if p.canBeAlias() && !p.isType(models.TokenTypeOn) && p.currentToken.Token.Value != "ON" { |
| 86 | stmt.SourceAlias = p.currentToken.Token.Value |
| 87 | p.advance() |
| 88 | } |
| 89 | |
| 90 | // Parse ON condition |
| 91 | if !p.isType(models.TokenTypeOn) { |
| 92 | return nil, p.expectedError("ON") |
| 93 | } |
no test coverage detected