Skip to content

Commit a62432f

Browse files
authored
Fix issues with special characters in files (#463)
1 parent 53b6fe7 commit a62432f

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

pkg/sqlcmd/commands_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ func TestCommandParsing(t *testing.T) {
4343
{`:Setvar A1 "some value" `, "SETVAR", []string{`A1 "some value" `}},
4444
{` :Listvar`, "LISTVAR", []string{""}},
4545
{`:EXIT (select 100 as count)`, "EXIT", []string{" (select 100 as count)"}},
46+
{"\t:EXIT (select 100 as count)", "EXIT", []string{" (select 100 as count)"}},
4647
{`:EXIT ( )`, "EXIT", []string{" ( )"}},
4748
{`EXIT `, "EXIT", []string{" "}},
4849
{`:Connect someserver -U someuser`, "CONNECT", []string{"someserver -U someuser"}},
49-
{`:r c:\$(var)\file.sql`, "READFILE", []string{`c:\$(var)\file.sql`}},
50+
{":r\tc:\\$(var)\\file.sql", "READFILE", []string{`c:\$(var)\file.sql`}},
5051
{`:!! notepad`, "EXEC", []string{" notepad"}},
5152
{`:!!notepad`, "EXEC", []string{"notepad"}},
5253
{` !! dir c:\`, "EXEC", []string{` dir c:\`}},

pkg/sqlcmd/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func readMultilineComment(r []rune, i, end int) (int, bool) {
3434
func readCommand(c Commands, r []rune, i, end int) (*Command, []string, int) {
3535
for ; i < end; i++ {
3636
next := grab(r, i, end)
37-
if next == 0 || unicode.IsControl(next) {
37+
if next == 0 || (unicode.IsControl(next) && next != '\t') {
3838
break
3939
}
4040
}

pkg/sqlcmd/sqlcmd.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,24 +389,26 @@ func (s *Sqlcmd) getRunnableQuery(q string) string {
389389
}
390390
b := new(strings.Builder)
391391
b.Grow(len(q))
392+
// The varmap index is rune based not byte based
393+
r := []rune(q)
392394
keys := make([]int, 0, len(s.batch.varmap))
393395
for k := range s.batch.varmap {
394396
keys = append(keys, k)
395397
}
396398
sort.Ints(keys)
397399
last := 0
398400
for _, i := range keys {
399-
b.WriteString(q[last:i])
401+
b.WriteString(string(r[last:i]))
400402
v := s.batch.varmap[i]
401403
if val, ok := s.resolveVariable(v); ok {
402404
b.WriteString(val)
403405
} else {
404406
_, _ = fmt.Fprintf(s.GetError(), "'%s' scripting variable not defined.%s", v, SqlcmdEol)
405407
b.WriteString(fmt.Sprintf("$(%s)", v))
406408
}
407-
last = i + len(v) + 3
409+
last = i + len([]rune(v)) + 3
408410
}
409-
b.WriteString(q[last:])
411+
b.WriteString(string(r[last:]))
410412
return b.String()
411413
}
412414

pkg/sqlcmd/sqlcmd_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ func TestGetRunnableQuery(t *testing.T) {
246246
{"$ (var2)", "$ (var2)"},
247247
{"select '$(VAR1) $(VAR2)' as c", "select 'v1 variable2' as c"},
248248
{" $(VAR1) ' $(VAR2) ' as $(VAR1)", " v1 ' variable2 ' as v1"},
249+
{"í $(VAR1)", "í v1"},
249250
}
250251
s := New(nil, "", v)
251252
for _, test := range tests {

0 commit comments

Comments
 (0)