blob: 54e5cca4bee25b16d814ff3b15f4588a408ce754 [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package command
8
9import (
10 "context"
11
12 "github.com/mongodb/mongo-go-driver/bson"
13 "github.com/mongodb/mongo-go-driver/x/bsonx"
14 "github.com/mongodb/mongo-go-driver/x/mongo/driver/session"
15 "github.com/mongodb/mongo-go-driver/x/network/description"
16 "github.com/mongodb/mongo-go-driver/x/network/result"
17 "github.com/mongodb/mongo-go-driver/x/network/wiremessage"
18)
19
20// AbortTransaction represents the abortTransaction() command
21type AbortTransaction struct {
22 Session *session.Client
23 err error
24 result result.TransactionResult
25}
26
27// Encode will encode this command into a wiremessage for the given server description.
28func (at *AbortTransaction) Encode(desc description.SelectedServer) (wiremessage.WireMessage, error) {
29 cmd := at.encode(desc)
30 return cmd.Encode(desc)
31}
32
33func (at *AbortTransaction) encode(desc description.SelectedServer) *Write {
34 cmd := bsonx.Doc{{"abortTransaction", bsonx.Int32(1)}}
35 return &Write{
36 DB: "admin",
37 Command: cmd,
38 Session: at.Session,
39 WriteConcern: at.Session.CurrentWc,
40 }
41}
42
43// Decode will decode the wire message using the provided server description. Errors during decoding are deferred until
44// either the Result or Err methods are called.
45func (at *AbortTransaction) Decode(desc description.SelectedServer, wm wiremessage.WireMessage) *AbortTransaction {
46 rdr, err := (&Write{}).Decode(desc, wm).Result()
47 if err != nil {
48 at.err = err
49 return at
50 }
51
52 return at.decode(desc, rdr)
53}
54
55func (at *AbortTransaction) decode(desc description.SelectedServer, rdr bson.Raw) *AbortTransaction {
56 at.err = bson.Unmarshal(rdr, &at.result)
57 if at.err == nil && at.result.WriteConcernError != nil {
58 at.err = Error{
59 Code: int32(at.result.WriteConcernError.Code),
60 Message: at.result.WriteConcernError.ErrMsg,
61 }
62 }
63 return at
64}
65
66// Result returns the result of a decoded wire message and server description.
67func (at *AbortTransaction) Result() (result.TransactionResult, error) {
68 if at.err != nil {
69 return result.TransactionResult{}, at.err
70 }
71
72 return at.result, nil
73}
74
75// Err returns the error set on this command
76func (at *AbortTransaction) Err() error {
77 return at.err
78}
79
80// RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter
81func (at *AbortTransaction) RoundTrip(ctx context.Context, desc description.SelectedServer, rw wiremessage.ReadWriter) (result.TransactionResult, error) {
82 cmd := at.encode(desc)
83 rdr, err := cmd.RoundTrip(ctx, desc, rw)
84 if err != nil {
85 return result.TransactionResult{}, err
86 }
87
88 return at.decode(desc, rdr).Result()
89}