Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ type Constraint interface {

func (ForeignKey) isConstraint() {}
func (Check) isConstraint() {}
func (TablePrimaryKey) isConstraint() {}


// TableAlteration represents ALTER TABLE action.
type TableAlteration interface {
Expand Down Expand Up @@ -2659,6 +2661,21 @@ type TableConstraint struct {
Constraint Constraint
}

// TablePrimaryKey is primary key constraint in CREATE TABLE.
// Note: While it implements Constraint interface to be stored in TableConstraints,
// it is only used for anonymous style as Spanner doesn't support named primary keys.
//
// PRIMARY KEY ({{.Columns | sqlJoin ", "}})
type TablePrimaryKey struct {
// pos = Primary
// end = Rparen + 1

Primary token.Pos // position of "PRIMARY" keyword
Rparen token.Pos // position of ")" after columns

Columns []*IndexKey
}

// ForeignKey is foreign key specifier in CREATE TABLE and ALTER TABLE.
//
// FOREIGN KEY ({{.ColumnNames | sqlJoin ","}}) REFERENCES {{.ReferenceTable}} ({{.ReferenceColumns | sqlJoin ","}})
Expand Down
8 changes: 8 additions & 0 deletions ast/pos.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,10 @@ func (c *TableConstraint) SQL() string {
return sqlOpt("CONSTRAINT ", c.Name, " ") + c.Constraint.SQL()
}

func (p *TablePrimaryKey) SQL() string {
return "PRIMARY KEY (" + sqlJoin(p.Columns, ", ") + ")"
}

func (f *ForeignKey) SQL() string {
return "FOREIGN KEY (" + sqlJoin(f.Columns, ", ") + ") " +
"REFERENCES " + f.ReferenceTable.SQL() + " (" +
Expand Down
3 changes: 3 additions & 0 deletions ast/walk_internal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3292,6 +3292,12 @@ func (p *Parser) parseCreateTable(pos token.Pos) *ast.CreateTable {
ConstraintPos: token.InvalidPos,
Constraint: c,
})
case p.Token.IsKeywordLike("PRIMARY"):
pk := p.parseTablePrimaryKey()
constraints = append(constraints, &ast.TableConstraint{
ConstraintPos: token.InvalidPos,
Constraint: pk,
})
case p.Token.IsKeywordLike("SYNONYM"):
synonym := p.parseSynonym()
synonyms = append(synonyms, synonym)
Expand Down Expand Up @@ -3498,6 +3504,21 @@ func (p *Parser) parseConstraint() *ast.TableConstraint {
}
}

func (p *Parser) parseTablePrimaryKey() *ast.TablePrimaryKey {
pos := p.expectKeywordLike("PRIMARY").Pos
p.expectKeywordLike("KEY")

p.expect("(")
keys := parseCommaSeparatedList(p, p.parseIndexKey)
rparen := p.expect(")").Pos

return &ast.TablePrimaryKey{
Primary: pos,
Rparen: rparen,
Columns: keys,
}
}

func (p *Parser) parseForeignKey() *ast.ForeignKey {
pos := p.expectKeywordLike("FOREIGN").Pos
p.expectKeywordLike("KEY")
Expand Down
5 changes: 5 additions & 0 deletions testdata/input/ddl/create_table_anonymous_primary_key.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE PKsTable(
PK1 INT64,
PK2 INT64,
PRIMARY KEY (PK1, PK2)
)
84 changes: 84 additions & 0 deletions testdata/result/ddl/create_table_anonymous_primary_key.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
--- create_table_anonymous_primary_key.sql
CREATE TABLE PKsTable(
PK1 INT64,
PK2 INT64,
PRIMARY KEY (PK1, PK2)
)
--- AST
&ast.CreateTable{
Rparen: 74,
PrimaryKeyRparen: -1,
Name: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 13,
NameEnd: 21,
Name: "PKsTable",
},
},
},
Columns: []*ast.ColumnDef{
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 25,
NameEnd: 28,
Name: "PK1",
},
Type: &ast.ScalarSchemaType{
NamePos: 29,
Name: "INT64",
},
Hidden: -1,
},
&ast.ColumnDef{
Null: -1,
Key: -1,
Name: &ast.Ident{
NamePos: 38,
NameEnd: 41,
Name: "PK2",
},
Type: &ast.ScalarSchemaType{
NamePos: 42,
Name: "INT64",
},
Hidden: -1,
},
},
TableConstraints: []*ast.TableConstraint{
&ast.TableConstraint{
ConstraintPos: -1,
Constraint: &ast.TablePrimaryKey{
Primary: 51,
Rparen: 72,
Columns: []*ast.IndexKey{
&ast.IndexKey{
DirPos: -1,
Name: &ast.Ident{
NamePos: 64,
NameEnd: 67,
Name: "PK1",
},
},
&ast.IndexKey{
DirPos: -1,
Name: &ast.Ident{
NamePos: 69,
NameEnd: 72,
Name: "PK2",
},
},
},
},
},
},
}

--- SQL
CREATE TABLE PKsTable (
PK1 INT64,
PK2 INT64,
PRIMARY KEY (PK1, PK2)
)
Loading