forked from go-rel/rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
on_conflict.go
64 lines (54 loc) · 1.83 KB
/
on_conflict.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
package rel
// OnConflict mutation.
type OnConflict struct {
Keys []string
Ignore bool
Replace bool
Fragment string
FragmentArgs []any
}
// Apply mutation.
func (ocm OnConflict) Apply(doc *Document, mutation *Mutation) {
if ocm.Keys == nil && ocm.Fragment == "" {
ocm.Keys = doc.PrimaryFields()
}
mutation.OnConflict = ocm
}
// OnConflictIgnore insertion when conflict happens.
func OnConflictIgnore() OnConflict {
return OnConflict{Ignore: true}
}
// OnConflictKeyIgnore insertion when conflict happens on specific keys.
//
// Specifying key is not supported by all database and may be ignored.
func OnConflictKeyIgnore(key string) OnConflict {
return OnConflictKeysIgnore([]string{key})
}
// OnConflictKeysIgnore insertion when conflict happens on specific keys.
//
// Specifying key is not supported by all database and may be ignored.
func OnConflictKeysIgnore(keys []string) OnConflict {
return OnConflict{Keys: keys, Ignore: true}
}
// OnConflictReplace insertion when conflict happens.
func OnConflictReplace() OnConflict {
return OnConflict{Replace: true}
}
// OnConflictKeyReplace insertion when conflict happens on specific keys.
//
// Specifying key is not supported by all database and may be ignored.
func OnConflictKeyReplace(key string) OnConflict {
return OnConflictKeysReplace([]string{key})
}
// OnConflictKeysReplace insertion when conflict happens on specific keys.
//
// Specifying key is not supported by all database and may be ignored.
func OnConflictKeysReplace(keys []string) OnConflict {
return OnConflict{Keys: keys, Replace: true}
}
// OnConflictFragment allows to write custom sql for on conflict.
//
// This will add custom sql after ON CONFLICT, example: ON CONFLICT [FRAGMENT]
func OnConflictFragment(sql string, args ...any) OnConflict {
return OnConflict{Fragment: sql, FragmentArgs: args}
}