Example #1
0
func (sei *session) handleQuery(data []byte) error {
	sql := strings.TrimRight(string(data), ";")
	stmt, err := sqlparser.Parse(sql)
	if err != nil {
		glog.Infof("parse sql(%s) error:%v", sql, err)
		return sei.writeError(mysql.NewDefaultError(mysql.ER_SYNTAX_ERROR))
	}
	switch v := stmt.(type) {
	case *sqlparser.Explain:
		return sei.handleExplain(v)

	case *sqlparser.Select:
		return sei.handleSelect(v)

	case *sqlparser.Insert:
		return sei.handleInsert(v)

	case *sqlparser.Update:
		return sei.handleUpdate(v)

	case *sqlparser.Delete:
		return sei.handleDelete(v)

	case *sqlparser.Set:
		// only support like `SET autocommit=1`
		return sei.handleSet(v)

	case *sqlparser.DDL:
		return sei.handleDDL(sql)

	case *sqlparser.Show:
		r, err := sei.handleShow(v)
		if err != nil {
			glog.Infof("handle show stmt has error:%v", err)
			sei.writeError(err)
			// not throw the error
			return nil
		}
		return sei.writeResultset(sei.status, r)

	case *sqlparser.UseDB:
		return sei.handleUseDB(v)

	default:
		return fmt.Errorf("statement %T not support now", stmt)
	}
	return nil
}
Example #2
0
// serverCmd represents the server command
var astCmd = &cobra.Command{
	Use:   "ast",
	Short: "start rambo ast web front",
	Run: func(cmd *cobra.Command, args []string) {
		listen, _ := cmd.Flags().GetString("http")

		http.HandleFunc("/ast", func(w http.ResponseWriter, r *http.Request) {
			// get sql
			sql, err := ioutil.ReadAll(r.Body)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			log.Println("sql is:", string(sql))
			stmt, err := sqlparser.Parse(string(sql))
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			ntree := sqlparser.NewTree()
			ntree.SetTree(stmt)
			data, err := json.Marshal(ntree)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			w.Write(data)
		})
		gopath := os.Getenv("GOPATH")
		fs := http.FileServer(http.Dir(path.Join(gopath, "src", "github.com", "Alienero", "Rambo", "front")))