Skip to content

Commit

Permalink
Support generic DDLs (#136)
Browse files Browse the repository at this point in the history
* Support generic DDLs

* Delete unused alter patterns

* Delete redundant case

* Flatten nested switches
  • Loading branch information
apstndb authored Jun 9, 2022
1 parent 40e3fe3 commit 06ce1ea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
37 changes: 10 additions & 27 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,12 @@ var (
selectRe = regexp.MustCompile(`(?is)^(?:WITH|@{.+|SELECT)\s.+$`)

// DDL
createDatabaseRe = regexp.MustCompile(`(?is)^CREATE\s+DATABASE\s.+$`)
dropDatabaseRe = regexp.MustCompile(`(?is)^DROP\s+DATABASE\s+(.+)$`)
alterDatabaseRe = regexp.MustCompile(`(?is)^ALTER\s+DATABASE\s.+$`)
createTableRe = regexp.MustCompile(`(?is)^CREATE\s+TABLE\s.+$`)
alterTableRe = regexp.MustCompile(`(?is)^ALTER\s+TABLE\s.+$`)
dropTableRe = regexp.MustCompile(`(?is)^DROP\s+TABLE\s.+$`)
createIndexRe = regexp.MustCompile(`(?is)^CREATE\s+(UNIQUE\s+)?(NULL_FILTERED\s+)?INDEX\s.+$`)
dropIndexRe = regexp.MustCompile(`(?is)^DROP\s+INDEX\s.+$`)
truncateTableRe = regexp.MustCompile(`(?is)^TRUNCATE\s+TABLE\s+(.+)$`)
createViewRe = regexp.MustCompile(`(?is)^CREATE\s+VIEW\s.+$`)
createOrReplaceViewRe = regexp.MustCompile(`(?is)^CREATE\s+OR\s+REPLACE\s+VIEW\s.+$`)
dropViewRe = regexp.MustCompile(`(?is)^DROP\s+VIEW\s.+$`)
alterStatisticsRe = regexp.MustCompile(`(?is)^ALTER\s+STATISTICS\s.+$`)
createDatabaseRe = regexp.MustCompile(`(?is)^CREATE\s+DATABASE\s.+$`)
dropDatabaseRe = regexp.MustCompile(`(?is)^DROP\s+DATABASE\s+(.+)$`)
createRe = regexp.MustCompile(`(?is)^CREATE\s.+$`)
dropRe = regexp.MustCompile(`(?is)^DROP\s.+$`)
alterRe = regexp.MustCompile(`(?is)^ALTER\s.+$`)
truncateTableRe = regexp.MustCompile(`(?is)^TRUNCATE\s+TABLE\s+(.+)$`)

// DML
dmlRe = regexp.MustCompile(`(?is)^(INSERT|UPDATE|DELETE)\s+.+$`)
Expand Down Expand Up @@ -141,28 +134,18 @@ func BuildStatement(input string) (Statement, error) {
return &SelectStatement{Query: input}, nil
case createDatabaseRe.MatchString(input):
return &CreateDatabaseStatement{CreateStatement: input}, nil
case createRe.MatchString(input):
return &DdlStatement{Ddl: input}, nil
case dropDatabaseRe.MatchString(input):
matched := dropDatabaseRe.FindStringSubmatch(input)
return &DropDatabaseStatement{DatabaseId: unquoteIdentifier(matched[1])}, nil
case alterDatabaseRe.MatchString(input):
return &DdlStatement{Ddl: input}, nil
case createTableRe.MatchString(input):
return &DdlStatement{Ddl: input}, nil
case alterTableRe.MatchString(input):
return &DdlStatement{Ddl: input}, nil
case dropTableRe.MatchString(input):
case dropRe.MatchString(input):
return &DdlStatement{Ddl: input}, nil
case createIndexRe.MatchString(input):
return &DdlStatement{Ddl: input}, nil
case dropIndexRe.MatchString(input):
case alterRe.MatchString(input):
return &DdlStatement{Ddl: input}, nil
case truncateTableRe.MatchString(input):
matched := truncateTableRe.FindStringSubmatch(input)
return &TruncateTableStatement{Table: unquoteIdentifier(matched[1])}, nil
case createViewRe.MatchString(input), createOrReplaceViewRe.MatchString(input), dropViewRe.MatchString(input):
return &DdlStatement{Ddl: input}, nil
case alterStatisticsRe.MatchString(input):
return &DdlStatement{Ddl: input}, nil
case showDatabasesRe.MatchString(input):
return &ShowDatabasesStatement{}, nil
case showCreateTableRe.MatchString(input):
Expand Down
30 changes: 30 additions & 0 deletions statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@ func TestBuildStatement(t *testing.T) {
input: "DROP VIEW t1view",
want: &DdlStatement{Ddl: "DROP VIEW t1view"},
},
{
desc: "CREATE CHANGE STREAM FOR ALL statement",
input: "CREATE CHANGE STREAM EverythingStream FOR ALL",
want: &DdlStatement{Ddl: "CREATE CHANGE STREAM EverythingStream FOR ALL"},
},
{
desc: "CREATE CHANGE STREAM FOR specific columns statement",
input: "CREATE CHANGE STREAM NamesAndTitles FOR Singers(FirstName, LastName), Albums(Title)",
want: &DdlStatement{Ddl: "CREATE CHANGE STREAM NamesAndTitles FOR Singers(FirstName, LastName), Albums(Title)"},
},
{
desc: "ALTER CHANGE STREAM SET FOR statement",
input: "ALTER CHANGE STREAM NamesAndAlbums SET FOR Singers(FirstName, LastName), Albums, Songs",
want: &DdlStatement{Ddl: "ALTER CHANGE STREAM NamesAndAlbums SET FOR Singers(FirstName, LastName), Albums, Songs"},
},
{
desc: "ALTER CHANGE STREAM SET OPTIONS statement",
input: "ALTER CHANGE STREAM NamesAndAlbums SET OPTIONS( retention_period = '36h' )",
want: &DdlStatement{Ddl: "ALTER CHANGE STREAM NamesAndAlbums SET OPTIONS( retention_period = '36h' )"},
},
{
desc: "ALTER CHANGE STREAM DROP FOR ALL statement",
input: "ALTER CHANGE STREAM MyStream DROP FOR ALL",
want: &DdlStatement{Ddl: "ALTER CHANGE STREAM MyStream DROP FOR ALL"},
},
{
desc: "DROP CHANGE STREAM statement",
input: "DROP CHANGE STREAM NamesAndAlbums",
want: &DdlStatement{Ddl: "DROP CHANGE STREAM NamesAndAlbums"},
},
{
desc: "ALTER STATISTICS statement",
input: "ALTER STATISTICS package SET OPTIONS (allow_gc = false)",
Expand Down

0 comments on commit 06ce1ea

Please sign in to comment.