Skip to content
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
30 changes: 30 additions & 0 deletions ast/dbcc_statement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Package ast provides AST types for T-SQL parsing.
package ast

// DbccStatement represents a DBCC statement.
type DbccStatement struct {
DllName string
Command string
ParenthesisRequired bool
Literals []*DbccNamedLiteral
Options []*DbccOption
OptionsUseJoin bool
}

func (s *DbccStatement) statement() {}
func (s *DbccStatement) node() {}

// DbccNamedLiteral represents a parameter in a DBCC statement.
type DbccNamedLiteral struct {
Name string
Value ScalarExpression
}

func (l *DbccNamedLiteral) node() {}

// DbccOption represents an option in a DBCC statement.
type DbccOption struct {
OptionKind string
}

func (o *DbccOption) node() {}
9 changes: 9 additions & 0 deletions ast/declare_variable_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,12 @@ type XmlDataTypeReference struct {

func (x *XmlDataTypeReference) node() {}
func (x *XmlDataTypeReference) dataTypeReference() {}

// UserDataTypeReference represents a user-defined data type reference.
type UserDataTypeReference struct {
Name *SchemaObjectName `json:"Name,omitempty"`
Parameters []ScalarExpression `json:"Parameters,omitempty"`
}

func (u *UserDataTypeReference) node() {}
func (u *UserDataTypeReference) dataTypeReference() {}
46 changes: 46 additions & 0 deletions ast/sequence_statements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Package ast provides AST types for T-SQL parsing.
package ast

// SequenceOption represents a sequence option without a value.
type SequenceOption struct {
OptionKind string
NoValue bool
}

func (o *SequenceOption) node() {}

// ScalarExpressionSequenceOption represents a sequence option with a value.
type ScalarExpressionSequenceOption struct {
OptionKind string
OptionValue ScalarExpression
NoValue bool
}

func (o *ScalarExpressionSequenceOption) node() {}

// DataTypeSequenceOption represents a sequence option with a data type (AS clause).
type DataTypeSequenceOption struct {
OptionKind string
DataType DataTypeReference
NoValue bool
}

func (o *DataTypeSequenceOption) node() {}

// CreateSequenceStatement represents a CREATE SEQUENCE statement.
type CreateSequenceStatement struct {
Name *SchemaObjectName
SequenceOptions []interface{} // Can be SequenceOption or ScalarExpressionSequenceOption
}

func (s *CreateSequenceStatement) statement() {}
func (s *CreateSequenceStatement) node() {}

// AlterSequenceStatement represents an ALTER SEQUENCE statement.
type AlterSequenceStatement struct {
Name *SchemaObjectName
SequenceOptions []interface{} // Can be SequenceOption or ScalarExpressionSequenceOption
}

func (s *AlterSequenceStatement) statement() {}
func (s *AlterSequenceStatement) node() {}
40 changes: 40 additions & 0 deletions ast/workload_group_statements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Package ast provides AST types for T-SQL parsing.
package ast

// WorkloadGroupResourceParameter represents a resource parameter in a workload group statement.
type WorkloadGroupResourceParameter struct {
ParameterValue ScalarExpression
ParameterType string
}

func (p *WorkloadGroupResourceParameter) node() {}

// WorkloadGroupImportanceParameter represents an importance parameter in a workload group statement.
type WorkloadGroupImportanceParameter struct {
ParameterValue string
ParameterType string
}

func (p *WorkloadGroupImportanceParameter) node() {}

// CreateWorkloadGroupStatement represents a CREATE WORKLOAD GROUP statement.
type CreateWorkloadGroupStatement struct {
Name *Identifier
PoolName *Identifier
ExternalPoolName *Identifier
WorkloadGroupParameters []interface{} // Can be WorkloadGroupResourceParameter or WorkloadGroupImportanceParameter
}

func (s *CreateWorkloadGroupStatement) statement() {}
func (s *CreateWorkloadGroupStatement) node() {}

// AlterWorkloadGroupStatement represents an ALTER WORKLOAD GROUP statement.
type AlterWorkloadGroupStatement struct {
Name *Identifier
PoolName *Identifier
ExternalPoolName *Identifier
WorkloadGroupParameters []interface{} // Can be WorkloadGroupResourceParameter or WorkloadGroupImportanceParameter
}

func (s *AlterWorkloadGroupStatement) statement() {}
func (s *AlterWorkloadGroupStatement) node() {}
2 changes: 2 additions & 0 deletions parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ const (
TokenDecryption
TokenAsymmetric
TokenCertificate
TokenDbcc
)

// Token represents a lexical token.
Expand Down Expand Up @@ -704,6 +705,7 @@ var keywords = map[string]TokenType{
"DECRYPTION": TokenDecryption,
"ASYMMETRIC": TokenAsymmetric,
"CERTIFICATE": TokenCertificate,
"DBCC": TokenDbcc,
}

func lookupKeyword(ident string) TokenType {
Expand Down
204 changes: 204 additions & 0 deletions parser/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ func statementToJSON(stmt ast.Statement) jsonNode {
return dropWorkloadGroupStatementToJSON(s)
case *ast.DropWorkloadClassifierStatement:
return dropWorkloadClassifierStatementToJSON(s)
case *ast.CreateWorkloadGroupStatement:
return createWorkloadGroupStatementToJSON(s)
case *ast.AlterWorkloadGroupStatement:
return alterWorkloadGroupStatementToJSON(s)
case *ast.AlterSequenceStatement:
return alterSequenceStatementToJSON(s)
case *ast.CreateSequenceStatement:
return createSequenceStatementToJSON(s)
case *ast.DbccStatement:
return dbccStatementToJSON(s)
case *ast.DropTypeStatement:
return dropTypeStatementToJSON(s)
case *ast.DropAggregateStatement:
Expand Down Expand Up @@ -2721,11 +2731,23 @@ func dataTypeReferenceToJSON(d ast.DataTypeReference) jsonNode {
return sqlDataTypeReferenceToJSON(dt)
case *ast.XmlDataTypeReference:
return xmlDataTypeReferenceToJSON(dt)
case *ast.UserDataTypeReference:
return userDataTypeReferenceToJSON(dt)
default:
return jsonNode{"$type": "UnknownDataType"}
}
}

func userDataTypeReferenceToJSON(dt *ast.UserDataTypeReference) jsonNode {
node := jsonNode{
"$type": "UserDataTypeReference",
}
if dt.Name != nil {
node["Name"] = schemaObjectNameToJSON(dt.Name)
}
return node
}

func xmlDataTypeReferenceToJSON(dt *ast.XmlDataTypeReference) jsonNode {
node := jsonNode{
"$type": "XmlDataTypeReference",
Expand Down Expand Up @@ -5504,6 +5526,188 @@ func dropWorkloadClassifierStatementToJSON(s *ast.DropWorkloadClassifierStatemen
return node
}

func createWorkloadGroupStatementToJSON(s *ast.CreateWorkloadGroupStatement) jsonNode {
node := jsonNode{
"$type": "CreateWorkloadGroupStatement",
}
if s.Name != nil {
node["Name"] = identifierToJSON(s.Name)
}
if len(s.WorkloadGroupParameters) > 0 {
params := make([]jsonNode, len(s.WorkloadGroupParameters))
for i, p := range s.WorkloadGroupParameters {
params[i] = workloadGroupParameterToJSON(p)
}
node["WorkloadGroupParameters"] = params
}
if s.PoolName != nil {
node["PoolName"] = identifierToJSON(s.PoolName)
}
if s.ExternalPoolName != nil {
node["ExternalPoolName"] = identifierToJSON(s.ExternalPoolName)
}
return node
}

func alterWorkloadGroupStatementToJSON(s *ast.AlterWorkloadGroupStatement) jsonNode {
node := jsonNode{
"$type": "AlterWorkloadGroupStatement",
}
if s.Name != nil {
node["Name"] = identifierToJSON(s.Name)
}
if len(s.WorkloadGroupParameters) > 0 {
params := make([]jsonNode, len(s.WorkloadGroupParameters))
for i, p := range s.WorkloadGroupParameters {
params[i] = workloadGroupParameterToJSON(p)
}
node["WorkloadGroupParameters"] = params
}
if s.PoolName != nil {
node["PoolName"] = identifierToJSON(s.PoolName)
}
if s.ExternalPoolName != nil {
node["ExternalPoolName"] = identifierToJSON(s.ExternalPoolName)
}
return node
}

func workloadGroupParameterToJSON(p interface{}) jsonNode {
switch param := p.(type) {
case *ast.WorkloadGroupResourceParameter:
node := jsonNode{
"$type": "WorkloadGroupResourceParameter",
"ParameterType": param.ParameterType,
}
if param.ParameterValue != nil {
node["ParameterValue"] = scalarExpressionToJSON(param.ParameterValue)
}
return node
case *ast.WorkloadGroupImportanceParameter:
return jsonNode{
"$type": "WorkloadGroupImportanceParameter",
"ParameterType": param.ParameterType,
"ParameterValue": param.ParameterValue,
}
default:
return jsonNode{}
}
}

func alterSequenceStatementToJSON(s *ast.AlterSequenceStatement) jsonNode {
node := jsonNode{
"$type": "AlterSequenceStatement",
}
if s.Name != nil {
node["Name"] = schemaObjectNameToJSON(s.Name)
}
if len(s.SequenceOptions) > 0 {
opts := make([]jsonNode, len(s.SequenceOptions))
for i, opt := range s.SequenceOptions {
opts[i] = sequenceOptionToJSON(opt)
}
node["SequenceOptions"] = opts
}
return node
}

func createSequenceStatementToJSON(s *ast.CreateSequenceStatement) jsonNode {
node := jsonNode{
"$type": "CreateSequenceStatement",
}
if s.Name != nil {
node["Name"] = schemaObjectNameToJSON(s.Name)
}
if len(s.SequenceOptions) > 0 {
opts := make([]jsonNode, len(s.SequenceOptions))
for i, opt := range s.SequenceOptions {
opts[i] = sequenceOptionToJSON(opt)
}
node["SequenceOptions"] = opts
}
return node
}

func sequenceOptionToJSON(opt interface{}) jsonNode {
switch o := opt.(type) {
case *ast.SequenceOption:
return jsonNode{
"$type": "SequenceOption",
"OptionKind": o.OptionKind,
"NoValue": o.NoValue,
}
case *ast.ScalarExpressionSequenceOption:
node := jsonNode{
"$type": "ScalarExpressionSequenceOption",
"OptionKind": o.OptionKind,
"NoValue": o.NoValue,
}
if o.OptionValue != nil {
node["OptionValue"] = scalarExpressionToJSON(o.OptionValue)
}
return node
case *ast.DataTypeSequenceOption:
node := jsonNode{
"$type": "DataTypeSequenceOption",
"OptionKind": o.OptionKind,
"NoValue": o.NoValue,
}
if o.DataType != nil {
node["DataType"] = dataTypeReferenceToJSON(o.DataType)
}
return node
default:
return jsonNode{}
}
}

func dbccStatementToJSON(s *ast.DbccStatement) jsonNode {
node := jsonNode{
"$type": "DbccStatement",
"Command": s.Command,
"ParenthesisRequired": s.ParenthesisRequired,
"OptionsUseJoin": s.OptionsUseJoin,
}
if s.DllName != "" {
node["DllName"] = s.DllName
}
if len(s.Literals) > 0 {
lits := make([]jsonNode, len(s.Literals))
for i, lit := range s.Literals {
lits[i] = dbccNamedLiteralToJSON(lit)
}
node["Literals"] = lits
}
if len(s.Options) > 0 {
opts := make([]jsonNode, len(s.Options))
for i, opt := range s.Options {
opts[i] = dbccOptionToJSON(opt)
}
node["Options"] = opts
}
return node
}

func dbccNamedLiteralToJSON(l *ast.DbccNamedLiteral) jsonNode {
node := jsonNode{
"$type": "DbccNamedLiteral",
}
if l.Name != "" {
node["Name"] = l.Name
}
if l.Value != nil {
node["Value"] = scalarExpressionToJSON(l.Value)
}
return node
}

func dbccOptionToJSON(o *ast.DbccOption) jsonNode {
return jsonNode{
"$type": "DbccOption",
"OptionKind": o.OptionKind,
}
}

func dropTypeStatementToJSON(s *ast.DropTypeStatement) jsonNode {
node := jsonNode{
"$type": "DropTypeStatement",
Expand Down
Loading
Loading