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
1 change: 1 addition & 0 deletions ast/bulk_insert_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (o *OrderBulkInsertOption) bulkInsertOption() {}
type BulkOpenRowset struct {
DataFiles []ScalarExpression `json:"DataFiles,omitempty"`
Options []BulkInsertOption `json:"Options,omitempty"`
Columns []*Identifier `json:"Columns,omitempty"`
Alias *Identifier `json:"Alias,omitempty"`
ForPath bool `json:"ForPath"`
}
Expand Down
15 changes: 13 additions & 2 deletions ast/execute_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ func (e *ExecuteStatement) statement() {}

// ExecuteSpecification contains the details of an EXECUTE.
type ExecuteSpecification struct {
Variable *VariableReference `json:"Variable,omitempty"`
ExecutableEntity ExecutableEntity `json:"ExecutableEntity,omitempty"`
Variable *VariableReference `json:"Variable,omitempty"`
LinkedServer *Identifier `json:"LinkedServer,omitempty"`
ExecuteContext *ExecuteContext `json:"ExecuteContext,omitempty"`
ExecutableEntity ExecutableEntity `json:"ExecutableEntity,omitempty"`
}

// ExecutableEntity is an interface for executable entities.
Expand All @@ -27,6 +29,15 @@ type ExecutableProcedureReference struct {

func (e *ExecutableProcedureReference) executableEntity() {}

// ExecutableStringList represents an EXECUTE with a string expression list.
// e.g., EXECUTE ('SELECT * FROM t1', param1, param2)
type ExecutableStringList struct {
Strings []ScalarExpression `json:"Strings,omitempty"`
Parameters []*ExecuteParameter `json:"Parameters,omitempty"`
}

func (e *ExecutableStringList) executableEntity() {}

// ProcedureReferenceName holds either a variable or a procedure reference.
type ProcedureReferenceName struct {
ProcedureVariable *VariableReference `json:"ProcedureVariable,omitempty"`
Expand Down
32 changes: 32 additions & 0 deletions parser/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,13 @@ func tableReferenceToJSON(ref ast.TableReference) jsonNode {
}
node["Options"] = opts
}
if len(r.Columns) > 0 {
cols := make([]jsonNode, len(r.Columns))
for i, c := range r.Columns {
cols[i] = identifierToJSON(c)
}
node["Columns"] = cols
}
if r.Alias != nil {
node["Alias"] = identifierToJSON(r.Alias)
}
Expand Down Expand Up @@ -1869,6 +1876,12 @@ func executeSpecificationToJSON(spec *ast.ExecuteSpecification) jsonNode {
if spec.Variable != nil {
node["Variable"] = scalarExpressionToJSON(spec.Variable)
}
if spec.LinkedServer != nil {
node["LinkedServer"] = identifierToJSON(spec.LinkedServer)
}
if spec.ExecuteContext != nil {
node["ExecuteContext"] = executeContextToJSON(spec.ExecuteContext)
}
if spec.ExecutableEntity != nil {
node["ExecutableEntity"] = executableEntityToJSON(spec.ExecutableEntity)
}
Expand All @@ -1892,6 +1905,25 @@ func executableEntityToJSON(entity ast.ExecutableEntity) jsonNode {
node["Parameters"] = params
}
return node
case *ast.ExecutableStringList:
node := jsonNode{
"$type": "ExecutableStringList",
}
if len(e.Strings) > 0 {
strs := make([]jsonNode, len(e.Strings))
for i, s := range e.Strings {
strs[i] = scalarExpressionToJSON(s)
}
node["Strings"] = strs
}
if len(e.Parameters) > 0 {
params := make([]jsonNode, len(e.Parameters))
for i, p := range e.Parameters {
params[i] = executeParameterToJSON(p)
}
node["Parameters"] = params
}
return node
default:
return jsonNode{"$type": "UnknownExecutableEntity"}
}
Expand Down
154 changes: 154 additions & 0 deletions parser/parse_dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,25 @@ func (p *Parser) parseBulkOpenRowset() (*ast.BulkOpenRowset, error) {
}
}

// Parse optional column list (e.g., AS a(c1, c2))
if p.curTok.Type == TokenLParen {
p.nextToken()
for {
if p.curTok.Type == TokenIdent || p.curTok.Type == TokenLBracket {
result.Columns = append(result.Columns, p.parseIdentifier())
}
if p.curTok.Type == TokenComma {
p.nextToken()
continue
}
break
}
if p.curTok.Type != TokenRParen {
return nil, fmt.Errorf("expected ) after column list, got %s", p.curTok.Literal)
}
p.nextToken()
}

return result, nil
}

