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

add create and drop event parsing #227

Merged
merged 4 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
147 changes: 142 additions & 5 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,56 @@ type ProcedureParam struct {
Type ColumnType
}

type EventSpec struct {
EventName EventName
Definer string
IfNotExists bool
OnSchedule *EventScheduleSpec
OnCompletionPreserve bool
Status EventStatus
Comment *SQLVal
Body Statement
}

type EventStatus string

const (
EventStatus_Enable EventStatus = "enable"
EventStatus_Disable EventStatus = "disable"
EventStatus_DisableOnSlave EventStatus = "disable on slave"
)

type EventScheduleSpec struct {
At *EventScheduleTimeSpec
EveryInterval IntervalExpr
Starts *EventScheduleTimeSpec
Ends *EventScheduleTimeSpec
}

type EventScheduleTimeSpec struct {
EventTimestamp Expr
EventIntervals []IntervalExpr
}

// Format returns a canonical string representation of the type and all relevant options
func (est *EventScheduleTimeSpec) Format(buf *TrackedBuffer) {
sb := strings.Builder{}

sb.WriteString(fmt.Sprintf("%s ", String(est.EventTimestamp)))
for _, interval := range est.EventIntervals {
sb.WriteString(fmt.Sprintf("+ interval %v %s ", interval.Expr, interval.Unit))
}

buf.Myprintf("%s", sb.String())
}

// String returns a canonical string representation of the type and all relevant options
func (est *EventScheduleTimeSpec) String() string {
buf := NewTrackedBuffer(nil)
est.Format(buf)
return buf.String()
}

type CharacteristicValue string

