Skip to content

Commit

Permalink
Parse optional parentheses on DEFAULT clause
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Jul 30, 2021
1 parent 402b5a6 commit 1654f35
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/sql/parser/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ func (p *Parser) parseFieldConstraint(fc *database.FieldConstraint) error {
return newParseError(scanner.Tokstr(tok, lit), []string{"CONSTRAINT", ")"}, pos)
}

withParentheses, err := p.parseOptional(scanner.LPAREN)
if err != nil {
return err
}

// Parse default value expression.
// Only a few tokens are allowed.
e, err := p.parseExprWithMinPrecedence(scanner.EQ.Precedence(),
Expand Down Expand Up @@ -217,6 +222,13 @@ func (p *Parser) parseFieldConstraint(fc *database.FieldConstraint) error {
}

fc.DefaultValue = expr.Constraint(e)

if withParentheses {
_, err = p.parseOptional(scanner.RPAREN)
if err != nil {
return err
}
}
case scanner.UNIQUE:
// if it's already unique we return an error
if fc.IsUnique {
Expand Down
10 changes: 10 additions & 0 deletions internal/sql/parser/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ func TestParserCreateTable(t *testing.T) {
},
},
}, false},
{"With default", "CREATE TABLE test(foo DEFAULT (\"10\"))",
&statement.CreateTableStmt{
Info: database.TableInfo{
TableName: "test",
FieldConstraints: []*database.FieldConstraint{
{Path: document.Path(testutil.ParsePath(t, "foo")), DefaultValue: expr.Constraint(expr.LiteralValue{Value: types.NewTextValue("10")})},
},
},
}, false},
{"With default twice", "CREATE TABLE test(foo DEFAULT 10 DEFAULT 10)", nil, true},
{"With default and no parentheses", "CREATE TABLE test(foo DEFAULT (10)", nil, true},
{"With forbidden tokens", "CREATE TABLE test(foo DEFAULT a)", nil, true},
{"With forbidden tokens", "CREATE TABLE test(foo DEFAULT 1 AND 2)", nil, true},
{"With unique", "CREATE TABLE test(foo UNIQUE)",
Expand Down

0 comments on commit 1654f35

Please sign in to comment.