Skip to content

Commit

Permalink
fix: (close #93) boolean parsing in INSERT query
Browse files Browse the repository at this point in the history
  • Loading branch information
proullon committed Oct 5, 2023
1 parent 0fc26c5 commit b525f28
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 36 deletions.
27 changes: 24 additions & 3 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,6 @@ func TestInsertSingle(t *testing.T) {
t.Fatalf("sql.Exec: Error: %s\n", err)
}

log.SetLevel(log.DebugLevel)
result, err := db.Exec("INSERT INTO cat (breed, name, funny) VALUES ('indeterminate', 'Uhura', false)")
if err != nil {
t.Fatalf("Cannot insert into table account: %s", err)
Expand All @@ -1497,21 +1496,43 @@ func TestInsertSingle(t *testing.T) {
t.Fatalf("Cannot check last inserted ID: %s", err)
}

row := db.QueryRow("SELECT breed, name FROM cat WHERE id = ?", insertedId)
row := db.QueryRow("SELECT breed, name, funny FROM cat WHERE id = ?", insertedId)
if row == nil {
t.Fatalf("sql.Query failed")
}

var breed string
var name string
err = row.Scan(&breed, &name)
var funny bool
funny = true

err = row.Scan(&breed, &name, &funny)
if err != nil {
t.Fatalf("row.Scan: %s", err)
}

if breed != "indeterminate" || name != "Uhura" {
t.Fatalf("Expected breed 'indeterminate' and name 'Uhura', got breed '%v' and name '%v'", breed, name)
}

if funny != false {
t.Fatalf("Expected funny=false, got true")
}

result, err = db.Exec("INSERT INTO cat (breed, name, funny) VALUES ('indeterminate', 'Kuhura', true)")
if err != nil {
t.Fatalf("Cannot insert into table account: %s", err)
}

err = db.QueryRow("SELECT breed, name, funny FROM cat WHERE name = ?", "Kuhura").Scan(&breed, &name, &funny)
if err != nil {
t.Fatalf("row.Scan: %s", err)
}

if funny != true {
t.Fatalf("Expected funny=true, got false")
}

}

func TestInsertSingleReturning(t *testing.T) {
Expand Down
32 changes: 32 additions & 0 deletions engine/parser/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,35 @@ func (p *parser) parseInsert() (*Instruction, error) {

return i, nil
}

func (p *parser) parseListElement() (*Decl, error) {
quoted := false

// In case of INSERT, can be DEFAULT here
if p.is(DefaultToken) {
v, err := p.consumeToken(DefaultToken)
if err != nil {
return nil, err
}
return v, nil
}

if p.is(SimpleQuoteToken) || p.is(DoubleQuoteToken) {
quoted = true
p.next()
}

var valueDecl *Decl
valueDecl, err := p.consumeToken(FloatToken, StringToken, NumberToken, NullToken, DateToken, NowToken, ArgToken, NamedArgToken, FalseToken)
if err != nil {
return nil, err
}

if quoted {
if _, err := p.consumeToken(SimpleQuoteToken, DoubleQuoteToken); err != nil {
return nil, err
}
}

return valueDecl, nil
}
2 changes: 1 addition & 1 deletion engine/parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (l *lexer) lex(instruction []byte) ([]Token, error) {
matchers = append(matchers, l.genericStringMatcher("for", ForToken))
matchers = append(matchers, l.genericStringMatcher("default", DefaultToken))
matchers = append(matchers, l.genericStringMatcher("localtimestamp", LocalTimestampToken))
matchers = append(matchers, l.genericStringMatcher("false", LocalTimestampToken))
matchers = append(matchers, l.genericStringMatcher("false", FalseToken))
matchers = append(matchers, l.genericStringMatcher("unique", UniqueToken))
matchers = append(matchers, l.genericStringMatcher("now()", NowToken))
matchers = append(matchers, l.genericStringMatcher("offset", OffsetToken))
Expand Down
32 changes: 0 additions & 32 deletions engine/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,38 +689,6 @@ func (p *parser) parseJoin() (*Decl, error) {
return joinDecl, nil
}

func (p *parser) parseListElement() (*Decl, error) {
quoted := false

// In case of INSERT, can be DEFAULT here
if p.is(DefaultToken) {
v, err := p.consumeToken(DefaultToken)
if err != nil {
return nil, err
}
return v, nil
}

if p.is(SimpleQuoteToken) || p.is(DoubleQuoteToken) {
quoted = true
p.next()
}

var valueDecl *Decl
valueDecl, err := p.consumeToken(FloatToken, StringToken, NumberToken, NullToken, DateToken, NowToken, ArgToken, NamedArgToken)
if err != nil {
return nil, err
}

if quoted {
if _, err := p.consumeToken(SimpleQuoteToken, DoubleQuoteToken); err != nil {
return nil, err
}
}

return valueDecl, nil
}

func (p *parser) next() error {
if !p.hasNext() {
return fmt.Errorf("Unexpected end")
Expand Down

0 comments on commit b525f28

Please sign in to comment.