Skip to content

Commit 7e7f4bc

Browse files
committed
Add comprehensive SET statement parsing support
Adds parsing for: - SetCommandStatement with SetFipsFlaggerCommand and GeneralSetCommand - SET TRANSACTION ISOLATION LEVEL - SET TEXTSIZE - SET IDENTITY_INSERT - SET ERRLVL - Various SET commands: FIPS_FLAGGER, LANGUAGE, DATEFORMAT, DATEFIRST, DEADLOCK_PRIORITY, LOCK_TIMEOUT, CONTEXT_INFO, QUERY_GOVERNOR_COST_LIMIT
1 parent 855cfe9 commit 7e7f4bc

4 files changed

Lines changed: 431 additions & 2 deletions

File tree

ast/set_command_statement.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package ast
2+
3+
// SetCommandStatement represents a SET statement with commands (not variables)
4+
type SetCommandStatement struct {
5+
Commands []SetCommand
6+
}
7+
8+
func (s *SetCommandStatement) node() {}
9+
func (s *SetCommandStatement) statement() {}
10+
11+
// SetCommand is an interface for SET commands
12+
type SetCommand interface {
13+
Node
14+
setCommand()
15+
}
16+
17+
// SetFipsFlaggerCommand represents SET FIPS_FLAGGER command
18+
type SetFipsFlaggerCommand struct {
19+
ComplianceLevel string // "Off", "Entry", "Intermediate", "Full"
20+
}
21+
22+
func (s *SetFipsFlaggerCommand) node() {}
23+
func (s *SetFipsFlaggerCommand) setCommand() {}
24+
25+
// GeneralSetCommand represents SET commands like LANGUAGE, DATEFORMAT, etc.
26+
type GeneralSetCommand struct {
27+
CommandType string // "Language", "DateFormat", "DateFirst", "DeadlockPriority", "LockTimeout", "ContextInfo", "QueryGovernorCostLimit"
28+
Parameter ScalarExpression // The parameter value
29+
}
30+
31+
func (s *GeneralSetCommand) node() {}
32+
func (s *GeneralSetCommand) setCommand() {}
33+
34+
// SetTransactionIsolationLevelStatement represents SET TRANSACTION ISOLATION LEVEL statement
35+
type SetTransactionIsolationLevelStatement struct {
36+
Level string // "ReadUncommitted", "ReadCommitted", "RepeatableRead", "Serializable", "Snapshot"
37+
}
38+
39+
func (s *SetTransactionIsolationLevelStatement) node() {}
40+
func (s *SetTransactionIsolationLevelStatement) statement() {}
41+
42+
// SetTextSizeStatement represents SET TEXTSIZE statement
43+
type SetTextSizeStatement struct {
44+
TextSize ScalarExpression
45+
}
46+
47+
func (s *SetTextSizeStatement) node() {}
48+
func (s *SetTextSizeStatement) statement() {}
49+
50+
// SetIdentityInsertStatement represents SET IDENTITY_INSERT statement
51+
type SetIdentityInsertStatement struct {
52+
Table *SchemaObjectName
53+
IsOn bool
54+
}
55+
56+
func (s *SetIdentityInsertStatement) node() {}
57+
func (s *SetIdentityInsertStatement) statement() {}
58+
59+
// SetErrorLevelStatement represents SET ERRLVL statement
60+
type SetErrorLevelStatement struct {
61+
Level ScalarExpression
62+
}
63+
64+
func (s *SetErrorLevelStatement) node() {}
65+
func (s *SetErrorLevelStatement) statement() {}

