@gingkoo/pandora-metabase 1.0.0-alpha.2 → 1.0.0-alpha.3

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.
@@ -2,7 +2,7 @@ import { UnionEnum } from '../../store/enum';
2
2
  interface PropsType {
3
3
  store?: any;
4
4
  meta: any;
5
- operator: UnionEnum;
5
+ union: UnionEnum;
6
6
  groupIndex: number;
7
7
  }
8
8
  declare const RowLimit: (props: PropsType) => import("react/jsx-runtime").JSX.Element;
package/lib/es/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @gingkoo/pandora-metabase v1.0.0-alpha.2
2
+ * @gingkoo/pandora-metabase v1.0.0-alpha.3
3
3
  */
4
4
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
5
5
  import * as React from 'react';
@@ -89,7 +89,7 @@ var TypeEnum;
89
89
  TypeEnum["summarize"] = "summarize";
90
90
  TypeEnum["sort"] = "sort";
91
91
  TypeEnum["rowLimit"] = "rowLimit";
92
- TypeEnum["group"] = "group";
92
+ TypeEnum["union"] = "union";
93
93
  })(TypeEnum || (TypeEnum = {}));
94
94
  var JoinEnum;
95
95
  (function (JoinEnum) {
@@ -869,6 +869,86 @@ const changeFieldAlias = (list, curObj) => {
869
869
  });
870
870
  return newList;
871
871
  };
872
+ function splitByUnion(data) {
873
+ const original = cloneDeep(data);
874
+ const result = [];
875
+ let i = 0;
876
+ while (i < original.length) {
877
+ const item = original[i];
878
+ if (item.type === 'group') {
879
+ // group.list 中每一项原样推入结果
880
+ for (const subItem of item.list) {
881
+ result.push(subItem);
882
+ }
883
+ i++;
884
+ } else if (item.type === 'union') {
885
+ const {
886
+ list,
887
+ ...otehr
888
+ } = item;
889
+ // 查找下一个 group,并将其 list 转成 subquery
890
+ const nextItem = original[i + 1];
891
+ if (nextItem && nextItem.type === 'group') {
892
+ result.push({
893
+ ...otehr,
894
+ subquery: nextItem.list.map(subItem => subItem)
895
+ });
896
+ i += 2;
897
+ } else {
898
+ result.push({
899
+ ...otehr,
900
+ subquery: []
901
+ });
902
+ i++;
903
+ }
904
+ }
905
+ }
906
+ return result;
907
+ }
908
+ function reassembleByUnion(target) {
909
+ const result = [];
910
+ let i = 0;
911
+ const len = target.length;
912
+ // 如果没有任何 union,直接放入一个 group.list
913
+ const hasUnion = target.some(item => item.type === 'union');
914
+ if (!hasUnion) {
915
+ return [{
916
+ type: 'group',
917
+ name: 'default',
918
+ list: [...target]
919
+ }];
920
+ }
921
+ // 否则正常处理
922
+ while (i < len) {
923
+ const item = target[i];
924
+ if (item.type !== 'union') {
925
+ // 收集连续非 union 的 item,统一放入一个 group.list
926
+ const groupList = [];
927
+ while (i < len && target[i].type !== 'union') {
928
+ groupList.push(target[i]);
929
+ i++;
930
+ }
931
+ result.push({
932
+ type: 'group',
933
+ name: 'default',
934
+ list: groupList
935
+ });
936
+ } else {
937
+ // 处理 union
938
+ result.push({
939
+ type: 'union',
940
+ name: 'union',
941
+ union: item.union ?? 'UNION',
942
+ list: []
943
+ });
944
+ const subquery = item.subquery || [];
945
+ const convertedSubquery = reassembleByUnion(subquery);
946
+ result.push(...convertedSubquery);
947
+ i++; // 跳过当前 union
948
+ }
949
+ }
950
+ return result;
951
+ }
872
952
 
873
953
  let metaKey = 1;
874
954
  const SummarizeAlias = 'source';
@@ -893,9 +973,9 @@ const useStore = () => {
893
973
  list: defaultMetaList
894
974
  }];
895
975
  const defaultOperator = {
896
- type: 'group',
897
- name: 'group',
898
- operator: UnionEnum.union,
976
+ type: 'union',
977
+ name: 'union',
978
+ union: UnionEnum.union,
899
979
  list: []
900
980
  };
901
981
  const [showFields, setShowFields] = useState(true); //显示字段
