-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbr-sync.go
201 lines (160 loc) · 4.36 KB
/
dbr-sync.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package dbr_sync
import (
"fmt"
"sync"
"github.com/joaosoft/dbr"
"github.com/joaosoft/json"
"github.com/joaosoft/logger"
"github.com/joaosoft/manager"
migration "github.com/joaosoft/migration/services"
"github.com/labstack/gommon/log"
"github.com/streadway/amqp"
)
type mode string
type DbrSync struct {
config *DbrSyncConfig
isLogExternal bool
pm *manager.Manager
storage *dbr.Dbr
logger logger.ILogger
mux sync.Mutex
}
// NewDbrSync ...
func NewDbrSync(options ...DbrSyncOption) (*DbrSync, error) {
config, simpleConfig, err := NewConfig()
service := &DbrSync{
pm: manager.NewManager(manager.WithRunInBackground(true)),
logger: logger.NewLogDefault("dbr-sync", logger.WarnLevel),
config: config.DbrSync,
}
if service.isLogExternal {
service.pm.Reconfigure(manager.WithLogger(logger.Instance))
}
if err != nil {
service.logger.Error(err.Error())
} else if config.DbrSync != nil {
service.pm.AddConfig("config_app", simpleConfig)
level, _ := logger.ParseLevel(config.DbrSync.Log.Level)
service.logger.Debugf("setting log level to %s", level)
service.logger.Reconfigure(logger.WithLevel(level))
} else {
config.DbrSync = &DbrSyncConfig{}
}
service.Reconfigure(options...)
// execute migrations
migrationService, err := migration.NewCmdService(migration.WithCmdConfiguration(service.config.Migration))
if err != nil {
return nil, err
}
if _, err := migrationService.Execute(migration.OptionUp, 0, migration.ExecutorModeDatabase); err != nil {
return nil, err
}
service.storage, err = dbr.New(dbr.WithConfiguration(config.DbrSync.Dbr))
if err != nil {
return nil, err
}
// rabbitmq
rabbitmqConsumer, err := service.pm.NewSimpleRabbitmqConsumer(
config.DbrSync.Rabbitmq.RabbitmqConfig,
config.DbrSync.Rabbitmq.Queue,
config.DbrSync.Rabbitmq.Binding,
"dbr-sync", service.consumeRabbitMessage)
if err != nil {
log.Errorf("%s", err)
}
service.pm.AddRabbitmqConsumer("rabbitmq_consumer", rabbitmqConsumer)
return service, nil
}
// Start ...
func (m *DbrSync) Start() error {
return m.pm.Start()
}
// Stop ...
func (m *DbrSync) Stop() error {
return m.pm.Stop()
}
func (m *DbrSync) consumeRabbitMessage(msg amqp.Delivery) error {
log.Info("Handling message...")
var operations OperationList
err := json.Unmarshal(msg.Body, &operations, "json")
if err != nil {
return err
}
var connection dbr.IDbr
connection = m.storage
var executor dbr.Executor
lenOperations := len(operations)
if lenOperations > 1 {
tx, err := m.storage.Begin()
if err != nil {
return err
}
connection = tx
}
for _, operation := range operations {
if operation.Details != nil {
fmt.Println(operation.Details.Values)
switch operation.Operation {
case dbr.InsertOperation:
// columns / values
var columns []interface{}
var values []interface{}
for column, value := range operation.Details.Values {
columns = append(columns, column)
values = append(values, value)
}
executor = connection.
Insert().
Into(operation.Details.Table).
Columns(columns...).
Values(values...)
case dbr.UpdateOperation:
stmtUpdate := connection.
Update(operation.Details.Table)
for column, value := range operation.Details.Values {
stmtUpdate.Set(column, value)
}
// query
var query string
var values []interface{}
lenC := len(operation.Details.Conditions)
for column, value := range operation.Details.Conditions {
query += fmt.Sprintf("%s = ?", column)
values = append(values, value)
if len(values) < lenC {
query += ", "
}
}
stmtUpdate.Where(query, values...)
executor = stmtUpdate
case dbr.DeleteOperation:
stmtDelete := connection.
Delete().From(operation.Details.Table)
// query
var query string
var values []interface{}
lenC := len(operation.Details.Conditions)
for column, value := range operation.Details.Conditions {
query += fmt.Sprintf("%s = ?", column)
values = append(values, value)
if len(values) < lenC {
query += ", "
}
}
stmtDelete.Where(query, values...)
executor = stmtDelete
}
} else {
executor = connection.
Execute(*operation.Query)
}
_, err = executor.Exec()
if err != nil {
return err
}
}
if lenOperations > 1 {
return connection.(*dbr.Transaction).Commit()
}
return nil
}