semanticdb-core 1.1.40 → 1.1.42

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,83 @@ 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 explainComparedValue = (key, value, schemas) => {
69
+ const segments = Object.entries(value)
70
+ .filter(([operator]) => operator !== '$options')
71
+ .map(([operator, operatorValue]) => {
72
+ if ((operator === '$in' || operator === '$nin') &&
73
+ Array.isArray(operatorValue) &&
74
+ operatorValue.every((item) => isSubQueryLikeValue(item))) {
75
+ const subQueryItems = operatorValue.map((item) => {
76
+ const queryValues = explainInlineQueryValues(item.query, schemas);
77
+ if (item.entity_id) {
78
+ queryValues.push(`ID为${item.entity_id}`);
79
+ }
80
+ return queryValues.join('、') || stringifyJson(item);
81
+ });
82
+ return `${key}${operator === '$in' ? ' 属于' : ' 不属于'}(${subQueryItems.join(';')})`;
83
+ }
84
+ if (operator === '$eq') {
85
+ return `${key} = ${stringifyExplainValue(operatorValue, schemas)}`;
86
+ }
87
+ if (operator === '$gt') {
88
+ return `${key} > ${stringifyExplainValue(operatorValue, schemas)}`;
89
+ }
90
+ if (operator === '$gte') {
91
+ return `${key} >= ${stringifyExplainValue(operatorValue, schemas)}`;
92
+ }
93
+ if (operator === '$lt') {
94
+ return `${key} < ${stringifyExplainValue(operatorValue, schemas)}`;
95
+ }
96
+ if (operator === '$lte') {
97
+ return `${key} <= ${stringifyExplainValue(operatorValue, schemas)}`;
98
+ }
99
+ if (operator === '$ne') {
100
+ return `${key} != ${stringifyExplainValue(operatorValue, schemas)}`;
101
+ }
102
+ if (operator === '$exists') {
103
+ return `${key}${operatorValue ? '存在' : '不存在'}`;
104
+ }
105
+ if (operator === '$regex') {
106
+ return `${key} 匹配 ${stringifyExplainValue(operatorValue, schemas)}`;
107
+ }
108
+ if (operator === '$in') {
109
+ return `${key} in ${stringifyExplainValue(operatorValue, schemas)}`;
110
+ }
111
+ if (operator === '$nin') {
112
+ return `${key} not in ${stringifyExplainValue(operatorValue, schemas)}`;
113
+ }
114
+ return `${key} ${operator} ${stringifyExplainValue(operatorValue, schemas)}`;
115
+ });
116
+ return segments.join(' 且 ');
117
+ };
41
118
  const getEntityIdFromValue = (value) => {
42
119
  if (!value || typeof value !== 'object' || Array.isArray(value)) {
43
120
  return undefined;
@@ -48,7 +125,7 @@ const getEntityIdFromValue = (value) => {
48
125
  const entityId = value.entity_id;
49
126
  return typeof entityId === 'string' && entityId ? entityId : undefined;
50
127
  };
51
- const explainFilterLikeQuery = (query, prefix = '筛选', includeValue = false) => {
128
+ const explainFilterLikeQuery = (query, prefix = '筛选', includeValue = false, schemas = {}) => {
52
129
  if (!query || Object.keys(query).length === 0) {
53
130
  return buildExplainResult(prefix, []);
54
131
  }
@@ -60,7 +137,10 @@ const explainFilterLikeQuery = (query, prefix = '筛选', includeValue = false)
60
137
  if (k === 'entity_id') {
61
138
  return `ID为${stringifyJson(v)}`;
62
139
  }
63
- return `${key} = ${stringifyJson(v)}`;
140
+ if (isCompareOperatorObject(v)) {
141
+ return explainComparedValue(key, v, schemas);
142
+ }
143
+ return `${key} = ${stringifyExplainValue(v, schemas)}`;
64
144
  }
65
145
  const entityId = getEntityIdFromValue(query[k]);
66
146
  if (entityId) {
@@ -111,7 +191,7 @@ const explainSchema = (logicform, schemas, locale = exports.DEFAULT_EXPLAIN_LOGI
111
191
  exports.explainSchema = explainSchema;
112
192
  const explainQuery = (logicform, _schemas, locale = exports.DEFAULT_EXPLAIN_LOGICFORM_LOCALE) => {
113
193
  (0, exports.normalizeExplainLogicformLocale)(locale);
114
- return explainFilterLikeQuery(logicform.query ? (0, logicform_utils_1.getFlattenQuery)(logicform.query) : undefined, '筛选', true);
194
+ return explainFilterLikeQuery(logicform.query ? (0, logicform_utils_1.getFlattenQuery)(logicform.query, _schemas) : undefined, '筛选', true, _schemas);
115
195
  };
116
196
  exports.explainQuery = explainQuery;
117
197
  const explainEntityID = (logicform, _schemas, locale = exports.DEFAULT_EXPLAIN_LOGICFORM_LOCALE) => {
@@ -128,7 +208,16 @@ const explainPreds = (logicform, _schemas, locale = exports.DEFAULT_EXPLAIN_LOGI
128
208
  return buildExplainResult('计算', []);
129
209
  }
130
210
  return buildExplainResult('计算', logicform.preds
131
- .map((p) => p.name)
211
+ .map((p) => {
212
+ if (typeof p.name !== 'string' || p.name.length === 0) {
213
+ return null;
214
+ }
215
+ const predQueryExplanation = explainFilterLikeQuery(p.query ? (0, logicform_utils_1.getFlattenQuery)(p.query, _schemas) : undefined, '', true, _schemas).values.join('、');
216
+ if (!predQueryExplanation) {
217
+ return p.name;
218
+ }
219
+ return `${p.name}(${predQueryExplanation})`;
220
+ })
132
221
  .filter((name) => typeof name === 'string' && name.length > 0));
133
222
  };
134
223
  exports.explainPreds = explainPreds;
@@ -150,7 +239,7 @@ const explainLimit = (logicform, _schemas, locale = exports.DEFAULT_EXPLAIN_LOGI
150
239
  exports.explainLimit = explainLimit;
151
240
  const explainHaving = (logicform, _schemas, locale = exports.DEFAULT_EXPLAIN_LOGICFORM_LOCALE) => {
152
241
  (0, exports.normalizeExplainLogicformLocale)(locale);
153
- return explainFilterLikeQuery(logicform.having, '结果过滤');
242
+ return explainFilterLikeQuery(logicform.having, '结果过滤', false, _schemas);
154
243
  };
155
244
  exports.explainHaving = explainHaving;
156
245
  const explainLogicform = (logicform, schemas, locale = exports.DEFAULT_EXPLAIN_LOGICFORM_LOCALE) => {
@@ -55,7 +55,7 @@ export declare const isDimensionInLogicform: (logicform: LogicformType, dimensio
55
55
  * }
56
56
  * ```
57
57
  */
58
- export declare const getFlattenQuery: (query: QueryType) => QueryType;
58
+ export declare const getFlattenQuery: (query: QueryType, schemasDict?: Record<string, SchemaType>) => QueryType;
59
59
  /**
60
60
  * 根据链条字符串在查询对象中查找对应的值
61
61
  * @param query - 查询对象
@@ -216,14 +216,14 @@ exports.isDimensionInLogicform = isDimensionInLogicform;
216
216
  * }
217
217
  * ```
218
218
  */
219
- const getFlattenQuery = (query) => {
219
+ const getFlattenQuery = (query, schemasDict) => {
220
220
  let flattenQuery = {};
221
221
  for (const [k, v] of Object.entries(query)) {
222
222
  if (Array.isArray(v) && (k === '$and' || k === '$or')) {
223
223
  const tmp = [];
224
224
  v.forEach((item) => {
225
225
  if (typeof item === 'object' && item) {
226
- tmp.push((0, exports.getFlattenQuery)(item));
226
+ tmp.push((0, exports.getFlattenQuery)(item, schemasDict));
227
227
  }
228
228
  });
229
229
  flattenQuery[k] = tmp;
@@ -232,11 +232,16 @@ const getFlattenQuery = (query) => {
232
232
  v &&
233
233
  'schema' in v &&
234
234
  'query' in v &&
235
- !('groupby' in v) && // 不是复合的logicform
236
- !('entity_id' in v) // entity_id的话,也没必要收起来
235
+ !('groupby' in v) // 不是复合的logicform
237
236
  ) {
238
- // 这里是一个logicform
239
- const flattenSubQuery = (0, exports.getFlattenQuery)(v.query || {});
237
+ const subSchema = typeof v.schema === 'string' && schemasDict ? schemasDict[v.schema] : undefined;
238
+ const idProperty = subSchema ? (0, schema_utils_1.findPropertyByType)('ID', subSchema) : undefined;
239
+ if ('entity_id' in v && !idProperty) {
240
+ flattenQuery[k] = v;
241
+ continue;
242
+ }
243
+ // 这里是一个logicform。传了schemas时,把entity_id临时映射成ID属性参与拍平。
244
+ const flattenSubQuery = (0, exports.getFlattenQuery)(Object.assign(Object.assign({}, (v.query || {})), ('entity_id' in v && idProperty ? { [idProperty.name]: v.entity_id } : {})), schemasDict);
240
245
  for (const [sk, sv] of Object.entries(flattenSubQuery)) {
241
246
  if (Array.isArray(sv) &&
242
247
  sv.length &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "semanticdb-core",
3
- "version": "1.1.40",
3
+ "version": "1.1.42",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [