-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package antlr | ||
|
||
import ( | ||
"fmt" | ||
"github.com/artie-labs/reader/lib/antlr/generated" | ||
"slices" | ||
) | ||
|
||
func processRenameTable(ctx *generated.RenameTableContext) ([]Event, error) { | ||
var renameEvents []Event | ||
for _, child := range ctx.GetChildren() { | ||
switch castedChild := child.(type) { | ||
case *generated.RenameTableClauseContext: | ||
var allTableNames []string | ||
for _, tableName := range castedChild.AllTableName() { | ||
allTableNames = append(allTableNames, tableName.GetText()) | ||
} | ||
|
||
// Must be at least two table names | ||
if len(allTableNames) < 2 { | ||
return nil, fmt.Errorf("expected at least 2 table names, got %d", len(allTableNames)) | ||
} | ||
|
||
// Make sure it's divisible by 2 | ||
if len(allTableNames)%2 != 0 { | ||
return nil, fmt.Errorf("unexpected number of table names: %d", len(allTableNames)) | ||
} | ||
|
||
for group := range slices.Chunk(allTableNames, 2) { | ||
renameEvents = append(renameEvents, RenameTableEvent{ | ||
tableName: group[0], | ||
newTableName: group[1], | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return renameEvents, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package antlr | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestRenameTable(t *testing.T) { | ||
{ | ||
// One table | ||
events, err := Parse(`RENAME TABLE table_b TO table_a;`) | ||
assert.NoError(t, err) | ||
assert.Len(t, events, 1) | ||
|
||
renameTableEvent, isOk := events[0].(RenameTableEvent) | ||
assert.True(t, isOk) | ||
|
||
assert.Equal(t, "table_b", renameTableEvent.GetTable()) | ||
assert.Equal(t, "table_a", renameTableEvent.GetNewTableName()) | ||
} | ||
{ | ||
// Multiple tables | ||
events, err := Parse(`RENAME TABLE old_table TO tmp_table, new_table TO old_table, tmp_table TO new_table;`) | ||
assert.NoError(t, err) | ||
assert.Len(t, events, 3) | ||
|
||
renameTableEvent1, isOk := events[0].(RenameTableEvent) | ||
assert.True(t, isOk) | ||
assert.Equal(t, "old_table", renameTableEvent1.GetTable()) | ||
assert.Equal(t, "tmp_table", renameTableEvent1.GetNewTableName()) | ||
|
||
renameTableEvent2, isOk := events[1].(RenameTableEvent) | ||
assert.True(t, isOk) | ||
assert.Equal(t, "new_table", renameTableEvent2.GetTable()) | ||
assert.Equal(t, "old_table", renameTableEvent2.GetNewTableName()) | ||
|
||
renameTableEvent3, isOk := events[2].(RenameTableEvent) | ||
assert.True(t, isOk) | ||
assert.Equal(t, "tmp_table", renameTableEvent3.GetTable()) | ||
assert.Equal(t, "new_table", renameTableEvent3.GetNewTableName()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters