forked from scylladb/gocqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewrite.go
41 lines (36 loc) · 1.12 KB
/
rewrite.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
// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found in the LICENSE file.
package dbutil
import (
"github.com/oanogin/gocqlx/v2"
"github.com/oanogin/gocqlx/v2/table"
)
// RewriteTable rewrites src table to dst table.
// Rows can be transformed using the transform function.
// If row map is empty after transformation the row is skipped.
// Additional options can be passed to modify the insert query.
func RewriteTable(session gocqlx.Session, dst, src *table.Table, transform func(map[string]interface{}), options ...func(q *gocqlx.Queryx)) error {
insert := dst.InsertQuery(session)
defer insert.Release()
// Apply query options
for _, o := range options {
o(insert)
}
// Iterate over all rows and reinsert them to dst table
iter := session.Query(src.SelectAll()).Iter()
m := make(map[string]interface{})
for iter.MapScan(m) {
if transform != nil {
transform(m)
}
if len(m) == 0 {
continue // map is empty - no need to clean
}
if err := insert.BindMap(m).Exec(); err != nil {
return err
}
m = map[string]interface{}{}
}
return iter.Close()
}