TODO(weiguo): isQuery is a hacky way to decide which API to call: https://golang.org/pkg/database/sql/#DB.Exec . We will need to extend our parser to be a full SQL parser in the future.
(slct string)
| 33 | // https://golang.org/pkg/database/sql/#DB.Exec . |
| 34 | // We will need to extend our parser to be a full SQL parser in the future. |
| 35 | func isQuery(slct string) bool { |
| 36 | s := strings.ToUpper(strings.TrimSpace(slct)) |
| 37 | has := strings.Contains |
| 38 | // remove comment lines |
| 39 | if strings.HasPrefix(s, "-- ") { |
| 40 | lines := strings.Split(s, "\n") |
| 41 | noCommentLines := []string{} |
| 42 | for _, l := range lines { |
| 43 | if !strings.HasPrefix(l, "-- ") { |
| 44 | noCommentLines = append(noCommentLines, l) |
| 45 | } |
| 46 | } |
| 47 | s = strings.Join(noCommentLines, "\n") |
| 48 | } |
| 49 | if strings.HasPrefix(s, "SELECT") && !has(s, "INTO") { |
| 50 | return true |
| 51 | } |
| 52 | if strings.HasPrefix(s, "SHOW") && (has(s, "CREATE") || has(s, "DATABASES") || has(s, "TABLES")) { |
| 53 | return true |
| 54 | } |
| 55 | if strings.HasPrefix(s, "DESC") || strings.HasPrefix(s, "EXPLAIN") { |
| 56 | return true |
| 57 | } |
| 58 | return false |
| 59 | } |
| 60 | |
| 61 | // query runs slct and writes the retrieved rows into pipe wr. |
| 62 | func runQuery(wr *pipe.Writer, slct string, db *database.DB) error { |
no outgoing calls