// StreamExecute is part of tabletconn.TabletConn // We need to copy the bind variables as tablet server will change them. func (itc *internalTabletConn) StreamExecute(ctx context.Context, query string, bindVars map[string]interface{}, transactionID int64) (<-chan *sqltypes.Result, tabletconn.ErrFunc, error) { bv, err := querytypes.BindVariablesToProto3(bindVars) if err != nil { return nil, nil, err } bindVars, err = querytypes.Proto3ToBindVariables(bv) if err != nil { return nil, nil, err } result := make(chan *sqltypes.Result, 10) var finalErr error go func() { finalErr = itc.tablet.qsc.QueryService().StreamExecute(ctx, &querypb.Target{ Keyspace: itc.tablet.keyspace, Shard: itc.tablet.shard, TabletType: itc.tablet.tabletType, }, query, bindVars, 0, func(reply *sqltypes.Result) error { // We need to deep-copy the reply before returning, // because the underlying buffers are reused. result <- reply.Copy() return nil }) // the client will only access finalErr after the // channel is closed, and then it's already set. close(result) }() return result, func() error { return tabletconn.TabletErrorFromGRPC(tabletserver.ToGRPCError(finalErr)) }, nil }
// SplitQuery is the RPC version of vtgateservice.VTGateService method func (vtg *VTGate) SplitQuery(ctx context.Context, request *vtgatepb.SplitQueryRequest) (response *vtgatepb.SplitQueryResponse, err error) { defer vtg.server.HandlePanic(&err) ctx = withCallerIDContext(ctx, request.CallerId) bv, err := querytypes.Proto3ToBindVariables(request.Query.BindVariables) if err != nil { return nil, vterrors.ToGRPCError(err) } splits, vtgErr := vtgateservice.CallCorrectSplitQuery( vtg.server, request.UseSplitQueryV2, ctx, request.Keyspace, string(request.Query.Sql), bv, request.SplitColumn, request.SplitCount, request.NumRowsPerQueryPart, request.Algorithm) if vtgErr != nil { return nil, vterrors.ToGRPCError(vtgErr) } return &vtgatepb.SplitQueryResponse{ Splits: splits, }, nil }
// BeginExecute is part of the queryservice.QueryServer interface func (q *query) BeginExecute(ctx context.Context, request *querypb.BeginExecuteRequest) (response *querypb.BeginExecuteResponse, err error) { defer q.server.HandlePanic(&err) ctx = callerid.NewContext(callinfo.GRPCCallInfo(ctx), request.EffectiveCallerId, request.ImmediateCallerId, ) bv, err := querytypes.Proto3ToBindVariables(request.Query.BindVariables) if err != nil { return nil, vterrors.ToGRPCError(err) } result, transactionID, err := q.server.BeginExecute(ctx, request.Target, request.Query.Sql, bv, request.Options) if err != nil { // if we have a valid transactionID, return the error in-band if transactionID != 0 { return &querypb.BeginExecuteResponse{ Error: vterrors.VtRPCErrorFromVtError(err), TransactionId: transactionID, }, nil } return nil, vterrors.ToGRPCError(err) } return &querypb.BeginExecuteResponse{ Result: sqltypes.ResultToProto3(result), TransactionId: transactionID, }, nil }
// StreamExecute is part of tabletconn.TabletConn // We need to copy the bind variables as tablet server will change them. func (itc *internalTabletConn) StreamExecute(ctx context.Context, target *querypb.Target, query string, bindVars map[string]interface{}, options *querypb.ExecuteOptions) (sqltypes.ResultStream, error) { bv, err := querytypes.BindVariablesToProto3(bindVars) if err != nil { return nil, err } bindVars, err = querytypes.Proto3ToBindVariables(bv) if err != nil { return nil, err } result := make(chan *sqltypes.Result, 10) var finalErr error go func() { finalErr = itc.tablet.qsc.QueryService().StreamExecute(ctx, target, query, bindVars, options, func(reply *sqltypes.Result) error { // We need to deep-copy the reply before returning, // because the underlying buffers are reused. result <- reply.Copy() return nil }) finalErr = tabletconn.TabletErrorFromGRPC(vterrors.ToGRPCError(finalErr)) // the client will only access finalErr after the // channel is closed, and then it's already set. close(result) }() return &streamExecuteAdapter{result, &finalErr}, nil }
// Execute is the RPC version of vtgateservice.VTGateService method func (vtg *VTGate) Execute(ctx context.Context, request *vtgatepb.ExecuteRequest) (response *vtgatepb.ExecuteResponse, err error) { defer vtg.server.HandlePanic(&err) ctx = withCallerIDContext(ctx, request.CallerId) bv, err := querytypes.Proto3ToBindVariables(request.Query.BindVariables) if err != nil { return nil, vterrors.ToGRPCError(err) } result, err := vtg.server.Execute(ctx, string(request.Query.Sql), bv, request.Keyspace, request.TabletType, request.Session, request.NotInTransaction) return &vtgatepb.ExecuteResponse{ Result: sqltypes.ResultToProto3(result), Session: request.Session, Error: vterrors.VtRPCErrorFromVtError(err), }, nil }
// Execute is part of tabletconn.TabletConn // We need to copy the bind variables as tablet server will change them. func (itc *internalTabletConn) Execute(ctx context.Context, target *querypb.Target, query string, bindVars map[string]interface{}, transactionID int64, options *querypb.ExecuteOptions) (*sqltypes.Result, error) { bv, err := querytypes.BindVariablesToProto3(bindVars) if err != nil { return nil, err } bindVars, err = querytypes.Proto3ToBindVariables(bv) if err != nil { return nil, err } reply, err := itc.tablet.qsc.QueryService().Execute(ctx, target, query, bindVars, transactionID, options) if err != nil { return nil, tabletconn.TabletErrorFromGRPC(vterrors.ToGRPCError(err)) } return reply, nil }
// ProtoToBoundShardQueries transforms a list of BoundShardQuery from proto3 func ProtoToBoundShardQueries(bsq []*vtgatepb.BoundShardQuery) ([]BoundShardQuery, error) { if len(bsq) == 0 { return nil, nil } result := make([]BoundShardQuery, len(bsq)) for i, q := range bsq { result[i].Sql = string(q.Query.Sql) bv, err := querytypes.Proto3ToBindVariables(q.Query.BindVariables) if err != nil { return nil, err } result[i].BindVariables = bv result[i].Keyspace = q.Keyspace result[i].Shards = q.Shards } return result, nil }
// Execute is part of the queryservice.QueryServer interface func (q *query) Execute(ctx context.Context, request *querypb.ExecuteRequest) (response *querypb.ExecuteResponse, err error) { defer q.server.HandlePanic(&err) ctx = callerid.NewContext(callinfo.GRPCCallInfo(ctx), request.EffectiveCallerId, request.ImmediateCallerId, ) bv, err := querytypes.Proto3ToBindVariables(request.Query.BindVariables) if err != nil { return nil, tabletserver.ToGRPCError(err) } result, err := q.server.Execute(ctx, request.Target, request.Query.Sql, bv, request.SessionId, request.TransactionId) if err != nil { return nil, tabletserver.ToGRPCError(err) } return &querypb.ExecuteResponse{ Result: sqltypes.ResultToProto3(result), }, nil }
// Execute is part of tabletconn.TabletConn // We need to copy the bind variables as tablet server will change them. func (itc *internalTabletConn) Execute(ctx context.Context, query string, bindVars map[string]interface{}, transactionID int64) (*sqltypes.Result, error) { bv, err := querytypes.BindVariablesToProto3(bindVars) if err != nil { return nil, err } bindVars, err = querytypes.Proto3ToBindVariables(bv) if err != nil { return nil, err } reply, err := itc.tablet.qsc.QueryService().Execute(ctx, &querypb.Target{ Keyspace: itc.tablet.keyspace, Shard: itc.tablet.shard, TabletType: itc.tablet.tabletType, }, query, bindVars, 0, transactionID) if err != nil { return nil, tabletconn.TabletErrorFromGRPC(tabletserver.ToGRPCError(err)) } return reply, nil }
// StreamExecute is the RPC version of vtgateservice.VTGateService method func (vtg *VTGate) StreamExecute(request *vtgatepb.StreamExecuteRequest, stream vtgateservicepb.Vitess_StreamExecuteServer) (err error) { defer vtg.server.HandlePanic(&err) ctx := withCallerIDContext(stream.Context(), request.CallerId) bv, err := querytypes.Proto3ToBindVariables(request.Query.BindVariables) if err != nil { return vterrors.ToGRPCError(err) } vtgErr := vtg.server.StreamExecute(ctx, string(request.Query.Sql), bv, request.Keyspace, request.TabletType, func(value *sqltypes.Result) error { return stream.Send(&vtgatepb.StreamExecuteResponse{ Result: sqltypes.ResultToProto3(value), }) }) return vterrors.ToGRPCError(vtgErr) }
// StreamExecute is part of the queryservice.QueryServer interface func (q *query) StreamExecute(request *querypb.StreamExecuteRequest, stream queryservicepb.Query_StreamExecuteServer) (err error) { defer q.server.HandlePanic(&err) ctx := callerid.NewContext(callinfo.GRPCCallInfo(stream.Context()), request.EffectiveCallerId, request.ImmediateCallerId, ) bv, err := querytypes.Proto3ToBindVariables(request.Query.BindVariables) if err != nil { return tabletserver.ToGRPCError(err) } if err := q.server.StreamExecute(ctx, request.Target, request.Query.Sql, bv, request.SessionId, func(reply *sqltypes.Result) error { return stream.Send(&querypb.StreamExecuteResponse{ Result: sqltypes.ResultToProto3(reply), }) }); err != nil { return tabletserver.ToGRPCError(err) } return nil }
// ExecuteBatch is part of tabletconn.TabletConn // We need to copy the bind variables as tablet server will change them. func (itc *internalTabletConn) ExecuteBatch(ctx context.Context, target *querypb.Target, queries []querytypes.BoundQuery, asTransaction bool, transactionID int64, options *querypb.ExecuteOptions) ([]sqltypes.Result, error) { q := make([]querytypes.BoundQuery, len(queries)) for i, query := range queries { bv, err := querytypes.BindVariablesToProto3(query.BindVariables) if err != nil { return nil, err } bindVars, err := querytypes.Proto3ToBindVariables(bv) if err != nil { return nil, err } q[i].Sql = query.Sql q[i].BindVariables = bindVars } results, err := itc.tablet.qsc.QueryService().ExecuteBatch(ctx, target, q, asTransaction, transactionID, options) if err != nil { return nil, tabletconn.TabletErrorFromGRPC(vterrors.ToGRPCError(err)) } return results, nil }
// StreamExecute is the RPC version of vtgateservice.VTGateService method func (vtg *VTGate) StreamExecute(request *vtgatepb.StreamExecuteRequest, stream vtgateservicepb.Vitess_StreamExecuteServer) (err error) { defer vtg.server.HandlePanic(&err) ctx := callerid.NewContext(callinfo.GRPCCallInfo(stream.Context()), request.CallerId, callerid.NewImmediateCallerID("grpc client")) bv, err := querytypes.Proto3ToBindVariables(request.Query.BindVariables) if err != nil { return vterrors.ToGRPCError(err) } vtgErr := vtg.server.StreamExecute(ctx, string(request.Query.Sql), bv, request.TabletType, func(value *sqltypes.Result) error { return stream.Send(&vtgatepb.StreamExecuteResponse{ Result: sqltypes.ResultToProto3(value), }) }) return vterrors.ToGRPCError(vtgErr) }
// ExecuteKeyRanges is the RPC version of vtgateservice.VTGateService method func (vtg *VTGate) ExecuteKeyRanges(ctx context.Context, request *vtgatepb.ExecuteKeyRangesRequest) (response *vtgatepb.ExecuteKeyRangesResponse, err error) { defer vtg.server.HandlePanic(&err) ctx = callerid.NewContext(callinfo.GRPCCallInfo(ctx), request.CallerId, callerid.NewImmediateCallerID("grpc client")) bv, err := querytypes.Proto3ToBindVariables(request.Query.BindVariables) if err != nil { return nil, vterrors.ToGRPCError(err) } result, err := vtg.server.ExecuteKeyRanges(ctx, string(request.Query.Sql), bv, request.Keyspace, request.KeyRanges, request.TabletType, request.Session, request.NotInTransaction) return &vtgatepb.ExecuteKeyRangesResponse{ Result: sqltypes.ResultToProto3(result), Session: request.Session, Error: vterrors.VtRPCErrorFromVtError(err), }, nil }
// SplitQuery is the RPC version of vtgateservice.VTGateService method func (vtg *VTGate) SplitQuery(ctx context.Context, request *vtgatepb.SplitQueryRequest) (response *vtgatepb.SplitQueryResponse, err error) { defer vtg.server.HandlePanic(&err) ctx = callerid.NewContext(callinfo.GRPCCallInfo(ctx), request.CallerId, callerid.NewImmediateCallerID("grpc client")) bv, err := querytypes.Proto3ToBindVariables(request.Query.BindVariables) if err != nil { return nil, vterrors.ToGRPCError(err) } splits, vtgErr := vtg.server.SplitQuery(ctx, request.Keyspace, string(request.Query.Sql), bv, request.SplitColumn, request.SplitCount) if vtgErr != nil { return nil, vterrors.ToGRPCError(vtgErr) } return &vtgatepb.SplitQueryResponse{ Splits: splits, }, nil }
// ExecuteBatch is part of tabletconn.TabletConn // We need to copy the bind variables as tablet server will change them. func (itc *internalTabletConn) ExecuteBatch(ctx context.Context, queries []querytypes.BoundQuery, asTransaction bool, transactionID int64) ([]sqltypes.Result, error) { q := make([]querytypes.BoundQuery, len(queries)) for i, query := range queries { bv, err := querytypes.BindVariablesToProto3(query.BindVariables) if err != nil { return nil, err } bindVars, err := querytypes.Proto3ToBindVariables(bv) if err != nil { return nil, err } q[i].Sql = query.Sql q[i].BindVariables = bindVars } results, err := itc.tablet.qsc.QueryService().ExecuteBatch(ctx, &querypb.Target{ Keyspace: itc.tablet.keyspace, Shard: itc.tablet.shard, TabletType: itc.tablet.tabletType, }, q, 0, asTransaction, transactionID) if err != nil { return nil, tabletconn.TabletErrorFromGRPC(tabletserver.ToGRPCError(err)) } return results, nil }