Skip to content

Commit

Permalink
Merge branch 'master' into nv/bump-x-net
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie authored Dec 19, 2024
2 parents 7d8f9e8 + 14f58c2 commit 856bd9d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
18 changes: 18 additions & 0 deletions lib/antlr/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,25 @@ func processPrimaryKeyConstraintNode(node *generated.PrimaryKeyTableConstraintCo
}

return colNames, nil
}

func processCopyTable(ctx *generated.CopyCreateTableContext) (Event, error) {
tableNames := ctx.AllTableName()
if len(tableNames) != 2 {
return nil, fmt.Errorf("expected exactly 2 table names, got %d", len(tableNames))
}

tableName, err := getTextFromSingleNodeBranch(tableNames[0])
if err != nil {
return nil, err
}

copiedFromTableName, err := getTextFromSingleNodeBranch(tableNames[1])
if err != nil {
return nil, err
}

return CopyTableEvent{tableName: tableName, copyFromTableName: copiedFromTableName}, nil
}

func processCreateTable(ctx *generated.ColumnCreateTableContext) (Event, error) {
Expand Down
23 changes: 19 additions & 4 deletions lib/antlr/create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,25 @@ import (

func TestCreateTable(t *testing.T) {
{
// Create table LIKE (not currently supported)
events, err := Parse("CREATE TABLE table_name LIKE other_table;")
assert.NoError(t, err)
assert.Len(t, events, 0)
// Create table LIKE
sameQueries := []string{
"CREATE TABLE table_name LIKE other_table;",
"create table table_name (like other_table);",
}

for _, query := range sameQueries {
events, err := Parse(query)
assert.NoError(t, err)
assert.Len(t, events, 1)

createTableEvent, isOk := events[0].(CopyTableEvent)
assert.True(t, isOk)

assert.Equal(t, "table_name", createTableEvent.GetTable())
assert.Len(t, createTableEvent.GetColumns(), 0)
assert.Equal(t, "other_table", createTableEvent.GetCopyFromTableName())
}

}
{
// Create table with column as CHARACTER SET and collation specified at the column level
Expand Down
8 changes: 7 additions & 1 deletion lib/antlr/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func visit(tree antlr.Tree) ([]Event, error) {
}

return events, nil
case *generated.CopyCreateTableContext:
evt, err := processCopyTable(ctx)
if err != nil {
return nil, err
}

return []Event{evt}, nil
case *generated.ColumnCreateTableContext:
evt, err := processCreateTable(ctx)
if err != nil {
Expand All @@ -78,7 +85,6 @@ func visit(tree antlr.Tree) ([]Event, error) {
return processDropTable(ctx)
case
*generated.EmptyStatement_Context,
*generated.CopyCreateTableContext,
*generated.TruncateTableContext,
*generated.AdministrationStatementContext,
*generated.CreateDatabaseContext,
Expand Down
17 changes: 17 additions & 0 deletions lib/antlr/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ func (a AfterPosition) Kind() string {
return "after"
}

type CopyTableEvent struct {
tableName string
copyFromTableName string
}

func (c CopyTableEvent) GetTable() string {
return unescape(c.tableName)
}

func (c CopyTableEvent) GetCopyFromTableName() string {
return unescape(c.copyFromTableName)
}

func (c CopyTableEvent) GetColumns() []Column {
return nil
}

type CreateTableEvent struct {
TableName string
Columns []Column
Expand Down

0 comments on commit 856bd9d

Please sign in to comment.