baja-lite 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/boot-remote.d.ts +2 -0
- package/cjs/boot-remote.js +35 -0
- package/cjs/boot.d.ts +2 -0
- package/cjs/boot.js +152 -0
- package/cjs/code.d.ts +1 -0
- package/cjs/code.js +359 -1
- package/cjs/convert-xml.d.ts +10 -0
- package/cjs/convert-xml.js +414 -0
- package/cjs/enum.d.ts +10 -0
- package/cjs/enum.js +32 -0
- package/cjs/error.js +1 -1
- package/cjs/fn.js +3 -3
- package/cjs/index.d.ts +3 -0
- package/cjs/index.js +3 -0
- package/cjs/list.d.ts +10 -0
- package/cjs/list.js +36 -0
- package/cjs/object.d.ts +7 -1
- package/cjs/object.js +36 -2
- package/cjs/set-ex.d.ts +41 -15
- package/cjs/set-ex.js +68 -52
- package/cjs/sql.d.ts +760 -305
- package/cjs/sql.js +1702 -1041
- package/cjs/sqlite.d.ts +38 -0
- package/cjs/sqlite.js +194 -0
- package/cjs/test-mysql.d.ts +2 -1
- package/cjs/test-mysql.js +85 -63
- package/cjs/test-sqlite.d.ts +1 -1
- package/cjs/test-sqlite.js +3 -1
- package/cjs/test-xml.d.ts +1 -0
- package/cjs/test-xml.js +75 -0
- package/es/boot-remote.d.ts +2 -0
- package/es/boot-remote.js +31 -0
- package/es/boot.d.ts +2 -0
- package/es/boot.js +125 -0
- package/es/code.d.ts +1 -0
- package/es/code.js +355 -2
- package/es/convert-xml.d.ts +10 -0
- package/es/convert-xml.js +410 -0
- package/es/enum.d.ts +10 -0
- package/es/enum.js +28 -0
- package/es/error.js +1 -1
- package/es/index.d.ts +3 -0
- package/es/index.js +3 -0
- package/es/list.d.ts +10 -0
- package/es/list.js +32 -0
- package/es/object.d.ts +7 -1
- package/es/object.js +28 -1
- package/es/set-ex.d.ts +41 -15
- package/es/set-ex.js +68 -52
- package/es/sql.d.ts +760 -305
- package/es/sql.js +1573 -917
- package/es/sqlite.d.ts +38 -0
- package/es/sqlite.js +164 -0
- package/es/test-mysql.d.ts +2 -1
- package/es/test-mysql.js +85 -64
- package/es/test-sqlite.d.ts +1 -1
- package/es/test-sqlite.js +3 -1
- package/es/test-xml.d.ts +1 -0
- package/es/test-xml.js +70 -0
- package/package.json +15 -10
- package/src/boot-remote.ts +31 -0
- package/src/boot.ts +129 -0
- package/src/code.ts +342 -1
- package/src/convert-xml.ts +462 -0
- package/src/enum.ts +31 -0
- package/src/error.ts +1 -1
- package/src/index.ts +4 -1
- package/src/list.ts +31 -0
- package/src/object.ts +48 -14
- package/src/set-ex.ts +91 -70
- package/src/sql.ts +1652 -965
- package/src/sqlite.ts +161 -0
- package/src/test-mysql.ts +93 -65
- package/src/test-sqlite.ts +3 -1
- package/src/test-xml.ts +70 -0
- package/cjs/constant.d.ts +0 -13
- package/cjs/constant.js +0 -19
- package/cjs/redis.d.ts +0 -0
- package/cjs/redis.js +0 -1
- package/es/constant.d.ts +0 -13
- package/es/constant.js +0 -16
- package/es/redis.d.ts +0 -0
- package/es/redis.js +0 -1
- package/src/constant.ts +0 -14
- package/src/redis.ts +0 -0
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
export interface XML {
|
|
2
|
+
type: 'tag' | 'text';
|
|
3
|
+
name: string;
|
|
4
|
+
id?: string;
|
|
5
|
+
voidElement: boolean;
|
|
6
|
+
attrs: Record<string, string>;
|
|
7
|
+
children: XML[];
|
|
8
|
+
content: string;
|
|
9
|
+
}
|
|
10
|
+
export const convert = function (childrens: XML[], param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
11
|
+
let statement = '';
|
|
12
|
+
for (let i = 0, children: XML; children = childrens[i]!; i++) {
|
|
13
|
+
// Convert SQL statement recursively
|
|
14
|
+
statement += convertChildren(children, param, parentIds, myBatisMapper);
|
|
15
|
+
}
|
|
16
|
+
// Check not converted Parameters
|
|
17
|
+
var regexList = ['\\#{\\S*}', '\\${\\S*}'];
|
|
18
|
+
for (var i = 0, regexString: string; regexString = regexList[i]!; i++) {
|
|
19
|
+
var regex = new RegExp(regex, 'g');
|
|
20
|
+
var checkParam = statement.match(regexString);
|
|
21
|
+
|
|
22
|
+
if (checkParam != null && checkParam.length > 0) {
|
|
23
|
+
throw new Error("Parameter " + checkParam.join(",") + " is not converted.");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return statement;
|
|
27
|
+
}
|
|
28
|
+
const convertChildren = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
29
|
+
param ??= {};
|
|
30
|
+
if (!isDict(param)) {
|
|
31
|
+
throw new Error('Parameter argument should be Key-Value type or Null.');
|
|
32
|
+
}
|
|
33
|
+
if (children.type == 'text') {
|
|
34
|
+
// Convert Parameters
|
|
35
|
+
return convertParameters(children, param);
|
|
36
|
+
|
|
37
|
+
} else if (children.type == 'tag') {
|
|
38
|
+
switch (children.name.toLowerCase()) {
|
|
39
|
+
case 'if':
|
|
40
|
+
return convertIf(children, param, parentIds, myBatisMapper);
|
|
41
|
+
case 'choose':
|
|
42
|
+
return convertChoose(children, param, parentIds, myBatisMapper);
|
|
43
|
+
case 'trim':
|
|
44
|
+
case 'where':
|
|
45
|
+
return convertTrimWhere(children, param, parentIds, myBatisMapper);
|
|
46
|
+
case 'set':
|
|
47
|
+
return convertSet(children, param, parentIds, myBatisMapper);
|
|
48
|
+
case 'foreach':
|
|
49
|
+
return convertForeach(children, param, parentIds, myBatisMapper);
|
|
50
|
+
case 'bind':
|
|
51
|
+
param = convertBind(children, param);
|
|
52
|
+
return '';
|
|
53
|
+
case 'include':
|
|
54
|
+
return convertInclude(children, param, parentIds, myBatisMapper);
|
|
55
|
+
default:
|
|
56
|
+
throw new Error('XML is not well-formed character or markup. Consider using CDATA section.');
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
return '';
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const convertParameters = function (children: XML, param: Record<string, any>) {
|
|
64
|
+
let convertString = children.content;
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
convertString = convertParametersInner('#', convertString, param);
|
|
68
|
+
convertString = convertParametersInner('$', convertString, param);
|
|
69
|
+
} catch (err) {
|
|
70
|
+
throw new Error('Error occurred during convert parameters.');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
// convert CDATA string
|
|
75
|
+
convertString = convertString.replace(/(\&\;)/g, '&');
|
|
76
|
+
convertString = convertString.replace(/(\<\;)/g, '<');
|
|
77
|
+
convertString = convertString.replace(/(\>\;)/g, '>');
|
|
78
|
+
convertString = convertString.replace(/(\"\;)/g, '"');
|
|
79
|
+
} catch (err) {
|
|
80
|
+
throw new Error('Error occurred during convert CDATA section.');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return convertString;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const isObject = function (variable: any) {
|
|
87
|
+
return typeof variable === 'object' && variable !== null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const isArray = function (variable: any) {
|
|
91
|
+
return isObject(variable) && variable.hasOwnProperty('length');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const convertParametersInner = function (change: string, convertString: string, param: Record<string, any>) {
|
|
95
|
+
const stringReg = new RegExp('(\\' + change + '\\{[a-zA-Z0-9._\\$]+\\})', 'g');
|
|
96
|
+
let stringTarget = convertString.match(stringReg);
|
|
97
|
+
|
|
98
|
+
if (stringTarget != null && stringTarget.length > 0) {
|
|
99
|
+
const _stringTarget = uniqueArray(stringTarget);
|
|
100
|
+
|
|
101
|
+
let target: string | undefined;
|
|
102
|
+
for (let i = 0; i < _stringTarget.length; i++) {
|
|
103
|
+
target = _stringTarget[i];
|
|
104
|
+
const t = target!.replace(change + '{', '').replace('}', '');
|
|
105
|
+
let tempParamKey = eval('param.' + t);
|
|
106
|
+
|
|
107
|
+
if (tempParamKey !== undefined) {
|
|
108
|
+
const reg = new RegExp('\\' + change + '{' + t + '}', 'g');
|
|
109
|
+
|
|
110
|
+
if (tempParamKey === null) {
|
|
111
|
+
tempParamKey = 'NULL';
|
|
112
|
+
convertString = convertString.replace(reg, tempParamKey);
|
|
113
|
+
} else {
|
|
114
|
+
if (change == '#') {
|
|
115
|
+
// processing JSON fields structures
|
|
116
|
+
if (isObject(tempParamKey) || isArray(tempParamKey)) {
|
|
117
|
+
tempParamKey = JSON.stringify(tempParamKey);
|
|
118
|
+
} else {
|
|
119
|
+
tempParamKey = tempParamKey.toString().replace(/"/g, '\\\"');
|
|
120
|
+
tempParamKey = mysqlRealEscapeParam(tempParamKey);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
tempParamKey = tempParamKey.replace(/'/g, "''");
|
|
124
|
+
const replaceWith = "'" + tempParamKey + "'"
|
|
125
|
+
convertString = convertString.replace(reg, () => replaceWith);
|
|
126
|
+
} else if (change == '$') {
|
|
127
|
+
convertString = convertString.replace(reg, tempParamKey);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return convertString;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const convertIf = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
137
|
+
let evalString = children.attrs['test']!;
|
|
138
|
+
try {
|
|
139
|
+
// Create Evaluate string
|
|
140
|
+
evalString = replaceEvalString(evalString, param);
|
|
141
|
+
|
|
142
|
+
evalString = evalString.replace(/ and /gi, ' && ');
|
|
143
|
+
evalString = evalString.replace(/ or /gi, ' || ');
|
|
144
|
+
|
|
145
|
+
// replace == to === for strict evaluate
|
|
146
|
+
// TODO: fix != null & != ''
|
|
147
|
+
// evalString = evalString.replace(/==/g, '===');
|
|
148
|
+
// evalString = evalString.replace(/!=/g, '!==');
|
|
149
|
+
|
|
150
|
+
evalString = evalString.replace(/^'(.*?)'\.equalsIgnoreCase\( ([a-zA-Z]+\.[a-zA-Z]+) \)/i, `($2 && $2.toUpperCase() === '$1'.toUpperCase())`);
|
|
151
|
+
evalString = evalString.replace(/\('(.*?)'\.equalsIgnoreCase\( ([a-zA-Z]+\.[a-zA-Z]+) \)/i, `(($2 && $2.toUpperCase() === '$1'.toUpperCase())`);
|
|
152
|
+
|
|
153
|
+
} catch (err) {
|
|
154
|
+
throw new Error('Error occurred during convert <if> element.');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Execute Evaluate string
|
|
158
|
+
try {
|
|
159
|
+
if (eval(evalString)) {
|
|
160
|
+
let convertString = '';
|
|
161
|
+
for (let i = 0, nextChildren: XML; nextChildren = children['children'][i]!; i++) {
|
|
162
|
+
convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
|
|
163
|
+
}
|
|
164
|
+
return convertString;
|
|
165
|
+
|
|
166
|
+
} else {
|
|
167
|
+
return '';
|
|
168
|
+
}
|
|
169
|
+
} catch (e) {
|
|
170
|
+
return '';
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const convertForeach = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
175
|
+
try {
|
|
176
|
+
const collection = eval('param.' + children.attrs['collection']);
|
|
177
|
+
const item = children.attrs['item']!;
|
|
178
|
+
const open = (children.attrs['open'] == null) ? '' : children.attrs['open'];
|
|
179
|
+
const close = (children.attrs['close'] == null) ? '' : children.attrs['close'];
|
|
180
|
+
const separator = (children.attrs['separator'] == null) ? '' : children.attrs['separator'];
|
|
181
|
+
|
|
182
|
+
const foreachTexts: string[] = [];
|
|
183
|
+
let coll = null;
|
|
184
|
+
for (let j = 0; j < collection.length; j++) {
|
|
185
|
+
coll = collection[j];
|
|
186
|
+
const foreachParam = param;
|
|
187
|
+
foreachParam[item] = coll;
|
|
188
|
+
|
|
189
|
+
let foreachText = '';
|
|
190
|
+
for (let k = 0, nextChildren: XML; nextChildren = children.children[k]!; k++) {
|
|
191
|
+
let fText = convertChildren(nextChildren, foreachParam, parentIds, myBatisMapper);
|
|
192
|
+
fText = fText.replace(/^\s*$/g, '');
|
|
193
|
+
if (fText != null && fText.length > 0) {
|
|
194
|
+
foreachText += fText;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (foreachText != null && foreachText.length > 0) {
|
|
199
|
+
foreachTexts.push(foreachText);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return (open + foreachTexts.join(separator) + close);
|
|
204
|
+
} catch (err) {
|
|
205
|
+
throw new Error('Error occurred during convert <foreach> element.');
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const convertChoose = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
210
|
+
try {
|
|
211
|
+
for (let i = 0, whenChildren; whenChildren = children.children[i]; i++) {
|
|
212
|
+
if (whenChildren.type == 'tag' && whenChildren.name.toLowerCase() == 'when') {
|
|
213
|
+
let evalString = whenChildren.attrs.test;
|
|
214
|
+
|
|
215
|
+
// Create Evaluate string
|
|
216
|
+
evalString = replaceEvalString(evalString, param);
|
|
217
|
+
|
|
218
|
+
evalString = evalString.replace(/ and /gi, ' && ');
|
|
219
|
+
evalString = evalString.replace(/ or /gi, ' || ');
|
|
220
|
+
|
|
221
|
+
// Execute Evaluate string
|
|
222
|
+
try {
|
|
223
|
+
if (eval(evalString)) {
|
|
224
|
+
// If <when> condition is true, do it.
|
|
225
|
+
let convertString = '';
|
|
226
|
+
for (let k = 0, nextChildren: XML; nextChildren = whenChildren.children[k]; k++) {
|
|
227
|
+
convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
|
|
228
|
+
}
|
|
229
|
+
return convertString;
|
|
230
|
+
} else {
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
} catch (e) {
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
} else if (whenChildren.type == 'tag' && whenChildren.name.toLowerCase() == 'otherwise') {
|
|
237
|
+
// If reached <otherwise> tag, do it.
|
|
238
|
+
let convertString = '';
|
|
239
|
+
for (let k = 0, nextChildren: XML; nextChildren = whenChildren.children[k]; k++) {
|
|
240
|
+
convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
|
|
241
|
+
}
|
|
242
|
+
return convertString;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// If there is no suitable when and otherwise, just return null.
|
|
247
|
+
return '';
|
|
248
|
+
|
|
249
|
+
} catch (err) {
|
|
250
|
+
throw new Error('Error occurred during convert <choose> element.');
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const convertTrimWhere = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
255
|
+
let convertString = '';
|
|
256
|
+
let prefix: string | undefined;
|
|
257
|
+
let prefixOverrides: string | undefined;
|
|
258
|
+
let suffix: string | undefined;
|
|
259
|
+
let suffixOverrides: string | undefined;
|
|
260
|
+
let globalSet: string | undefined;
|
|
261
|
+
|
|
262
|
+
try {
|
|
263
|
+
switch (children.name.toLowerCase()) {
|
|
264
|
+
case 'trim':
|
|
265
|
+
prefix = children.attrs["prefix"];
|
|
266
|
+
prefixOverrides = children.attrs["prefixOverrides"];
|
|
267
|
+
suffix = children.attrs["suffix"];
|
|
268
|
+
suffixOverrides = children.attrs["suffixOverrides"];
|
|
269
|
+
globalSet = 'g';
|
|
270
|
+
break;
|
|
271
|
+
case 'where':
|
|
272
|
+
prefix = 'WHERE';
|
|
273
|
+
prefixOverrides = 'and|or';
|
|
274
|
+
globalSet = 'gi';
|
|
275
|
+
break;
|
|
276
|
+
default:
|
|
277
|
+
throw new Error('Error occurred during convert <trim/where> element.');
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Convert children first.
|
|
281
|
+
for (let j = 0, nextChildren: XML; nextChildren = children.children[j]!; j++) {
|
|
282
|
+
convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Remove prefixOverrides
|
|
286
|
+
let trimRegex = new RegExp('(^)([\\s]*?)(' + prefixOverrides + ')', globalSet);
|
|
287
|
+
convertString = convertString.replace(trimRegex, '');
|
|
288
|
+
// Remove suffixOverrides
|
|
289
|
+
trimRegex = new RegExp('(' + suffixOverrides + ')([\\s]*?)($)', globalSet);
|
|
290
|
+
convertString = convertString.replace(trimRegex, '');
|
|
291
|
+
|
|
292
|
+
if (children.name.toLowerCase() != 'trim') {
|
|
293
|
+
trimRegex = new RegExp('(' + prefixOverrides + ')([\\s]*?)($)', globalSet);
|
|
294
|
+
convertString = convertString.replace(trimRegex, '');
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Add Prefix if String is not empty.
|
|
298
|
+
trimRegex = new RegExp('([a-zA-Z])', 'g');
|
|
299
|
+
const w = convertString.match(trimRegex);
|
|
300
|
+
|
|
301
|
+
if (w != null && w.length > 0) {
|
|
302
|
+
convertString = prefix + ' ' + convertString;
|
|
303
|
+
if (suffix) {
|
|
304
|
+
convertString = convertString + ' ' + suffix
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Remove comma(,) before WHERE
|
|
309
|
+
if (children.name.toLowerCase() != 'where') {
|
|
310
|
+
const regex = new RegExp('(,)([\\s]*?)(where)', 'gi');
|
|
311
|
+
convertString = convertString.replace(regex, ' WHERE ');
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return convertString;
|
|
315
|
+
} catch (err) {
|
|
316
|
+
throw new Error('Error occurred during convert <' + children.name.toLowerCase() + '> element.');
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const convertSet = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
321
|
+
let convertString = '';
|
|
322
|
+
|
|
323
|
+
try {
|
|
324
|
+
// Convert children first.
|
|
325
|
+
for (let j = 0, nextChildren: XML; nextChildren = children.children[j]!; j++) {
|
|
326
|
+
convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Remove comma repeated more than 2.
|
|
330
|
+
let regex = new RegExp('(,)(,|\\s){2,}', 'g');
|
|
331
|
+
convertString = convertString.replace(regex, ',\n');
|
|
332
|
+
|
|
333
|
+
// Remove first comma if exists.
|
|
334
|
+
regex = new RegExp('(^)([\\s]*?)(,)', 'g');
|
|
335
|
+
convertString = convertString.replace(regex, '');
|
|
336
|
+
|
|
337
|
+
// Remove last comma if exists.
|
|
338
|
+
regex = new RegExp('(,)([\\s]*?)($)', 'g');
|
|
339
|
+
convertString = convertString.replace(regex, '');
|
|
340
|
+
|
|
341
|
+
convertString = ' SET ' + convertString;
|
|
342
|
+
return convertString;
|
|
343
|
+
} catch (err) {
|
|
344
|
+
throw new Error('Error occurred during convert <set> element.');
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const convertBind = function (children: XML, param: Record<string, any>) {
|
|
349
|
+
let evalString = children.attrs["value"]!;
|
|
350
|
+
|
|
351
|
+
// Create Evaluate string
|
|
352
|
+
evalString = replaceEvalString(evalString, param);
|
|
353
|
+
|
|
354
|
+
param[children.attrs["name"]!] = eval(evalString);
|
|
355
|
+
|
|
356
|
+
return param;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const convertInclude = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
360
|
+
try {
|
|
361
|
+
// Add Properties to param
|
|
362
|
+
for (let j = 0, nextChildren: XML; nextChildren = children.children[j]!; j++) {
|
|
363
|
+
if (nextChildren.type == 'tag' && nextChildren.name == 'property') {
|
|
364
|
+
param[nextChildren.attrs['name']!] = nextChildren.attrs['value'];
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
} catch (err) {
|
|
368
|
+
throw new Error('Error occurred during read <property> element in <include> element.');
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
try {
|
|
372
|
+
let refid = convertParametersInner('#', children.attrs['refid']!, param);
|
|
373
|
+
refid = convertParametersInner('$', refid, param);
|
|
374
|
+
let mapper: XML[] | undefined;
|
|
375
|
+
for (const psqlid of parentIds) {
|
|
376
|
+
mapper = myBatisMapper[`${psqlid}.${refid}`];
|
|
377
|
+
if (mapper) {
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
let statement = '';
|
|
382
|
+
for (let i = 0, children: XML; children = mapper![i]!; i++) {
|
|
383
|
+
statement += convertChildren(children, param, parentIds, myBatisMapper);
|
|
384
|
+
}
|
|
385
|
+
return statement;
|
|
386
|
+
} catch (err) {
|
|
387
|
+
throw new Error('Error occurred during convert refid attribute in <include> element.');
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const isDict = function (v: any) {
|
|
393
|
+
return typeof v === 'object' && v !== null && !(v instanceof Array) && !(v instanceof Date);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const replaceEvalString = function (evalString: string, param: Record<string, any>) {
|
|
397
|
+
const keys = Object.keys(param);
|
|
398
|
+
|
|
399
|
+
for (let i = 0; i < keys.length; i++) {
|
|
400
|
+
let replacePrefix = '';
|
|
401
|
+
let replacePostfix = '';
|
|
402
|
+
let paramRegex: RegExp | undefined;
|
|
403
|
+
|
|
404
|
+
if (isDict(param[keys[i]!])) {
|
|
405
|
+
replacePrefix = ' param.';
|
|
406
|
+
replacePostfix = '';
|
|
407
|
+
|
|
408
|
+
paramRegex = new RegExp('(^|[^a-zA-Z0-9_])(' + keys[i] + '\\.)([a-zA-Z0-9_]+)', 'g');
|
|
409
|
+
} else {
|
|
410
|
+
replacePrefix = ' param.';
|
|
411
|
+
replacePostfix = ' ';
|
|
412
|
+
paramRegex = new RegExp('(^|[^a-zA-Z0-9_])(' + keys[i] + ')($|[^a-zA-Z0-9_])', 'g');
|
|
413
|
+
}
|
|
414
|
+
evalString = evalString.replace(paramRegex, ('$1' + replacePrefix + '$2' + replacePostfix + '$3'));
|
|
415
|
+
}
|
|
416
|
+
return evalString;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const uniqueArray = function (a: RegExpMatchArray) {
|
|
420
|
+
const seen = {};
|
|
421
|
+
const out: string[] = [];
|
|
422
|
+
const len = a.length;
|
|
423
|
+
let j = 0;
|
|
424
|
+
for (let i = 0; i < len; i++) {
|
|
425
|
+
const item = a[i]!;
|
|
426
|
+
if (seen[item] !== 1) {
|
|
427
|
+
seen[item] = 1;
|
|
428
|
+
out[j++] = item;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return out;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const mysqlRealEscapeParam = function (param) {
|
|
435
|
+
if (typeof param != 'string')
|
|
436
|
+
return param;
|
|
437
|
+
|
|
438
|
+
return param.replace(/[\0\x08\x09\x1a\n\r''\\\%]/g, function (char) {
|
|
439
|
+
switch (char) {
|
|
440
|
+
case '\0':
|
|
441
|
+
return '\\0';
|
|
442
|
+
case '\x08':
|
|
443
|
+
return '\\b';
|
|
444
|
+
case '\x09':
|
|
445
|
+
return '\\t';
|
|
446
|
+
case '\x1a':
|
|
447
|
+
return '\\z';
|
|
448
|
+
case '\n':
|
|
449
|
+
return '\\n';
|
|
450
|
+
case '\r':
|
|
451
|
+
return '\\r';
|
|
452
|
+
case '\'':
|
|
453
|
+
case `'`:
|
|
454
|
+
case '\\':
|
|
455
|
+
case '%':
|
|
456
|
+
return '\\' + char;
|
|
457
|
+
default:
|
|
458
|
+
return char;
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
package/src/enum.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export class Enum {
|
|
2
|
+
private _value: string;
|
|
3
|
+
private _desc: string;
|
|
4
|
+
private _config: string[];
|
|
5
|
+
constructor (value: string, desc: string, ...config: string[]) {
|
|
6
|
+
this._value = value;
|
|
7
|
+
this._desc = desc;
|
|
8
|
+
this._config = config;
|
|
9
|
+
}
|
|
10
|
+
eq(value: string | number | undefined | null): boolean {
|
|
11
|
+
if (value === undefined) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
if (value === null) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
if (typeof value === 'number') {
|
|
18
|
+
return this._value === `${ value }`;
|
|
19
|
+
}
|
|
20
|
+
return this._value === `${ value }`;
|
|
21
|
+
}
|
|
22
|
+
value(): string {
|
|
23
|
+
return this._value;
|
|
24
|
+
}
|
|
25
|
+
desc(): string {
|
|
26
|
+
return this._desc;
|
|
27
|
+
}
|
|
28
|
+
config(): string[] {
|
|
29
|
+
return this._config;
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/error.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
1
2
|
export * from './error';
|
|
2
3
|
export * from './fn';
|
|
3
4
|
export * from './math';
|
|
4
5
|
export * from './object';
|
|
5
6
|
export * from './set-ex';
|
|
6
7
|
export * from './sql';
|
|
7
|
-
export * from './string';
|
|
8
|
+
export * from './string';
|
|
9
|
+
export * from './enum';
|
|
10
|
+
export * from './list';
|
package/src/list.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export class ArrayList<T> extends Array<T> {
|
|
2
|
+
constructor(array?: Array<T> | T | undefined) {
|
|
3
|
+
super();
|
|
4
|
+
if (array instanceof Array) {
|
|
5
|
+
super.push(...array);
|
|
6
|
+
} else if (typeof array !== 'undefined') {
|
|
7
|
+
super.push(array);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
add(...items: T[]) {
|
|
11
|
+
this.push(...items);
|
|
12
|
+
}
|
|
13
|
+
size() {
|
|
14
|
+
return this.length;
|
|
15
|
+
}
|
|
16
|
+
isEmpty() {
|
|
17
|
+
return this.length === 0;
|
|
18
|
+
}
|
|
19
|
+
get(index: number) {
|
|
20
|
+
return this[index];
|
|
21
|
+
}
|
|
22
|
+
clear() {
|
|
23
|
+
this.length = 0;
|
|
24
|
+
}
|
|
25
|
+
set(index: number, item: T) {
|
|
26
|
+
this[index] = item;
|
|
27
|
+
}
|
|
28
|
+
remove(index: number) {
|
|
29
|
+
this.splice(index, 1);
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/object.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import iterate from "iterare";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* 对象对象(等同与convertBean)
|
|
3
5
|
* 仅会将classType有的属性进行转换
|
|
4
|
-
* 相当与一次属性过滤
|
|
6
|
+
* * 相当与一次属性过滤
|
|
5
7
|
* @param source
|
|
6
8
|
* @param classType
|
|
7
9
|
*/
|
|
@@ -77,13 +79,13 @@ export const createBeanFromArray = <F, T = F>(
|
|
|
77
79
|
if (value) {
|
|
78
80
|
source.forEach((item) => {
|
|
79
81
|
if (item[key]) {
|
|
80
|
-
result[`${
|
|
82
|
+
result[`${item[key]}`] = item[value] as unknown as T;
|
|
81
83
|
}
|
|
82
84
|
});
|
|
83
85
|
} else {
|
|
84
86
|
source.forEach((item) => {
|
|
85
87
|
if (item[key]) {
|
|
86
|
-
result[`${
|
|
88
|
+
result[`${item[key]}`] = item as unknown as T;
|
|
87
89
|
}
|
|
88
90
|
});
|
|
89
91
|
}
|
|
@@ -98,7 +100,7 @@ export const createBeanFromArray = <F, T = F>(
|
|
|
98
100
|
export const coverComplexBean = <T>(
|
|
99
101
|
source: any,
|
|
100
102
|
classType: any
|
|
101
|
-
): {data: T; array: {[key: string]: any[]}} => {
|
|
103
|
+
): { data: T; array: { [key: string]: any[] } } => {
|
|
102
104
|
const result = {};
|
|
103
105
|
const arrayData = {};
|
|
104
106
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
@@ -140,13 +142,13 @@ export const fixEmptyPrototy = async (
|
|
|
140
142
|
};
|
|
141
143
|
|
|
142
144
|
|
|
143
|
-
export const mixArray = <T>(array: T[], key: keyof T, defKey?: string): {[key: string]: number} => {
|
|
145
|
+
export const mixArray = <T>(array: T[], key: keyof T, defKey?: string): { [key: string]: number } => {
|
|
144
146
|
const obj = array.map(item => item[key]);
|
|
145
|
-
const result: {[k: string]: number} = {};
|
|
147
|
+
const result: { [k: string]: number } = {};
|
|
146
148
|
for (const i of obj) {
|
|
147
149
|
let ki = '';
|
|
148
150
|
if (i !== undefined && i !== null) {
|
|
149
|
-
ki = `${
|
|
151
|
+
ki = `${i}`;
|
|
150
152
|
} else if (defKey) {
|
|
151
153
|
ki = defKey;
|
|
152
154
|
}
|
|
@@ -158,12 +160,12 @@ export const mixArray = <T>(array: T[], key: keyof T, defKey?: string): {[key: s
|
|
|
158
160
|
return result;
|
|
159
161
|
};
|
|
160
162
|
|
|
161
|
-
export const mixList = <T, V = T>(array: T[], key: keyof T, value?: keyof T, defKey?: string): {[key: string]: V[]} => {
|
|
162
|
-
const result: {[k: string]: V[]} = {};
|
|
163
|
+
export const mixList = <T, V = T>(array: T[], key: keyof T, value?: keyof T, defKey?: string): { [key: string]: V[] } => {
|
|
164
|
+
const result: { [k: string]: V[] } = {};
|
|
163
165
|
for (const i of array) {
|
|
164
166
|
let ki = '';
|
|
165
167
|
if (i[key] !== undefined && i[key] !== null) {
|
|
166
|
-
ki = `${
|
|
168
|
+
ki = `${i[key]}`;
|
|
167
169
|
} else if (defKey) {
|
|
168
170
|
ki = defKey;
|
|
169
171
|
}
|
|
@@ -179,8 +181,8 @@ export const mixList = <T, V = T>(array: T[], key: keyof T, value?: keyof T, def
|
|
|
179
181
|
return result;
|
|
180
182
|
};
|
|
181
183
|
|
|
182
|
-
export const array2map = <T = string | number>(array: string[], v: T): {[key: string]: T} => {
|
|
183
|
-
const ot: {[key: string]: T} = {};
|
|
184
|
+
export const array2map = <T = string | number>(array: string[], v: T): { [key: string]: T } => {
|
|
185
|
+
const ot: { [key: string]: T } = {};
|
|
184
186
|
for (const item of array) {
|
|
185
187
|
ot[item] = v;
|
|
186
188
|
}
|
|
@@ -192,7 +194,7 @@ export const array2map = <T = string | number>(array: string[], v: T): {[key: st
|
|
|
192
194
|
* @param config(二选一) everyLength=每组个数(最后一组可能不足次数), groupCount=拆分几组
|
|
193
195
|
* @returns T[][]
|
|
194
196
|
*/
|
|
195
|
-
export const arraySplit = <T = any>(datas: T[], {everyLength = 0, groupCount = 0} = {}) => {
|
|
197
|
+
export const arraySplit = <T = any>(datas: T[], { everyLength = 0, groupCount = 0 } = {}) => {
|
|
196
198
|
if (groupCount > 0) {
|
|
197
199
|
everyLength = Math.floor(datas.length / groupCount + 0.9);
|
|
198
200
|
const result: T[][] = [];
|
|
@@ -210,4 +212,36 @@ export const arraySplit = <T = any>(datas: T[], {everyLength = 0, groupCount = 0
|
|
|
210
212
|
} else {
|
|
211
213
|
throw new Error('参数错误!');
|
|
212
214
|
}
|
|
213
|
-
};
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
const P2CEX = /[A-Z]/g;
|
|
219
|
+
export const P2C = (pro: string, IF = true) => IF ? pro.replace(P2CEX, (a: string) => `_${a.toLowerCase()}`) : pro;
|
|
220
|
+
const C2PEX = /_([a-z])/g;
|
|
221
|
+
export const C2P = (pro: string, IF = true) => IF ? pro.replace(C2PEX, (a: string, b: string) => `${b.toUpperCase()}`) : pro;
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
export function C2P2<T extends Object = any, L extends Object = T>(datas: L[]): T[];
|
|
225
|
+
export function C2P2<T extends Object = any, L extends Object = T>(datas: L): T;
|
|
226
|
+
export function C2P2<T extends Object = any, L extends Object = T>(datas: L | L[]): T | T[] {
|
|
227
|
+
if (datas instanceof Array) {
|
|
228
|
+
return iterate(datas).map((data: L) => Object.fromEntries<T>(Object.entries(data).map(([K, V]) => [C2P(K), V]))).toArray() as unknown as T[];
|
|
229
|
+
} else if (datas) {
|
|
230
|
+
return Object.fromEntries<T>(Object.entries(datas).map(([K, V]) => [C2P(K), V])) as unknown as T;
|
|
231
|
+
} else {
|
|
232
|
+
return datas;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function P2C2<T extends Object = any, L extends Object = T>(datas: L[]): T[];
|
|
237
|
+
export function P2C2<T extends Object = any, L extends Object = T>(datas: L): T;
|
|
238
|
+
export function P2C2<T extends Object = any, L extends Object = T>(datas: L | L[]): T | T[] {
|
|
239
|
+
if (datas instanceof Array) {
|
|
240
|
+
return iterate(datas).map((data: L) => Object.fromEntries<T>(Object.entries(data).map(([K, V]) => [P2C(K), V]))).toArray() as unknown as T[];
|
|
241
|
+
} else if (datas) {
|
|
242
|
+
return Object.fromEntries<T>(Object.entries(datas).map(([K, V]) => [P2C(K), V])) as unknown as T;
|
|
243
|
+
} else {
|
|
244
|
+
return datas;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|