Skip to content

Commit

Permalink
Handle named params with in clause (#71)
Browse files Browse the repository at this point in the history
* Handle named params with in clause

Fixes #70

* Try using args flag
  • Loading branch information
Michael committed Feb 28, 2020
1 parent 7b6204c commit ad9899f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ before_install:
- sleep 60

script:
- go test --locator localhost:5433 --user dbadmin -race .
- go test --locator localhost:5433 --user dbadmin --use_prepared_statements=0 -race .
- go test -race . -args --locator localhost:5433 --user dbadmin
- go test -race . -args --locator localhost:5433 --user dbadmin --use_prepared_statements=0
- go test -race ./logger
- docker exec -u dbadmin vertica_docker /opt/vertica/bin/vsql -c "SELECT SET_CONFIG_PARAMETER('SSLPrivateKey', '`cat ./resources/tests/ssl/root.key`');"
- docker exec -u dbadmin vertica_docker /opt/vertica/bin/vsql -c "SELECT SET_CONFIG_PARAMETER('SSLCertificate', '`cat ./resources/tests/ssl/root.crt`');"
# docker exec -u dbadmin vertica_docker /opt/vertica/bin/vsql -c "ALTER DATABASE docker SET SSLCA='`cat ./resources/tests/ssl/root.crt`';"
- docker exec -u dbadmin vertica_docker /opt/vertica/bin/vsql -c "ALTER DATABASE docker SET EnableSSL=1;"
- docker exec -u dbadmin vertica_docker /opt/vertica/bin/admintools --tool stop_db --database docker
- docker exec -u dbadmin vertica_docker /opt/vertica/bin/admintools --tool start_db --database docker
- go test --locator localhost:5433 --user dbadmin --tlsmode=server -race .
- go test --locator localhost:5433 --user dbadmin --tlsmode=server --use_prepared_statements=0 -race .
- go test -race . -args --locator localhost:5433 --user dbadmin --tlsmode=server
- go test -race . -args --locator localhost:5433 --user dbadmin --tlsmode=server --use_prepared_statements=0
# go test --locator localhost:5433 --user dbadmin --tlsmode=server-strict -race .
# go test --locator localhost:5433 --user dbadmin --tlsmode=server-strict --use_prepared_statements=0 -race .
17 changes: 11 additions & 6 deletions parse/queryLex.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,19 @@ func (l *Lexer) skipUntil(val rune) {
}
}

func (l *Lexer) consumeToSpace() {
for !l.done() && !unicode.IsSpace(l.next()) {
func (l *Lexer) consumeIdent() {
for !l.done() && !l.isEndIdent(l.next()) {
}
}

func (l *Lexer) isEndIdent(r rune) bool {
shouldEnd := unicode.IsSpace(r) || strings.ContainsRune(",)", r)
if shouldEnd {
l.backup()
}
return shouldEnd
}

func (l *Lexer) next() rune {
if l.done() {
l.width = 0
Expand Down Expand Up @@ -201,10 +209,7 @@ func lexNamedParam(l *Lexer) stateFunc {
l.next()
l.start = l.pos
// advance through the name
l.consumeToSpace()
if !l.done() || strings.HasSuffix(l.input[l.start:l.pos], "\n") {
l.backup() // move back before the whitespace character
}
l.consumeIdent()
l.onNamed(strings.ToUpper(l.input[l.start:l.pos]))
l.start = l.pos
l.output.WriteRune('?')
Expand Down
12 changes: 12 additions & 0 deletions parse/queryLex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ func TestLexNamed(t *testing.T) {
expectedNamed: []string{"FIRST", "SECOND"},
expectedOutput: "select * from whatever where a = ? and b = ? and c = '@fooledYou'",
},
{
name: "named params with in clause",
query: "select * from whatever where a in (@first, @second)",
expectedNamed: []string{"FIRST", "SECOND"},
expectedOutput: "select * from whatever where a in (?, ?)",
},
{
name: "single named param with in clause",
query: "select * from whatever where a in (@first)",
expectedNamed: []string{"FIRST"},
expectedOutput: "select * from whatever where a in (?)",
},
{
name: "with mixed case named parameters",
query: "select * from whatever where a = @first and b = @fIrSt",
Expand Down

0 comments on commit ad9899f

Please sign in to comment.