Repository navigation
Expand file tree
/
Copy pathrow.go
More file actions
145 lines (128 loc) · 2.68 KB
/
Copy pathrow.go
File metadata and controls
145 lines (128 loc) · 2.68 KB
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
package main
import "fmt"
import "sort"
// Row : db row data..
type Row struct {
Cols map[string]int
Data []interface{}
}
// Get : get row form column name
func (r *Row) Get(key string) interface{} {
if idx, exist := r.Cols[key]; exist {
if r.Data[idx] != nil {
return r.Data[idx]
}
return ""
}
return nil
}
// GetKeyValue : return keyvalue store
func (r *Row) GetKeyValue(keys []string) []*KeyValue {
var result []*KeyValue
if keys != nil {
for _, k := range keys {
result = append(result, &KeyValue{
Key: k,
Value: r.Data[r.Cols[k]],
})
}
} else {
for i := 0; i < len(r.Data); i++ {
result = append(result, &KeyValue{
Key: r.getKeyStr(i),
Value: r.Data[i],
})
}
}
return result
}
func (r *Row) getKeyStr(idx int) string {
for k, v := range r.Cols {
if v == idx {
return k
}
}
return ""
}
// GetPrimaryKey : get primary key data.
func (r *Row) GetPrimaryKey(keys []string) string {
var result string
for _, k := range keys {
result += fmt.Sprintf("%v|", r.Get(k))
}
return result
}
func (r *Row) compare(dst *Row) []string {
var changeCols []string
for i := 0; i < len(r.Data); i++ {
if r.Data[i] != dst.Data[i] {
changeCols = append(changeCols, r.getKeyStr(i))
}
}
return changeCols
}
func reMapRows(src []*Row, keys []string) map[string]*Row {
srcLists := make(map[string]*Row)
for _, s := range src {
key := s.GetPrimaryKey(keys)
srcLists[key] = s
}
return srcLists
}
func checkIgnoreColumn(src, dst []string) bool {
cnt := 0
for _, ic := range src {
for _, cc := range dst {
if ic == cc {
cnt++
break
}
}
}
return cnt != len(dst)
}
// CompareRows : compare rows
func CompareRows(src, dst []*Row, keys []string, ignoreColumns []string) []*DiffRow {
srcMap := reMapRows(src, keys)
dstMap := reMapRows(dst, keys)
var result []*DiffRow
for k, s := range srcMap {
// get dst..
d, exist := dstMap[k]
if !exist {
continue
}
// compare.. rows..
if changeCols := s.compare(d); changeCols != nil {
if checkIgnoreColumn(ignoreColumns, changeCols) {
result = append(result, &DiffRow{
Compare: UPDATE,
Keys: s.GetKeyValue(keys),
Data: s.GetKeyValue(changeCols),
})
}
}
// delete..
delete(srcMap, k)
delete(dstMap, k)
}
// src insert..
for _, s := range srcMap {
result = append(result, &DiffRow{
Compare: INSERT,
Keys: s.GetKeyValue(keys), // for sort
Data: s.GetKeyValue(nil),
})
}
// dst drop..
for _, d := range dstMap {
result = append(result, &DiffRow{
Compare: DELETE,
Keys: d.GetKeyValue(keys),
})
}
sort.Slice(result, func(i, j int) bool {
return result[i].GetKey() < result[j].GetKey()
})
return result
}