Skip to content

Commit 05186e9

Browse files
committed
Fixing the review comments
1 parent 6bb3fc0 commit 05186e9

File tree

8 files changed

+251
-26
lines changed

8 files changed

+251
-26
lines changed

connection.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ func (v *connection) defaultMessageHandler(bMsg msgs.BackEndMsg) (bool, error) {
569569
connectionLogger.Info("NOTICE: %s", msg.Message)
570570
case *msgs.BEParamStatusMsg:
571571
connectionLogger.Debug("%v", msg)
572+
case *msgs.BEParseCompleteMsg:
573+
connectionLogger.Trace("parse complete")
572574
default:
573575
handled = false
574576
err = fmt.Errorf("unhandled message: %v", msg)

go.mod

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@ module github.com/vertica/vertica-sql-go
22

33
go 1.13
44

5-
require (
6-
github.com/elastic/go-sysinfo v1.8.1
7-
github.com/pquerna/otp v1.5.0
8-
github.com/stretchr/testify v1.3.0
9-
)
5+
require github.com/elastic/go-sysinfo v1.8.1

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI=
2-
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
31
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
42
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
53
github.com/elastic/go-sysinfo v1.8.1 h1:4Yhj+HdV6WjbCRgGdZpPJ8lZQlXZLKDAeIkmQ/VRvi4=
@@ -16,8 +14,6 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
1614
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1715
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1816
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
19-
github.com/pquerna/otp v1.5.0 h1:NMMR+WrmaqXU4EzdGJEE1aUUI0AMRzsp96fFFWNPwxs=
20-
github.com/pquerna/otp v1.5.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg=
2117
github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0 h1:c8R11WC8m7KNMkTv/0+Be8vvwo4I3/Ut9AC2FW8fX3U=
2218
github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
2319
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

parse/statements.go

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"unicode"
66
)
77

8-
// Copyright (c) 2020-2024 Open Text.
8+
// Copyright (c) 2020-2026 Open Text.
99