Expand Down Expand Up @@ -579,6 +598,32 @@ func (p *Parser) parseExecuteSpecification() (*ast.ExecuteSpecification, error)

spec := &ast.ExecuteSpecification{}

// Check for EXECUTE ('string') form - ExecutableStringList
if p.curTok.Type == TokenLParen {
strList, err := p.parseExecutableStringList()
if err != nil {
return nil, err
}
spec.ExecutableEntity = strList

// Parse optional AS USER/LOGIN context
if p.curTok.Type == TokenAs {
ctx, err := p.parseExecuteContextForSpec()
if err != nil {
return nil, err
}
spec.ExecuteContext = ctx
}

// Parse optional AT LinkedServer
if p.curTok.Type == TokenIdent && strings.ToUpper(p.curTok.Literal) == "AT" {
p.nextToken()
spec.LinkedServer = p.parseIdentifier()
}

return spec, nil
}

// Check for return variable assignment @var =
if p.curTok.Type == TokenIdent && strings.HasPrefix(p.curTok.Literal, "@") {
varName := p.curTok.Literal
Expand Down Expand Up @@ -636,6 +681,115 @@ func (p *Parser) parseExecuteSpecification() (*ast.ExecuteSpecification, error)
return spec, nil
}

func (p *Parser) parseExecutableStringList() (*ast.ExecutableStringList, error) {
// We're positioned on (, consume it
p.nextToken()

strList := &ast.ExecutableStringList{}

// Parse the first string expression (may be concatenated with +)
for {
if p.curTok.Type == TokenString || p.curTok.Type == TokenNationalString {
expr, err := p.parseScalarExpression()
if err != nil {
return nil, err
}
// parseScalarExpression handles the + concatenation, so we get a BinaryExpression
// But we need to flatten it to individual StringLiterals for the Strings array
p.flattenStringExpression(expr, &strList.Strings)
} else {
break
}

// Check for comma (parameters follow) or closing paren
if p.curTok.Type == TokenComma {
p.nextToken()
break
}
if p.curTok.Type == TokenRParen {
break
}
}

// Parse parameters (after the first comma)
for p.curTok.Type != TokenRParen && p.curTok.Type != TokenEOF {
param, err := p.parseExecuteParameter()
if err != nil {
return nil, err
}
strList.Parameters = append(strList.Parameters, param)

if p.curTok.Type != TokenComma {
break
}
p.nextToken()
}

if p.curTok.Type != TokenRParen {
return nil, fmt.Errorf("expected ) after EXECUTE string list, got %s", p.curTok.Literal)
}
p.nextToken()

return strList, nil
}

func (p *Parser) flattenStringExpression(expr ast.ScalarExpression, strings *[]ast.ScalarExpression) {
switch e := expr.(type) {
case *ast.BinaryExpression:
// Recursively flatten for + concatenation
p.flattenStringExpression(e.FirstExpression, strings)
p.flattenStringExpression(e.SecondExpression, strings)
default:
*strings = append(*strings, expr)
}
}

func (p *Parser) parseExecuteContextForSpec() (*ast.ExecuteContext, error) {
// We're positioned on AS, consume it
p.nextToken()

ctx := &ast.ExecuteContext{}

upper := strings.ToUpper(p.curTok.Literal)
switch upper {
case "USER":
ctx.Kind = "User"
p.nextToken()
if p.curTok.Type == TokenEquals {
p.nextToken()
expr, err := p.parseScalarExpression()
if err != nil {
return nil, err
}
ctx.Principal = expr
}
case "LOGIN":
ctx.Kind = "Login"
p.nextToken()
if p.curTok.Type == TokenEquals {
p.nextToken()
expr, err := p.parseScalarExpression()
if err != nil {
return nil, err
}
ctx.Principal = expr
}
case "CALLER":
ctx.Kind = "Caller"
p.nextToken()
case "OWNER":
ctx.Kind = "Owner"
p.nextToken()
case "SELF":
ctx.Kind = "Self"
p.nextToken()
default:
return nil, fmt.Errorf("expected USER, LOGIN, CALLER, OWNER, or SELF after AS, got %s", p.curTok.Literal)
}

return ctx, nil
}

func (p *Parser) parseExecuteParameter() (*ast.ExecuteParameter, error) {
param := &ast.ExecuteParameter{IsOutput: false}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"todo": true}
{}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"todo": true}
{}
2 changes: 1 addition & 1 deletion parser/testdata/ExecuteStatementTests90/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"todo": true}
{}
2 changes: 1 addition & 1 deletion parser/testdata/RowsetsInSelectTests90/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"todo": true}
{}
Loading