parser/marshal.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ func statementToJSON(stmt ast.Statement) jsonNode {
248248
return setRowCountStatementToJSON(s)
249249
case *ast.SetOffsetsStatement:
250250
return setOffsetsStatementToJSON(s)
251+
case *ast.SetCommandStatement:
252+
return setCommandStatementToJSON(s)
253+
case *ast.SetTransactionIsolationLevelStatement:
254+
return setTransactionIsolationLevelStatementToJSON(s)
255+
case *ast.SetTextSizeStatement:
256+
return setTextSizeStatementToJSON(s)
257+
case *ast.SetIdentityInsertStatement:
258+
return setIdentityInsertStatementToJSON(s)
259+
case *ast.SetErrorLevelStatement:
260+
return setErrorLevelStatementToJSON(s)
251261
case *ast.CommitTransactionStatement:
252262
return commitTransactionStatementToJSON(s)
253263
case *ast.RollbackTransactionStatement:
@@ -6226,6 +6236,79 @@ func setOffsetsStatementToJSON(s *ast.SetOffsetsStatement) jsonNode {
62266236
}
62276237
}
62286238

6239+
func setCommandStatementToJSON(s *ast.SetCommandStatement) jsonNode {
6240+
node := jsonNode{
6241+
"$type": "SetCommandStatement",
6242+
}
6243+
if len(s.Commands) > 0 {
6244+
cmds := make([]jsonNode, len(s.Commands))
6245+
for i, cmd := range s.Commands {
6246+
cmds[i] = setCommandToJSON(cmd)
6247+
}
6248+
node["Commands"] = cmds
6249+
}
6250+
return node
6251+
}
6252+
6253+
func setCommandToJSON(cmd ast.SetCommand) jsonNode {
6254+
switch c := cmd.(type) {
6255+
case *ast.SetFipsFlaggerCommand:
6256+
return jsonNode{
6257+
"$type": "SetFipsFlaggerCommand",
6258+
"ComplianceLevel": c.ComplianceLevel,
6259+
}
6260+
case *ast.GeneralSetCommand:
6261+
node := jsonNode{
6262+
"$type": "GeneralSetCommand",
6263+
"CommandType": c.CommandType,
6264+
}
6265+
if c.Parameter != nil {
6266+
node["Parameter"] = scalarExpressionToJSON(c.Parameter)
6267+
}
6268+
return node
6269+
default:
6270+
return jsonNode{"$type": "UnknownSetCommand"}
6271+
}
6272+
}
6273+
6274+
func setTransactionIsolationLevelStatementToJSON(s *ast.SetTransactionIsolationLevelStatement) jsonNode {
6275+
return jsonNode{
6276+
"$type": "SetTransactionIsolationLevelStatement",
6277+
"Level": s.Level,
6278+
}
6279+
}
6280+
6281+
func setTextSizeStatementToJSON(s *ast.SetTextSizeStatement) jsonNode {
6282+
node := jsonNode{
6283+
"$type": "SetTextSizeStatement",
6284+
}
6285+
if s.TextSize != nil {
6286+
node["TextSize"] = scalarExpressionToJSON(s.TextSize)
6287+
}
6288+
return node
6289+
}
6290+
6291+
func setIdentityInsertStatementToJSON(s *ast.SetIdentityInsertStatement) jsonNode {
6292+
node := jsonNode{
6293+
"$type": "SetIdentityInsertStatement",
6294+
"IsOn": s.IsOn,
6295+
}
6296+
if s.Table != nil {
6297+
node["Table"] = schemaObjectNameToJSON(s.Table)
6298+
}
6299+
return node
6300+
}
6301+
6302+
func setErrorLevelStatementToJSON(s *ast.SetErrorLevelStatement) jsonNode {
6303+
node := jsonNode{
6304+
"$type": "SetErrorLevelStatement",
6305+
}
6306+
if s.Level != nil {
6307+
node["Level"] = scalarExpressionToJSON(s.Level)
6308+
}
6309+
return node
6310+
}
6311+
62296312
func commitTransactionStatementToJSON(s *ast.CommitTransactionStatement) jsonNode {
62306313
node := jsonNode{
62316314
"$type": "CommitTransactionStatement",

0 commit comments

Comments
 (0)