Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // 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 | |
| 7 | package options |
| 8 | |
| 9 | import ( |
| 10 | "github.com/mongodb/mongo-go-driver/mongo/readconcern" |
| 11 | "github.com/mongodb/mongo-go-driver/mongo/readpref" |
| 12 | "github.com/mongodb/mongo-go-driver/mongo/writeconcern" |
| 13 | ) |
| 14 | |
| 15 | // TransactionOptions represents all possible options for starting a transaction. |
| 16 | type TransactionOptions struct { |
| 17 | ReadConcern *readconcern.ReadConcern // The read concern for the transaction. Defaults to the session's read concern. |
| 18 | ReadPreference *readpref.ReadPref // The read preference for the transaction. Defaults to the session's read preference. |
| 19 | WriteConcern *writeconcern.WriteConcern // The write concern for the transaction. Defaults to the session's write concern. |
| 20 | } |
| 21 | |
| 22 | // Transaction creates a new *TransactionOptions |
| 23 | func Transaction() *TransactionOptions { |
| 24 | return &TransactionOptions{} |
| 25 | } |
| 26 | |
| 27 | // SetReadConcern sets the read concern for the transaction. |
| 28 | func (t *TransactionOptions) SetReadConcern(rc *readconcern.ReadConcern) *TransactionOptions { |
| 29 | t.ReadConcern = rc |
| 30 | return t |
| 31 | } |
| 32 | |
| 33 | // SetReadPreference sets the read preference for the transaction. |
| 34 | func (t *TransactionOptions) SetReadPreference(rp *readpref.ReadPref) *TransactionOptions { |
| 35 | t.ReadPreference = rp |
| 36 | return t |
| 37 | } |
| 38 | |
| 39 | // SetWriteConcern sets the write concern for the transaction. |
| 40 | func (t *TransactionOptions) SetWriteConcern(wc *writeconcern.WriteConcern) *TransactionOptions { |
| 41 | t.WriteConcern = wc |
| 42 | return t |
| 43 | } |
| 44 | |
| 45 | // MergeTransactionOptions combines the given *TransactionOptions into a single *TransactionOptions in a last one wins |
| 46 | // fashion. |
| 47 | func MergeTransactionOptions(opts ...*TransactionOptions) *TransactionOptions { |
| 48 | t := Transaction() |
| 49 | for _, opt := range opts { |
| 50 | if opt == nil { |
| 51 | continue |
| 52 | } |
| 53 | if opt.ReadConcern != nil { |
| 54 | t.ReadConcern = opt.ReadConcern |
| 55 | } |
| 56 | if opt.ReadPreference != nil { |
| 57 | t.ReadPreference = opt.ReadPreference |
| 58 | } |
| 59 | if opt.WriteConcern != nil { |
| 60 | t.WriteConcern = opt.WriteConcern |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return t |
| 65 | } |