Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

# -*- coding: utf-8 -*- 

# Copyright: (c) 2021, XLAB Steampunk <steampunk@xlab.si> 

# 

# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 

 

from __future__ import absolute_import, division, print_function 

 

__metaclass__ = type 

 

 

# https://docs.servicenow.com/bundle/quebec-platform-user-interface/page/use/common-ui-elements/reference/r_OpAvailableFiltersQueries.html 

OPERATORS_STRING = set( 

( 

"STARTSWITH", 

"%", 

"ENDSWITH", 

"*", 

"LIKE", 

"!*", 

"NOT LIKE", # Space added based on example query in the upstream docs 

"=", 

"!=", 

"ISEMPTY", 

"ISNOTEMPTY", 

"ANYTHING", 

"EMPTYSTRING", 

"<=", 

">=", 

"BETWEEN", 

"SAMEAS", 

"NSAMEAS", 

) 

) 

OPERATORS_REFERENCE = set( 

( 

"=", 

"!=", 

"ISEMPTY", 

"ISNOTEMPTY", 

"STARTSWITH", 

"%", 

"ENDSWITH", 

"*", 

"LIKE", 

"!*", 

"NOT LIKE", # Space added based on example query in the upstream docs 

"ANYTHING", 

"SAMEAS", 

"NSAMEAS", 

"EMPTYSTRING", 

"DYNAMIC", 

) 

) 

OPERATORS_CHOICE_STRINGS = set( 

( 

"=", 

"!=", 

"IN", 

"NOT IN", 

"LIKE", 

"STARTSWITH", 

"%", 

"ENDSWITH", 

"NOT LIKE", 

"ANYTHING", 

"SAMEAS", 

"NSAMEAS", 

) 

) 

OPERATORS_CHOICE_INTEGERS = set( 

( 

"=", 

"!=", 

"IN", 

"NOT IN", 

"EMPTY", 

"NOTEMPTY", 

"<", 

">", 

"<=", 

">=", 

"BETWEEN", 

"ANYTHING", 

"SAMEAS", 

"NSAMEAS", 

) 

) 

OPERATORS_DATE_TIME = set( 

( 

"ONToday", 

"NOTONToday", 

"<", 

"<=", 

">", 

">=", 

"BETWEEN", 

"DATEPART", 

"RELATIVEGE", 

"RELATIVELE", 

"RELATIVEGT", 

"RELATIVELT", 

"RELATIVEEE", 

"ISEMPTY", 

"ISNOTEMPTY", 

"ANYTHING", 

"SAMEAS", 

"NSAMEAS", 

"MORETHAN", 

"LESSTHAN", 

) 

) 

OPERATORS_NUMERIC = set( 

( 

"=", 

"!=", 

"ISEMPTY", # EMPTY changed to ISEMPTY based on example query in the upstream docs 

"ISNOTEMPTY", # NOTEMPTY changed to ISNOTEMPTY based on example query in the upstream docs 

"<", 

">", 

"<=", 

">=", 

"BETWEEN", 

"ANYTHING", 

"SAMEAS", 

"NSAMEAS", 

"GT_FIELD", 

"LT_FIELD", 

"GT_OR_EQUALS_FIELD", 

"LT_OR_EQUALS_FIELD", 

) 

) 

OPERATORS_BOOLEAN = set( 

( 

"=", 

"!=", 

"ISEMPTY", 

"ISNOTEMPTY", 

"ANYTHING", 

"SAMEAS", 

"NSAMEAS", 

) 

) 

OPERATORS_EMAIL = set(("VALCHANGES", "CHANGESFROM", "CHANGESTO")) 

 

UNARY_OPERATORS = set( 

( 

"ISEMPTY", 

"ISNOTEMPTY", 

"ANYTHING", 

"EMPTYSTRING", 

"ONToday", 

"NOTONToday", 

"VALCHANGES", 

) 

) 

 

OPERATORS = ( 

OPERATORS_STRING 

| OPERATORS_REFERENCE 

| OPERATORS_CHOICE_STRINGS 

| OPERATORS_CHOICE_INTEGERS 

| OPERATORS_DATE_TIME 

| OPERATORS_NUMERIC 

| OPERATORS_BOOLEAN 

| OPERATORS_EMAIL 

) 

 

 

def get_operator_and_value(condition): 

# Return operator and value 

for o in OPERATORS: 

if condition == o: 

return (o, "") 

 

if condition.startswith(o) and condition[len(o)] == " ": 

new_len = len(o) + 1 

return (o, condition[new_len:]) 

 

return None, None 

 

 

def parse_query(query): 

# input: list({"caller": "= abel.tuter", "state": "= new"}, {"short_description": "LIKE SAP"}) 

# output: list({"caller": ("=", "able.tuter"), "state": ("=", "new")}, {"short_description": ("LIKE", "SAP")}}) 

parsed_query, errors = [], [] 

 

for old_subquery in query: 

new_subquery = dict() 

for column, condition in old_subquery.items(): 

oper, field = get_operator_and_value(condition) 

 

if not oper: 

errors.append( 

"Invalid condition '{0}' for column '{1}'.".format( 

condition, column 

) 

) 

continue 

 

if oper in UNARY_OPERATORS and field: 

errors.append("Operator {0} does not take any arguments".format(oper)) 

continue 

 

new_subquery[column] = (oper, field) 

 

if new_subquery: 

parsed_query.append(new_subquery) 

 

return parsed_query, errors 

 

 

def serialize_query(query): 

# input: list({"caller_id": ("=", "712083754987"), "state": ("=", "3")}, {}) 

# output: "caller_id=712083754987^state=3" 

subqueries = [] 

 

for subquery in query: 

conditions = [] 

for column, (operator, value) in subquery.items(): 

conditions.append(column + operator + value) 

# isert AND between items 

subqueries.append("^".join(conditions)) 

 

# insert OR between items 

return "^NQ".join(subqueries) 

 

 

def map_query_values(query, mapper): 

for subquery in query: 

for_lut = dict((key, value[1]) for (key, value) in subquery.items()) 

lut = mapper.to_snow(for_lut) 

 

for k, v in lut.items(): 

subquery[k] = (subquery[k][0], v) 

 

return query