-
Notifications
You must be signed in to change notification settings - Fork 0
/
axl-sql.go
166 lines (150 loc) · 4.37 KB
/
axl-sql.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
163
164
165
166
package main
import (
log "github.com/sirupsen/logrus"
"regexp"
"strings"
)
const (
token01 = "TOKEN_01"
token02 = "TOKEN_02"
token03 = "TOKEN_03"
)
const SelectCompleteTable = `select eu.pkid as user_pkid,
d.pkid as device_pkid,
np.pkid as line_pkid,
eu.firstname,
eu.middlename,
eu.lastname,
eu.userid,
eu.department,
eu.status,
eu.islocaluser,
eunp.uccx,
eu.directoryuri,
eu.mailid,
d.name as devicename,
d.description as devicedescrition,
np.dnorpattern,
np.alertingnameascii,
(select paramvalue from processconfig where paramname = 'ClusterID') as cluster_name,
np.description as line_description
from enduser eu
LEFT OUTER JOIN (SELECT fkenduser, max(CASE tkdnusage WHEN 2 THEN tkdnusage ELSE null END) is not null AS uccx
FROM endusernumplanmap
GROUP BY fkenduser
) AS eunp ON eunp.fkenduser = eu.pkid
INNER JOIN enduserdevicemap eudm ON eudm.fkenduser = eu.pkid
INNER JOIN device d ON d.pkid = eudm.fkdevice
INNER JOIN devicenumplanmap dnpm ON d.pkid = dnpm.fkdevice
INNER JOIN numplan np ON np.pkid = dnpm.fknumplan
WHERE d.pkid IN (
select fkdevice
from applicationuserdevicemap
where fkapplicationuser in (select au.pkid from applicationuser au where lower(name) in (` + token01 + `))
union
select fkdevice
from enduserdevicemap
where fkenduser in (select au.pkid from enduser au where lower(userid) in (` + token01 + `))
)
ORDER BY eu.pkid, d.pkid, np.pkid`
const SelectLoginUsers = `select enduser.pkid as user_pkid,
enduser.firstname,
enduser.middlename,
enduser.lastname,
enduser.userid,
enduser.department,
enduser.status,
enduser.islocaluser,
eunp.uccx,
enduser.directoryuri,
enduser.mailid,
(select paramvalue from processconfig where paramname = 'ClusterID') as cluster_name
from enduser
LEFT OUTER JOIN (SELECT fkenduser, max(CASE tkdnusage WHEN 2 THEN tkdnusage ELSE null END) is not null AS uccx
FROM endusernumplanmap
GROUP BY fkenduser
) AS eunp ON eunp.fkenduser = enduser.pkid
where enduser.pkid in (
select e.fkenduser
from enduserdirgroupmap as e
inner join dirgroup as dg on e.fkdirgroup = dg.pkid
where dg.name = '` + token01 + `')`
const SelectCompleteTableMax = "select * from device"
var tokens = []string{token01, token02, token03}
type ApiSqlBody interface {
sqlParameterClean(data string) string
addParameter(data string) int
ToString() string
IsParametersValid() bool
}
type SqlBody struct {
id string
sql string
parameter []string
needParams int
}
func NewUserDeviceLineSql(users []string) *SqlBody {
return stringListParameterSql(users, SelectCompleteTable)
}
func NewLoginUserSql(accessGroup string) *SqlBody {
n := newSqlBody(SelectLoginUsers, 1)
n.addParameter(accessGroup)
return n
}
func stringListParameterSql(devices []string, sql string) *SqlBody {
listId := ""
addComa := ""
for _, r := range devices {
listId += addComa + "'" + r + "'"
addComa = ","
}
n := newSqlBody(sql, 1)
n.addParameter(listId)
return n
}
func newSqlBody(sql string, parameters int) *SqlBody {
a := RandomString()
log.WithField("id", a).Tracef("Prepare SQL request with %d parameters", parameters)
return &SqlBody{
id: a,
sql: sql,
parameter: nil,
needParams: parameters,
}
}
func (a *SqlBody) sqlParameterClean(data string) string {
invalid := []string{"%", "*", "?", ";"}
if data == "" {
return ""
}
for _, s := range invalid {
data = strings.ReplaceAll(data, s, " ")
}
return data
}
func (a *SqlBody) IsParametersValid() bool {
var re = regexp.MustCompile(`(?mi)[\s;]+(insert|update|select|delete)[\s;]+`)
ret := true
for _, s := range a.parameter {
match := re.MatchString(s)
ret = ret && !match
}
return ret
}
func (a *SqlBody) addParameter(data string) int {
data = a.sqlParameterClean(data)
if data != "" {
a.parameter = append(a.parameter, data)
}
return len(a.parameter)
}
func (a *SqlBody) ToString() string {
if a.needParams > len(a.parameter) || !a.IsParametersValid() {
return ""
}
data := a.sql
for i, parameter := range a.parameter {
data = strings.ReplaceAll(data, tokens[i], parameter)
}
return data
}