-
Notifications
You must be signed in to change notification settings - Fork 115
/
searching.go
58 lines (54 loc) · 1.36 KB
/
searching.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
package gorm
import (
"reflect"
"time"
)
// GetFullTextSearchDBMask ...
func GetFullTextSearchDBMask(object interface{}, fields []string, separator string) string {
mask := ""
objectVal := indirectValue(reflect.ValueOf(object))
if objectVal.Kind() != reflect.Struct {
return mask
}
fieldsSize := len(fields)
for i, fieldName := range fields {
fieldVal := objectVal.FieldByName(camelCase(fieldName))
if !fieldVal.IsValid() {
continue
}
underlyingVal := indirectValue(fieldVal)
if !underlyingVal.IsValid() {
switch fieldVal.Interface().(type) {
case *time.Time:
underlyingVal = fieldVal
default:
continue
}
}
switch underlyingVal.Interface().(type) {
case int32:
mask += fieldName
case string:
mask += fieldName
mask += " || '" + separator + "' || "
mask += "replace(" + fieldName + ", '@', ' ')"
mask += " || '" + separator + "' || "
mask += "replace(" + fieldName + ", '.', ' ')"
case *time.Time:
mask += "coalesce(to_char(" + fieldName + ", 'MM/DD/YY HH:MI pm'), '')"
case bool:
mask += fieldName
default:
continue
}
if i != fieldsSize-1 {
mask += " || '" + separator + "' || "
}
}
return mask
}
// FormFullTextSearchQuery ...
func FormFullTextSearchQuery(mask string) string {
fullTextSearchQuery := "to_tsvector('simple', " + mask + ") @@ to_tsquery('simple', ?)"
return fullTextSearchQuery
}