@@ -1053,7 +1133,8 @@ const useStore = () => {
1053
1133
  return {
1054
1134
  name: item.name || 'default',
1055
1135
  list: newList.length < 1 ? defaultMetaList : newList,
1056
- type: item.type || 'group'
1136
+ type: item.type || 'group',
1137
+ union: item.union
1057
1138
  };
1058
1139
  });
1059
1140
  const validMetaKeys = _metaList.flatMap(group => group.list).map(v => Number(v.metaKey)).filter(num => !isNaN(num));
@@ -1161,7 +1242,7 @@ const useStore = () => {
1161
1242
  }
1162
1243
  };
1163
1244
  }
1164
- if (type === TypeEnum.group) {
1245
+ if (type === TypeEnum.union) {
1165
1246
  // 添加分组
1166
1247
  let newMetaList = metaList.slice();
1167
1248
  newMetaList.splice(groupIndex + 1, 0, defaultOperator, ...defaultMeta);
@@ -2236,7 +2317,7 @@ const List = [{
2236
2317
  const RowLimit$1 = props => {
2237
2318
  const {
2238
2319
  meta,
2239
- operator,
2320
+ union,
2240
2321
  groupIndex
2241
2322
  } = props;
2242
2323
  const store = useStore$1();
@@ -2246,10 +2327,10 @@ const RowLimit$1 = props => {
2246
2327
  node: e.currentTarget,
2247
2328
  content: jsx(SelectList, {
2248
2329
  list: List,
2249
- value: operator,
2330
+ value: union,
2250
2331
  onChange: type => {
2251
2332
  let newMeta = store.metaList.slice();
2252
- newMeta[groupIndex].operator = type;
2333
+ newMeta[groupIndex].union = type;
2253
2334
  store._setMeta(newMeta);
2254
2335
  store.setPopup({
2255
2336
  visible: false
@@ -2272,7 +2353,7 @@ const RowLimit$1 = props => {
2272
2353
  children: jsx("div", {
2273
2354
  className: `Sqb-TableName`,
2274
2355
  onClick: e => selectOperator(e),
2275
- children: operator
2356
+ children: union
2276
2357
  })
2277
2358
  })
2278
2359
  })]
@@ -5015,17 +5096,17 @@ const IconTypeMap = new Map([[TypeEnum.filter, {
5015
5096
  name: __('SqlQueryBuilder.rowLimit'),
5016
5097
  icon: jsx(RowLimitIcon, {}),
5017
5098
  className: 'rowLimit'
5018
- }], [TypeEnum.group, {
5099
+ }], [TypeEnum.union, {
5019
5100
  name: __('SqlQueryBuilder.union'),
5020
5101
  icon: jsx(GroupIcon, {}),
5021
5102
  className: 'union'
5022
5103
  }]]);
5023
5104
  // 前端展示的icon顺序 随便改不影响逻辑
5024
- const DisplayOrder = [TypeEnum.filter, TypeEnum.summarize, TypeEnum.joinData, TypeEnum.permissionTable, TypeEnum.customColumn, TypeEnum.sort, TypeEnum.rowLimit, TypeEnum.group];
5105
+ const DisplayOrder = [TypeEnum.filter, TypeEnum.summarize, TypeEnum.joinData, TypeEnum.permissionTable, TypeEnum.customColumn, TypeEnum.sort, TypeEnum.rowLimit, TypeEnum.union];
5025
5106
  // js逻辑顺序 正常顺序
5026
- const OrderType = [TypeEnum.data, TypeEnum.joinData, TypeEnum.permissionTable, TypeEnum.customColumn, TypeEnum.filter, TypeEnum.summarize, TypeEnum.sort, TypeEnum.rowLimit, TypeEnum.group];
5107
+ const OrderType = [TypeEnum.data, TypeEnum.joinData, TypeEnum.permissionTable, TypeEnum.customColumn, TypeEnum.filter, TypeEnum.summarize, TypeEnum.sort, TypeEnum.rowLimit, TypeEnum.union];
5027
5108
  // js逻辑顺序 聚合下面的顺序是这个样子
5028
- const OrderNewType = [TypeEnum.sort, TypeEnum.rowLimit, TypeEnum.joinData, TypeEnum.permissionTable, TypeEnum.customColumn, TypeEnum.filter, TypeEnum.summarize, TypeEnum.group];
5109
+ const OrderNewType = [TypeEnum.sort, TypeEnum.rowLimit, TypeEnum.joinData, TypeEnum.permissionTable, TypeEnum.customColumn, TypeEnum.filter, TypeEnum.summarize, TypeEnum.union];
5029
5110
  const metaIcon = (size, handleClick) => {
5030
5111
  return ({
5031
5112
  type
@@ -5130,7 +5211,7 @@ const findNextIcon = (store, props) => {
5130
5211
  if (meta.table2.name) {
5131
5212
  available = OrderType.slice(curLocation + 1);
5132
5213
  } else {
5133
- available = [TypeEnum.customColumn, TypeEnum.filter, TypeEnum.summarize, TypeEnum.group];
5214
+ available = [TypeEnum.customColumn, TypeEnum.filter, TypeEnum.summarize, TypeEnum.union];
5134
5215
  }
5135
5216
  } else {
5136
5217
  if (isLast) {
@@ -5152,7 +5233,7 @@ const findNextIcon = (store, props) => {
5152
5233
  if (meta.customColumn.length || ~joinIndex && prevList[joinIndex].table2.name) {
5153
5234
  available = OrderType.slice(curLocation + 1);
5154
5235
  } else {
5155
- available = [TypeEnum.filter, TypeEnum.summarize, TypeEnum.group];
5236
+ available = [TypeEnum.filter, TypeEnum.summarize, TypeEnum.union];
5156
5237
  }
5157
5238
  } else {
5158
5239
  if (isLast) {
@@ -5185,7 +5266,7 @@ const findNextIcon = (store, props) => {
5185
5266
  if (isLast) {
5186
5267
  if (!ExistAboveGroupBy) {
5187
5268
  if (meta.group.length && !meta.by.length) ; else if (!meta.group.length && !meta.by.length) {
5188
- available = [TypeEnum.sort, TypeEnum.rowLimit, TypeEnum.group];
5269
+ available = [TypeEnum.sort, TypeEnum.rowLimit, TypeEnum.union];
5189
5270
  } else {
5190
5271
  available = OrderNewType;
5191
5272
  }
@@ -8545,21 +8626,16 @@ const Metabase = props => {
8545
8626
  }, [store.popupData2]);
8546
8627
  const onSave = async () => {
8547
8628
  let intercept = false; // 是否返回
8629
+ const _metaList = splitByUnion(store.metaList);
8548
8630
  store.metaList.map(v => {
8549
- if (v.type !== 'group' && !(v.list?.[0]).table.name) {
8631
+ if (v.type !== 'union' && !(v.list?.[0]).table.name) {
8550
8632
  intercept = true;
8551
8633
  return Toast.warning(__('data.pleaseSelectDataTable'));
8552
8634
  }
8553
8635
  });
8554
8636
  if (saveLoading || intercept) return null;
8555
8637
  setSaveLoading(true);
8556
- if (store.toolbar.includes('group')) {
8557
- // 分组
8558
- await onOk?.(store.metaList);
8559
- } else {
8560
- // 非分组
8561
- await onOk?.(store.metaList[0].list);
8562
- }
8638
+ await onOk?.(_metaList);
8563
8639
  setSaveLoading(false);
8564
8640
  };
8565
8641
  let zIndex = popupContainer.current ? getMaxZIndexInParents(popupContainer.current) : null;
@@ -8570,15 +8646,15 @@ const Metabase = props => {
8570
8646
  children: jsxs("div", {
8571
8647
  className: 'Sqb',
8572
8648
  children: [store.metaList.map((v, index) => {
8573
- if (v.type === 'group' && v.operator) {
8649
+ if (v.type === 'union' && v.union) {
8574
8650
  return jsx("div", {
8575
8651
  className: cx(`Sqb-list`),
8576
8652
  children: jsx(RowLimit$1, {
8577
- operator: v.operator,
8653
+ union: v.union,
8578
8654
  meta: v,
8579
8655
  groupIndex: index
8580
8656
  })
8581
- }, 'group' + index);
8657
+ }, 'union' + index);
8582
8658
  }
8583
8659
  return jsx("div", {
8584
8660
  className: cx(`Sqb-list`),
@@ -8665,15 +8741,9 @@ const SqlVisionBuilder = /*#__PURE__*/React__default.forwardRef((props, ref) =>
8665
8741
  store.setProps(props);
8666
8742
  }, [props]);
8667
8743
  useEffect(() => {
8668
- if (toolbar.includes('group')) {
8669
- store.setPreData(value);
8670
- } else {
8671
- store.setPreData([{
8672
- name: 'default',
8673
- list: value
8674
- }]);
8675
- }
8676
- }, [value, toolbar]);
8744
+ const _value = reassembleByUnion(value);
8745
+ store.setPreData(_value);
8746
+ }, [value]);
8677
8747
  useEffect(() => {
8678
8748
  store.setSourceList(sourceList);
8679
8749
  }, [sourceList]);