-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathexport.js
214 lines (195 loc) · 5.82 KB
/
export.js
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
import SqlStringOrig from "sqlstring";
export const SqlString = SqlStringOrig;
SqlString.trim = (val) => {
if (val?.charAt(0) == "'")
return val.substring(1, val.length-1);
else
return val;
};
SqlString.unescapeLike = (val, sqlDialect = undefined) => {
if (typeof val !== "string") {
return val;
}
let res = val;
// unescape % and _
if (sqlDialect === "BigQuery") {
// https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#like_operator
res = res.replace(/\\\\([%_])/g, "$1");
} else {
res = res.replace(/\\([%_])/g, "$1");
}
return res;
};
SqlString.escapeLike = (val, any_start = true, any_end = true, sqlDialect = undefined) => {
if (typeof val !== "string") {
return val;
}
// normal escape
let res = SqlString.escape(val);
// unwrap ''
res = SqlString.trim(res);
// escape % and _
if (sqlDialect === "BigQuery") {
// https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#like_operator
res = res.replace(/[%_\\]/g, "\\\\$&");
} else {
res = res.replace(/[%_]/g, "\\$&");
}
// wrap with % for LIKE
res = (any_start ? "%" : "") + res + (any_end ? "%" : "");
// wrap ''
res = "'" + res + "'";
return res;
};
export const sqlEmptyValue = (fieldDef) => {
let v = "''";
const type = fieldDef?.type;
if (type == "date") {
//todo: support other SQL dialects? 0001-01-01 for oracle, 1970-01-01 for timestamp
v = "'0000-00-00'";
} else if (type == "datetime") {
v = "'0000-00-00 00:00'";
} else if (type == "time") {
v = "'00:00'";
} else if (type == "number") {
v = "0";
}
return v;
};
export const mongoEmptyValue = (fieldDef) => {
let v = "";
const type = fieldDef?.type;
if (type == "number") {
v = 0;
}
return v;
};
const spelEscapeString = (val) => {
// Strings are delimited by single quotes. To put a single quote itself in a string, use two single quote characters.
return "'" + val.replace(/'/g, "''") + "'";
};
const spelInlineList = (vals, toArray = false) => {
// find java type of values
let javaType;
let jt;
const numberJavaTypes = ["int", "float"];
vals.map(v => {
if (v !== undefined && v !== null) {
if (typeof v === "string") {
jt = "String";
} else if (typeof v === "number") {
jt = Number.isInteger(v) ? "int" : "float";
} else throw new Error(`spelEscape: Can't use value ${v} in array`);
if (!javaType) {
javaType = jt;
} else if (javaType != jt) {
if (numberJavaTypes.includes(javaType) && numberJavaTypes.includes(jt)) {
// found int and float in collecton - use float
javaType = "float";
} else throw new Error(`spelEscape: Can't use different types in array: found ${javaType} and ${jt}`);
}
}
});
if (!javaType) {
javaType = "String"; //default if empty array
}
// for floats we should add 'f' to all items
let escapedVals;
if (javaType == "float") {
escapedVals = vals.map(v => spelEscape(v, true));
} else {
escapedVals = vals.map(v => spelEscape(v));
}
// build inline list or array
let res;
if (toArray) {
res = `new ${javaType}[]{${escapedVals.join(", ")}}`;
} else {
res = `{${escapedVals.join(", ")}}`;
}
return res;
};
export const spelFixList = (val) => {
// `{1,2}.contains(1)` NOT works
// `{1,2}.?[true].contains(1)` works
return `${val}.?[true]`;
};
export const spelEscape = (val, numberToFloat = false, arrayToArray = false) => {
// https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/expressions.html#expressions-ref-literal
if (val === undefined || val === null) {
return "null";
}
switch (typeof val) {
case "boolean":
return (val) ? "true" : "false";
case "number":
if (!Number.isFinite(val) || isNaN(val))
return undefined;
return val + (!Number.isInteger(val) || numberToFloat ? "f" : "");
case "object":
if (Array.isArray(val)) {
return spelInlineList(val, arrayToArray);
} else {
// see `spelFormatValue` for Date, LocalTime
throw new Error("spelEscape: Object is not supported");
}
default: return spelEscapeString(val);
}
};
export const spelFormatConcat = (parts) => {
if (parts && Array.isArray(parts) && parts.length) {
return parts
.map(part => {
if (part.type == "const") {
return spelEscape(part.value);
} else if (part.type == "property") {
return ""+part.value;
} else if (part.type == "variable") {
return "#"+part.value;
} return undefined;
})
.filter(r => r != undefined)
.join(" + ");
} else {
return "null";
}
};
// `val` is {value, valueType, valueSrc}
// If `valueType` == "case_value", `value` is array of such items (to be considered as concatenation)
export const spelImportConcat = (val) => {
if (val == undefined)
return [undefined, []];
let errors = [];
const value = val.valueType == "case_value" ? val.value : val;
const valueArr = Array.isArray(value) ? value : [value];
const res = valueArr.map(child => {
if (child.valueSrc === "value") {
if (child.value === null) {
return undefined;
} else {
return {
type: "const",
value: child.value
};
}
} else if (child.valueSrc === "field") {
return {
type: (child.isVariable ? "variable" : "property"),
value: child.value
};
} else {
errors.push(`Unsupported valueSrc ${child.valueSrc} in concatenation`);
}
}).filter(v => v != undefined);
return [res, errors];
};
export const stringifyForDisplay = (v) => (v == null ? "NULL" : v.toString());
export const wrapWithBrackets = (v) => {
if (v == undefined)
return v;
if (v?.[0] === "(" && v?.[v?.length - 1] === ")") {
// already wrapped
return v;
}
return "(" + v + ")";
};