-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGORMAdapter.go
118 lines (104 loc) · 3.95 KB
/
GORMAdapter.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
package query_parser_to_db
import "gorm.io/gorm"
var gormDBOperations = DBOperations{
"equal": func(fieldName, value string, q interface{}, r QueryInterface) (interface{}, error) {
query := q.(*gorm.DB)
query = query.Where(fieldName+" = ?", value)
return query, nil
},
"not-equal": func(fieldName, value string, q interface{}, r QueryInterface) (interface{}, error) {
query := q.(*gorm.DB)
query = query.Where(fieldName+" != ?", value)
return query, nil
},
"is-null": func(fieldName, value string, q interface{}, r QueryInterface) (interface{}, error) {
query := q.(*gorm.DB)
query = query.Where(fieldName + " IS NULL")
return query, nil
},
"is-not-null": func(fieldName, value string, q interface{}, r QueryInterface) (interface{}, error) {
query := q.(*gorm.DB)
query = query.Where(fieldName + " IS NOT NULL")
return query, nil
},
"starts-with": func(fieldName, value string, q interface{}, r QueryInterface) (interface{}, error) {
query := q.(*gorm.DB)
query = query.Where(fieldName+" LIKE ?", value+"%")
return query, nil
},
"not-starts-with": func(fieldName, value string, q interface{}, r QueryInterface) (interface{}, error) {
query := q.(*gorm.DB)
query = query.Where(fieldName+" NOT LIKE ?", value+"%")
return query, nil
},
"ends-with": func(fieldName, value string, q interface{}, r QueryInterface) (interface{}, error) {
query := q.(*gorm.DB)
query = query.Where(fieldName+" LIKE ?", "%"+value)
return query, nil
},
"not-ends-with": func(fieldName, value string, q interface{}, r QueryInterface) (interface{}, error) {
query := q.(*gorm.DB)
query = query.Where(fieldName+" NOT LIKE ?", "%"+value)
return query, nil
},
"contains": func(fieldName, value string, q interface{}, r QueryInterface) (interface{}, error) {
query := q.(*gorm.DB)
query = query.Where(fieldName+" LIKE ?", "%"+value+"%")
return query, nil
},
"not-contains": func(fieldName, value string, q interface{}, r QueryInterface) (interface{}, error) {
query := q.(*gorm.DB)
query = query.Where(fieldName+" NOT LIKE ?", "%"+value+"%")
return query, nil
},
}
func NewGORMDBAdapter() DBAdapter {
GORMDBAdapter = DBAdapter{
"default": {
"equal": gormDBOperations["equal"],
"not-equal": gormDBOperations["not-equal"],
"is-null": gormDBOperations["is-null"],
"is-not-null": gormDBOperations["is-not-null"],
},
"string": {
"equal": gormDBOperations["equal"],
"not-equal": gormDBOperations["not-equal"],
"is-null": gormDBOperations["is-null"],
"is-not-null": gormDBOperations["is-not-null"],
"starts-with": gormDBOperations["starts-with"],
"not-starts-with": gormDBOperations["not-starts-with"],
"ends-with": gormDBOperations["ends-with"],
"not-ends-with": gormDBOperations["not-ends-with"],
"contains": gormDBOperations["contains"],
"not-contains": gormDBOperations["not-contains"],
},
"bool": {
"equal": gormDBOperations["equal"],
"not-equal": gormDBOperations["not-equal"],
"is-null": gormDBOperations["is-null"],
"is-not-null": gormDBOperations["is-not-null"],
},
"number": {
"equal": gormDBOperations["equal"],
"not-equal": gormDBOperations["not-equal"],
"is-null": gormDBOperations["is-null"],
"is-not-null": gormDBOperations["is-not-null"],
},
"pagination": {
"pager": func(fieldName, value string, dbQuery interface{}, r QueryInterface) (interface{}, error) {
query := dbQuery.(*gorm.DB)
query = query.Limit(int(r.GetLimit())).Offset(r.GetOffset())
return query, nil
},
},
}
// text and blob here will have same operations like string:
GORMDBAdapter["text"] = GORMDBAdapter["string"]
GORMDBAdapter["blob"] = GORMDBAdapter["string"]
// TODO! add a bette support for JSON
GORMDBAdapter["json"] = GORMDBAdapter["default"]
// Date for all date like formats:
GORMDBAdapter["time"] = GORMDBAdapter["date"]
GORMDBAdapter["dateOnly"] = GORMDBAdapter["date"]
return GORMDBAdapter
}