Skip to content

Commit

Permalink
WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Dec 19, 2024
1 parent 7265ea7 commit 809844e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/antlr/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func processCreateTable(ctx *generated.ColumnCreateTableContext) (Event, error)
}
}
}
default:
slog.Warn(fmt.Sprintf("Skipping unsupported create table types: %T", castedChild))
}
}

Expand Down
15 changes: 12 additions & 3 deletions lib/antlr/create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ import (
"testing"
)

func TestCreateTable(t *testing.T) {
func TestCreateTablePoC(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)
assert.Len(t, events, 1)

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

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

func TestCreateTable(t *testing.T) {
{
// Create table with column as CHARACTER SET and collation specified at the column level
events, err := Parse("CREATE TABLE table_name (id INT, name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci);")
Expand Down
10 changes: 8 additions & 2 deletions lib/antlr/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func Parse(sqlCmd string) ([]Event, error) {
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
// This will go through our custom visit function. If you are trying to print out the AST, split this function into [sqlStatements] and [parser]
// Then have print [sqlStatements.ToStringTree(nil, parser)]
return visit(generated.NewMySqlParser(stream).SqlStatements())

parser := generated.NewMySqlParser(stream)
statements := parser.SqlStatements()
fmt.Println("##", statements.ToStringTree(nil, parser))
return visit(statements)
}

func visit(tree antlr.Tree) ([]Event, error) {
Expand All @@ -65,6 +69,9 @@ func visit(tree antlr.Tree) ([]Event, error) {
}

return events, nil
case *generated.CopyCreateTableContext:
fmt.Println("Here?")
return nil, 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
5 changes: 5 additions & 0 deletions lib/antlr/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ func (a AfterPosition) Kind() string {
return "after"
}

type LikeTable struct {
TableName string
}

type CreateTableEvent struct {
TableName string
LikeTable *LikeTable
Columns []Column
}

Expand Down

0 comments on commit 809844e

Please sign in to comment.