executeUpdateWithTrailer is a hand-rolled equivalent of flightsql.Client.ExecuteUpdate that exposes the gRPC trailer. ExecuteUpdate is a bidirectional-streaming RPC (DoPut). Passing grpc.Trailer(&md) as a CallOption is documented to work for unary RPCs; for streams the trailer is only reachable via
(ctx context.Context, c *flightsql.Client, query string)
| 29 | // This re-implements the same wire interaction (descriptor + CommandStatementUpdate), |
| 30 | // then drains the stream to io.EOF and pulls the trailer. |
| 31 | func executeUpdateWithTrailer(ctx context.Context, c *flightsql.Client, query string) (int64, metadata.MD, error) { |
| 32 | cmd := &pb.CommandStatementUpdate{Query: query} |
| 33 | |
| 34 | var anyMsg anypb.Any |
| 35 | if err := anyMsg.MarshalFrom(cmd); err != nil { |
| 36 | return 0, nil, fmt.Errorf("marshal CommandStatementUpdate: %w", err) |
| 37 | } |
| 38 | descBytes, err := proto.Marshal(&anyMsg) |
| 39 | if err != nil { |
| 40 | return 0, nil, fmt.Errorf("marshal Any: %w", err) |
| 41 | } |
| 42 | desc := &flight.FlightDescriptor{ |
| 43 | Type: flight.DescriptorCMD, |
| 44 | Cmd: descBytes, |
| 45 | } |
| 46 | |
| 47 | stream, err := c.Client.DoPut(ctx) |
| 48 | if err != nil { |
| 49 | return 0, nil, fmt.Errorf("flight doput: %w", err) |
| 50 | } |
| 51 | |
| 52 | if err := stream.Send(&flight.FlightData{FlightDescriptor: desc}); err != nil { |
| 53 | return 0, stream.Trailer(), fmt.Errorf("flight doput send: %w", err) |
| 54 | } |
| 55 | if err := stream.CloseSend(); err != nil { |
| 56 | return 0, stream.Trailer(), fmt.Errorf("flight doput close-send: %w", err) |
| 57 | } |
| 58 | |
| 59 | res, err := stream.Recv() |
| 60 | if err != nil { |
| 61 | return 0, stream.Trailer(), err |
| 62 | } |
| 63 | |
| 64 | // Drain to io.EOF so the trailer is materialised. The worker sends a |
| 65 | // single PutResult and closes the stream, so this Recv should return |
| 66 | // io.EOF immediately. |
| 67 | if drainErr := drainToEOF(stream); drainErr != nil { |
| 68 | // Surface the error, but the trailer may still be valid. |
| 69 | return 0, stream.Trailer(), drainErr |
| 70 | } |
| 71 | |
| 72 | trailer := stream.Trailer() |
| 73 | |
| 74 | var updateResult pb.DoPutUpdateResult |
| 75 | if err := proto.Unmarshal(res.GetAppMetadata(), &updateResult); err != nil { |
| 76 | return 0, trailer, fmt.Errorf("unmarshal DoPutUpdateResult: %w", err) |
| 77 | } |
| 78 | return updateResult.GetRecordCount(), trailer, nil |
| 79 | } |
| 80 | |
| 81 | // drainToEOF reads from the stream until io.EOF or another non-nil error. |
| 82 | // Per the gRPC contract, the trailer is only reachable after Recv returns |