validateQuery reports whether the given statement is valid for the SQL editor, which only permits read-only queries. It returns (canRunInReadOnly, returnsData, error): - canRunInReadOnly: every statement can run in read-only mode; - returnsData: every statement returns data; - error: a syntax error
(statement string)
| 21 | // executes the inner query, it is only accepted when getQueryType reports it as |
| 22 | // read-only (base.Select); it then counts as read-only and data-returning. |
| 23 | func validateQuery(statement string) (bool, bool, error) { |
| 24 | parsed, err := parseTrinoSQL(statement) |
| 25 | if err != nil { |
| 26 | return false, false, err |
| 27 | } |
| 28 | |
| 29 | allReadOnly := true |
| 30 | allReturnData := true |
| 31 | |
| 32 | for _, p := range parsed { |
| 33 | queryType, isAnalyze := getQueryType(p.Node()) |
| 34 | |
| 35 | if isAnalyze { |
| 36 | // EXPLAIN ANALYZE runs the query. Only allow it when the type is a |
| 37 | // read-only SELECT; in that case it is read-only and returns data. |
| 38 | if queryType != base.Select { |
| 39 | return false, false, nil |
| 40 | } |
| 41 | continue |
| 42 | } |
| 43 | |
| 44 | readOnly := queryType == base.Select || |
| 45 | queryType == base.Explain || |
| 46 | queryType == base.SelectInfoSchema |
| 47 | returnsData := readOnly |
| 48 | |
| 49 | if !readOnly { |
| 50 | allReadOnly = false |
| 51 | } |
| 52 | if !returnsData { |
| 53 | allReturnData = false |
| 54 | } |
| 55 | if !allReadOnly { |
| 56 | break |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return allReadOnly, allReturnData, nil |
| 61 | } |