isReadOnlySQL reports whether the (single) statement looks read-only. Heuristic leading-keyword check, mirroring the conservative stance in server/conn.go's DML detection: err toward classifying as a write.
(sql string)
| 119 | // leading-keyword check, mirroring the conservative stance in server/conn.go's |
| 120 | // DML detection: err toward classifying as a write. |
| 121 | func isReadOnlySQL(sql string) bool { |
| 122 | s := strings.TrimSpace(strings.ToLower(sql)) |
| 123 | // Strip a leading line comment / block comment cheaply. |
| 124 | s = strings.TrimSpace(s) |
| 125 | for _, kw := range readOnlyLeadingKeywords { |
| 126 | if s == kw || strings.HasPrefix(s, kw+" ") || strings.HasPrefix(s, kw+"\n") || strings.HasPrefix(s, kw+"\t") || strings.HasPrefix(s, kw+"(") { |
| 127 | // Reject multi-statement batches (a read-only prefix followed by a |
| 128 | // second statement that could mutate). |
| 129 | if strings.Contains(strings.TrimRight(s, "; \n\t"), ";") { |
| 130 | return false |
| 131 | } |
| 132 | // EXPLAIN ANALYZE executes the inner statement to profile it, so |
| 133 | // EXPLAIN ANALYZE <DML/DDL> is a WRITE despite the read-only-looking |
| 134 | // EXPLAIN prefix. Treat it as a write so allow_write is required. |
| 135 | if kw == "explain" && explainExecutes(s) { |
| 136 | return false |
| 137 | } |
| 138 | return true |
| 139 | } |
| 140 | } |
| 141 | return false |
| 142 | } |
| 143 | |
| 144 | // explainExecutes reports whether an EXPLAIN statement actually runs its inner |
| 145 | // query (EXPLAIN ANALYZE, or EXPLAIN (ANALYZE[, ...])) — and can therefore |