-
Notifications
You must be signed in to change notification settings - Fork 22
/
write_message.go
171 lines (138 loc) · 3.78 KB
/
write_message.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
package message
import (
"slices"
"time"
"github.com/apache/arrow/go/v16/arrow"
"github.com/cloudquery/plugin-sdk/v4/schema"
)
type writeBaseMessage struct{}
func (*writeBaseMessage) IsWriteMessage() bool { return true }
type WriteMessage interface {
GetTable() *schema.Table
IsWriteMessage() bool
}
type WriteMessages []WriteMessage
func (messages WriteMessages) InsertItems() int64 {
items := int64(0)
for _, msg := range messages {
if m, ok := msg.(*WriteInsert); ok {
items += m.Record.NumRows()
}
}
return items
}
func (messages WriteMessages) GetInserts() WriteInserts {
inserts := make(WriteInserts, 0, len(messages))
for _, msg := range messages {
if m, ok := msg.(*WriteInsert); ok {
inserts = append(inserts, m)
}
}
return slices.Clip(inserts)
}
type WriteMigrateTable struct {
writeBaseMessage
Table *schema.Table
MigrateForce bool
}
func (m WriteMigrateTable) GetTable() *schema.Table { return m.Table }
type WriteMigrateTables []*WriteMigrateTable
func (m WriteMigrateTables) Exists(tableName string) bool {
return slices.ContainsFunc(m, func(msg *WriteMigrateTable) bool {
return msg.Table.Name == tableName
})
}
func (m WriteMigrateTables) GetMessageByTable(tableName string) *WriteMigrateTable {
for _, msg := range m {
if msg.Table.Name == tableName {
return msg
}
}
return nil
}
type WriteInsert struct {
writeBaseMessage
Record arrow.Record
}
func (m *WriteInsert) GetTable() *schema.Table {
table, err := schema.NewTableFromArrowSchema(m.Record.Schema())
if err != nil {
panic(err)
}
return table
}
type WriteInserts []*WriteInsert
func (m WriteInserts) Exists(tableName string) bool {
return slices.ContainsFunc(m, func(msg *WriteInsert) bool {
tableNameMeta, ok := msg.Record.Schema().Metadata().GetValue(schema.MetadataTableName)
return ok && tableNameMeta == tableName
})
}
func (m WriteInserts) GetRecords() []arrow.Record {
res := make([]arrow.Record, len(m))
for i := range m {
res[i] = m[i].Record
}
return res
}
func (m WriteInserts) GetRecordsForTable(table *schema.Table) []arrow.Record {
res := make([]arrow.Record, 0, len(m))
for _, insert := range m {
tableNameMeta, ok := insert.Record.Schema().Metadata().GetValue(schema.MetadataTableName)
if !ok || tableNameMeta != table.Name {
continue
}
res = append(res, insert.Record)
}
return slices.Clip(res)
}
// WriteDeleteStale is a pretty specific message which requires the destination to be aware of a CLI use-case
// thus it might be deprecated in the future
// in favour of MessageDelete or MessageRawQuery
// The message indicates that the destination needs to run something like "DELETE FROM table WHERE _cq_source_name=$1 and sync_time < $2"
type WriteDeleteStale struct {
writeBaseMessage
TableName string
SourceName string
SyncTime time.Time
}
func (m WriteDeleteStale) GetTable() *schema.Table {
return &schema.Table{Name: m.TableName}
}
type WriteDeleteStales []*WriteDeleteStale
func (m WriteDeleteStales) Exists(tableName string) bool {
return slices.ContainsFunc(m, func(msg *WriteDeleteStale) bool {
return msg.TableName == tableName
})
}
type TableRelation struct {
TableName string
ParentTable string
}
type TableRelations []TableRelation
type Predicate struct {
Operator string
Column string
Record arrow.Record
}
type Predicates []Predicate
type PredicateGroup struct {
// This will be AND or OR
GroupingType string
Predicates Predicates
}
type PredicateGroups []PredicateGroup
type DeleteRecord struct {
TableName string
TableRelations TableRelations
WhereClause PredicateGroups
SyncTime time.Time
}
type WriteDeleteRecord struct {
writeBaseMessage
DeleteRecord
}
func (m WriteDeleteRecord) GetTable() *schema.Table {
return &schema.Table{Name: m.TableName}
}
type WriteDeleteRecords []*WriteDeleteRecord