1010
// Licensed under the Apache License, Version 2.0 (the "License");
1111
// you may not use this file except in compliance with the License.
@@ -36,23 +36,30 @@ func SplitStatements(query string) []string {
3636
inLineComment := false
3737
inBlockComment := false
3838
var dollarTag string
39+
statementHasContent := false
3940

41+
markNonWhitespace := func(b byte) {
42+
if !unicode.IsSpace(rune(b)) {
43+
statementHasContent = true
44+
}
45+
}
4046
flush := func() {
4147
statement := strings.TrimSpace(current.String())
4248
current.Reset()
43-
if statement != "" {
49+
if statement != "" && statementHasContent {
4450
statements = append(statements, statement)
4551
}
52+
statementHasContent = false
4653
}
4754

4855
i := 0
4956
for i < len(query) {
5057
ch := query[i]
5158

5259
if inLineComment {
53-
// Consume everything until the newline terminator.
54-
current.WriteByte(ch)
60+
// Swallow comment text but keep the newline terminator so tokens remain separated.
5561
if ch == '\n' || ch == '\r' {
62+
current.WriteByte(ch)
5663
inLineComment = false
5764
}
5865
i++
@@ -76,9 +83,11 @@ func SplitStatements(query string) []string {
7683
if inSingleQuote {
7784
// Stay inside the literal, handling doubled single quotes.
7885
current.WriteByte(ch)
86+
markNonWhitespace(ch)
7987
if ch == '\'' {
8088
if i+1 < len(query) && query[i+1] == '\'' {
8189
current.WriteByte('\'')
90+
markNonWhitespace('\'')
8291
i += 2
8392
continue
8493
}
@@ -92,9 +101,11 @@ func SplitStatements(query string) []string {
92101
// Identifiers can be quoted with double quotes; treat them like
93102
// strings for splitter purposes.
94103
current.WriteByte(ch)
104+
markNonWhitespace(ch)
95105
if ch == '"' {
96106
if i+1 < len(query) && query[i+1] == '"' {
97107
current.WriteByte('"')
108+
markNonWhitespace('"')
98109
i += 2
99110
continue
100111
}
@@ -105,10 +116,12 @@ func SplitStatements(query string) []string {
105116
}
106117

107118
if dollarTag != "" {
119+
statementHasContent = true
108120
// Inside a dollar-quoted literal; exit only when the exact tag is
109121
// observed again.
110122
if i+len(dollarTag) <= len(query) && query[i:i+len(dollarTag)] == dollarTag {
111123
current.WriteString(dollarTag)
124+
markNonWhitespace(dollarTag[0])
112125
i += len(dollarTag)
113126
dollarTag = ""
114127
continue
@@ -121,37 +134,46 @@ func SplitStatements(query string) []string {
121134
if ch == '\'' {
122135
inSingleQuote = true
123136
current.WriteByte(ch)
137+
markNonWhitespace(ch)
124138
i++
125139
continue
126140
}
127141

128142
if ch == '"' {
129143
inDoubleQuote = true
130144
current.WriteByte(ch)
145+
markNonWhitespace(ch)
131146
i++
132147
continue
133148
}
134149

135150
if ch == '-' && i+1 < len(query) && query[i+1] == '-' {
136-
current.WriteByte('-')
137-
current.WriteByte('-')
138151
i += 2
139152
inLineComment = true
140153
continue
141154
}
142155

143-
if ch == '/' && i+1 < len(query) && query[i+1] == '*' {
144-
current.WriteByte('/')
145-
current.WriteByte('*')
146-
i += 2
147-
inBlockComment = true
148-
continue
156+
if ch == '/' && i+1 < len(query) {
157+
next := query[i+1]
158+
if next == '*' {
159+
current.WriteByte('/')
160+
current.WriteByte('*')
161+
i += 2
162+
inBlockComment = true
163+
continue
164+
}
165+
if next == '/' {
166+
i += 2
167+
inLineComment = true
168+
continue
169+
}
149170
}
150171

151172
if ch == '$' {
152173
if tag, length, ok := readDollarTag(query, i); ok {
153174
dollarTag = tag
154175
current.WriteString(tag)
176+
markNonWhitespace(tag[0])
155177
i += length
156178
continue
157179
}
@@ -164,6 +186,7 @@ func SplitStatements(query string) []string {
164186
}
165187

166188
current.WriteByte(ch)
189+
markNonWhitespace(ch)
167190
i++
168191
}
169192

parse/statements_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ SELECT 1; /* block;comment */ SELECT 2;`,
4242
query: " SELECT 1;\n\n ; SELECT 2 ;",
4343
expected: []string{"SELECT 1", "SELECT 2"},
4444
},
45+
{
46+
name: "line comment only",
47+
query: "-- just a comment",
48+
expected: nil,
49+
},
50+
{
51+
name: "double slash line comment",
52+
query: "SELECT 1; // comment about next\nSELECT 2;",
53+
expected: []string{"SELECT 1", "SELECT 2"},
54+
},
4555
}
4656

4757
for _, tc := range testCases {

rows_multi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package vertigo
22

3-
// Copyright (c) 2019-2024 Open Text.
3+
// Copyright (c) 2019-2026 Open Text.
44
//
55
// Licensed under the Apache License, Version 2.0 (the "License");
66
// you may not use this file except in compliance with the License.

stmt.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type stmt struct {
7474
paramTypes []common.ParameterType
7575
lastRowDesc *msgs.BERowDescMsg
7676
// set if Vertica issues an error of ROLLBACK severity
77-
rolledBack bool
77+
rolledBack bool
7878
multiStatements bool
7979
}
8080

@@ -87,7 +87,12 @@ func newStmt(connection *connection, command string) (*stmt, error) {
8787
parseState: parseStateUnparsed,
8888
}
8989

90-
s.multiStatements = len(parse.SplitStatements(command)) > 1
90+
initialStatements := parse.SplitStatements(command)
91+
if len(initialStatements) == 0 {
92+
s.command = ""
93+
return s, nil
94+
}
95+
s.multiStatements = len(initialStatements) > 1
9196

9297
if len(command) == 0 || emailRegexPattern.MatchString(command) {
9398
return s, nil
@@ -98,7 +103,13 @@ func newStmt(connection *connection, command string) (*stmt, error) {
98103
return "?"
99104
}
100105
s.command = parse.Lex(command, parse.WithNamedCallback(s.pushNamed), parse.WithPositionalSubstitution(argCounter))
101-
s.multiStatements = len(parse.SplitStatements(s.command)) > 1
106+
finalStatements := parse.SplitStatements(s.command)
107+
if len(finalStatements) == 0 {
108+
s.command = ""
109+
s.multiStatements = false
110+
} else {
111+
s.multiStatements = len(finalStatements) > 1
112+
}
102113
return s, nil
103114
}
104115

@@ -364,7 +375,7 @@ func (s *stmt) runSimpleStatement(ctx context.Context, sql string) (*rows, error
364375
result = newRows(ctx, msg, s.conn.serverTZOffset)
365376
case *msgs.BECmdDescriptionMsg:
366377
continue
367-
case *msgs.BECmdCompleteMsg:
378+
case *msgs.BECmdCompleteMsg, *msgs.BEParseCompleteMsg:
368379
continue
369380
case *msgs.BEErrorMsg:
370381
return newEmptyRows(), s.evaluateErrorMsg(msg)

0 commit comments

Comments
 (0)