semanticdb-core 1.1.41 → 1.1.43

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.
@@ -38,6 +38,133 @@ const stringifyJson = (value) => {
38
38
  const stringified = JSON.stringify(value);
39
39
  return stringified === undefined ? String(value) : stringified;
40
40
  };
41
+ const isCompareOperatorObject = (value) => {
42
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
43
+ return false;
44
+ }
45
+ const keys = Object.keys(value);
46
+ return keys.length > 0 && keys.every((key) => key.startsWith('$'));
47
+ };
48
+ const isSubQueryLikeValue = (value) => {
49
+ return Boolean(value &&
50
+ typeof value === 'object' &&
51
+ !Array.isArray(value) &&
52
+ 'schema' in value &&
53
+ 'query' in value &&
54
+ !('groupby' in value));
55
+ };
56
+ const explainInlineQueryValues = (query, schemas) => {
57
+ return explainFilterLikeQuery(query ? (0, logicform_utils_1.getFlattenQuery)(query, schemas) : undefined, '', true, schemas).values;
58
+ };
59
+ const stringifyExplainValue = (value, schemas) => {
60
+ if (Array.isArray(value)) {
61
+ return stringifyJson(value);
62
+ }
63
+ if (isCompareOperatorObject(value)) {
64
+ return stringifyJson(value);
65
+ }
66
+ return stringifyJson(value);
67
+ };
68
+ const explainSingleComparedValue = (key, operator, operatorValue, schemas) => {
69
+ if ((operator === '$in' || operator === '$nin') &&
70
+ Array.isArray(operatorValue) &&
71
+ operatorValue.every((item) => isSubQueryLikeValue(item))) {
72
+ const subQueryItems = operatorValue.map((item) => {
73
+ const queryValues = explainInlineQueryValues(item.query, schemas);
74
+ if (item.entity_id) {
75
+ queryValues.push(`ID为${item.entity_id}`);
76
+ }
77
+ return queryValues.join('、') || stringifyJson(item);
78
+ });
79
+ return `${key}${operator === '$in' ? ' 属于' : ' 不属于'}(${subQueryItems.join(';')})`;
80
+ }
81
+ if (operator === '$eq') {
82
+ return `${key} = ${stringifyExplainValue(operatorValue, schemas)}`;
83
+ }
84
+ if (operator === '$gt') {
85
+ return `${key} > ${stringifyExplainValue(operatorValue, schemas)}`;
86
+ }
87
+ if (operator === '$gte') {
88
+ return `${key} >= ${stringifyExplainValue(operatorValue, schemas)}`;
89
+ }
90
+ if (operator === '$lt') {
91
+ return `${key} < ${stringifyExplainValue(operatorValue, schemas)}`;
92
+ }
93
+ if (operator === '$lte') {
94
+ return `${key} <= ${stringifyExplainValue(operatorValue, schemas)}`;
95
+ }
96
+ if (operator === '$exists') {
97
+ return `${key}${operatorValue ? '存在' : '不存在'}`;
98
+ }
99
+ if (operator === '$regex') {
100
+ return `${key} 匹配 ${stringifyExplainValue(operatorValue, schemas)}`;
101
+ }
102
+ if (operator === '$in') {
103
+ return `${key} 属于 ${stringifyExplainValue(operatorValue, schemas)}`;
104
+ }
105
+ if (operator === '$nin') {
106
+ return `${key} 不属于 ${stringifyExplainValue(operatorValue, schemas)}`;
107
+ }
108
+ if (operator === '$contains') {
109
+ return `${key} 包含 ${stringifyExplainValue(operatorValue, schemas)}`;
110
+ }
111
+ return `${key} ${operator} ${stringifyExplainValue(operatorValue, schemas)}`;
112
+ };
113
+ const explainNegatedComparedValue = (key, value, schemas) => {
114
+ const entries = Object.entries(value).filter(([operator]) => operator !== '$options');
115
+ if (entries.length !== 1) {
116
+ return `${key} 不满足(${explainComparedValue(key, value, schemas)})`;
117
+ }
118
+ const [operator, operatorValue] = entries[0];
119
+ if (operator === '$eq') {
120
+ return `${key} != ${stringifyExplainValue(operatorValue, schemas)}`;
121
+ }
122
+ if (operator === '$gt') {
123
+ return `${key} <= ${stringifyExplainValue(operatorValue, schemas)}`;
124
+ }
125
+ if (operator === '$gte') {
126
+ return `${key} < ${stringifyExplainValue(operatorValue, schemas)}`;
127
+ }
128
+ if (operator === '$lt') {
129
+ return `${key} >= ${stringifyExplainValue(operatorValue, schemas)}`;
130
+ }
131
+ if (operator === '$lte') {
132
+ return `${key} > ${stringifyExplainValue(operatorValue, schemas)}`;
133
+ }
134
+ if (operator === '$ne') {
135
+ return `${key} = ${stringifyExplainValue(operatorValue, schemas)}`;
136
+ }
137
+ if (operator === '$exists') {
138
+ return `${key}${operatorValue ? '不存在' : '存在'}`;
139
+ }
140
+ if (operator === '$regex') {
141
+ return `${key} 不匹配 ${stringifyExplainValue(operatorValue, schemas)}`;
142
+ }
143
+ if (operator === '$in') {
144
+ return `${key} 不属于 ${stringifyExplainValue(operatorValue, schemas)}`;
145
+ }
146
+ if (operator === '$nin') {
147
+ return `${key} 属于 ${stringifyExplainValue(operatorValue, schemas)}`;
148
+ }
149
+ if (operator === '$contains') {
150
+ return `${key} 不包含 ${stringifyExplainValue(operatorValue, schemas)}`;
151
+ }
152
+ return `${key} 不满足 ${operator} ${stringifyExplainValue(operatorValue, schemas)}`;
153
+ };
154
+ const explainComparedValue = (key, value, schemas) => {
155
+ const segments = Object.entries(value)
156
+ .filter(([operator]) => operator !== '$options')
157
+ .map(([operator, operatorValue]) => {
158
+ if (operator === '$ne') {
159
+ if (isCompareOperatorObject(operatorValue)) {
160
+ return explainNegatedComparedValue(key, operatorValue, schemas);
161
+ }
162
+ return `${key} != ${stringifyExplainValue(operatorValue, schemas)}`;
163
+ }
164
+ return explainSingleComparedValue(key, operator, operatorValue, schemas);
165
+ });
166
+ return segments.join(' 且 ');
167
+ };
41
168
  const getEntityIdFromValue = (value) => {
42
169
  if (!value || typeof value !== 'object' || Array.isArray(value)) {
43
170
  return undefined;
@@ -48,7 +175,7 @@ const getEntityIdFromValue = (value) => {
48
175
  const entityId = value.entity_id;
49
176
  return typeof entityId === 'string' && entityId ? entityId : undefined;
50
177
  };
51
- const explainFilterLikeQuery = (query, prefix = '筛选', includeValue = false) => {
178
+ const explainFilterLikeQuery = (query, prefix = '筛选', includeValue = false, schemas = {}) => {
52
179
  if (!query || Object.keys(query).length === 0) {
53
180
  return buildExplainResult(prefix, []);
54
181
  }
@@ -60,7 +187,10 @@ const explainFilterLikeQuery = (query, prefix = '筛选', includeValue = false)
60
187
  if (k === 'entity_id') {
61
188
  return `ID为${stringifyJson(v)}`;
62
189
  }
63
- return `${key} = ${stringifyJson(v)}`;
190
+ if (isCompareOperatorObject(v)) {
191
+ return explainComparedValue(key, v, schemas);
192
+ }
193
+ return `${key} = ${stringifyExplainValue(v, schemas)}`;
64
194
  }
65
195
  const entityId = getEntityIdFromValue(query[k]);
66
196
  if (entityId) {
@@ -111,7 +241,7 @@ const explainSchema = (logicform, schemas, locale = exports.DEFAULT_EXPLAIN_LOGI
111
241
  exports.explainSchema = explainSchema;
112
242
  const explainQuery = (logicform, _schemas, locale = exports.DEFAULT_EXPLAIN_LOGICFORM_LOCALE) => {
113
243
  (0, exports.normalizeExplainLogicformLocale)(locale);
114
- return explainFilterLikeQuery(logicform.query ? (0, logicform_utils_1.getFlattenQuery)(logicform.query, _schemas) : undefined, '筛选', true);
244
+ return explainFilterLikeQuery(logicform.query ? (0, logicform_utils_1.getFlattenQuery)(logicform.query, _schemas) : undefined, '筛选', true, _schemas);
115
245
  };
116
246
  exports.explainQuery = explainQuery;
117
247
  const explainEntityID = (logicform, _schemas, locale = exports.DEFAULT_EXPLAIN_LOGICFORM_LOCALE) => {
@@ -128,7 +258,16 @@ const explainPreds = (logicform, _schemas, locale = exports.DEFAULT_EXPLAIN_LOGI
128
258
  return buildExplainResult('计算', []);
129
259
  }
130
260
  return buildExplainResult('计算', logicform.preds
131
- .map((p) => p.name)
261
+ .map((p) => {
262
+ if (typeof p.name !== 'string' || p.name.length === 0) {
263
+ return null;
264
+ }
265
+ const predQueryExplanation = explainFilterLikeQuery(p.query ? (0, logicform_utils_1.getFlattenQuery)(p.query, _schemas) : undefined, '', true, _schemas).values.join('、');
266
+ if (!predQueryExplanation) {
267
+ return p.name;
268
+ }
269
+ return `${p.name}(${predQueryExplanation})`;
270
+ })
132
271
  .filter((name) => typeof name === 'string' && name.length > 0));
133
272
  };
134
273
  exports.explainPreds = explainPreds;
@@ -150,7 +289,7 @@ const explainLimit = (logicform, _schemas, locale = exports.DEFAULT_EXPLAIN_LOGI
150
289
  exports.explainLimit = explainLimit;
151
290
  const explainHaving = (logicform, _schemas, locale = exports.DEFAULT_EXPLAIN_LOGICFORM_LOCALE) => {
152
291
  (0, exports.normalizeExplainLogicformLocale)(locale);
153
- return explainFilterLikeQuery(logicform.having, '结果过滤');
292
+ return explainFilterLikeQuery(logicform.having, '结果过滤', false, _schemas);
154
293
  };
155
294
  exports.explainHaving = explainHaving;
156
295
  const explainLogicform = (logicform, schemas, locale = exports.DEFAULT_EXPLAIN_LOGICFORM_LOCALE) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "semanticdb-core",
3
- "version": "1.1.41",
3
+ "version": "1.1.43",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [