parseInsertCommand - Return ast.InsertCommand created from tokens and validate the syntax Example of input parsable to the ast.InsertCommand: insert into tbl values( 'hello', 10 );
()
| 157 | // Example of input parsable to the ast.InsertCommand: |
| 158 | // insert into tbl values( 'hello', 10 ); |
| 159 | func (parser *Parser) parseInsertCommand() (ast.Command, error) { |
| 160 | // token.INSERT already at current position in parser |
| 161 | insertCommand := &ast.InsertCommand{Token: parser.currentToken} |
| 162 | |
| 163 | // Ignore token.INSERT |
| 164 | parser.nextToken() |
| 165 | |
| 166 | err := validateTokenAndSkip(parser, []token.Type{token.INTO}) |
| 167 | if err != nil { |
| 168 | return nil, err |
| 169 | } |
| 170 | |
| 171 | err = validateToken(parser.currentToken.Type, []token.Type{token.IDENT}) |
| 172 | if err != nil { |
| 173 | return nil, err |
| 174 | } |
| 175 | insertCommand.Name = ast.Identifier{Token: parser.currentToken} |
| 176 | // Ignore token.INDENT |
| 177 | parser.nextToken() |
| 178 | |
| 179 | err = validateTokenAndSkip(parser, []token.Type{token.VALUES}) |
| 180 | if err != nil { |
| 181 | return nil, err |
| 182 | } |
| 183 | err = validateTokenAndSkip(parser, []token.Type{token.LPAREN}) |
| 184 | if err != nil { |
| 185 | return nil, err |
| 186 | } |
| 187 | |
| 188 | for parser.currentToken.Type == token.IDENT || parser.currentToken.Type == token.LITERAL || parser.currentToken.Type == token.NULL || parser.currentToken.Type == token.APOSTROPHE { |
| 189 | startedWithApostrophe := parser.skipIfCurrentTokenIsApostrophe() |
| 190 | |
| 191 | err = validateToken(parser.currentToken.Type, []token.Type{token.IDENT, token.LITERAL, token.NULL}) |
| 192 | if err != nil { |
| 193 | return nil, err |
| 194 | } |
| 195 | value := parser.currentToken |
| 196 | insertCommand.Values = append(insertCommand.Values, value) |
| 197 | // Ignore token.IDENT, token.LITERAL or token.NULL |
| 198 | parser.nextToken() |
| 199 | |
| 200 | finishedWithApostrophe := parser.skipIfCurrentTokenIsApostrophe() |
| 201 | |
| 202 | err = validateApostropheWrapping(startedWithApostrophe, finishedWithApostrophe, value) |
| 203 | |
| 204 | if err != nil { |
| 205 | return nil, err |
| 206 | } |
| 207 | |
| 208 | if parser.currentToken.Type != token.COMMA { |
| 209 | break |
| 210 | } |
| 211 | |
| 212 | // Ignore token.COMMA |
| 213 | parser.nextToken() |
| 214 | } |
| 215 | |
| 216 | err = validateTokenAndSkip(parser, []token.Type{token.RPAREN}) |
no test coverage detected