Skip to content

Commit 90ad1a1

Browse files
committed
Add comprehensive CREATE EVENT SESSION parsing support
- Update parseCreateEventSessionStatementFromEvent to fully parse the statement - Add JSON marshalling for EventDeclaration, TargetDeclaration, SessionOption types - Add EventDeclarationCompareFunctionParameter as BooleanExpression for predicate parsing - Add SourceDeclaration as both ScalarExpression and BooleanExpression - Add sourceDeclarationToJSON helper function - Enable 7 CreateEventSessionNotLikePredicate tests
1 parent df16bfc commit 90ad1a1

11 files changed

Lines changed: 645 additions & 30 deletions

File tree

ast/event_statements.go

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,113 @@ package ast
22

33
// CreateEventSessionStatement represents CREATE EVENT SESSION statement
44
type CreateEventSessionStatement struct {
5-
Name *Identifier
6-
ServerName *Identifier
7-
Events []*EventDeclaration
8-
Targets []*EventTarget
9-
Options []*EventSessionOption
5+
Name *Identifier
6+
SessionScope string // "Server" or "Database"
7+
EventDeclarations []*EventDeclaration
8+
TargetDeclarations []*TargetDeclaration
9+
SessionOptions []SessionOption
1010
}
1111

1212
func (s *CreateEventSessionStatement) node() {}
1313
func (s *CreateEventSessionStatement) statement() {}
1414

1515
// EventDeclaration represents an event in the event session
1616
type EventDeclaration struct {
17-
PackageName *Identifier
18-
EventName *Identifier
19-
Actions []*EventAction
20-
WhereClause ScalarExpression
17+
ObjectName *EventSessionObjectName
18+
EventDeclarationActionParameters []*EventSessionObjectName
19+
EventDeclarationPredicateParameter BooleanExpression
2120
}
2221

23-
// EventAction represents an action for an event
22+
// Note: EventSessionObjectName is defined in server_audit_statement.go
23+
24+
// TargetDeclaration represents a target for the event session
25+
type TargetDeclaration struct {
26+
ObjectName *EventSessionObjectName
27+
TargetDeclarationParameters []*EventDeclarationSetParameter
28+
}
29+
30+
// EventDeclarationSetParameter represents a SET parameter
31+
type EventDeclarationSetParameter struct {
32+
EventField *Identifier
33+
EventValue ScalarExpression
34+
}
35+
36+
// SessionOption interface for event session options
37+
type SessionOption interface {
38+
sessionOption()
39+
}
40+
41+
// LiteralSessionOption represents a literal session option like MAX_MEMORY
42+
type LiteralSessionOption struct {
43+
OptionKind string
44+
Value ScalarExpression
45+
Unit string
46+
}
47+
48+
func (o *LiteralSessionOption) sessionOption() {}
49+
50+
// OnOffSessionOption represents an ON/OFF session option
51+
type OnOffSessionOption struct {
52+
OptionKind string
53+
OptionState string // "On" or "Off"
54+
}
55+
56+
func (o *OnOffSessionOption) sessionOption() {}
57+
58+
// EventRetentionSessionOption represents EVENT_RETENTION_MODE option
59+
type EventRetentionSessionOption struct {
60+
OptionKind string
61+
Value string // e.g. "AllowSingleEventLoss"
62+
}
63+
64+
func (o *EventRetentionSessionOption) sessionOption() {}
65+
66+
// MaxDispatchLatencySessionOption represents MAX_DISPATCH_LATENCY option
67+
type MaxDispatchLatencySessionOption struct {
68+
OptionKind string
69+
Value ScalarExpression
70+
IsInfinite bool
71+
}
72+
73+
func (o *MaxDispatchLatencySessionOption) sessionOption() {}
74+
75+
// MemoryPartitionSessionOption represents MEMORY_PARTITION_MODE option
76+
type MemoryPartitionSessionOption struct {
77+
OptionKind string
78+
Value string // e.g. "None"
79+
}
80+
81+
func (o *MemoryPartitionSessionOption) sessionOption() {}
82+
83+
// EventDeclarationCompareFunctionParameter for function calls in WHERE clause
84+
type EventDeclarationCompareFunctionParameter struct {
85+
Name *EventSessionObjectName
86+
SourceDeclaration *SourceDeclaration
87+
EventValue ScalarExpression
88+
}
89+
90+
func (e *EventDeclarationCompareFunctionParameter) node() {}
91+
func (e *EventDeclarationCompareFunctionParameter) booleanExpression() {}
92+
93+
// Note: SourceDeclaration is defined in server_audit_statement.go
94+
95+
// Legacy fields for backwards compatibility
2496
type EventAction struct {
2597
PackageName *Identifier
2698
ActionName *Identifier
2799
}
28100

