Ejemplo n.º 1
0
func (m *Manager) handleMySQLQuery(cmd *proto.Cmd, si *proto.ServiceInstance) *proto.Reply {
	m.logger.Debug("handleMySQLQuery:call")
	defer m.logger.Debug("handleMySQLQuery:return")

	// Connect to MySQL.
	mysqlIt := &proto.MySQLInstance{}
	if err := m.instanceRepo.Get(si.Service, si.InstanceId, mysqlIt); err != nil {
		return cmd.Reply(nil, err)
	}
	conn := m.connFactory.Make(mysqlIt.DSN)
	if err := conn.Connect(1); err != nil {
		return cmd.Reply(nil, fmt.Errorf("Cannot connect to MySQL: %s", err))
	}
	defer conn.Close()

	// Create a MySQL query executor to do the actual work.
	e := mysqlExec.NewQueryExecutor(conn)

	// Get the instance name, e.g. mysql-db01, to make status human-readable.
	instanceName := m.instanceRepo.Name(si.Service, si.InstanceId)

	// Execute the query.
	m.logger.Debug(cmd.Cmd + ":" + instanceName)
	switch cmd.Cmd {
	case "Explain":
		m.status.Update(SERVICE_NAME, "EXPLAIN query on "+instanceName)
		q := &proto.ExplainQuery{}
		if err := json.Unmarshal(cmd.Data, q); err != nil {
			return cmd.Reply(nil, err)
		}
		res, err := e.Explain(q.Db, q.Query)
		if err != nil {
			return cmd.Reply(nil, fmt.Errorf("EXPLAIN failed: %s", err))
		}
		return cmd.Reply(res, nil)
	case "TableInfo":
		m.status.Update(SERVICE_NAME, "Table Info queries on "+instanceName)
		tableInfo := &proto.TableInfoQuery{}
		if err := json.Unmarshal(cmd.Data, tableInfo); err != nil {
			return cmd.Reply(nil, err)
		}
		res, err := e.TableInfo(tableInfo)
		if err != nil {
			return cmd.Reply(nil, fmt.Errorf("Table Info failed: %s", err))
		}
		return cmd.Reply(res, nil)
	default:
		return cmd.Reply(nil, pct.UnknownCmdError{Cmd: cmd.Cmd})
	}
}
Ejemplo n.º 2
0
func (s *TestSuite) SetUpTest(t *C) {
	s.e = mysqlExec.NewQueryExecutor(s.conn)
}