Skip to content

Commit

Permalink
Fix issues with special characters in files (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
shueybubbles committed Oct 10, 2023
1 parent 53b6fe7 commit a62432f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/sqlcmd/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ func TestCommandParsing(t *testing.T) {
{`:Setvar A1 "some value" `, "SETVAR", []string{`A1 "some value" `}},
{` :Listvar`, "LISTVAR", []string{""}},
{`:EXIT (select 100 as count)`, "EXIT", []string{" (select 100 as count)"}},
{"\t:EXIT (select 100 as count)", "EXIT", []string{" (select 100 as count)"}},
{`:EXIT ( )`, "EXIT", []string{" ( )"}},
{`EXIT `, "EXIT", []string{" "}},
{`:Connect someserver -U someuser`, "CONNECT", []string{"someserver -U someuser"}},
{`:r c:\$(var)\file.sql`, "READFILE", []string{`c:\$(var)\file.sql`}},
{":r\tc:\\$(var)\\file.sql", "READFILE", []string{`c:\$(var)\file.sql`}},
{`:!! notepad`, "EXEC", []string{" notepad"}},
{`:!!notepad`, "EXEC", []string{"notepad"}},
{` !! dir c:\`, "EXEC", []string{` dir c:\`}},
Expand Down
2 changes: 1 addition & 1 deletion pkg/sqlcmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func readMultilineComment(r []rune, i, end int) (int, bool) {
func readCommand(c Commands, r []rune, i, end int) (*Command, []string, int) {
for ; i < end; i++ {
next := grab(r, i, end)
if next == 0 || unicode.IsControl(next) {
if next == 0 || (unicode.IsControl(next) && next != '\t') {
break
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,24 +389,26 @@ func (s *Sqlcmd) getRunnableQuery(q string) string {
}
b := new(strings.Builder)
b.Grow(len(q))
// The varmap index is rune based not byte based
r := []rune(q)
keys := make([]int, 0, len(s.batch.varmap))
for k := range s.batch.varmap {
keys = append(keys, k)
}
sort.Ints(keys)
last := 0
for _, i := range keys {
b.WriteString(q[last:i])
b.WriteString(string(r[last:i]))
v := s.batch.varmap[i]
if val, ok := s.resolveVariable(v); ok {
b.WriteString(val)
} else {
_, _ = fmt.Fprintf(s.GetError(), "'%s' scripting variable not defined.%s", v, SqlcmdEol)
b.WriteString(fmt.Sprintf("$(%s)", v))
}
last = i + len(v) + 3
last = i + len([]rune(v)) + 3
}
b.WriteString(q[last:])
b.WriteString(string(r[last:]))
return b.String()
}

Expand Down
1 change: 1 addition & 0 deletions pkg/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func TestGetRunnableQuery(t *testing.T) {
{"$ (var2)", "$ (var2)"},
{"select '$(VAR1) $(VAR2)' as c", "select 'v1 variable2' as c"},
{" $(VAR1) ' $(VAR2) ' as $(VAR1)", " v1 ' variable2 ' as v1"},
{"í $(VAR1)", "í v1"},
}
s := New(nil, "", v)
for _, test := range tests {
Expand Down

0 comments on commit a62432f

Please sign in to comment.