Cleanup parser statements, add delete and insert statement, add TASKS.md for tracking tasks

This commit is contained in:
Pablu
2025-12-02 17:06:27 +01:00
parent f6ca16b1f0
commit e07bb9e496
10 changed files with 438 additions and 266 deletions

View File

@@ -0,0 +1,69 @@
package sql
import "fmt"
func (p *Parser) parseInsert() (*InsertStatement, error) {
if !p.expectSequence(INTO, IDENT) {
return nil, p.unexpectedToken(INTO)
}
res := InsertStatement{}
_, _, res.Table = p.rescan()
if !p.expectNext(LPAREN) {
return nil, p.unexpectedToken(LPAREN)
}
fieldNames := make([]string, 0)
for loop := true; loop; {
_, tok, val := p.scan()
switch tok {
case IDENT:
fieldNames = append(fieldNames, val)
case RPAREN:
loop = false
case COMMA:
continue
default:
return nil, p.unexpectedToken(IDENT, RPAREN, COMMA)
}
}
if !p.expectSequence(VALUES, LPAREN) {
return nil, p.unexpectedToken()
}
values := make([]any, 0, len(fieldNames))
for loop := true; loop; {
_, tok, val := p.scan()
switch tok {
case IDENT:
// TODO, convert to actual datatype?
values = append(values, val)
case COMMA, QUOTE, SINGLE_QUOTE, BACKQUOTE:
continue
case RPAREN:
loop = false
default:
return nil, p.unexpectedToken(IDENT, RPAREN, COMMA)
}
}
if len(values) != len(fieldNames) {
return nil, fmt.Errorf("Expected same amount of Values as Fields, but got %v fields, and %v values", fieldNames, values)
}
// Handle things like RETURNING *, also handle multiple Values
if !p.consumeUntilOne(50, SEMI, EOF) {
return nil, fmt.Errorf("Expected semicolon but never found after 50 tries")
}
res.Values = make(map[string]any)
for i, name := range fieldNames {
res.Values[name] = values[i]
}
return &res, nil
}