Skip to content

Commit 7cabf6e

Browse files
authored
Add support for WITH query (#108)
* allow caching for queries starting with `WITH` statement
1 parent 2e8c798 commit 7cabf6e

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

utils.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,24 @@ func getFullQueryFromBody(req *http.Request) ([]byte, error) {
140140
return b, nil
141141
}
142142

143+
var cachableStatements = []string{"SELECT", "WITH"}
144+
143145
// canCacheQuery returns true if q can be cached.
144146
func canCacheQuery(q []byte) bool {
145147
q = skipLeadingComments(q)
146148

147-
// Currently only SELECT queries may be cached.
148-
if len(q) < len("SELECT") {
149-
return false
149+
for _, statement := range cachableStatements {
150+
if len(q) < len(statement) {
151+
continue
152+
}
153+
154+
l := bytes.ToUpper(q[:len(statement)])
155+
if bytes.HasPrefix(l, []byte(statement)) {
156+
return true
157+
}
150158
}
151-
q = bytes.ToUpper(q[:len("SELECT")])
152-
return bytes.HasPrefix(q, []byte("SELECT"))
159+
160+
return false
153161
}
154162

155163
func skipLeadingComments(q []byte) []byte {

utils_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func TestCanCacheQuery(t *testing.T) {
5757
testCanCacheQuery(t, "\t\t sElECt 123 ", true)
5858
testCanCacheQuery(t, " --- sd s\n /* dfsf */\n seleCT ", true)
5959
testCanCacheQuery(t, " --- sd s\n /* dfsf */\n insert ", false)
60+
testCanCacheQuery(t, "WITH 1 as alias SELECT alias FROM nothing ", true)
6061
}
6162

6263
func testCanCacheQuery(t *testing.T, q string, expected bool) {

0 commit comments

Comments
 (0)