-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
56 lines (44 loc) · 1.43 KB
/
plugin.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
package gormqonvert
import (
"gorm.io/gorm"
)
// Compile-time interface check
var _ gorm.Plugin = new(gormQonvert)
// Option can be given to the New() method to tweak its behaviour
type Option func(like *gormQonvert)
// CharacterConfig must be provided to indicate when a field must be changed, these should all be prefixes
type CharacterConfig struct {
GreaterThanPrefix string
GreaterOrEqualToPrefix string
LessThanPrefix string
LessOrEqualToPrefix string
NotEqualToPrefix string
LikePrefix string
NotLikePrefix string
}
// SettingOnly makes it so that only queries with the setting 'gormQonvert' set to true can be turned into LIKE queries.
// This can be configured using db.Set("gormQonvert", true) on the query.
func SettingOnly() Option {
return func(like *gormQonvert) {
like.conditionalSetting = true
}
}
// New creates a new instance of the plugin that can be registered in gorm. Without any settings, all queries will be
// LIKE-d.
func New(config CharacterConfig, opts ...Option) gorm.Plugin {
plugin := &gormQonvert{config: config}
for _, opt := range opts {
opt(plugin)
}
return plugin
}
type gormQonvert struct {
conditionalSetting bool
config CharacterConfig
}
func (d *gormQonvert) Name() string {
return "gormQonvert"
}
func (d *gormQonvert) Initialize(db *gorm.DB) error {
return db.Callback().Query().Before("gorm:query").Register("gormQonvert:query", d.queryCallback)
}