This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyword_search.go
131 lines (110 loc) · 2.93 KB
/
keyword_search.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
// Copyright (c) KwanJunWen
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
package pgquery
import (
"encoding/json"
"errors"
"strings"
"github.com/go-pg/pg/v10/orm"
"github.com/go-pg/pg/v10/types"
)
// KeywordSearch keyword search common filter.
type KeywordSearch struct {
column string
caseInsensitive bool
matchAll bool
matchStart bool
matchEnd bool
Value *string `json:"value,omitempty"`
}
// UnmarshalJSON custom JSON unmarshaler.
func (f *KeywordSearch) UnmarshalJSON(b []byte) error {
type alias KeywordSearch
m1 := alias{}
var m2 *string
if err := json.Unmarshal(b, &m1); err == nil {
f.Value = m1.Value
return nil
}
if err := json.Unmarshal(b, &m2); err == nil {
f.Value = m2
return nil
}
return errors.New("[KeywordSearch]: unsupported format when unmarshalling json")
}
// MarshalJSON custom JSON marshaler.
func (f *KeywordSearch) MarshalJSON() ([]byte, error) {
return json.Marshal(f.Value)
}
// NewKeywordSearch initializes a new keyword search filter.
func NewKeywordSearch(column string) *KeywordSearch {
return &KeywordSearch{
column: column,
}
}
// Column set the column for the keyword search filter. Suffix column with ",array" to use array search.
func (f *KeywordSearch) Column(column string) *KeywordSearch {
f.column = column
return f
}
// CaseInsensitive set keyword case insensitive for the keyword search filter.
func (f *KeywordSearch) CaseInsensitive() *KeywordSearch {
f.caseInsensitive = true
return f
}
// MatchAll set keyword match all for the keyword search filter.
func (f *KeywordSearch) MatchAll() *KeywordSearch {
f.matchAll = true
return f
}
// MatchStart set keyword match start for the keyword search filter.
func (f *KeywordSearch) MatchStart() *KeywordSearch {
f.matchStart = true
return f
}
// MatchEnd set keyword match end for the keyword search filter.
func (f *KeywordSearch) MatchEnd() *KeywordSearch {
f.matchEnd = true
return f
}
// Keyword set value.
func (f *KeywordSearch) Keyword(keyword string) *KeywordSearch {
f.Value = &keyword
return f
}
func (f *KeywordSearch) buildValue() string {
var v string
if f.Value != nil {
v = *f.Value
}
if f.matchAll {
return v
}
if !f.matchStart {
v = "%" + v
}
if !f.matchEnd {
v += "%"
}
return v
}
func (f *KeywordSearch) buildLike() string {
if f.caseInsensitive {
return "ILIKE"
}
return "LIKE"
}
func (f *KeywordSearch) buildColumn(column string) interface{} {
if strings.HasSuffix(column, ",array") {
return orm.SafeQuery("array_to_string(?, ?)", types.Ident(strings.TrimSuffix(column, ",array")), ",")
}
return types.Ident(column)
}
// Appender returns parameters for cond appender.
func (f *KeywordSearch) Appender() (string, interface{}, interface{}, interface{}) {
v := f.buildValue()
column := f.buildColumn(f.column)
like := f.buildLike()
return "? ? ?", column, types.Safe(like), v
}