const (
Expand Down Expand Up @@ -1907,6 +1957,9 @@ type DDL struct {
// AlterCollationSpec is set for CHARACTER SET / COLLATE operations on ALTER statements
AlterCollationSpec *AlterCollationSpec

// EventSpec is set for CREATE EVENT operations
EventSpec *EventSpec

// Temporary is set for CREATE TEMPORARY TABLE operations.
Temporary bool

Expand Down Expand Up @@ -2004,6 +2057,39 @@ func (node *DDL) Format(buf *TrackedBuffer) {
sb.WriteString(" " + characteristic.String())
}
buf.Myprintf("%s %v", sb.String(), proc.Body)
} else if node.EventSpec != nil {
event := node.EventSpec
sb := strings.Builder{}
sb.WriteString("create ")
if event.Definer != "" {
sb.WriteString(fmt.Sprintf("definer = %s ", event.Definer))
}
notExists := ""
if node.IfNotExists {
notExists = " if not exists"
}
sb.WriteString(fmt.Sprintf("event%s %s ", notExists, event.EventName))

if event.OnSchedule.At != nil {
sb.WriteString(fmt.Sprintf("on schedule at %s", event.OnSchedule.At.String()))
} else {
sb.WriteString(fmt.Sprintf("on schedule every %s %s ", String(event.OnSchedule.EveryInterval.Expr), event.OnSchedule.EveryInterval.Unit))
if event.OnSchedule.Starts != nil {
sb.WriteString(fmt.Sprintf("starts %s", event.OnSchedule.Starts.String()))
}
if event.OnSchedule.Ends != nil {
sb.WriteString(fmt.Sprintf("ends %s", event.OnSchedule.Ends.String()))
}
}

if !event.OnCompletionPreserve {
sb.WriteString("on completion not preserve ")
}
if event.Comment != nil {
sb.WriteString(fmt.Sprintf("comment %s ", event.Comment))
}

buf.Myprintf("%sdo %v", sb.String(), event.Body)
} else {
notExists := ""
if node.IfNotExists {
Expand Down Expand Up @@ -2037,17 +2123,23 @@ func (node *DDL) Format(buf *TrackedBuffer) {
if len(node.FromViews) > 0 {
buf.Myprintf("%s view%s %v", node.Action, exists, node.FromViews)
} else if node.TriggerSpec != nil {
exists := ""
exists = ""
if node.IfExists {
exists = " if exists"
}
buf.Myprintf(fmt.Sprintf("%s trigger%s %v", node.Action, exists, node.TriggerSpec.TrigName))
} else if node.ProcedureSpec != nil {
exists := ""
exists = ""
if node.IfExists {
exists = " if exists"
}
buf.Myprintf(fmt.Sprintf("%s procedure%s %v", node.Action, exists, node.ProcedureSpec.ProcName))
} else if node.EventSpec != nil {
exists = ""
if node.IfExists {
exists = " if exists"
}
buf.Myprintf(fmt.Sprintf("%s event%s %v", node.Action, exists, node.EventSpec.EventName))
} else {
buf.Myprintf("%s table%s %v", node.Action, exists, node.FromTables)
}
Expand Down Expand Up @@ -3970,6 +4062,51 @@ func (node ProcedureName) IsEmpty() bool {
return node.Name.IsEmpty()
}

// EventName represents an event name.
// Qualifier, if specified, represents a database name.
// EventName is a value struct whose fields are case-sensitive,
// so TableIdent struct is used for fields
type EventName struct {
Name ColIdent
Qualifier TableIdent
}

// Format formats the node.
func (node EventName) Format(buf *TrackedBuffer) {
if node.IsEmpty() {
return
}
if !node.Qualifier.IsEmpty() {
buf.Myprintf("%v.", node.Qualifier)
}
buf.Myprintf("%v", node.Name)
}

// Format formats the node.
func (node EventName) String() string {
if node.IsEmpty() {
return ""
}
if !node.Qualifier.IsEmpty() {
return fmt.Sprintf("%s.%s", node.Qualifier.String(), node.Name)
}
return node.Name.String()
}

func (node EventName) walkSubtree(visit Visit) error {
return Walk(
visit,
node.Name,
node.Qualifier,
)
}

// IsEmpty returns true if EventName is nil or empty.
func (node EventName) IsEmpty() bool {
// If Name is empty, Qualifier is also empty.
return node.Name.IsEmpty()
}

// TableName represents a table name.
// Qualifier, if specified, represents a database or keyspace.
// TableName is a value struct whose fields are case sensitive.
Expand Down Expand Up @@ -4280,7 +4417,7 @@ func (*IntervalExpr) iExpr() {}
func (*CollateExpr) iExpr() {}
func (*FuncExpr) iExpr() {}
func (*TimestampFuncExpr) iExpr() {}
func (*ExtractFuncExpr) iExpr() {}
func (*ExtractFuncExpr) iExpr() {}
func (*CurTimeFuncExpr) iExpr() {}
func (*CaseExpr) iExpr() {}
func (*ValuesFuncExpr) iExpr() {}
Expand Down Expand Up @@ -5058,8 +5195,8 @@ func (node *IntervalExpr) replace(from, to Expr) bool {

// ExtractFuncExpr represents the function and arguments for EXTRACT(<time_unit> from <expr>) functions.
type ExtractFuncExpr struct {
Name string
Unit string
Name string
Unit string
Expr Expr
}

Expand Down
34 changes: 31 additions & 3 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2577,7 +2577,7 @@ var (
}, {
input: "GRANT PROXY ON UserName TO Role1, Role2 WITH GRANT OPTION",
output: "grant proxy on `UserName`@`%` to `Role1`@`%`, `Role2`@`%` with grant option",
}, {
}, {
input: "GRANT REPLICATION_SLAVE_ADMIN, GROUP_REPLICATION_ADMIN, BINLOG_ADMIN ON *.* TO 'u1'@'localhost'",
output: "grant replication_slave_admin, group_replication_admin, binlog_admin on *.* to `u1`@`localhost`",
}, {
Expand Down Expand Up @@ -2859,6 +2859,34 @@ var (
{
input: "select EXTRACT(DAY from '2020-10-1')",
},
{
input: "DROP EVENT event1",
output: "drop event event1",
},
{
input: "DROP EVENT IF EXISTS event1",
output: "drop event if exists event1",
},
{
input: "CREATE EVENT event1 ON SCHEDULE AT '2006-02-10 23:59:00' DO INSERT INTO test.totals VALUES (NOW())",
output: "create event event1 on schedule at '2006-02-10 23:59:00' do insert into test.totals values (NOW())",
},
{
input: "CREATE DEFINER = `root`@`localhost` EVENT event1 ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 3 WEEK + INTERVAL 2 DAY DO INSERT INTO mytable VALUES (NOW())",
output: "create definer = `root`@`localhost` event event1 on schedule at CURRENT_TIMESTAMP() + interval 3 WEEK + interval 2 DAY do insert into mytable values (NOW())",
},
{
input: "CREATE EVENT event1 ON SCHEDULE EVERY 1 MINUTE ENDS CURRENT_TIMESTAMP + INTERVAL 3 HOUR ON COMPLETION PRESERVE COMMENT 'new event' DO INSERT INTO mytable VALUES (1)",
output: "create event event1 on schedule every 1 MINUTE ends CURRENT_TIMESTAMP() + interval 3 HOUR comment 'new event' do insert into mytable values (1)",
},
{
input: "CREATE EVENT event1 ON SCHEDULE EVERY 1 MINUTE STARTS CURRENT_TIMESTAMP + INTERVAL 2 HOUR ENDS CURRENT_TIMESTAMP + INTERVAL 3 HOUR ON COMPLETION NOT PRESERVE DO INSERT INTO mytable VALUES (1)",
output: "create event event1 on schedule every 1 MINUTE starts CURRENT_TIMESTAMP() + interval 2 HOUR ends CURRENT_TIMESTAMP() + interval 3 HOUR on completion not preserve do insert into mytable values (1)",
},
{
input: "CREATE EVENT e_call_myproc ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY DO CALL myproc(5, 27)",
output: "create event e_call_myproc on schedule at CURRENT_TIMESTAMP() + interval 1 DAY do call myproc(5, 27)",
},
}
// Any tests that contain multiple statements within the body (such as BEGIN/END blocks) should go here.
// validSQL is used by TestParseNextValid, which expects a semicolon to mean the end of a full statement.
Expand Down Expand Up @@ -3489,7 +3517,7 @@ BEGIN
VALUES ("delete", OLD.id,OLD.created_at,OLD.updated_at,OLD.created_by,OLD.updated_by,OLD.company_id,OLD.label_id,OLD.task_id,OLD.delete);
END */
`,
output: "create definer = `root`@`localhost` trigger forecast_db.before_ai_suggested_task_label_delete " +
output: "create definer = `root`@`localhost` trigger forecast_db.before_ai_suggested_task_label_delete " +
"before delete on forecast_db.ai_suggested_task_label for each row begin\n" +
"if OLD.`delete` != (1) then " +
"set @message_text = CONCAT('CANNOT DELETE ai_suggested_task_label IF DELETE FLAG IS NOT SET FOR: ', CAST(OLD.id, CHAR)); " +
Expand All @@ -3509,7 +3537,7 @@ func TestValidIgnoreWhitespace(t *testing.T) {
}
tree, err := Parse(tcase.input)
require.NoError(t, err)

out := String(tree)
normalize := regexp.MustCompile("\\s+")
normalizedOut := normalize.ReplaceAllLiteralString(out, " ")
Expand Down
Loading