/
ops.go
108 lines (85 loc) · 3.77 KB
/
ops.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
package db
import (
"fmt"
"go.uber.org/zap"
)
// Insert a row in the DB, it is assumed the table exists, you can do a
// check before with HasTable()
func (l *Loader) Insert(tableName string, primaryKey string, data map[string]string) error {
if l.tracer.Enabled() {
l.logger.Debug("processing insert operation", zap.String("table_name", tableName), zap.String("primary_key", primaryKey), zap.Int("field_count", len(data)))
}
if _, found := l.entries[tableName]; !found {
if l.tracer.Enabled() {
l.logger.Debug("adding tracking of table never seen before", zap.String("table_name", tableName))
}
l.entries[tableName] = map[string]*Operation{}
}
if _, found := l.entries[tableName][primaryKey]; found {
return fmt.Errorf("attempting to insert in table %q a primary key %q, that is already scheduled for insertion, insert should only be called once for a given primary key", tableName, primaryKey)
}
if l.tracer.Enabled() {
l.logger.Debug("primary key entry never existed for table, adding insert operation", zap.String("primary_key", primaryKey), zap.String("table_name", tableName))
}
// we need to make sure to add the primary key in the data so that
// it gets created
data[l.tablePrimaryKeys[tableName]] = primaryKey
l.entries[tableName][primaryKey] = l.newInsertOperation(tableName, primaryKey, data)
l.EntriesCount++
return nil
}
// Update a row in the DB, it is assumed the table exists, you can do a
// check before with HasTable()
func (l *Loader) Update(tableName string, primaryKey string, data map[string]string) error {
if l.tracer.Enabled() {
l.logger.Debug("processing update operation", zap.String("table_name", tableName), zap.String("primary_key", primaryKey), zap.Int("field_count", len(data)))
}
if _, found := l.entries[tableName]; !found {
if l.tracer.Enabled() {
l.logger.Debug("adding tracking of table never seen before", zap.String("table_name", tableName))
}
l.entries[tableName] = map[string]*Operation{}
}
if op, found := l.entries[tableName][primaryKey]; found {
if op.opType == OperationTypeDelete {
return fmt.Errorf("attempting to update an object with primary key %q, that schedule to be deleted", primaryKey)
}
if l.tracer.Enabled() {
l.logger.Debug("primary key entry already exist for table, merging fields together", zap.String("primary_key", primaryKey), zap.String("table_name", tableName))
}
op.mergeData(data)
l.entries[tableName][primaryKey] = op
return nil
} else {
l.EntriesCount++
}
if l.tracer.Enabled() {
l.logger.Debug("primary key entry never existed for table, adding update operation", zap.String("primary_key", primaryKey), zap.String("table_name", tableName))
}
l.entries[tableName][primaryKey] = l.newUpdateOperation(tableName, primaryKey, data)
return nil
}
// Delete a row in the DB, it is assumed the table exists, you can do a
// check before with HasTable()
func (l *Loader) Delete(tableName string, primaryKey string) error {
if l.tracer.Enabled() {
l.logger.Debug("processing delete operation", zap.String("table_name", tableName), zap.String("primary_key", primaryKey))
}
if _, found := l.entries[tableName]; !found {
if l.tracer.Enabled() {
l.logger.Debug("adding tracking of table never seen before", zap.String("table_name", tableName))
}
l.entries[tableName] = map[string]*Operation{}
}
if _, found := l.entries[tableName][primaryKey]; !found {
if l.tracer.Enabled() {
l.logger.Debug("primary key entry never existed for table", zap.String("primary_key", primaryKey), zap.String("table_name", tableName))
}
l.EntriesCount++
}
if l.tracer.Enabled() {
l.logger.Debug("adding deleting operation", zap.String("primary_key", primaryKey), zap.String("table_name", tableName))
}
l.entries[tableName][primaryKey] = l.newDeleteOperation(tableName, primaryKey)
return nil
}