-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-repo-logic.go
162 lines (136 loc) · 3.02 KB
/
db-repo-logic.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (c) 2024.
* Created by Andy Pangaribuan <https://github.com/apangaribuan>.
*
* This product is protected by copyright and distributed under
* licenses restricting copying, distribution and decompilation.
* All Rights Reserved.
*/
package db
import (
"strings"
"github.com/andypangaribuan/gmod/clog"
)
func (*stuRepo[T]) generateArgSign(column string) (argSign string) {
if column != "" {
ls := strings.Split(column, ",")
for i := range ls {
if i > 0 {
argSign += ","
}
argSign += " ?"
}
argSign = strings.TrimSpace(argSign)
}
return
}
func (*stuRepo[T]) formatInsertColumnArgs(val string) string {
var (
maxOneLine = 5
fourSpace = " "
formatted = ""
count = 0
ls = strings.Split(val, ",")
)
for i, v := range ls {
if count >= maxOneLine {
count = 0
}
count++
if count == 1 {
if i > 0 {
formatted += "\n"
}
formatted += fourSpace
}
formatted += strings.TrimSpace(v)
if i < len(ls)-1 {
formatted += ","
if count < maxOneLine {
formatted += " "
}
}
}
return formatted
}
func (slf *stuRepo[T]) getWhereQuery(condition string, args []any) string {
condition = strings.TrimSpace(condition)
var (
whereQuery = ""
withDeletedAtIsNull = slf.isWithDeletedAtIsNull(args)
)
if withDeletedAtIsNull {
whereQuery = "WHERE deleted_at IS NULL"
if condition != "" {
whereQuery += " AND " + condition
}
} else if condition != "" {
whereQuery = "WHERE " + condition
}
return whereQuery
}
func (slf *stuRepo[T]) isWithDeletedAtIsNull(args []any) bool {
isWith := slf.withDeletedAtIsNull
for _, arg := range args {
switch val := arg.(type) {
case FetchOptBuilder:
v, ok := val.(*stuFetchOptBuilder)
if ok && v != nil && v.withDeletedAtIsNull != nil {
isWith = *v.withDeletedAtIsNull
}
}
}
return isWith
}
func (slf *stuRepo[T]) getQuery(typ string, args []any) string {
query := ""
for _, arg := range args {
switch val := arg.(type) {
case FetchOptBuilder:
v, ok := val.(*stuFetchOptBuilder)
if ok && v != nil {
switch typ {
case "end-query":
if v.endQuery != nil {
if query != "" {
query += " "
}
query += strings.TrimSpace(*v.endQuery)
}
case "full-query":
if v.fullQuery != nil {
if query != "" {
query += " "
}
query += strings.TrimSpace(*v.fullQuery)
}
}
}
}
}
return strings.TrimSpace(query)
}
func (slf *stuRepo[T]) getArgs(args []any) []any {
filtered := make([]any, 0)
for _, arg := range args {
switch arg.(type) {
case FetchOptBuilder:
continue
default:
filtered = append(filtered, arg)
}
}
return filtered
}
func (slf *stuRepo[T]) result(report *stuReport, err error, id *int64, entities []*T) *stuRepoResult[T] {
return &stuRepoResult[T]{
report: report,
err: err,
id: id,
entities: entities,
}
}
func (slf *stuRepo[T]) override(clog clog.Instance, res *stuRepoResult[T]) *stuRepoResult[T] {
pushClogReport(clog, res.report, res.err, 4)
return res
}