Cleanup parser statements, add delete and insert statement, add TASKS.md for tracking tasks
This commit is contained in:
69
sql/parseInsertStatement.go
Normal file
69
sql/parseInsertStatement.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user