@gingkoo/pandora-metabase 1.0.105 → 1.0.107

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.
@@ -355,11 +355,16 @@ var NextDom = function NextDom(props) {
355
355
  var handlePaste = function handlePaste() {
356
356
  var _store$metaList3;
357
357
  var index = (_store$metaList3 = store.metaList) === null || _store$metaList3 === void 0 || (_store$metaList3 = _store$metaList3[groupIndex]) === null || _store$metaList3 === void 0 || (_store$metaList3 = _store$metaList3.list) === null || _store$metaList3 === void 0 ? void 0 : _store$metaList3.indexOf(meta);
358
- store.addMeta('', index + 1, groupIndex, store.metabaseCopyModule);
359
- // 粘贴后重置
360
- store.setMetabaseCopyModule([]);
361
- store.setIsMetabaseCopy(false);
362
- _storage["default"]._metabaseCopyModule = [];
358
+ var success = store.addMeta('', index + 1, groupIndex, store.metabaseCopyModule, function (conflictingAlias) {
359
+ // 冲突提示
360
+ _pandora.Toast.warning("\u8BE5\u8868\u5DF2\u5B58\u5728\u5173\u8054\uFF0C\u4E0D\u80FD\u590D\u5236");
361
+ });
362
+ // 仅在成功时重置
363
+ if (success) {
364
+ store.setMetabaseCopyModule([]);
365
+ store.setIsMetabaseCopy(false);
366
+ _storage["default"]._metabaseCopyModule = [];
367
+ }
363
368
  };
364
369
  return (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
365
370
  children: [store.metabaseCopyModule && store.metabaseCopyModule.length > 0 && isShowAdd && (0, _jsxRuntime.jsx)(_pandora.Tooltip, {
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports["default"] = exports.SummarizeAlias = void 0;
8
+ var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/typeof"));
8
9
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/toConsumableArray"));
9
10
  var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/objectWithoutProperties"));
10
11
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
@@ -192,14 +193,18 @@ var useStore = function useStore() {
192
193
  _useState66 = (0, _slicedToArray2["default"])(_useState65, 2),
193
194
  isMetabaseCopy = _useState66[0],
194
195
  setIsMetabaseCopy = _useState66[1]; // 是否开启勾选组复制
195
- var _useState67 = (0, _react.useState)([]),
196
+ var _useState67 = (0, _react.useState)(false),
196
197
  _useState68 = (0, _slicedToArray2["default"])(_useState67, 2),
197
- existsError = _useState68[0],
198
- setExistsError = _useState68[1]; // exists 的错误字段
199
- var _useState69 = (0, _react.useState)(_storage["default"]._metabaseCopyModule || []),
198
+ joinAliasConflictCheck = _useState68[0],
199
+ setJoinAliasConflictCheck = _useState68[1]; // 是否开启关联别名冲突检测
200
+ var _useState69 = (0, _react.useState)([]),
200
201
  _useState70 = (0, _slicedToArray2["default"])(_useState69, 2),
201
- metabaseCopyModule = _useState70[0],
202
- setMetabaseCopyModule = _useState70[1]; // 复制的内容
202
+ existsError = _useState70[0],
203
+ setExistsError = _useState70[1]; // exists 的错误字段
204
+ var _useState71 = (0, _react.useState)(_storage["default"]._metabaseCopyModule || []),
205
+ _useState72 = (0, _slicedToArray2["default"])(_useState71, 2),
206
+ metabaseCopyModule = _useState72[0],
207
+ setMetabaseCopyModule = _useState72[1]; // 复制的内容
203
208
  var setFormulaTemplates = function setFormulaTemplates(data) {
204
209
  var newData = data === null || data === void 0 ? void 0 : data.map(function (v) {
205
210
  var children = v.children.map(function (vv) {
@@ -611,10 +616,102 @@ var useStore = function useStore() {
611
616
  _setMeta(defaultMeta);
612
617
  }
613
618
  };
614
- var getNewMeta = function getNewMeta(_type, index, groupIndex, obj) {
619
+ // 重新生成 UUID 的辅助函数
620
+ // 只修改 table2 中的 alias、tableUuid,expressions 中相等的 tableUuid 和 tableAlias 同步修改
621
+ var _regenerateUuid = function regenerateUuid(obj) {
622
+ var uuidMap = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
623
+ var aliasMap = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
624
+ var tableAliasMap = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
625
+ var existingAliases = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
626
+ var originalTable2Uuid = arguments.length > 5 ? arguments[5] : undefined;
627
+ var
628
+ // 原始 table2 的 tableUuid,用于同步 expressions
629
+ newTable2Uuid = arguments.length > 6 ? arguments[6] : undefined;
630
+ var
631
+ // 新的 table2 的 tableUuid
632
+ originalTable2Alias = arguments.length > 7 ? arguments[7] : undefined;
633
+ if (!obj) return obj;
634
+ if (Array.isArray(obj)) {
635
+ return obj.map(function (item) {
636
+ return _regenerateUuid(item, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
637
+ });
638
+ }
639
+ if ((0, _typeof2["default"])(obj) === 'object') {
640
+ var newObj = (0, _objectSpread2["default"])({}, obj);
641
+ // 处理 joinData 类型:只修改 table2 的 alias 和 tableUuid
642
+ if (newObj.type === 'joinData') {
643
+ if (newObj.table2) {
644
+ var table2 = (0, _objectSpread2["default"])({}, newObj.table2);
645
+ var originalAlias = table2.alias;
646
+ // 修改 table2.alias
647
+ if (table2.alias) {
648
+ if (!aliasMap[table2.alias]) {
649
+ table2.alias = (0, _helper.getAlias)(table2.alias, [], [], [].concat((0, _toConsumableArray2["default"])(Object.values(aliasMap)), (0, _toConsumableArray2["default"])(existingAliases)));
650
+ aliasMap[originalAlias] = table2.alias; // 用原始 alias 作为 key
651
+ } else {
652
+ table2.alias = aliasMap[table2.alias];
653
+ }
654
+ }
655
+ // 修改 table2.tableUuid
656
+ if (table2.tableUuid && !uuidMap[table2.tableUuid]) {
657
+ var originalUuid = table2.tableUuid;
658
+ var newUuid = (0, _helper.uuidv4)('table');
659
+ uuidMap[originalUuid] = newUuid;
660
+ table2.tableUuid = newUuid;
661
+ // 记录原始和新 tableUuid,用于 expressions 同步
662
+ originalTable2Uuid = originalUuid;
663
+ newTable2Uuid = newUuid;
664
+ } else if (uuidMap[table2.tableUuid]) {
665
+ table2.tableUuid = uuidMap[table2.tableUuid];
666
+ newTable2Uuid = table2.tableUuid;
667
+ }
668
+ // 记录原始 alias,用于 expressions 同步 tableAlias
669
+ if (originalAlias) {
670
+ originalTable2Alias = originalAlias;
671
+ }
672
+ newObj.table2 = table2;
673
+ }
674
+ }
675
+ // 处理 expressions 中 tableUuid 相等的情况
676
+ if (originalTable2Uuid && newTable2Uuid && newObj.tableUuid === originalTable2Uuid) {
677
+ newObj.tableUuid = newTable2Uuid;
678
+ // 如果 originalTable2Alias 存在,tableAlias 要跟修改后的 table2.alias 保持一致
679
+ if (originalTable2Alias && aliasMap[originalTable2Alias]) {
680
+ newObj.tableAlias = aliasMap[originalTable2Alias];
681
+ }
682
+ }
683
+ // 递归处理嵌套对象
684
+ if (newObj.filter) newObj.filter = _regenerateUuid(newObj.filter, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
685
+ if (newObj.lhs) newObj.lhs = _regenerateUuid(newObj.lhs, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
686
+ if (newObj.rhs) newObj.rhs = _regenerateUuid(newObj.rhs, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
687
+ if (newObj.expressions) newObj.expressions = _regenerateUuid(newObj.expressions, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
688
+ if (newObj.customColumn) newObj.customColumn = _regenerateUuid(newObj.customColumn, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
689
+ if (newObj.group) newObj.group = _regenerateUuid(newObj.group, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
690
+ if (newObj.by) newObj.by = _regenerateUuid(newObj.by, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
691
+ if (newObj.columns) newObj.columns = _regenerateUuid(newObj.columns, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
692
+ if (newObj.subquery) newObj.subquery = _regenerateUuid(newObj.subquery, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
693
+ if (newObj.list) newObj.list = _regenerateUuid(newObj.list, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
694
+ if (newObj.table) newObj.table = _regenerateUuid(newObj.table, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
695
+ if (newObj.args) newObj.args = _regenerateUuid(newObj.args, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
696
+ return newObj;
697
+ }
698
+ return obj;
699
+ };
700
+ var getNewMeta = function getNewMeta(_type, index, groupIndex, obj, uuidMap, aliasMap, tableAliasMap, existingAliases) {
615
701
  var type = _type || obj.type;
616
702
  var item = false;
617
703
  var newMeta = metaList[groupIndex].list.slice();
704
+ // 如果没有传入 existingAliases,从 metaList 中提取
705
+ if (!existingAliases && newMeta) {
706
+ existingAliases = newMeta === null || newMeta === void 0 ? void 0 : newMeta.map(function (v) {
707
+ var _v$table3, _v$table4;
708
+ if (v.type === _enum.TypeEnum.data) return (_v$table3 = v.table) === null || _v$table3 === void 0 ? void 0 : _v$table3.alias;
709
+ if (v.type === _enum.TypeEnum.joinData) return (_v$table4 = v.table2) === null || _v$table4 === void 0 ? void 0 : _v$table4.alias;
710
+ return undefined;
711
+ }).filter(function (alias) {
712
+ return Boolean(alias);
713
+ });
714
+ }
618
715
  var mainTable = newMeta[0];
619
716
  metaKey += 1;
620
717
  if (type === _enum.TypeEnum.joinData) {
@@ -724,28 +821,105 @@ var useStore = function useStore() {
724
821
  return false;
725
822
  }
726
823
  if (obj) {
727
- item = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, obj), {}, {
728
- metaKey: metaKey
729
- });
824
+ // 只对 joinData 类型调用 regenerateUuid
825
+ if (obj.type === _enum.TypeEnum.joinData) {
826
+ item = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, _regenerateUuid(obj, uuidMap || {}, aliasMap || {}, tableAliasMap || {}, existingAliases || [])), {}, {
827
+ metaKey: metaKey
828
+ });
829
+ } else {
830
+ item = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, obj), {}, {
831
+ metaKey: metaKey
832
+ });
833
+ }
730
834
  }
731
835
  return item;
732
836
  };
733
- var addMeta = function addMeta(type, index, groupIndex, obj) {
837
+ // 检查粘贴位置上下级的 table1 是否与被粘贴项的 table1 有相同的 alias
838
+ var checkTable1Conflict = function checkTable1Conflict(objList, index, groupIndex) {
839
+ var _metaList$groupIndex, _objList$;
840
+ if (!objList || objList.length === 0) return {
841
+ hasConflict: false
842
+ };
843
+ var groupList = ((_metaList$groupIndex = metaList[groupIndex]) === null || _metaList$groupIndex === void 0 ? void 0 : _metaList$groupIndex.list) || [];
844
+ // 获取被粘贴项的 table1 alias
845
+ var pastedTable1Alias = (_objList$ = objList[0]) === null || _objList$ === void 0 || (_objList$ = _objList$.table1) === null || _objList$ === void 0 ? void 0 : _objList$.alias;
846
+ if (!pastedTable1Alias) return {
847
+ hasConflict: false
848
+ };
849
+ // 检查上下级的 table1 和 table2 alias
850
+ var checkItem = function checkItem(item) {
851
+ if (!item) return undefined;
852
+ if (item.type === _enum.TypeEnum.joinData) {
853
+ var _item$table, _item$table2;
854
+ if (((_item$table = item.table1) === null || _item$table === void 0 ? void 0 : _item$table.alias) === pastedTable1Alias) return item.table1.alias;
855
+ if (((_item$table2 = item.table2) === null || _item$table2 === void 0 ? void 0 : _item$table2.alias) === pastedTable1Alias) return item.table2.alias;
856
+ }
857
+ if (item.type === _enum.TypeEnum.data) {
858
+ var _item$table3;
859
+ if (((_item$table3 = item.table) === null || _item$table3 === void 0 ? void 0 : _item$table3.alias) === pastedTable1Alias) return item.table.alias;
860
+ }
861
+ return undefined;
862
+ };
863
+ // 检查上邻(index - 1 位置)
864
+ var prevItem = groupList[index - 1];
865
+ var prevConflict = checkItem(prevItem);
866
+ if (prevConflict) {
867
+ return {
868
+ hasConflict: true,
869
+ conflictingAlias: pastedTable1Alias
870
+ };
871
+ }
872
+ // 检查下邻(index 位置,即被插入位置的原元素)
873
+ var nextItem = groupList[index];
874
+ var nextConflict = checkItem(nextItem);
875
+ if (nextConflict) {
876
+ return {
877
+ hasConflict: true,
878
+ conflictingAlias: pastedTable1Alias
879
+ };
880
+ }
881
+ return {
882
+ hasConflict: false
883
+ };
884
+ };
885
+ var addMeta = function addMeta(type, index, groupIndex, obj, onConflict) {
886
+ // 检查 table1 冲突(仅在启用配置时检测)
887
+ if (joinAliasConflictCheck && obj && obj.length > 0) {
888
+ var conflict = checkTable1Conflict(obj, index, groupIndex);
889
+ if (conflict.hasConflict && conflict.conflictingAlias && onConflict) {
890
+ onConflict(conflict.conflictingAlias);
891
+ return false;
892
+ }
893
+ }
734
894
  var newMeta = metaList[groupIndex].list.slice();
735
895
  var items = [];
896
+ // 创建共享的映射对象,确保循环调用时保持一致
897
+ var sharedUuidMap = {};
898
+ var sharedAliasMap = {};
899
+ var sharedTableAliasMap = {};
900
+ // 提取当前组中已存在的 alias
901
+ var existingAliases = newMeta === null || newMeta === void 0 ? void 0 : newMeta.map(function (v) {
902
+ var _v$table5, _v$table6;
903
+ if (v.type === _enum.TypeEnum.data) return (_v$table5 = v.table) === null || _v$table5 === void 0 ? void 0 : _v$table5.alias;
904
+ if (v.type === _enum.TypeEnum.joinData) return (_v$table6 = v.table2) === null || _v$table6 === void 0 ? void 0 : _v$table6.alias;
905
+ return undefined;
906
+ }).filter(function (alias) {
907
+ return Boolean(alias);
908
+ });
736
909
  if (obj) {
737
910
  items = obj.map(function (v) {
738
- return getNewMeta(type, index, groupIndex, v);
911
+ return getNewMeta(type, index, groupIndex, v, sharedUuidMap, sharedAliasMap, sharedTableAliasMap, existingAliases);
739
912
  });
740
913
  } else {
741
- items = [getNewMeta(type, index, groupIndex, obj)];
914
+ items = [getNewMeta(type, index, groupIndex, obj, sharedUuidMap, sharedAliasMap, sharedTableAliasMap, existingAliases)];
742
915
  }
743
916
  if (items[0] === false) {
744
- return;
917
+ return false;
745
918
  }
746
919
  // @ts-ignore
747
920
  newMeta.splice.apply(newMeta, [index, 0].concat((0, _toConsumableArray2["default"])(items)));
748
921
  setMeta(newMeta, groupIndex);
922
+ return true;
749
923
  };
750
924
  var delMeta = function delMeta(meta, groupIndex) {
751
925
  if (meta.type === 'group') {
@@ -914,7 +1088,9 @@ var useStore = function useStore() {
914
1088
  operatorList: operatorList,
915
1089
  setOperatorList: setOperatorList,
916
1090
  setExistsErrorFn: setExistsErrorFn,
917
- existsError: existsError
1091
+ existsError: existsError,
1092
+ joinAliasConflictCheck: joinAliasConflictCheck,
1093
+ setJoinAliasConflictCheck: setJoinAliasConflictCheck
918
1094
  };
919
1095
  };
920
1096
  var _default = exports["default"] = useStore;
package/lib/cjs/index.js CHANGED
@@ -81,6 +81,8 @@ var SqlVisionBuilder = _react["default"].forwardRef(function (props, ref) {
81
81
  moduleDiff = _props$moduleDiff === void 0 ? false : _props$moduleDiff,
82
82
  _props$moduleCopy = props.moduleCopy,
83
83
  moduleCopy = _props$moduleCopy === void 0 ? false : _props$moduleCopy,
84
+ _props$joinAliasConfl = props.joinAliasConflictCheck,
85
+ joinAliasConflictCheck = _props$joinAliasConfl === void 0 ? false : _props$joinAliasConfl,
84
86
  _props$operatorList = props.operatorList,
85
87
  operatorList = _props$operatorList === void 0 ? EMPTY_ARRAY : _props$operatorList;
86
88
  var store = (0, _useState["default"])();
@@ -125,8 +127,9 @@ var SqlVisionBuilder = _react["default"].forwardRef(function (props, ref) {
125
127
  store.setModuleDiff(moduleDiff);
126
128
  store.setIsSubquery(isSubquery);
127
129
  store.setModuleCopy(moduleCopy);
130
+ store.setJoinAliasConflictCheck(joinAliasConflictCheck);
128
131
  store.setOperatorList(operatorList || []);
129
- }, [showFields, fieldNameTpl, tableNameTpl, toolbar, showSubquery, subShowSubquery, constantList, formulaTemplates, ignoreGroupByType, filterCustomType, isExit, tableEnableAlias, fieldEnableAlias, groupByEnableAlias, isSelectFields, tableFlat, copyType, metabaseCopy, moduleDiff, isSubquery, moduleCopy, operatorList]);
132
+ }, [showFields, fieldNameTpl, tableNameTpl, toolbar, showSubquery, subShowSubquery, constantList, formulaTemplates, ignoreGroupByType, filterCustomType, isExit, tableEnableAlias, fieldEnableAlias, groupByEnableAlias, isSelectFields, tableFlat, copyType, metabaseCopy, moduleDiff, isSubquery, moduleCopy, joinAliasConflictCheck, operatorList]);
130
133
  _react["default"].useImperativeHandle(ref, function () {
131
134
  return {
132
135
  // setDatasource: (list) => {
@@ -52,6 +52,7 @@ export interface MetabaseProps {
52
52
  metabaseCopy?: boolean;
53
53
  moduleDiff?: boolean;
54
54
  moduleCopy?: boolean;
55
+ joinAliasConflictCheck?: boolean;
55
56
  getModuleDiffCode?: (obj: {
56
57
  oldCode: MetaListType;
57
58
  newCode: MetaListType;
@@ -16,5 +16,5 @@ export declare const replaceTpl: (inputString: string, values: {
16
16
  [key: string]: any;
17
17
  }) => string;
18
18
  export declare const uuidv4: (prefix?: string) => string;
19
- export declare const getAlias: (name: string | undefined, list: MetaListType[], columns?: any[]) => string;
19
+ export declare const getAlias: (name?: string, list?: MetaListType[], columns?: any[], extraAliases?: string[]) => string;
20
20
  export declare const mergeNameAlias: (name: string, alias: string, isEnable: boolean) => string;
@@ -140,15 +140,32 @@ var uuidv4 = exports.uuidv4 = function uuidv4() {
140
140
  };
141
141
  var getAlias = exports.getAlias = function getAlias() {
142
142
  var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
143
- var list = arguments.length > 1 ? arguments[1] : undefined;
143
+ var list = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
144
144
  var columns = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
145
+ var extraAliases = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
145
146
  // 提取所有有效的 alias
146
- if (!list || list.length < 1) return name;
147
- // const baseOld = oldName?.split('_')[0] ?? '';
148
- // const baseNew = name?.split('_')[0] ?? '';
149
- // if (oldName && baseOld === baseNew) {
150
- // return oldName;
151
- // }
147
+ if (!list || list.length < 1) {
148
+ // 如果没有 list,只有 extraAliases,需要检查 extraAliases 中是否有冲突
149
+ if (extraAliases.length === 0) return name;
150
+ var _regex = new RegExp("^".concat(name, "_(\\d+)$"));
151
+ var _indexes = extraAliases.map(function (alias) {
152
+ var match = alias.match(_regex);
153
+ return match ? parseInt(match[1], 10) : 0;
154
+ }).filter(function (num) {
155
+ return num > 0;
156
+ });
157
+ var _hasPureName = extraAliases.includes(name);
158
+ if (_hasPureName) {
159
+ _indexes.push(1);
160
+ }
161
+ if (_indexes.length === 0) {
162
+ return name;
163
+ }
164
+ var _uniqueIndexes = (0, _toConsumableArray2["default"])(new Set(_indexes)).sort(function (a, b) {
165
+ return a - b;
166
+ });
167
+ return "".concat(name, "_").concat(Math.max.apply(Math, (0, _toConsumableArray2["default"])(_uniqueIndexes)) + 1);
168
+ }
152
169
  var aliass = list === null || list === void 0 ? void 0 : list.map(function (v) {
153
170
  if (v.type === _enum.TypeEnum.data) {
154
171
  var _v$table;
@@ -161,7 +178,7 @@ var getAlias = exports.getAlias = function getAlias() {
161
178
  return undefined;
162
179
  }).concat(columns.map(function (v) {
163
180
  return v.alias;
164
- })).filter(function (alias) {
181
+ })).concat(extraAliases).filter(function (alias) {
165
182
  return Boolean(alias);
166
183
  });
167
184
  // 收集所有 name_x 中的数字
@@ -5,7 +5,7 @@ import React, { useEffect } from 'react';
5
5
  import cx from 'classnames';
6
6
  import { __ } from '../../../locale';
7
7
  import { TypeEnum } from '../../../store/enum';
8
- import { Tooltip, Button } from '@gingkoo/pandora';
8
+ import { Tooltip, Button, Toast } from '@gingkoo/pandora';
9
9
  import { FfPlus } from '@gingkoo/pandora-icons';
10
10
  import storage from '../../../utils/storage';
11
11
  import { getHelper, isCanPaste } from '../../../utils';
@@ -345,11 +345,16 @@ var NextDom = function NextDom(props) {
345
345
  var handlePaste = function handlePaste() {
346
346
  var _store$metaList3;
347
347
  var index = (_store$metaList3 = store.metaList) === null || _store$metaList3 === void 0 || (_store$metaList3 = _store$metaList3[groupIndex]) === null || _store$metaList3 === void 0 || (_store$metaList3 = _store$metaList3.list) === null || _store$metaList3 === void 0 ? void 0 : _store$metaList3.indexOf(meta);
348
- store.addMeta('', index + 1, groupIndex, store.metabaseCopyModule);
349
- // 粘贴后重置
350
- store.setMetabaseCopyModule([]);
351
- store.setIsMetabaseCopy(false);
352
- storage._metabaseCopyModule = [];
348
+ var success = store.addMeta('', index + 1, groupIndex, store.metabaseCopyModule, function (conflictingAlias) {
349
+ // 冲突提示
350
+ Toast.warning("\u8BE5\u8868\u5DF2\u5B58\u5728\u5173\u8054\uFF0C\u4E0D\u80FD\u590D\u5236");
351
+ });
352
+ // 仅在成功时重置
353
+ if (success) {
354
+ store.setMetabaseCopyModule([]);
355
+ store.setIsMetabaseCopy(false);
356
+ storage._metabaseCopyModule = [];
357
+ }
353
358
  };
354
359
  return _jsxs(_Fragment, {
355
360
  children: [store.metabaseCopyModule && store.metabaseCopyModule.length > 0 && isShowAdd && _jsx(Tooltip, {
@@ -1,3 +1,4 @@
1
+ import _typeof from "@babel/runtime/helpers/esm/typeof";
1
2
  import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
3
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3
4
  import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
@@ -6,7 +7,7 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
6
7
  var _excluded = ["alias", "datasourceName", "datasourceId", "column"];
7
8
  import _regeneratorRuntime from "@babel/runtime/regenerator";
8
9
  import { useEffect, useState, useRef, useMemo } from 'react';
9
- import { uuidv4 } from '../utils/helper';
10
+ import { uuidv4, getAlias } from '../utils/helper';
10
11
  import { TypeEnum, JoinEnum, UnionEnum } from '../store/enum';
11
12
  import { splitByUnion } from '../utils';
12
13
  import storage from '../utils/storage';
@@ -186,14 +187,18 @@ var useStore = function useStore() {
186
187
  _useState66 = _slicedToArray(_useState65, 2),
187
188
  isMetabaseCopy = _useState66[0],
188
189
  setIsMetabaseCopy = _useState66[1]; // 是否开启勾选组复制
189
- var _useState67 = useState([]),
190
+ var _useState67 = useState(false),
190
191
  _useState68 = _slicedToArray(_useState67, 2),
191
- existsError = _useState68[0],
192
- setExistsError = _useState68[1]; // exists 的错误字段
193
- var _useState69 = useState(storage._metabaseCopyModule || []),
192
+ joinAliasConflictCheck = _useState68[0],
193
+ setJoinAliasConflictCheck = _useState68[1]; // 是否开启关联别名冲突检测
194
+ var _useState69 = useState([]),
194
195
  _useState70 = _slicedToArray(_useState69, 2),
195
- metabaseCopyModule = _useState70[0],
196
- setMetabaseCopyModule = _useState70[1]; // 复制的内容
196
+ existsError = _useState70[0],
197
+ setExistsError = _useState70[1]; // exists 的错误字段
198
+ var _useState71 = useState(storage._metabaseCopyModule || []),
199
+ _useState72 = _slicedToArray(_useState71, 2),
200
+ metabaseCopyModule = _useState72[0],
201
+ setMetabaseCopyModule = _useState72[1]; // 复制的内容
197
202
  var setFormulaTemplates = function setFormulaTemplates(data) {
198
203
  var newData = data === null || data === void 0 ? void 0 : data.map(function (v) {
199
204
  var children = v.children.map(function (vv) {
@@ -605,10 +610,102 @@ var useStore = function useStore() {
605
610
  _setMeta(defaultMeta);
606
611
  }
607
612
  };
608
- var getNewMeta = function getNewMeta(_type, index, groupIndex, obj) {
613
+ // 重新生成 UUID 的辅助函数
614
+ // 只修改 table2 中的 alias、tableUuid,expressions 中相等的 tableUuid 和 tableAlias 同步修改
615
+ var _regenerateUuid = function regenerateUuid(obj) {
616
+ var uuidMap = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
617
+ var aliasMap = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
618
+ var tableAliasMap = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
619
+ var existingAliases = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
620
+ var originalTable2Uuid = arguments.length > 5 ? arguments[5] : undefined;
621
+ var
622
+ // 原始 table2 的 tableUuid,用于同步 expressions
623
+ newTable2Uuid = arguments.length > 6 ? arguments[6] : undefined;
624
+ var
625
+ // 新的 table2 的 tableUuid
626
+ originalTable2Alias = arguments.length > 7 ? arguments[7] : undefined;
627
+ if (!obj) return obj;
628
+ if (Array.isArray(obj)) {
629
+ return obj.map(function (item) {
630
+ return _regenerateUuid(item, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
631
+ });
632
+ }
633
+ if (_typeof(obj) === 'object') {
634
+ var newObj = _objectSpread({}, obj);
635
+ // 处理 joinData 类型:只修改 table2 的 alias 和 tableUuid
636
+ if (newObj.type === 'joinData') {
637
+ if (newObj.table2) {
638
+ var table2 = _objectSpread({}, newObj.table2);
639
+ var originalAlias = table2.alias;
640
+ // 修改 table2.alias
641
+ if (table2.alias) {
642
+ if (!aliasMap[table2.alias]) {
643
+ table2.alias = getAlias(table2.alias, [], [], [].concat(_toConsumableArray(Object.values(aliasMap)), _toConsumableArray(existingAliases)));
644
+ aliasMap[originalAlias] = table2.alias; // 用原始 alias 作为 key
645
+ } else {
646
+ table2.alias = aliasMap[table2.alias];
647
+ }
648
+ }
649
+ // 修改 table2.tableUuid
650
+ if (table2.tableUuid && !uuidMap[table2.tableUuid]) {
651
+ var originalUuid = table2.tableUuid;
652
+ var newUuid = uuidv4('table');
653
+ uuidMap[originalUuid] = newUuid;
654
+ table2.tableUuid = newUuid;
655
+ // 记录原始和新 tableUuid,用于 expressions 同步
656
+ originalTable2Uuid = originalUuid;
657
+ newTable2Uuid = newUuid;
658
+ } else if (uuidMap[table2.tableUuid]) {
659
+ table2.tableUuid = uuidMap[table2.tableUuid];
660
+ newTable2Uuid = table2.tableUuid;
661
+ }
662
+ // 记录原始 alias,用于 expressions 同步 tableAlias
663
+ if (originalAlias) {
664
+ originalTable2Alias = originalAlias;
665
+ }
666
+ newObj.table2 = table2;
667
+ }
668
+ }
669
+ // 处理 expressions 中 tableUuid 相等的情况
670
+ if (originalTable2Uuid && newTable2Uuid && newObj.tableUuid === originalTable2Uuid) {
671
+ newObj.tableUuid = newTable2Uuid;
672
+ // 如果 originalTable2Alias 存在,tableAlias 要跟修改后的 table2.alias 保持一致
673
+ if (originalTable2Alias && aliasMap[originalTable2Alias]) {
674
+ newObj.tableAlias = aliasMap[originalTable2Alias];
675
+ }
676
+ }
677
+ // 递归处理嵌套对象
678
+ if (newObj.filter) newObj.filter = _regenerateUuid(newObj.filter, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
679
+ if (newObj.lhs) newObj.lhs = _regenerateUuid(newObj.lhs, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
680
+ if (newObj.rhs) newObj.rhs = _regenerateUuid(newObj.rhs, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
681
+ if (newObj.expressions) newObj.expressions = _regenerateUuid(newObj.expressions, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
682
+ if (newObj.customColumn) newObj.customColumn = _regenerateUuid(newObj.customColumn, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
683
+ if (newObj.group) newObj.group = _regenerateUuid(newObj.group, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
684
+ if (newObj.by) newObj.by = _regenerateUuid(newObj.by, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
685
+ if (newObj.columns) newObj.columns = _regenerateUuid(newObj.columns, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
686
+ if (newObj.subquery) newObj.subquery = _regenerateUuid(newObj.subquery, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
687
+ if (newObj.list) newObj.list = _regenerateUuid(newObj.list, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
688
+ if (newObj.table) newObj.table = _regenerateUuid(newObj.table, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
689
+ if (newObj.args) newObj.args = _regenerateUuid(newObj.args, uuidMap, aliasMap, tableAliasMap, existingAliases, originalTable2Uuid, newTable2Uuid, originalTable2Alias);
690
+ return newObj;
691
+ }
692
+ return obj;
693
+ };
694
+ var getNewMeta = function getNewMeta(_type, index, groupIndex, obj, uuidMap, aliasMap, tableAliasMap, existingAliases) {
609
695
  var type = _type || obj.type;
610
696
  var item = false;
611
697
  var newMeta = metaList[groupIndex].list.slice();
698
+ // 如果没有传入 existingAliases,从 metaList 中提取
699
+ if (!existingAliases && newMeta) {
700
+ existingAliases = newMeta === null || newMeta === void 0 ? void 0 : newMeta.map(function (v) {
701
+ var _v$table3, _v$table4;
702
+ if (v.type === TypeEnum.data) return (_v$table3 = v.table) === null || _v$table3 === void 0 ? void 0 : _v$table3.alias;
703
+ if (v.type === TypeEnum.joinData) return (_v$table4 = v.table2) === null || _v$table4 === void 0 ? void 0 : _v$table4.alias;
704
+ return undefined;
705
+ }).filter(function (alias) {
706
+ return Boolean(alias);
707
+ });
708
+ }
612
709
  var mainTable = newMeta[0];
613
710
  metaKey += 1;
614
711
  if (type === TypeEnum.joinData) {
@@ -718,28 +815,105 @@ var useStore = function useStore() {
718
815
  return false;
719
816
  }
720
817
  if (obj) {
721
- item = _objectSpread(_objectSpread({}, obj), {}, {
722
- metaKey: metaKey
723
- });
818
+ // 只对 joinData 类型调用 regenerateUuid
819
+ if (obj.type === TypeEnum.joinData) {
820
+ item = _objectSpread(_objectSpread({}, _regenerateUuid(obj, uuidMap || {}, aliasMap || {}, tableAliasMap || {}, existingAliases || [])), {}, {
821
+ metaKey: metaKey
822
+ });
823
+ } else {
824
+ item = _objectSpread(_objectSpread({}, obj), {}, {
825
+ metaKey: metaKey
826
+ });
827
+ }
724
828
  }
725
829
  return item;
726
830
  };
727
- var addMeta = function addMeta(type, index, groupIndex, obj) {
831
+ // 检查粘贴位置上下级的 table1 是否与被粘贴项的 table1 有相同的 alias
832
+ var checkTable1Conflict = function checkTable1Conflict(objList, index, groupIndex) {
833
+ var _metaList$groupIndex, _objList$;
834
+ if (!objList || objList.length === 0) return {
835
+ hasConflict: false
836
+ };
837
+ var groupList = ((_metaList$groupIndex = metaList[groupIndex]) === null || _metaList$groupIndex === void 0 ? void 0 : _metaList$groupIndex.list) || [];
838
+ // 获取被粘贴项的 table1 alias
839
+ var pastedTable1Alias = (_objList$ = objList[0]) === null || _objList$ === void 0 || (_objList$ = _objList$.table1) === null || _objList$ === void 0 ? void 0 : _objList$.alias;
840
+ if (!pastedTable1Alias) return {
841
+ hasConflict: false
842
+ };
843
+ // 检查上下级的 table1 和 table2 alias
844
+ var checkItem = function checkItem(item) {
845
+ if (!item) return undefined;
846
+ if (item.type === TypeEnum.joinData) {
847
+ var _item$table, _item$table2;
848
+ if (((_item$table = item.table1) === null || _item$table === void 0 ? void 0 : _item$table.alias) === pastedTable1Alias) return item.table1.alias;
849
+ if (((_item$table2 = item.table2) === null || _item$table2 === void 0 ? void 0 : _item$table2.alias) === pastedTable1Alias) return item.table2.alias;
850
+ }
851
+ if (item.type === TypeEnum.data) {
852
+ var _item$table3;
853
+ if (((_item$table3 = item.table) === null || _item$table3 === void 0 ? void 0 : _item$table3.alias) === pastedTable1Alias) return item.table.alias;
854
+ }
855
+ return undefined;
856
+ };
857
+ // 检查上邻(index - 1 位置)
858
+ var prevItem = groupList[index - 1];
859
+ var prevConflict = checkItem(prevItem);
860
+ if (prevConflict) {
861
+ return {
862
+ hasConflict: true,
863
+ conflictingAlias: pastedTable1Alias
864
+ };
865
+ }
866
+ // 检查下邻(index 位置,即被插入位置的原元素)
867
+ var nextItem = groupList[index];
868
+ var nextConflict = checkItem(nextItem);
869
+ if (nextConflict) {
870
+ return {
871
+ hasConflict: true,
872
+ conflictingAlias: pastedTable1Alias
873
+ };
874
+ }
875
+ return {
876
+ hasConflict: false
877
+ };
878
+ };
879
+ var addMeta = function addMeta(type, index, groupIndex, obj, onConflict) {
880
+ // 检查 table1 冲突(仅在启用配置时检测)
881
+ if (joinAliasConflictCheck && obj && obj.length > 0) {
882
+ var conflict = checkTable1Conflict(obj, index, groupIndex);
883
+ if (conflict.hasConflict && conflict.conflictingAlias && onConflict) {
884
+ onConflict(conflict.conflictingAlias);
885
+ return false;
886
+ }
887
+ }
728
888
  var newMeta = metaList[groupIndex].list.slice();
729
889
  var items = [];
890
+ // 创建共享的映射对象,确保循环调用时保持一致
891
+ var sharedUuidMap = {};
892
+ var sharedAliasMap = {};
893
+ var sharedTableAliasMap = {};
894
+ // 提取当前组中已存在的 alias
895
+ var existingAliases = newMeta === null || newMeta === void 0 ? void 0 : newMeta.map(function (v) {
896
+ var _v$table5, _v$table6;
897
+ if (v.type === TypeEnum.data) return (_v$table5 = v.table) === null || _v$table5 === void 0 ? void 0 : _v$table5.alias;
898
+ if (v.type === TypeEnum.joinData) return (_v$table6 = v.table2) === null || _v$table6 === void 0 ? void 0 : _v$table6.alias;
899
+ return undefined;
900
+ }).filter(function (alias) {
901
+ return Boolean(alias);
902
+ });
730
903
  if (obj) {
731
904
  items = obj.map(function (v) {
732
- return getNewMeta(type, index, groupIndex, v);
905
+ return getNewMeta(type, index, groupIndex, v, sharedUuidMap, sharedAliasMap, sharedTableAliasMap, existingAliases);
733
906
  });
734
907
  } else {
735
- items = [getNewMeta(type, index, groupIndex, obj)];
908
+ items = [getNewMeta(type, index, groupIndex, obj, sharedUuidMap, sharedAliasMap, sharedTableAliasMap, existingAliases)];
736
909
  }
737
910
  if (items[0] === false) {
738
- return;
911
+ return false;
739
912
  }
740
913
  // @ts-ignore
741
914
  newMeta.splice.apply(newMeta, [index, 0].concat(_toConsumableArray(items)));
742
915
  setMeta(newMeta, groupIndex);
916
+ return true;
743
917
  };
744
918
  var delMeta = function delMeta(meta, groupIndex) {
745
919
  if (meta.type === 'group') {
@@ -908,7 +1082,9 @@ var useStore = function useStore() {
908
1082
  operatorList: operatorList,
909
1083
  setOperatorList: setOperatorList,
910
1084
  setExistsErrorFn: setExistsErrorFn,
911
- existsError: existsError
1085
+ existsError: existsError,
1086
+ joinAliasConflictCheck: joinAliasConflictCheck,
1087
+ setJoinAliasConflictCheck: setJoinAliasConflictCheck
912
1088
  };
913
1089
  };
914
1090
  export default useStore;
package/lib/es/index.js CHANGED
@@ -71,6 +71,8 @@ var SqlVisionBuilder = React.forwardRef(function (props, ref) {
71
71
  moduleDiff = _props$moduleDiff === void 0 ? false : _props$moduleDiff,
72
72
  _props$moduleCopy = props.moduleCopy,
73
73
  moduleCopy = _props$moduleCopy === void 0 ? false : _props$moduleCopy,
74
+ _props$joinAliasConfl = props.joinAliasConflictCheck,
75
+ joinAliasConflictCheck = _props$joinAliasConfl === void 0 ? false : _props$joinAliasConfl,
74
76
  _props$operatorList = props.operatorList,
75
77
  operatorList = _props$operatorList === void 0 ? EMPTY_ARRAY : _props$operatorList;
76
78
  var store = useData();
@@ -115,8 +117,9 @@ var SqlVisionBuilder = React.forwardRef(function (props, ref) {
115
117
  store.setModuleDiff(moduleDiff);
116
118
  store.setIsSubquery(isSubquery);
117
119
  store.setModuleCopy(moduleCopy);
120
+ store.setJoinAliasConflictCheck(joinAliasConflictCheck);
118
121
  store.setOperatorList(operatorList || []);
119
- }, [showFields, fieldNameTpl, tableNameTpl, toolbar, showSubquery, subShowSubquery, constantList, formulaTemplates, ignoreGroupByType, filterCustomType, isExit, tableEnableAlias, fieldEnableAlias, groupByEnableAlias, isSelectFields, tableFlat, copyType, metabaseCopy, moduleDiff, isSubquery, moduleCopy, operatorList]);
122
+ }, [showFields, fieldNameTpl, tableNameTpl, toolbar, showSubquery, subShowSubquery, constantList, formulaTemplates, ignoreGroupByType, filterCustomType, isExit, tableEnableAlias, fieldEnableAlias, groupByEnableAlias, isSelectFields, tableFlat, copyType, metabaseCopy, moduleDiff, isSubquery, moduleCopy, joinAliasConflictCheck, operatorList]);
120
123
  React.useImperativeHandle(ref, function () {
121
124
  return {
122
125
  // setDatasource: (list) => {
package/lib/es/types.d.ts CHANGED
@@ -52,6 +52,7 @@ export interface MetabaseProps {
52
52
  metabaseCopy?: boolean;
53
53
  moduleDiff?: boolean;
54
54
  moduleCopy?: boolean;
55
+ joinAliasConflictCheck?: boolean;
55
56
  getModuleDiffCode?: (obj: {
56
57
  oldCode: MetaListType;
57
58
  newCode: MetaListType;
@@ -16,5 +16,5 @@ export declare const replaceTpl: (inputString: string, values: {
16
16
  [key: string]: any;
17
17
  }) => string;
18
18
  export declare const uuidv4: (prefix?: string) => string;
19
- export declare const getAlias: (name: string | undefined, list: MetaListType[], columns?: any[]) => string;
19
+ export declare const getAlias: (name?: string, list?: MetaListType[], columns?: any[], extraAliases?: string[]) => string;
20
20
  export declare const mergeNameAlias: (name: string, alias: string, isEnable: boolean) => string;
@@ -133,15 +133,32 @@ export var uuidv4 = function uuidv4() {
133
133
  };
134
134
  export var getAlias = function getAlias() {
135
135
  var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
136
- var list = arguments.length > 1 ? arguments[1] : undefined;
136
+ var list = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
137
137
  var columns = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
138
+ var extraAliases = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
138
139
  // 提取所有有效的 alias
139
- if (!list || list.length < 1) return name;
140
- // const baseOld = oldName?.split('_')[0] ?? '';
141
- // const baseNew = name?.split('_')[0] ?? '';
142
- // if (oldName && baseOld === baseNew) {
143
- // return oldName;
144
- // }
140
+ if (!list || list.length < 1) {
141
+ // 如果没有 list,只有 extraAliases,需要检查 extraAliases 中是否有冲突
142
+ if (extraAliases.length === 0) return name;
143
+ var _regex = new RegExp("^".concat(name, "_(\\d+)$"));
144
+ var _indexes = extraAliases.map(function (alias) {
145
+ var match = alias.match(_regex);
146
+ return match ? parseInt(match[1], 10) : 0;
147
+ }).filter(function (num) {
148
+ return num > 0;
149
+ });
150
+ var _hasPureName = extraAliases.includes(name);
151
+ if (_hasPureName) {
152
+ _indexes.push(1);
153
+ }
154
+ if (_indexes.length === 0) {
155
+ return name;
156
+ }
157
+ var _uniqueIndexes = _toConsumableArray(new Set(_indexes)).sort(function (a, b) {
158
+ return a - b;
159
+ });
160
+ return "".concat(name, "_").concat(Math.max.apply(Math, _toConsumableArray(_uniqueIndexes)) + 1);
161
+ }
145
162
  var aliass = list === null || list === void 0 ? void 0 : list.map(function (v) {
146
163
  if (v.type === TypeEnum.data) {
147
164
  var _v$table;
@@ -154,7 +171,7 @@ export var getAlias = function getAlias() {
154
171
  return undefined;
155
172
  }).concat(columns.map(function (v) {
156
173
  return v.alias;
157
- })).filter(function (alias) {
174
+ })).concat(extraAliases).filter(function (alias) {
158
175
  return Boolean(alias);
159
176
  });
160
177
  // 收集所有 name_x 中的数字
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gingkoo/pandora-metabase",
3
- "version": "1.0.105",
3
+ "version": "1.0.107",
4
4
  "description": "",
5
5
  "main": "lib/es/index.js",
6
6
  "module": "lib/es/index.js",