Skip to content

Commit 8fa9ff9

Browse files
authored
Add parser features and enable 6 more tests (#20)
1 parent 502c84d commit 8fa9ff9

20 files changed

Lines changed: 977 additions & 52 deletions

File tree

ast/dbcc_statement.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Package ast provides AST types for T-SQL parsing.
2+
package ast
3+
4+
// DbccStatement represents a DBCC statement.
5+
type DbccStatement struct {
6+
DllName string
7+
Command string
8+
ParenthesisRequired bool
9+
Literals []*DbccNamedLiteral
10+
Options []*DbccOption
11+
OptionsUseJoin bool
12+
}
13+
14+
func (s *DbccStatement) statement() {}
15+
func (s *DbccStatement) node() {}
16+
17+
// DbccNamedLiteral represents a parameter in a DBCC statement.
18+
type DbccNamedLiteral struct {
19+
Name string
20+
Value ScalarExpression
21+
}
22+
23+
func (l *DbccNamedLiteral) node() {}
24+
25+
// DbccOption represents an option in a DBCC statement.
26+
type DbccOption struct {
27+
OptionKind string
28+
}
29+
30+
func (o *DbccOption) node() {}

ast/declare_variable_statement.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,12 @@ type XmlDataTypeReference struct {
5252

5353
func (x *XmlDataTypeReference) node() {}
5454
func (x *XmlDataTypeReference) dataTypeReference() {}
55+
56+
// UserDataTypeReference represents a user-defined data type reference.
57+
type UserDataTypeReference struct {
58+
Name *SchemaObjectName `json:"Name,omitempty"`
59+
Parameters []ScalarExpression `json:"Parameters,omitempty"`
60+
}
61+
62+
func (u *UserDataTypeReference) node() {}
63+
func (u *UserDataTypeReference) dataTypeReference() {}

ast/sequence_statements.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Package ast provides AST types for T-SQL parsing.
2+
package ast
3+
4+
// SequenceOption represents a sequence option without a value.
5+
type SequenceOption struct {
6+
OptionKind string
7+
NoValue bool
8+
}
9+
10+
func (o *SequenceOption) node() {}
11+
12+
// ScalarExpressionSequenceOption represents a sequence option with a value.
13+
type ScalarExpressionSequenceOption struct {
14+
OptionKind string
15+
OptionValue ScalarExpression
16+
NoValue bool
17+
}
18+
19+
func (o *ScalarExpressionSequenceOption) node() {}
20+
21+
// DataTypeSequenceOption represents a sequence option with a data type (AS clause).
22+
type DataTypeSequenceOption struct {
23+
OptionKind string
24+
DataType DataTypeReference
25+
NoValue bool
26+
}
27+
28+
func (o *DataTypeSequenceOption) node() {}
29+
30+
// CreateSequenceStatement represents a CREATE SEQUENCE statement.
31+
type CreateSequenceStatement struct {
32+
Name *SchemaObjectName
33+
SequenceOptions []interface{} // Can be SequenceOption or ScalarExpressionSequenceOption
34+
}
35+
36+
func (s *CreateSequenceStatement) statement() {}
37+
func (s *CreateSequenceStatement) node() {}
38+
39+
// AlterSequenceStatement represents an ALTER SEQUENCE statement.
40+
type AlterSequenceStatement struct {
41+
Name *SchemaObjectName
42+
SequenceOptions []interface{} // Can be SequenceOption or ScalarExpressionSequenceOption
43+
}
44+
45+
func (s *AlterSequenceStatement) statement() {}
46+
func (s *AlterSequenceStatement) node() {}

ast/workload_group_statements.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Package ast provides AST types for T-SQL parsing.
2+
package ast
3+
4+
// WorkloadGroupResourceParameter represents a resource parameter in a workload group statement.
5+
type WorkloadGroupResourceParameter struct {
6+
ParameterValue ScalarExpression
7+
ParameterType string
8+
}
9+
10+
func (p *WorkloadGroupResourceParameter) node() {}
11+
12+
// WorkloadGroupImportanceParameter represents an importance parameter in a workload group statement.
13+
type WorkloadGroupImportanceParameter struct {
14+
ParameterValue string
15+
ParameterType string
16+
}
17+
18+
func (p *WorkloadGroupImportanceParameter) node() {}
19+
20+
// CreateWorkloadGroupStatement represents a CREATE WORKLOAD GROUP statement.
21+
type CreateWorkloadGroupStatement struct {
22+
Name *Identifier
23+
PoolName *Identifier
24+
ExternalPoolName *Identifier
25+
WorkloadGroupParameters []interface{} // Can be WorkloadGroupResourceParameter or WorkloadGroupImportanceParameter
26+
}
27+
28+
func (s *CreateWorkloadGroupStatement) statement() {}
29+
func (s *CreateWorkloadGroupStatement) node() {}
30+
31+
// AlterWorkloadGroupStatement represents an ALTER WORKLOAD GROUP statement.
32+
type AlterWorkloadGroupStatement struct {
33+
Name *Identifier
34+
PoolName *Identifier
35+
ExternalPoolName *Identifier
36+
WorkloadGroupParameters []interface{} // Can be WorkloadGroupResourceParameter or WorkloadGroupImportanceParameter
37+
}
38+
39+
func (s *AlterWorkloadGroupStatement) statement() {}
40+
func (s *AlterWorkloadGroupStatement) node() {}

parser/lexer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ const (
208208
TokenDecryption
209209
TokenAsymmetric
210210
TokenCertificate
211+
TokenDbcc
211212
)
212213

213214
// Token represents a lexical token.
@@ -704,6 +705,7 @@ var keywords = map[string]TokenType{
704705
"DECRYPTION": TokenDecryption,
705706
"ASYMMETRIC": TokenAsymmetric,
706707
"CERTIFICATE": TokenCertificate,
708+
"DBCC": TokenDbcc,
707709
}
708710

709711
func lookupKeyword(ident string) TokenType {

parser/marshal.go

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ func statementToJSON(stmt ast.Statement) jsonNode {
186186
return dropWorkloadGroupStatementToJSON(s)
187187
case *ast.DropWorkloadClassifierStatement:
188188
return dropWorkloadClassifierStatementToJSON(s)
189+
case *ast.CreateWorkloadGroupStatement:
190+
return createWorkloadGroupStatementToJSON(s)
191+
case *ast.AlterWorkloadGroupStatement:
192+
return alterWorkloadGroupStatementToJSON(s)
193+
case *ast.AlterSequenceStatement:
194+
return alterSequenceStatementToJSON(s)
195+
case *ast.CreateSequenceStatement:
196+
return createSequenceStatementToJSON(s)
197+
case *ast.DbccStatement:
198+
return dbccStatementToJSON(s)
189199
case *ast.DropTypeStatement:
190200
return dropTypeStatementToJSON(s)
191201
case *ast.DropAggregateStatement:
@@ -2721,11 +2731,23 @@ func dataTypeReferenceToJSON(d ast.DataTypeReference) jsonNode {
27212731
return sqlDataTypeReferenceToJSON(dt)
27222732
case *ast.XmlDataTypeReference:
27232733
return xmlDataTypeReferenceToJSON(dt)
2734+
case *ast.UserDataTypeReference:
2735+
return userDataTypeReferenceToJSON(dt)
27242736
default:
27252737
return jsonNode{"$type": "UnknownDataType"}
27262738
}
27272739
}
27282740

2741+
func userDataTypeReferenceToJSON(dt *ast.UserDataTypeReference) jsonNode {
2742+
node := jsonNode{
2743+
"$type": "UserDataTypeReference",
2744+
}
2745+
if dt.Name != nil {
2746+
node["Name"] = schemaObjectNameToJSON(dt.Name)
2747+
}
2748+
return node
2749+
}
2750+
27292751
func xmlDataTypeReferenceToJSON(dt *ast.XmlDataTypeReference) jsonNode {
27302752
node := jsonNode{
27312753
"$type": "XmlDataTypeReference",
@@ -5504,6 +5526,188 @@ func dropWorkloadClassifierStatementToJSON(s *ast.DropWorkloadClassifierStatemen
55045526
return node
55055527
}
55065528

5529+
func createWorkloadGroupStatementToJSON(s *ast.CreateWorkloadGroupStatement) jsonNode {
5530+
node := jsonNode{
5531+
"$type": "CreateWorkloadGroupStatement",
5532+
}
5533+
if s.Name != nil {
5534+
node["Name"] = identifierToJSON(s.Name)
5535+
}
5536+
if len(s.WorkloadGroupParameters) > 0 {
5537+
params := make([]jsonNode, len(s.WorkloadGroupParameters))
5538+
for i, p := range s.WorkloadGroupParameters {
5539+
params[i] = workloadGroupParameterToJSON(p)
5540+
}
5541+
node["WorkloadGroupParameters"] = params
5542+
}
5543+
if s.PoolName != nil {
5544+
node["PoolName"] = identifierToJSON(s.PoolName)
5545+
}
5546+
if s.ExternalPoolName != nil {
5547+
node["ExternalPoolName"] = identifierToJSON(s.ExternalPoolName)
5548+
}
5549+
return node
5550+
}
5551+
5552+
func alterWorkloadGroupStatementToJSON(s *ast.AlterWorkloadGroupStatement) jsonNode {
5553+
node := jsonNode{
5554+
"$type": "AlterWorkloadGroupStatement",
5555+
}
5556+
if s.Name != nil {
5557+
node["Name"] = identifierToJSON(s.Name)
5558+
}
5559+
if len(s.WorkloadGroupParameters) > 0 {
5560+
params := make([]jsonNode, len(s.WorkloadGroupParameters))
5561+
for i, p := range s.WorkloadGroupParameters {
5562+
params[i] = workloadGroupParameterToJSON(p)
5563+
}
5564+
node["WorkloadGroupParameters"] = params
5565+
}
5566+
if s.PoolName != nil {
5567+
node["PoolName"] = identifierToJSON(s.PoolName)
5568+
}
5569+
if s.ExternalPoolName != nil {
5570+
node["ExternalPoolName"] = identifierToJSON(s.ExternalPoolName)
5571+
}
5572+
return node
5573+
}
5574+
5575+
func workloadGroupParameterToJSON(p interface{}) jsonNode {
5576+
switch param := p.(type) {
5577+
case *ast.WorkloadGroupResourceParameter:
5578+
node := jsonNode{
5579+
"$type": "WorkloadGroupResourceParameter",
5580+
"ParameterType": param.ParameterType,
5581+
}
5582+
if param.ParameterValue != nil {
5583+
node["ParameterValue"] = scalarExpressionToJSON(param.ParameterValue)
5584+
}
5585+
return node
5586+
case *ast.WorkloadGroupImportanceParameter:
5587+
return jsonNode{
5588+
"$type": "WorkloadGroupImportanceParameter",
5589+
"ParameterType": param.ParameterType,
5590+
"ParameterValue": param.ParameterValue,
5591+
}
5592+
default:
5593+
return jsonNode{}
5594+
}
5595+
}
5596+
5597+
func alterSequenceStatementToJSON(s *ast.AlterSequenceStatement) jsonNode {
5598+
node := jsonNode{
5599+
"$type": "AlterSequenceStatement",
5600+
}
5601+
if s.Name != nil {
5602+
node["Name"] = schemaObjectNameToJSON(s.Name)
5603+
}
5604+
if len(s.SequenceOptions) > 0 {
5605+
opts := make([]jsonNode, len(s.SequenceOptions))
5606+
for i, opt := range s.SequenceOptions {
5607+
opts[i] = sequenceOptionToJSON(opt)
5608+
}
5609+
node["SequenceOptions"] = opts
5610+
}
5611+
return node
5612+
}
5613+
5614+
func createSequenceStatementToJSON(s *ast.CreateSequenceStatement) jsonNode {
5615+
node := jsonNode{
5616+
"$type": "CreateSequenceStatement",
5617+
}
5618+
if s.Name != nil {
5619+
node["Name"] = schemaObjectNameToJSON(s.Name)
5620+
}
5621+
if len(s.SequenceOptions) > 0 {
5622+
opts := make([]jsonNode, len(s.SequenceOptions))
5623+
for i, opt := range s.SequenceOptions {
5624+
opts[i] = sequenceOptionToJSON(opt)
5625+
}
5626+
node["SequenceOptions"] = opts
5627+
}
5628+
return node
5629+
}
5630+
5631+
func sequenceOptionToJSON(opt interface{}) jsonNode {
5632+
switch o := opt.(type) {
5633+
case *ast.SequenceOption:
5634+
return jsonNode{
5635+
"$type": "SequenceOption",
5636+
"OptionKind": o.OptionKind,
5637+
"NoValue": o.NoValue,
5638+
}
5639+
case *ast.ScalarExpressionSequenceOption:
5640+
node := jsonNode{
5641+
"$type": "ScalarExpressionSequenceOption",
5642+
"OptionKind": o.OptionKind,
5643+
"NoValue": o.NoValue,
5644+
}
5645+
if o.OptionValue != nil {
5646+
node["OptionValue"] = scalarExpressionToJSON(o.OptionValue)
5647+
}
5648+
return node
5649+
case *ast.DataTypeSequenceOption:
5650+
node := jsonNode{
5651+
"$type": "DataTypeSequenceOption",
5652+
"OptionKind": o.OptionKind,
5653+
"NoValue": o.NoValue,
5654+
}
5655+
if o.DataType != nil {
5656+
node["DataType"] = dataTypeReferenceToJSON(o.DataType)
5657+
}
5658+
return node
5659+
default:
5660+
return jsonNode{}
5661+
}
5662+
}
5663+
5664+
func dbccStatementToJSON(s *ast.DbccStatement) jsonNode {
5665+
node := jsonNode{
5666+
"$type": "DbccStatement",
5667+
"Command": s.Command,
5668+
"ParenthesisRequired": s.ParenthesisRequired,
5669+
"OptionsUseJoin": s.OptionsUseJoin,
5670+
}
5671+
if s.DllName != "" {
5672+
node["DllName"] = s.DllName
5673+
}
5674+
if len(s.Literals) > 0 {
5675+
lits := make([]jsonNode, len(s.Literals))
5676+
for i, lit := range s.Literals {
5677+
lits[i] = dbccNamedLiteralToJSON(lit)
5678+
}
5679+
node["Literals"] = lits
5680+
}
5681+
if len(s.Options) > 0 {
5682+
opts := make([]jsonNode, len(s.Options))
5683+
for i, opt := range s.Options {
5684+
opts[i] = dbccOptionToJSON(opt)
5685+
}
5686+
node["Options"] = opts
5687+
}
5688+
return node
5689+
}
5690+
5691+
func dbccNamedLiteralToJSON(l *ast.DbccNamedLiteral) jsonNode {
5692+
node := jsonNode{
5693+
"$type": "DbccNamedLiteral",
5694+
}
5695+
if l.Name != "" {
5696+
node["Name"] = l.Name
5697+
}
5698+
if l.Value != nil {
5699+
node["Value"] = scalarExpressionToJSON(l.Value)
5700+
}
5701+
return node
5702+
}
5703+
5704+
func dbccOptionToJSON(o *ast.DbccOption) jsonNode {
5705+
return jsonNode{
5706+
"$type": "DbccOption",
5707+
"OptionKind": o.OptionKind,
5708+
}
5709+
}
5710+
55075711
func dropTypeStatementToJSON(s *ast.DropTypeStatement) jsonNode {
55085712
node := jsonNode{
55095713
"$type": "DropTypeStatement",

0 commit comments

Comments
 (0)