Beispiel #1
0
// Execute routes a non-streaming query.
func (rtr *Router) Execute(ctx context.Context, query *proto.Query) (*mproto.QueryResult, error) {
	if query.BindVariables == nil {
		query.BindVariables = make(map[string]interface{})
	}
	vcursor := newRequestContext(ctx, query, rtr)
	plan := rtr.planner.GetPlan(string(query.Sql))

	switch plan.ID {
	case planbuilder.UpdateEqual:
		return rtr.execUpdateEqual(vcursor, plan)
	case planbuilder.DeleteEqual:
		return rtr.execDeleteEqual(vcursor, plan)
	case planbuilder.InsertSharded:
		return rtr.execInsertSharded(vcursor, plan)
	}

	var err error
	var params *scatterParams
	switch plan.ID {
	case planbuilder.SelectUnsharded, planbuilder.UpdateUnsharded,
		planbuilder.DeleteUnsharded, planbuilder.InsertUnsharded:
		params, err = rtr.paramsUnsharded(vcursor, plan)
	case planbuilder.SelectEqual:
		params, err = rtr.paramsSelectEqual(vcursor, plan)
	case planbuilder.SelectIN:
		params, err = rtr.paramsSelectIN(vcursor, plan)
	case planbuilder.SelectKeyrange:
		params, err = rtr.paramsSelectKeyrange(vcursor, plan)
	case planbuilder.SelectScatter:
		params, err = rtr.paramsSelectScatter(vcursor, plan)
	default:
		return nil, fmt.Errorf("cannot route query: %s: %s", query.Sql, plan.Reason)
	}
	if err != nil {
		return nil, err
	}
	return rtr.scatterConn.ExecuteMulti(
		ctx,
		params.query,
		params.ks,
		params.shardVars,
		query.TabletType,
		NewSafeSession(vcursor.query.Session),
		query.NotInTransaction,
	)
}
Beispiel #2
0
// StreamExecute executes a streaming query.
func (rtr *Router) StreamExecute(ctx context.Context, query *proto.Query, sendReply func(*mproto.QueryResult) error) error {
	if query.BindVariables == nil {
		query.BindVariables = make(map[string]interface{})
	}
	vcursor := newRequestContext(ctx, query, rtr)
	plan := rtr.planner.GetPlan(string(query.Sql))

	var err error
	var params *scatterParams
	switch plan.ID {
	case planbuilder.SelectUnsharded:
		params, err = rtr.paramsUnsharded(vcursor, plan)
	case planbuilder.SelectEqual:
		params, err = rtr.paramsSelectEqual(vcursor, plan)
	case planbuilder.SelectIN:
		params, err = rtr.paramsSelectIN(vcursor, plan)
	case planbuilder.SelectKeyrange:
		params, err = rtr.paramsSelectKeyrange(vcursor, plan)
	case planbuilder.SelectScatter:
		params, err = rtr.paramsSelectScatter(vcursor, plan)
	default:
		return fmt.Errorf("query %q cannot be used for streaming", query.Sql)
	}
	if err != nil {
		return err
	}
	return rtr.scatterConn.StreamExecuteMulti(
		ctx,
		params.query,
		params.ks,
		params.shardVars,
		query.TabletType,
		NewSafeSession(vcursor.query.Session),
		sendReply,
		query.NotInTransaction,
	)
}