Skip to content

Commit

Permalink
Fix: Apply x flag during batch parsing (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
shueybubbles authored Jul 16, 2024
1 parent 55e7675 commit c7ab91d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ jobs:
name: lint-pr-changes
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
- uses: actions/setup-go@v5
with:
go-version: 1.18
- uses: actions/checkout@v3
go-version: stable
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: latest
only-new-issues: true
13 changes: 9 additions & 4 deletions pkg/sqlcmd/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,20 @@ type Batch struct {
linevarmap map[int]string
// cmd is the set of Commands available
cmd Commands
// ParseVariables is a function that returns true if Next should parse variables
ParseVariables batchParseVariables
}

type batchScan func() (string, error)

type batchParseVariables func() bool

// NewBatch creates a Batch which converts runes provided by reader into SQL batches
func NewBatch(reader batchScan, cmd Commands) *Batch {
b := &Batch{
read: reader,
cmd: cmd,
read: reader,
cmd: cmd,
ParseVariables: func() bool { return true },
}
b.Reset(nil)
return b
Expand Down Expand Up @@ -125,7 +130,7 @@ parse:
case b.quote != 0 || b.comment:

// Handle variable references
case c == '$' && next == '(':
case (b.ParseVariables == nil || b.ParseVariables()) && c == '$' && next == '(':
vi, ok := readVariableReference(b.raw, i+2, b.rawlen)
if ok {
b.addVariableLocation(i, string(b.raw[i+2:vi]))
Expand Down Expand Up @@ -231,7 +236,7 @@ func (b *Batch) readString(r []rune, i, end int, quote rune, line uint) (int, bo
for ; i < end; i++ {
c, next = r[i], grab(r, i+1, end)
switch {
case c == '$' && next == '(':
case (b.ParseVariables == nil || b.ParseVariables()) && c == '$' && next == '(':
vl, ok := readVariableReference(r, i+2, end)
if ok {
b.addVariableLocation(i, string(r[i+2:vl]))
Expand Down
1 change: 1 addition & 0 deletions pkg/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func New(l Console, workingDirectory string, vars *Variables) *Sqlcmd {
colorizer: color.New(false),
}
s.batch = NewBatch(s.scanNext, s.Cmd)
s.batch.ParseVariables = func() bool { return !s.Connect.DisableVariableSubstitution }
mssql.SetContextLogger(s)
s.PrintError = func(msg string, severity uint8) bool {
return false
Expand Down
29 changes: 19 additions & 10 deletions pkg/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,22 +242,31 @@ func TestGetRunnableQuery(t *testing.T) {
q string
}
tests := []test{
{"$(var1)", "v1"},
{"$ (var2)", "$ (var2)"},
{"select '$(VAR1) $(VAR2)' as c", "select 'v1 variable2' as c"},
{" $(VAR1) ' $(VAR2) ' as $(VAR1)", " v1 ' variable2 ' as v1"},
{"í $(VAR1)", "í v1"},
// {"$(var1)", "v1"},
// {"$ (var2)", "$ (var2)"},
// {"select '$(VAR1) $(VAR2)' as c", "select 'v1 variable2' as c"},
// {" $(VAR1) ' $(VAR2) ' as $(VAR1)", " v1 ' variable2 ' as v1"},
// {"í $(VAR1)", "í v1"},
{"select '$('", ""},
}
s := New(nil, "", v)
for _, test := range tests {
s.batch.Reset([]rune(test.raw))
_, _, _ = s.batch.Next()
s.Connect.DisableVariableSubstitution = false
t.Log(test.raw)
r := s.getRunnableQuery(test.raw)
assert.Equalf(t, test.q, r, `runnableQuery for "%s"`, test.raw)
_, _, err := s.batch.Next()
if test.q == "" {
assert.Error(t, err, "expected variable parsing error")
} else {
assert.NoError(t, err, "Next should have succeeded")
t.Log(test.raw)
r := s.getRunnableQuery(test.raw)
assert.Equalf(t, test.q, r, `runnableQuery for "%s"`, test.raw)
}
s.batch.Reset([]rune(test.raw))
s.Connect.DisableVariableSubstitution = true
r = s.getRunnableQuery(test.raw)
_, _, err = s.batch.Next()
assert.NoError(t, err, "expected no variable parsing error")
r := s.getRunnableQuery(test.raw)
assert.Equalf(t, test.raw, r, `runnableQuery without variable subs for "%s"`, test.raw)
}
}
Expand Down

0 comments on commit c7ab91d

Please sign in to comment.