validateQuery validates the SQL statement for SQL editor. We validate the statement by following steps: 1. Remove all quoted text(quoted identifier, string literal) and comments from the statement. 2. Use regexp to check if the statement is a normal SELECT statement and EXPLAIN statement. 3. For CTE
(statement string)
| 17 | // 2. Use regexp to check if the statement is a normal SELECT statement and EXPLAIN statement. |
| 18 | // 3. For CTE, use regexp to check if the statement has UPDATE, DELETE and INSERT statements. |
| 19 | func validateQuery(statement string) (bool, bool, error) { |
| 20 | stmtList, err := ParseTiDB(statement, "", "") |
| 21 | if err != nil { |
| 22 | return false, false, err |
| 23 | } |
| 24 | hasExecute := false |
| 25 | readOnly := true |
| 26 | for _, stmt := range stmtList { |
| 27 | switch stmt := stmt.(type) { |
| 28 | case *ast.SelectStmt: |
| 29 | case *ast.SetOprStmt: |
| 30 | case *ast.SetStmt: |
| 31 | hasExecute = true |
| 32 | case *ast.ShowStmt: |
| 33 | case *ast.ExplainStmt: |
| 34 | if stmt.Analyze { |
| 35 | readOnly = false |
| 36 | } |
| 37 | default: |
| 38 | return false, false, nil |
| 39 | } |
| 40 | } |
| 41 | return readOnly, !hasExecute, nil |
| 42 | } |
| 43 | |
| 44 | // ExtractMySQLTableList extracts all the TableNames from node. |
| 45 | // If asName is true, extract AsName prior to OrigName. |