Skip to content

Commit ad9899f

Browse files
author
Michael
authored
Handle named params with in clause (#71)
* Handle named params with in clause Fixes #70 * Try using args flag
1 parent 7b6204c commit ad9899f

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ before_install:
1919
- sleep 60
2020

2121
script:
22-
- go test --locator localhost:5433 --user dbadmin -race .
23-
- go test --locator localhost:5433 --user dbadmin --use_prepared_statements=0 -race .
22+
- go test -race . -args --locator localhost:5433 --user dbadmin
23+
- go test -race . -args --locator localhost:5433 --user dbadmin --use_prepared_statements=0
2424
- go test -race ./logger
2525
- docker exec -u dbadmin vertica_docker /opt/vertica/bin/vsql -c "SELECT SET_CONFIG_PARAMETER('SSLPrivateKey', '`cat ./resources/tests/ssl/root.key`');"
2626
- docker exec -u dbadmin vertica_docker /opt/vertica/bin/vsql -c "SELECT SET_CONFIG_PARAMETER('SSLCertificate', '`cat ./resources/tests/ssl/root.crt`');"
2727
# docker exec -u dbadmin vertica_docker /opt/vertica/bin/vsql -c "ALTER DATABASE docker SET SSLCA='`cat ./resources/tests/ssl/root.crt`';"
2828
- docker exec -u dbadmin vertica_docker /opt/vertica/bin/vsql -c "ALTER DATABASE docker SET EnableSSL=1;"
2929
- docker exec -u dbadmin vertica_docker /opt/vertica/bin/admintools --tool stop_db --database docker
3030
- docker exec -u dbadmin vertica_docker /opt/vertica/bin/admintools --tool start_db --database docker
31-
- go test --locator localhost:5433 --user dbadmin --tlsmode=server -race .
32-
- go test --locator localhost:5433 --user dbadmin --tlsmode=server --use_prepared_statements=0 -race .
31+
- go test -race . -args --locator localhost:5433 --user dbadmin --tlsmode=server
32+
- go test -race . -args --locator localhost:5433 --user dbadmin --tlsmode=server --use_prepared_statements=0
3333
# go test --locator localhost:5433 --user dbadmin --tlsmode=server-strict -race .
3434
# go test --locator localhost:5433 --user dbadmin --tlsmode=server-strict --use_prepared_statements=0 -race .

parse/queryLex.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,19 @@ func (l *Lexer) skipUntil(val rune) {
114114
}
115115
}
116116

117-
func (l *Lexer) consumeToSpace() {
118-
for !l.done() && !unicode.IsSpace(l.next()) {
117+
func (l *Lexer) consumeIdent() {
118+
for !l.done() && !l.isEndIdent(l.next()) {
119119
}
120120
}
121121

122+
func (l *Lexer) isEndIdent(r rune) bool {
123+
shouldEnd := unicode.IsSpace(r) || strings.ContainsRune(",)", r)
124+
if shouldEnd {
125+
l.backup()
126+
}
127+
return shouldEnd
128+
}
129+
122130
func (l *Lexer) next() rune {
123131
if l.done() {
124132
l.width = 0
@@ -201,10 +209,7 @@ func lexNamedParam(l *Lexer) stateFunc {
201209
l.next()
202210
l.start = l.pos
203211
// advance through the name
204-
l.consumeToSpace()
205-
if !l.done() || strings.HasSuffix(l.input[l.start:l.pos], "\n") {
206-
l.backup() // move back before the whitespace character
207-
}
212+
l.consumeIdent()
208213
l.onNamed(strings.ToUpper(l.input[l.start:l.pos]))
209214
l.start = l.pos
210215
l.output.WriteRune('?')

parse/queryLex_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ func TestLexNamed(t *testing.T) {
8383
expectedNamed: []string{"FIRST", "SECOND"},
8484
expectedOutput: "select * from whatever where a = ? and b = ? and c = '@fooledYou'",
8585
},
86+
{
87+
name: "named params with in clause",
88+
query: "select * from whatever where a in (@first, @second)",
89+
expectedNamed: []string{"FIRST", "SECOND"},
90+
expectedOutput: "select * from whatever where a in (?, ?)",
91+
},
92+
{
93+
name: "single named param with in clause",
94+
query: "select * from whatever where a in (@first)",
95+
expectedNamed: []string{"FIRST"},
96+
expectedOutput: "select * from whatever where a in (?)",
97+
},
8698
{
8799
name: "with mixed case named parameters",
88100
query: "select * from whatever where a = @first and b = @fIrSt",

0 commit comments

Comments
 (0)