Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/v0.10 sql ddl #1075

Merged
merged 11 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 42 additions & 0 deletions internal/engine/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,48 @@ func TestGenerateDDLStatement(t *testing.T) {
},
},
},
{
name: "drop table",
want: `DROP TABLE users, posts;`,
sql: &parse.DropTableStatement{
Tables: []string{"users", "posts"},
Behavior: parse.DropBehaviorNon,
},
},
{
name: "drop table single table",
want: `DROP TABLE users;`,
sql: &parse.DropTableStatement{
Tables: []string{"users"},
Behavior: parse.DropBehaviorNon,
},
},
{
name: "drop table if exists",
want: `DROP TABLE IF EXISTS users, posts;`,
sql: &parse.DropTableStatement{
Tables: []string{"users", "posts"},
IfExists: true,
Behavior: parse.DropBehaviorNon,
},
},
{
name: "drop table CASCADE",
want: `DROP TABLE IF EXISTS users, posts CASCADE;`,
sql: &parse.DropTableStatement{
Tables: []string{"users", "posts"},
Behavior: parse.DropBehaviorCascade,
IfExists: true,
},
},
{
name: "drop table RESTRICT ",
want: `DROP TABLE users, posts RESTRICT;`,
sql: &parse.DropTableStatement{
Tables: []string{"users", "posts"},
Behavior: parse.DropBehaviorRestrict,
},
},
{
name: "create index",
want: `CREATE INDEX abc ON user(name);`,
Expand Down
20 changes: 20 additions & 0 deletions internal/engine/generate/plpgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,26 @@ func (s *sqlGenerator) VisitAlterTableStatement(p0 *parse.AlterTableStatement) a
return str.String()
}

func (s *sqlGenerator) VisitDropTableStatement(p0 *parse.DropTableStatement) any {
str := strings.Builder{}
str.WriteString("DROP TABLE ")
if p0.IfExists {
str.WriteString("IF EXISTS ")
}

str.WriteString(strings.Join(p0.Tables, ", "))
switch p0.Behavior {
case parse.DropBehaviorCascade:
str.WriteString(" CASCADE")
case parse.DropBehaviorRestrict:
str.WriteString(" RESTRICT")
case parse.DropBehaviorNon:
default:
panic("unknown drop behavior")
}
return str.String()
}

func (s *sqlGenerator) VisitCreateIndexStatement(p0 *parse.CreateIndexStatement) any {
str := strings.Builder{}
str.WriteString("CREATE ")
Expand Down
4 changes: 4 additions & 0 deletions parse/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,10 @@ func (s *sqlAnalyzer) VisitAlterTableStatement(p0 *AlterTableStatement) any {
panic("sqlAnalyzer: not implemented")
}

func (s *sqlAnalyzer) VisitDropTableStatement(p0 *DropTableStatement) any {
panic("sqlAnalyzer: not implemented")
}

func (s *sqlAnalyzer) VisitCreateIndexStatement(p0 *CreateIndexStatement) any {
panic("sqlAnalyzer: not implemented")
}
Expand Down
27 changes: 27 additions & 0 deletions parse/antlr.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,8 @@ func (s *schemaVisitor) VisitDdl_stmt(ctx *gen.Ddl_stmtContext) any {
return ctx.Create_table_statement().Accept(s).(*CreateTableStatement)
case ctx.Alter_table_statement() != nil:
return ctx.Alter_table_statement().Accept(s).(*AlterTableStatement)
case ctx.Drop_table_statement() != nil:
return ctx.Drop_table_statement().Accept(s).(*DropTableStatement)
case ctx.Create_index_statement() != nil:
return ctx.Create_index_statement().Accept(s).(*CreateIndexStatement)
case ctx.Drop_index_statement() != nil:
Expand Down Expand Up @@ -1232,6 +1234,31 @@ func (s *schemaVisitor) VisitTable_index_def(ctx *gen.Table_index_defContext) an
return index
}

func (s *schemaVisitor) VisitDrop_table_statement(ctx *gen.Drop_table_statementContext) any {
stmt := &DropTableStatement{
Tables: ctx.GetTables().Accept(s).([]string),
Behavior: ctx.Opt_drop_behavior().Accept(s).(DropBehavior),
}

if ctx.EXISTS() != nil {
stmt.IfExists = true
}

stmt.Set(ctx)
return stmt
}

func (s *schemaVisitor) VisitOpt_drop_behavior(ctx *gen.Opt_drop_behaviorContext) any {
switch {
case ctx.CASCADE() != nil:
return DropBehaviorCascade
case ctx.RESTRICT() != nil:
return DropBehaviorRestrict
default:
return DropBehaviorNon
}
}

func (s *schemaVisitor) VisitAlter_table_statement(ctx *gen.Alter_table_statementContext) any {
stmt := &AlterTableStatement{
Table: ctx.Identifier().Accept(s).(string),
Expand Down
27 changes: 26 additions & 1 deletion parse/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,9 @@ const (
SQLStatementTypeSelect SQLStatementType = "select"
SQLStatementTypeCreateTable SQLStatementType = "create_table"
SQLStatementTypeAlterTable SQLStatementType = "alter_table"
SQLStatementTypeDropTable SQLStatementType = "drop_table"
SQLStatementTypeCreateIndex SQLStatementType = "create_index"
SQLStatementTypeDropIndex SQLStatementType = "drop_index"
//SQLStatementTypeDropTable SQLStatementType = "drop_table"
)

// CreateTableStatement is a CREATE TABLE statement.
Expand Down Expand Up @@ -865,6 +865,30 @@ type ForeignKeyAction struct {
Do ForeignKeyActionDo `json:"do"`
}

type DropBehavior string

const (
DropBehaviorCascade DropBehavior = "CASCADE"
DropBehaviorRestrict DropBehavior = "RESTRICT"
DropBehaviorNon DropBehavior = ""
)

type DropTableStatement struct {
Position

Tables []string
IfExists bool
Behavior DropBehavior
}

func (s *DropTableStatement) Accept(v Visitor) any {
return v.VisitDropTableStatement(s)
}

func (s *DropTableStatement) StmtType() SQLStatementType {
return SQLStatementTypeDropTable
}

type AlterTableAction interface {
Node

Expand Down Expand Up @@ -1651,6 +1675,7 @@ type SQLVisitor interface {
// DDL
VisitCreateTableStatement(*CreateTableStatement) any
VisitAlterTableStatement(*AlterTableStatement) any
VisitDropTableStatement(*DropTableStatement) any
VisitCreateIndexStatement(*CreateIndexStatement) any
VisitDropIndexStatement(*DropIndexStatement) any
}
Expand Down
Loading