forked from ariefdarmawan/flexmgo
/
connection.go
158 lines (127 loc) · 3.06 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package flexmgo
import (
"context"
"fmt"
"strings"
"time"
"git.kanosolution.net/kano/dbflex"
"github.com/eaciit/toolkit"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Connection struct {
dbflex.ConnectionBase `bson:"-" json:"-"`
ctx context.Context
client *mongo.Client
db *mongo.Database
sess mongo.Session
}
func (c *Connection) Connect() error {
configString := "?"
for k, v := range c.Config {
configString += k + "=" + v.(string) + "&"
}
connURI := "mongodb://"
// Check if credentials given
if c.User != "" && c.Password != "" {
connURI += c.User + ":" + c.Password + "@"
}
connURI += c.Host + "/"
connURI += configString
opts := options.Client().ApplyURI(connURI)
for k, v := range c.Config {
klow := strings.ToLower(k)
switch klow {
case "serverselectiontimeout":
opts.SetServerSelectionTimeout(
time.Duration(toolkit.ToInt(v, toolkit.RoundingAuto)) * time.Millisecond)
case "replicaset":
opts.SetReplicaSet(v.(string))
//opts.SetWriteConcern()
}
}
//toolkit.Logger().Debugf("opts: %s", toolkit.JsonString(opts))
client, err := mongo.NewClient(opts)
if err != nil {
return err
}
//toolkit.Logger().Debug("client generated: OK")
if c.ctx == nil {
c.ctx = context.Background()
}
//toolkit.Logger().Debug("context generated: OK")
if err = client.Connect(c.ctx); err != nil {
return err
}
//toolkit.Logger().Debug("client connected: OK")
if err = client.Ping(c.ctx, nil); err != nil {
return err
}
c.client = client
if c.Database != "" {
c.db = c.client.Database(c.Database)
}
return nil
}
func (c *Connection) Mdb() *mongo.Database {
return c.db
}
func (c *Connection) State() string {
if c.client == nil {
return dbflex.StateUnknown
} else {
return dbflex.StateConnected
}
}
func (c *Connection) Close() {
if c.client != nil {
c.client.Disconnect(c.ctx)
c.client = nil
}
}
func (c *Connection) NewQuery() dbflex.IQuery {
q := new(Query)
q.SetThis(q)
q.SetConnection(c)
return q
}
func (c *Connection) DropTable(name string) error {
return c.db.Collection(name).Drop(c.ctx)
}
func (c *Connection) BeginTx() error {
if c.sess != nil {
return fmt.Errorf("session already exist. Pls commit or rollback last")
}
sess, err := c.client.StartSession()
if err != nil {
return fmt.Errorf("unable to start new transaction. %s", err.Error())
}
sess.StartTransaction()
c.sess = sess
return nil
}
func (c *Connection) Commit() error {
if c.sess == nil {
return fmt.Errorf("transaction session is not exists yet")
}
err := c.sess.CommitTransaction(c.ctx)
if err != nil {
return fmt.Errorf("unable to commit. %s", err.Error())
}
c.sess = nil
return nil
}
func (c *Connection) Rollback() error {
if c.sess == nil {
return fmt.Errorf("transaction session is not exists yet")
}
err := c.sess.AbortTransaction(c.ctx)
if err != nil {
return fmt.Errorf("unable to commit. %s", err.Error())
}
c.sess = nil
return nil
}
func (c *Connection) IsTx() bool {
return c.sess != nil
}