Skip to content

Commit cff4987

Browse files
authored
allow _ and - in variable names (#43)
1 parent 32462b7 commit cff4987

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

pkg/sqlcmd/batch_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func TestReadStringVarmap(t *testing.T) {
175175
}
176176
tests := []mapTest{
177177
{`'var $(var1) var2 $(var2)'`, map[int]string{5: "var1", 18: "var2"}},
178+
{`'var $(va_1) var2 $(va-2)'`, map[int]string{5: "va_1", 18: "va-2"}},
178179
}
179180
for _, test := range tests {
180181
b := NewBatch(nil, newCommands())

pkg/sqlcmd/parse.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package sqlcmd
55

66
import (
7+
"strings"
78
"unicode"
89
)
910

@@ -63,7 +64,7 @@ func readVariableReference(r []rune, i int, end int) (int, bool) {
6364
if r[i] == ')' {
6465
return i, true
6566
}
66-
if (r[i] >= 'a' && r[i] <= 'z') || (r[i] >= 'A' && r[i] <= 'Z') || (r[i] >= '0' && r[i] <= '9') {
67+
if (r[i] >= 'a' && r[i] <= 'z') || (r[i] >= 'A' && r[i] <= 'Z') || (r[i] >= '0' && r[i] <= '9') || strings.ContainsRune(validVariableRunes, r[i]) {
6768
continue
6869
}
6970
break

pkg/sqlcmd/variables.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,14 @@ func (variables *Variables) Setvar(name, value string) error {
252252
return nil
253253
}
254254

255+
const validVariableRunes = "_-"
256+
255257
// ValidIdentifier determines if a given string can be used as a variable name
256-
// The native sqlcmd allowed some punctuation characters as part of a variable name
257-
// but this version will not.
258258
func ValidIdentifier(name string) error {
259259

260260
first := true
261261
for _, c := range name {
262-
if !unicode.IsLetter(c) && (first || !unicode.IsDigit(c)) {
262+
if !unicode.IsLetter(c) && (first || (!unicode.IsDigit(c) && !strings.ContainsRune(validVariableRunes, c))) {
263263
return fmt.Errorf("Invalid variable identifier %s", name)
264264
}
265265
first = false

pkg/sqlcmd/variables_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func TestValidIdentifier(t *testing.T) {
103103
{"1A", false},
104104
{"A1", true},
105105
{"A+", false},
106+
{"A-_b", true},
106107
}
107108
for _, tst := range tests {
108109
err := ValidIdentifier(tst.raw)

0 commit comments

Comments
 (0)