Skip to content

Commit b6dc811

Browse files
authored
Merge pull request #186 from tetafro/master
Fix parsing number of argumnets for queries with BETWEEN
2 parents 3297e8f + ed43992 commit b6dc811

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

helpers.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ func numInput(query string) int {
1717
args = make(map[string]struct{})
1818
reader = bytes.NewReader([]byte(query))
1919
quote, keyword bool
20-
like = newMatcher("like")
20+
inBetween bool
21+
like = newMatcher("like")
2122
limit = newMatcher("limit")
23+
between = newMatcher("between")
24+
and = newMatcher("and")
2225
)
2326
for {
2427
if char, _, err := reader.ReadRune(); err == nil {
@@ -50,6 +53,12 @@ func numInput(query string) int {
5053
default:
5154
if limit.matchRune(char) || like.matchRune(char) {
5255
keyword = true
56+
} else if between.matchRune(char) {
57+
keyword = true
58+
inBetween = true
59+
} else if inBetween && and.matchRune(char) {
60+
keyword = true
61+
inBetween = false
5362
} else {
5463
keyword = keyword && (char == ' ' || char == '\t' || char == '\n')
5564
}

helpers_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ func Test_NumInput(t *testing.T) {
2727
?
2828
)
2929
`: 3,
30-
"SELECT * from EXAMPLE LIMIT ?": 1,
31-
"SELECT * from EXAMPLE LIMIT ?, ?": 2,
32-
"SELECT * from EXAMPLE WHERE os_id like ?": 1,
30+
"SELECT * from EXAMPLE LIMIT ?": 1,
31+
"SELECT * from EXAMPLE LIMIT ?, ?": 2,
32+
"SELECT * from EXAMPLE WHERE os_id like ?": 1,
33+
"SELECT * FROM example WHERE a BETWEEN ? AND ?": 2,
34+
"SELECT * FROM example WHERE a BETWEEN ? AND ? AND b = ?": 3,
35+
"SELECT * FROM example WHERE a = ? AND b BETWEEN ? AND ?": 3,
36+
"SELECT * FROM example WHERE a BETWEEN ? AND ? AND b BETWEEN ? AND ?": 4,
3337
} {
3438
assert.Equal(t, num, numInput(query), query)
3539
}

0 commit comments

Comments
 (0)