namirasoft-node 1.3.60 → 1.3.61
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/dist/BaseDatabase.d.ts +61 -0
- package/dist/BaseDatabase.js +129 -0
- package/dist/BaseDatabase.js.map +1 -1
- package/package.json +1 -1
- package/src/BaseDatabase.ts +176 -1
package/dist/BaseDatabase.d.ts
CHANGED
|
@@ -1,3 +1,57 @@
|
|
|
1
|
+
import { FilterItem } from "namirasoft-core";
|
|
2
|
+
export interface IFilterableDatabase {
|
|
3
|
+
getIn: (filter: FilterItem, values: string[]) => {
|
|
4
|
+
[table: string]: string[];
|
|
5
|
+
};
|
|
6
|
+
getNotIn: (filter: FilterItem, values: string[]) => {
|
|
7
|
+
[table: string]: string[];
|
|
8
|
+
};
|
|
9
|
+
getLike: (filter: FilterItem, value: string) => {
|
|
10
|
+
[table: string]: string[];
|
|
11
|
+
};
|
|
12
|
+
getNotLike: (filter: FilterItem, value: string) => {
|
|
13
|
+
[table: string]: string[];
|
|
14
|
+
};
|
|
15
|
+
getRegex: (filter: FilterItem, value: string) => {
|
|
16
|
+
[table: string]: string[];
|
|
17
|
+
};
|
|
18
|
+
getNotRegex: (filter: FilterItem, value: string) => {
|
|
19
|
+
[table: string]: string[];
|
|
20
|
+
};
|
|
21
|
+
getEmpty: (filter: FilterItem) => {
|
|
22
|
+
[table: string]: string[];
|
|
23
|
+
};
|
|
24
|
+
getNotEmpty: (filter: FilterItem) => {
|
|
25
|
+
[table: string]: string[];
|
|
26
|
+
};
|
|
27
|
+
getExists: (filter: FilterItem, not: boolean) => {
|
|
28
|
+
[table: string]: string[];
|
|
29
|
+
};
|
|
30
|
+
getStartsWith: (filter: FilterItem, value: string) => {
|
|
31
|
+
[table: string]: string[];
|
|
32
|
+
};
|
|
33
|
+
getNotStartsWith: (filter: FilterItem, value: string) => {
|
|
34
|
+
[table: string]: string[];
|
|
35
|
+
};
|
|
36
|
+
getEndsWith: (filter: FilterItem, value: string) => {
|
|
37
|
+
[table: string]: string[];
|
|
38
|
+
};
|
|
39
|
+
getNotEndsWith: (filter: FilterItem, value: string) => {
|
|
40
|
+
[table: string]: string[];
|
|
41
|
+
};
|
|
42
|
+
getLT: (filter: FilterItem, value: any) => {
|
|
43
|
+
[table: string]: string[];
|
|
44
|
+
};
|
|
45
|
+
getLTE: (filter: FilterItem, value: any) => {
|
|
46
|
+
[table: string]: string[];
|
|
47
|
+
};
|
|
48
|
+
getGT: (filter: FilterItem, value: any) => {
|
|
49
|
+
[table: string]: string[];
|
|
50
|
+
};
|
|
51
|
+
getGTE: (filter: FilterItem, value: any) => {
|
|
52
|
+
[table: string]: string[];
|
|
53
|
+
};
|
|
54
|
+
}
|
|
1
55
|
export declare abstract class BaseDatabase {
|
|
2
56
|
private tables;
|
|
3
57
|
abstract init(): void;
|
|
@@ -9,4 +63,11 @@ export declare abstract class BaseDatabase {
|
|
|
9
63
|
offset: number;
|
|
10
64
|
limit: number;
|
|
11
65
|
};
|
|
66
|
+
protected GetFiltersConditions(tables: {
|
|
67
|
+
[table: string]: string[];
|
|
68
|
+
}, idatabase: IFilterableDatabase, filters?: FilterItem[] | undefined): {
|
|
69
|
+
[table: string]: {
|
|
70
|
+
[name: string]: any;
|
|
71
|
+
}[];
|
|
72
|
+
};
|
|
12
73
|
}
|
package/dist/BaseDatabase.js
CHANGED
|
@@ -27,6 +27,135 @@ class BaseDatabase {
|
|
|
27
27
|
let limit = page_size;
|
|
28
28
|
return { offset, limit };
|
|
29
29
|
}
|
|
30
|
+
GetFiltersConditions(tables, idatabase, filters) {
|
|
31
|
+
let ans = {};
|
|
32
|
+
Object.keys(tables).forEach(table => ans[table] = []);
|
|
33
|
+
if (filters) {
|
|
34
|
+
let processed = {};
|
|
35
|
+
for (let i = 0; i < filters.length; i++)
|
|
36
|
+
if (!processed[i]) {
|
|
37
|
+
const filter = filters[i];
|
|
38
|
+
if (!tables[filter.table.name])
|
|
39
|
+
namirasoft_core_1.ErrorOperation.throwHTTP(404, `Invalid table name '${filter.table.name}'. Valid tables are '${Object.keys(tables).join(", ")}'.`);
|
|
40
|
+
let values = filter.values;
|
|
41
|
+
if (filter.operator.count > 0)
|
|
42
|
+
for (let j = i + 1; j < filters.length; j++) {
|
|
43
|
+
const f = filters[j];
|
|
44
|
+
if (filter.table.name == f.table.name)
|
|
45
|
+
if (filter.column.name == f.column.name)
|
|
46
|
+
if (filter.not == f.not)
|
|
47
|
+
if (filter.operator.sign == f.operator.sign) {
|
|
48
|
+
processed[j] = true;
|
|
49
|
+
values.push(...f.values);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
let getNumbers = () => {
|
|
53
|
+
let nums = values.map(x => parseFloat(x));
|
|
54
|
+
if (nums.filter(x => isNaN(x)).length > 0)
|
|
55
|
+
namirasoft_core_1.ErrorOperation.throwHTTP(400, `Invalid number values for: '${values.join(",")}'`);
|
|
56
|
+
return nums;
|
|
57
|
+
};
|
|
58
|
+
if (filter.operator == namirasoft_core_1.FilterItemOperator.all.equals) {
|
|
59
|
+
let wh = {};
|
|
60
|
+
if (filter.not)
|
|
61
|
+
wh[filter.column.name] = idatabase.getNotIn(filter, values);
|
|
62
|
+
else
|
|
63
|
+
wh[filter.column.name] = idatabase.getIn(filter, values);
|
|
64
|
+
ans[filter.table.name].push(wh);
|
|
65
|
+
}
|
|
66
|
+
else if (filter.operator == namirasoft_core_1.FilterItemOperator.all.contains) {
|
|
67
|
+
values.forEach(value => {
|
|
68
|
+
let wh = {};
|
|
69
|
+
if (filter.not)
|
|
70
|
+
wh[filter.column.name] = idatabase.getNotLike(filter, value);
|
|
71
|
+
else
|
|
72
|
+
wh[filter.column.name] = idatabase.getLike(filter, value);
|
|
73
|
+
ans[filter.table.name].push(wh);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
else if (filter.operator == namirasoft_core_1.FilterItemOperator.all.regex) {
|
|
77
|
+
values.forEach(value => {
|
|
78
|
+
let wh = {};
|
|
79
|
+
if (filter.not)
|
|
80
|
+
wh[filter.column.name] = idatabase.getNotRegex(filter, value);
|
|
81
|
+
else
|
|
82
|
+
wh[filter.column.name] = idatabase.getRegex(filter, value);
|
|
83
|
+
ans[filter.table.name].push(wh);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else if (filter.operator == namirasoft_core_1.FilterItemOperator.all.empty) {
|
|
87
|
+
if (filter.not) {
|
|
88
|
+
let wh = {};
|
|
89
|
+
wh[filter.column.name] = idatabase.getEmpty(filter);
|
|
90
|
+
ans[filter.table.name].push(wh);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
let wh = {};
|
|
94
|
+
wh[filter.column.name] = idatabase.getNotEmpty(filter);
|
|
95
|
+
ans[filter.table.name].push(wh);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else if (filter.operator == namirasoft_core_1.FilterItemOperator.all.exists) {
|
|
99
|
+
let wh = {};
|
|
100
|
+
wh[filter.column.name] = idatabase.getExists(filter, filter.not);
|
|
101
|
+
ans[filter.table.name].push(wh);
|
|
102
|
+
}
|
|
103
|
+
else if (filter.operator == namirasoft_core_1.FilterItemOperator.all.startswith) {
|
|
104
|
+
values.forEach(value => {
|
|
105
|
+
let wh = {};
|
|
106
|
+
if (filter.not)
|
|
107
|
+
wh[filter.column.name] = idatabase.getStartsWith(filter, value);
|
|
108
|
+
else
|
|
109
|
+
wh[filter.column.name] = idatabase.getNotStartsWith(filter, value);
|
|
110
|
+
ans[filter.table.name].push(wh);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
else if (filter.operator == namirasoft_core_1.FilterItemOperator.all.endswith) {
|
|
114
|
+
values.forEach(value => {
|
|
115
|
+
let wh = {};
|
|
116
|
+
if (filter.not)
|
|
117
|
+
wh[filter.column.name] = idatabase.getEndsWith(filter, value);
|
|
118
|
+
else
|
|
119
|
+
wh[filter.column.name] = idatabase.getNotEndsWith(filter, value);
|
|
120
|
+
ans[filter.table.name].push(wh);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
else if (filter.operator == namirasoft_core_1.FilterItemOperator.all.lessthan) {
|
|
124
|
+
let wh = {};
|
|
125
|
+
if (filter.not)
|
|
126
|
+
wh[filter.column.name] = idatabase.getGTE(filter, Math.max(...getNumbers()));
|
|
127
|
+
else
|
|
128
|
+
wh[filter.column.name] = idatabase.getLT(filter, Math.max(...getNumbers()));
|
|
129
|
+
ans[filter.table.name].push(wh);
|
|
130
|
+
}
|
|
131
|
+
else if (filter.operator == namirasoft_core_1.FilterItemOperator.all.lessthanequal) {
|
|
132
|
+
let wh = {};
|
|
133
|
+
if (filter.not)
|
|
134
|
+
wh[filter.column.name] = idatabase.getGT(filter, Math.max(...getNumbers()));
|
|
135
|
+
else
|
|
136
|
+
wh[filter.column.name] = idatabase.getLTE(filter, Math.max(...getNumbers()));
|
|
137
|
+
ans[filter.table.name].push(wh);
|
|
138
|
+
}
|
|
139
|
+
else if (filter.operator == namirasoft_core_1.FilterItemOperator.all.morethan) {
|
|
140
|
+
let wh = {};
|
|
141
|
+
if (filter.not)
|
|
142
|
+
wh[filter.column.name] = idatabase.getLTE(filter, Math.max(...getNumbers()));
|
|
143
|
+
else
|
|
144
|
+
wh[filter.column.name] = idatabase.getGT(filter, Math.max(...getNumbers()));
|
|
145
|
+
ans[filter.table.name].push(wh);
|
|
146
|
+
}
|
|
147
|
+
else if (filter.operator == namirasoft_core_1.FilterItemOperator.all.morethanequal) {
|
|
148
|
+
let wh = {};
|
|
149
|
+
if (filter.not)
|
|
150
|
+
wh[filter.column.name] = idatabase.getLT(filter, Math.max(...getNumbers()));
|
|
151
|
+
else
|
|
152
|
+
wh[filter.column.name] = idatabase.getGTE(filter, Math.max(...getNumbers()));
|
|
153
|
+
ans[filter.table.name].push(wh);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return ans;
|
|
158
|
+
}
|
|
30
159
|
}
|
|
31
160
|
exports.BaseDatabase = BaseDatabase;
|
|
32
161
|
//# sourceMappingURL=BaseDatabase.js.map
|
package/dist/BaseDatabase.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseDatabase.js","sourceRoot":"","sources":["../src/BaseDatabase.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"BaseDatabase.js","sourceRoot":"","sources":["../src/BaseDatabase.ts"],"names":[],"mappings":";;;AAAA,qDAAiF;AAuBjF,MAAsB,YAAY;IAAlC;QAEY,WAAM,GAA4B,EAAE,CAAC;IAyLjD,CAAC;IArLG,QAAQ,CAAC,IAAY,EAAE,KAAU;QAE7B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;IAC9B,CAAC;IACD,QAAQ,CAAI,IAAY;QAEpB,IAAI,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAM,CAAC;QACjC,IAAI,CAAC,GAAG;YACJ,gCAAc,CAAC,SAAS,CAAC,GAAG,EAAE,UAAU,IAAI,aAAa,CAAC,CAAC;QAC/D,OAAO,GAAG,CAAC;IACf,CAAC;IACD,QAAQ,CAAC,WAAmB,EAAE,SAAiB,EAAE,iBAA0B;QAGvE,IAAI,KAAK,CAAC,WAAW,CAAC;YAClB,WAAW,GAAG,CAAC,CAAC;QAEpB,IAAI,KAAK,CAAC,SAAS,CAAC;YAChB,IAAI,iBAAiB;gBACjB,SAAS,GAAG,iBAAiB,CAAC;QACtC,IAAI,KAAK,CAAC,SAAS,CAAC;YAChB,SAAS,GAAG,EAAE,CAAC;QAEnB,IAAI,MAAM,GAAG,CAAC,WAAW,GAAG,CAAC,CAAC,GAAG,SAAS,CAAC;QAC3C,IAAI,KAAK,GAAG,SAAS,CAAC;QACtB,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAC7B,CAAC;IACS,oBAAoB,CAAC,MAAqC,EAAE,SAA8B,EAAE,OAAkC;QAEpI,IAAI,GAAG,GAAmD,EAAE,CAAC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MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,UAAU,EAAE,CAAC,CAAC,CAAC;wBACjF,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;qBACnC;iBACJ;SACR;QACD,OAAO,GAAG,CAAC;IACf,CAAC;CACJ;AA3LD,oCA2LC"}
|
package/package.json
CHANGED
package/src/BaseDatabase.ts
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
|
-
import { ErrorOperation } from "namirasoft-core";
|
|
1
|
+
import { ErrorOperation, FilterItem, FilterItemOperator } from "namirasoft-core";
|
|
2
|
+
|
|
3
|
+
export interface IFilterableDatabase
|
|
4
|
+
{
|
|
5
|
+
getIn: (filter: FilterItem, values: string[]) => { [table: string]: string[] };
|
|
6
|
+
getNotIn: (filter: FilterItem, values: string[]) => { [table: string]: string[] };
|
|
7
|
+
getLike: (filter: FilterItem, value: string) => { [table: string]: string[] };
|
|
8
|
+
getNotLike: (filter: FilterItem, value: string) => { [table: string]: string[] };
|
|
9
|
+
getRegex: (filter: FilterItem, value: string) => { [table: string]: string[] };
|
|
10
|
+
getNotRegex: (filter: FilterItem, value: string) => { [table: string]: string[] };
|
|
11
|
+
getEmpty: (filter: FilterItem) => { [table: string]: string[] };
|
|
12
|
+
getNotEmpty: (filter: FilterItem) => { [table: string]: string[] };
|
|
13
|
+
getExists: (filter: FilterItem, not: boolean) => { [table: string]: string[] };
|
|
14
|
+
getStartsWith: (filter: FilterItem, value: string) => { [table: string]: string[] };
|
|
15
|
+
getNotStartsWith: (filter: FilterItem, value: string) => { [table: string]: string[] };
|
|
16
|
+
getEndsWith: (filter: FilterItem, value: string) => { [table: string]: string[] };
|
|
17
|
+
getNotEndsWith: (filter: FilterItem, value: string) => { [table: string]: string[] };
|
|
18
|
+
getLT: (filter: FilterItem, value: any) => { [table: string]: string[] };
|
|
19
|
+
getLTE: (filter: FilterItem, value: any) => { [table: string]: string[] };
|
|
20
|
+
getGT: (filter: FilterItem, value: any) => { [table: string]: string[] };
|
|
21
|
+
getGTE: (filter: FilterItem, value: any) => { [table: string]: string[] };
|
|
22
|
+
}
|
|
2
23
|
|
|
3
24
|
export abstract class BaseDatabase
|
|
4
25
|
{
|
|
@@ -33,4 +54,158 @@ export abstract class BaseDatabase
|
|
|
33
54
|
let limit = page_size;
|
|
34
55
|
return { offset, limit };
|
|
35
56
|
}
|
|
57
|
+
protected GetFiltersConditions(tables: { [table: string]: string[] }, idatabase: IFilterableDatabase, filters?: FilterItem[] | undefined): { [table: string]: { [name: string]: any }[] }
|
|
58
|
+
{
|
|
59
|
+
let ans: { [table: string]: { [name: string]: any }[] } = {};
|
|
60
|
+
Object.keys(tables).forEach(table => ans[table] = []);
|
|
61
|
+
if (filters)
|
|
62
|
+
{
|
|
63
|
+
let processed: { [index: number]: boolean } = {};
|
|
64
|
+
for (let i = 0; i < filters.length; i++)
|
|
65
|
+
if (!processed[i])
|
|
66
|
+
{
|
|
67
|
+
const filter = filters[i];
|
|
68
|
+
if (!tables[filter.table.name])
|
|
69
|
+
ErrorOperation.throwHTTP(404, `Invalid table name '${filter.table.name}'. Valid tables are '${Object.keys(tables).join(", ")}'.`);
|
|
70
|
+
let values = filter.values;
|
|
71
|
+
if (filter.operator.count > 0)
|
|
72
|
+
for (let j = i + 1; j < filters.length; j++)
|
|
73
|
+
{
|
|
74
|
+
const f = filters[j];
|
|
75
|
+
if (filter.table.name == f.table.name)
|
|
76
|
+
if (filter.column.name == f.column.name)
|
|
77
|
+
if (filter.not == f.not)
|
|
78
|
+
if (filter.operator.sign == f.operator.sign)
|
|
79
|
+
{
|
|
80
|
+
processed[j] = true;
|
|
81
|
+
values.push(...f.values);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let getNumbers = () =>
|
|
86
|
+
{
|
|
87
|
+
let nums: number[] = values.map(x => parseFloat(x));
|
|
88
|
+
if (nums.filter(x => isNaN(x)).length > 0)
|
|
89
|
+
ErrorOperation.throwHTTP(400, `Invalid number values for: '${values.join(",")}'`);
|
|
90
|
+
return nums;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
if (filter.operator == FilterItemOperator.all.equals)
|
|
94
|
+
{
|
|
95
|
+
let wh: { [name: string]: any } = {};
|
|
96
|
+
if (filter.not)
|
|
97
|
+
wh[filter.column.name] = idatabase.getNotIn(filter, values);
|
|
98
|
+
else
|
|
99
|
+
wh[filter.column.name] = idatabase.getIn(filter, values);
|
|
100
|
+
ans[filter.table.name].push(wh);
|
|
101
|
+
}
|
|
102
|
+
else if (filter.operator == FilterItemOperator.all.contains)
|
|
103
|
+
{
|
|
104
|
+
values.forEach(value =>
|
|
105
|
+
{
|
|
106
|
+
let wh: { [name: string]: any } = {};
|
|
107
|
+
if (filter.not)
|
|
108
|
+
wh[filter.column.name] = idatabase.getNotLike(filter, value);
|
|
109
|
+
else
|
|
110
|
+
wh[filter.column.name] = idatabase.getLike(filter, value);
|
|
111
|
+
ans[filter.table.name].push(wh);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
else if (filter.operator == FilterItemOperator.all.regex)
|
|
115
|
+
{
|
|
116
|
+
values.forEach(value =>
|
|
117
|
+
{
|
|
118
|
+
let wh: { [name: string]: any } = {};
|
|
119
|
+
if (filter.not)
|
|
120
|
+
wh[filter.column.name] = idatabase.getNotRegex(filter, value);
|
|
121
|
+
else
|
|
122
|
+
wh[filter.column.name] = idatabase.getRegex(filter, value);
|
|
123
|
+
ans[filter.table.name].push(wh);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
else if (filter.operator == FilterItemOperator.all.empty)
|
|
127
|
+
{
|
|
128
|
+
if (filter.not)
|
|
129
|
+
{
|
|
130
|
+
let wh: { [name: string]: any } = {};
|
|
131
|
+
wh[filter.column.name] = idatabase.getEmpty(filter);
|
|
132
|
+
ans[filter.table.name].push(wh);
|
|
133
|
+
}
|
|
134
|
+
else
|
|
135
|
+
{
|
|
136
|
+
let wh: { [name: string]: any } = {};
|
|
137
|
+
wh[filter.column.name] = idatabase.getNotEmpty(filter);
|
|
138
|
+
ans[filter.table.name].push(wh);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else if (filter.operator == FilterItemOperator.all.exists)
|
|
142
|
+
{
|
|
143
|
+
let wh: { [name: string]: any } = {};
|
|
144
|
+
wh[filter.column.name] = idatabase.getExists(filter, filter.not);
|
|
145
|
+
ans[filter.table.name].push(wh);
|
|
146
|
+
}
|
|
147
|
+
else if (filter.operator == FilterItemOperator.all.startswith)
|
|
148
|
+
{
|
|
149
|
+
values.forEach(value =>
|
|
150
|
+
{
|
|
151
|
+
let wh: { [name: string]: any } = {};
|
|
152
|
+
if (filter.not)
|
|
153
|
+
wh[filter.column.name] = idatabase.getStartsWith(filter, value);
|
|
154
|
+
else
|
|
155
|
+
wh[filter.column.name] = idatabase.getNotStartsWith(filter, value);
|
|
156
|
+
ans[filter.table.name].push(wh);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
else if (filter.operator == FilterItemOperator.all.endswith)
|
|
160
|
+
{
|
|
161
|
+
values.forEach(value =>
|
|
162
|
+
{
|
|
163
|
+
let wh: { [name: string]: any } = {};
|
|
164
|
+
if (filter.not)
|
|
165
|
+
wh[filter.column.name] = idatabase.getEndsWith(filter, value);
|
|
166
|
+
else
|
|
167
|
+
wh[filter.column.name] = idatabase.getNotEndsWith(filter, value);
|
|
168
|
+
ans[filter.table.name].push(wh);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
else if (filter.operator == FilterItemOperator.all.lessthan)
|
|
172
|
+
{
|
|
173
|
+
let wh: { [name: string]: any } = {};
|
|
174
|
+
if (filter.not)
|
|
175
|
+
wh[filter.column.name] = idatabase.getGTE(filter, Math.max(...getNumbers()));
|
|
176
|
+
else
|
|
177
|
+
wh[filter.column.name] = idatabase.getLT(filter, Math.max(...getNumbers()));
|
|
178
|
+
ans[filter.table.name].push(wh);
|
|
179
|
+
}
|
|
180
|
+
else if (filter.operator == FilterItemOperator.all.lessthanequal)
|
|
181
|
+
{
|
|
182
|
+
let wh: { [name: string]: any } = {};
|
|
183
|
+
if (filter.not)
|
|
184
|
+
wh[filter.column.name] = idatabase.getGT(filter, Math.max(...getNumbers()));
|
|
185
|
+
else
|
|
186
|
+
wh[filter.column.name] = idatabase.getLTE(filter, Math.max(...getNumbers()));
|
|
187
|
+
ans[filter.table.name].push(wh);
|
|
188
|
+
}
|
|
189
|
+
else if (filter.operator == FilterItemOperator.all.morethan)
|
|
190
|
+
{
|
|
191
|
+
let wh: { [name: string]: any } = {};
|
|
192
|
+
if (filter.not)
|
|
193
|
+
wh[filter.column.name] = idatabase.getLTE(filter, Math.max(...getNumbers()));
|
|
194
|
+
else
|
|
195
|
+
wh[filter.column.name] = idatabase.getGT(filter, Math.max(...getNumbers()));
|
|
196
|
+
ans[filter.table.name].push(wh);
|
|
197
|
+
}
|
|
198
|
+
else if (filter.operator == FilterItemOperator.all.morethanequal)
|
|
199
|
+
{
|
|
200
|
+
let wh: { [name: string]: any } = {};
|
|
201
|
+
if (filter.not)
|
|
202
|
+
wh[filter.column.name] = idatabase.getLT(filter, Math.max(...getNumbers()));
|
|
203
|
+
else
|
|
204
|
+
wh[filter.column.name] = idatabase.getGTE(filter, Math.max(...getNumbers()));
|
|
205
|
+
ans[filter.table.name].push(wh);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return ans;
|
|
210
|
+
}
|
|
36
211
|
}
|