29-
// EventTarget represents a target for the event session
30101
type EventTarget struct {
31102
PackageName *Identifier
32103
TargetName *Identifier
33104
Options []*EventTargetOption
34105
}
35106

36-
// EventTargetOption represents an option for an event target
37107
type EventTargetOption struct {
38108
Name *Identifier
39109
Value ScalarExpression
40110
}
41111

42-
// EventSessionOption represents an option for the event session
43112
type EventSessionOption struct {
44113
OptionKind string
45114
Value ScalarExpression

ast/server_audit_statement.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ type SourceDeclaration struct {
8484
Value *EventSessionObjectName
8585
}
8686

87-
func (s *SourceDeclaration) node() {}
88-
func (s *SourceDeclaration) scalarExpression() {}
87+
func (s *SourceDeclaration) node() {}
88+
func (s *SourceDeclaration) scalarExpression() {}
89+
func (s *SourceDeclaration) booleanExpression() {}
8990

9091
// EventSessionObjectName represents an event session object name
9192
type EventSessionObjectName struct {

parser/marshal.go

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,16 @@ func eventSessionObjectNameToJSON(e *ast.EventSessionObjectName) jsonNode {
16971697
return node
16981698
}
16991699

1700+
func sourceDeclarationToJSON(s *ast.SourceDeclaration) jsonNode {
1701+
node := jsonNode{
1702+
"$type": "SourceDeclaration",
1703+
}
1704+
if s.Value != nil {
1705+
node["Value"] = eventSessionObjectNameToJSON(s.Value)
1706+
}
1707+
return node
1708+
}
1709+
17001710
func identifierOrValueExpressionToJSON(iove *ast.IdentifierOrValueExpression) jsonNode {
17011711
node := jsonNode{
17021712
"$type": "IdentifierOrValueExpression",
@@ -2036,6 +2046,22 @@ func booleanExpressionToJSON(expr ast.BooleanExpression) jsonNode {
20362046
node["ThirdExpression"] = scalarExpressionToJSON(e.ThirdExpression)
20372047
}
20382048
return node
2049+
case *ast.EventDeclarationCompareFunctionParameter:
2050+
node := jsonNode{
2051+
"$type": "EventDeclarationCompareFunctionParameter",
2052+
}
2053+
if e.Name != nil {
2054+
node["Name"] = eventSessionObjectNameToJSON(e.Name)
2055+
}
2056+
if e.SourceDeclaration != nil {
2057+
node["SourceDeclaration"] = sourceDeclarationToJSON(e.SourceDeclaration)
2058+
}
2059+
if e.EventValue != nil {
2060+
node["EventValue"] = scalarExpressionToJSON(e.EventValue)
2061+
}
2062+
return node
2063+
case *ast.SourceDeclaration:
2064+
return sourceDeclarationToJSON(e)
20392065
default:
20402066
return jsonNode{"$type": "UnknownBooleanExpression"}
20412067
}
@@ -10460,9 +10486,149 @@ func createEventSessionStatementToJSON(s *ast.CreateEventSessionStatement) jsonN
1046010486
if s.Name != nil {
1046110487
node["Name"] = identifierToJSON(s.Name)
1046210488
}
10489+
if s.SessionScope != "" {
10490+
node["SessionScope"] = s.SessionScope
10491+
}
10492+
if len(s.EventDeclarations) > 0 {
10493+
events := make([]jsonNode, len(s.EventDeclarations))
10494+
for i, e := range s.EventDeclarations {
10495+
events[i] = eventDeclarationToJSON(e)
10496+
}
10497+
node["EventDeclarations"] = events
10498+
}
10499+
if len(s.TargetDeclarations) > 0 {
10500+
targets := make([]jsonNode, len(s.TargetDeclarations))
10501+
for i, t := range s.TargetDeclarations {
10502+
targets[i] = targetDeclarationToJSON(t)
10503+
}
10504+
node["TargetDeclarations"] = targets
10505+
}
10506+
if len(s.SessionOptions) > 0 {
10507+
opts := make([]jsonNode, len(s.SessionOptions))
10508+
for i, o := range s.SessionOptions {
10509+
opts[i] = sessionOptionToJSON(o)
10510+
}
10511+
node["SessionOptions"] = opts
10512+
}
10513+
return node
10514+
}
10515+
10516+
func eventDeclarationToJSON(e *ast.EventDeclaration) jsonNode {
10517+
node := jsonNode{
10518+
"$type": "EventDeclaration",
10519+
}
10520+
if e.ObjectName != nil {
10521+
node["ObjectName"] = eventSessionObjectNameToJSON(e.ObjectName)
10522+
}
10523+
if len(e.EventDeclarationActionParameters) > 0 {
10524+
actions := make([]jsonNode, len(e.EventDeclarationActionParameters))
10525+
for i, a := range e.EventDeclarationActionParameters {
10526+
actions[i] = eventSessionObjectNameToJSON(a)
10527+
}
10528+
node["EventDeclarationActionParameters"] = actions
10529+
}
10530+
if e.EventDeclarationPredicateParameter != nil {
10531+
node["EventDeclarationPredicateParameter"] = booleanExpressionToJSON(e.EventDeclarationPredicateParameter)
10532+
}
10533+
return node
10534+
}
10535+
10536+
func targetDeclarationToJSON(t *ast.TargetDeclaration) jsonNode {
10537+
node := jsonNode{
10538+
"$type": "TargetDeclaration",
10539+
}
10540+
if t.ObjectName != nil {
10541+
node["ObjectName"] = eventSessionObjectNameToJSON(t.ObjectName)
10542+
}
10543+
if len(t.TargetDeclarationParameters) > 0 {
10544+
params := make([]jsonNode, len(t.TargetDeclarationParameters))
10545+
for i, p := range t.TargetDeclarationParameters {
10546+
params[i] = eventDeclarationSetParameterToJSON(p)
10547+
}
10548+
node["TargetDeclarationParameters"] = params
10549+
}
10550+
return node
10551+
}
10552+
10553+
func eventDeclarationSetParameterToJSON(p *ast.EventDeclarationSetParameter) jsonNode {
10554+
node := jsonNode{
10555+
"$type": "EventDeclarationSetParameter",
10556+
}
10557+
if p.EventField != nil {
10558+
node["EventField"] = identifierToJSON(p.EventField)
10559+
}
10560+
if p.EventValue != nil {
10561+
node["EventValue"] = scalarExpressionToJSON(p.EventValue)
10562+
}
1046310563
return node
1046410564
}
1046510565

10566+
func sessionOptionToJSON(o ast.SessionOption) jsonNode {
10567+
switch opt := o.(type) {
10568+
case *ast.LiteralSessionOption:
10569+
node := jsonNode{
10570+
"$type": "LiteralSessionOption",
10571+
}
10572+
if opt.Value != nil {
10573+
node["Value"] = scalarExpressionToJSON(opt.Value)
10574+
}
10575+
if opt.Unit != "" {
10576+
node["Unit"] = opt.Unit
10577+
}
10578+
if opt.OptionKind != "" {
10579+
node["OptionKind"] = opt.OptionKind
10580+
}
10581+
return node
10582+
case *ast.OnOffSessionOption:
10583+
node := jsonNode{
10584+
"$type": "OnOffSessionOption",
10585+
}
10586+
if opt.OptionState != "" {
10587+
node["OptionState"] = opt.OptionState
10588+
}
10589+
if opt.OptionKind != "" {
10590+
node["OptionKind"] = opt.OptionKind
10591+
}
10592+
return node
10593+
case *ast.EventRetentionSessionOption:
10594+
node := jsonNode{
10595+
"$type": "EventRetentionSessionOption",
10596+
}
10597+
if opt.Value != "" {
10598+
node["Value"] = opt.Value
10599+
}
10600+
if opt.OptionKind != "" {
10601+
node["OptionKind"] = opt.OptionKind
10602+
}
10603+
return node
10604+
case *ast.MaxDispatchLatencySessionOption:
10605+
node := jsonNode{
10606+
"$type": "MaxDispatchLatencySessionOption",
10607+
"IsInfinite": opt.IsInfinite,
10608+
}
10609+
if opt.Value != nil {
10610+
node["Value"] = scalarExpressionToJSON(opt.Value)
10611+
}
10612+
if opt.OptionKind != "" {
10613+
node["OptionKind"] = opt.OptionKind
10614+
}
10615+
return node
10616+
case *ast.MemoryPartitionSessionOption:
10617+
node := jsonNode{
10618+
"$type": "MemoryPartitionSessionOption",
10619+
}
10620+
if opt.Value != "" {
10621+
node["Value"] = opt.Value
10622+
}
10623+
if opt.OptionKind != "" {
10624+
node["OptionKind"] = opt.OptionKind
10625+
}
10626+
return node
10627+
default:
10628+
return jsonNode{"$type": "UnknownSessionOption"}
10629+
}
10630+
}
10631+
1046610632
func insertBulkStatementToJSON(s *ast.InsertBulkStatement) jsonNode {
1046710633
node := jsonNode{
1046810634
"$type": "InsertBulkStatement",

0 commit comments

Comments
 (0)