-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdt.go
More file actions
111 lines (94 loc) · 3.37 KB
/
Copy pathdt.go
File metadata and controls
111 lines (94 loc) · 3.37 KB
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
package gormdt
import (
"fmt"
"strings"
)
func FilterAndPaginate(param FilterAndPaginateParam) (DataTableResponse, error) {
var totalRecords int64
var filteredRecords int64
var data []map[string]interface{}
countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s", param.TableName)
err := param.DB.Raw(countQuery).Scan(&totalRecords).Error
if err != nil {
return DataTableResponse{}, err
}
searchCondition := fmt.Sprintf(" WHERE %s.deleted_at IS NULL", param.TableName)
if param.Request.Search != "" {
conditions := []string{}
for _, field := range param.Fields {
conditions = append(conditions, fmt.Sprintf("LOWER(%s) LIKE LOWER('%%%s%%')", field, strings.ToLower(param.Request.Search)))
}
searchCondition = searchCondition + " AND ( " + strings.Join(conditions, " OR ") + ")"
}
filteredCountQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s%s", param.TableName, searchCondition)
err = param.DB.Raw(filteredCountQuery).Scan(&filteredRecords).Error
if err != nil {
return DataTableResponse{}, err
}
dataQuery := fmt.Sprintf("SELECT * FROM %s%s LIMIT %d OFFSET %d", param.TableName, searchCondition, param.Request.Length, param.Request.Start)
err = param.DB.Raw(dataQuery).Scan(&data).Error
if err != nil {
return DataTableResponse{}, err
}
return DataTableResponse{
Draw: param.Request.Draw,
RecordsTotal: int(totalRecords),
RecordsFiltered: int(filteredRecords),
Data: convertMapToInterfaceSlice(data),
}, nil
}
func convertMapToInterfaceSlice(data []map[string]interface{}) []interface{} {
result := make([]interface{}, len(data))
for i, d := range data {
result[i] = d
}
return result
}
func FilterAndPaginateCustomQuery(param FilterAndPaginateCustomQueryParam) (DataTableResponse, error) {
var totalRecords int64
var filteredRecords int64
var data []map[string]interface{}
searchCondition := ""
if param.Where != "" {
searchCondition = " WHERE " + param.Where
}
if param.Request.Search != "" {
conditions := []string{}
for _, field := range param.Fields {
conditions = append(conditions, fmt.Sprintf("LOWER(%s) LIKE LOWER('%%%s%%')", field, strings.ToLower(param.Request.Search)))
}
if searchCondition != "" {
searchCondition += " AND (" + strings.Join(conditions, " OR ") + ")"
} else {
searchCondition = " WHERE " + strings.Join(conditions, " OR ")
}
}
if param.GroupBy != "" {
searchCondition += " GROUP BY " + param.GroupBy
}
if param.Having != "" {
searchCondition += " HAVING " + param.Having
}
countQuery := fmt.Sprintf("SELECT COUNT(*) FROM (%s%s) AS total", param.BaseQuery, searchCondition)
err := param.DB.Raw(countQuery).Scan(&totalRecords).Error
if err != nil {
return DataTableResponse{}, err
}
filteredCountQuery := fmt.Sprintf("SELECT COUNT(*) FROM (%s%s) AS filtered", param.BaseQuery, searchCondition)
err = param.DB.Raw(filteredCountQuery).Scan(&filteredRecords).Error
if err != nil {
return DataTableResponse{}, err
}
dataQuery := fmt.Sprintf("%s%s LIMIT %d OFFSET %d", param.BaseQuery, searchCondition, param.Request.Length, param.Request.Start)
fmt.Println("\n\n\n", dataQuery)
err = param.DB.Raw(dataQuery).Scan(&data).Error
if err != nil {
return DataTableResponse{}, err
}
return DataTableResponse{
Draw: param.Request.Draw,
RecordsTotal: int(totalRecords),
RecordsFiltered: int(filteredRecords),
Data: convertMapToInterfaceSlice(data),
}, nil
}