@gingkoo/pandora-metabase 1.0.136 → 1.0.137-alpha.2

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.
@@ -43,9 +43,10 @@ var Filter = function Filter(props) {
43
43
  if (prevGroupBy !== null && prevGroupBy !== void 0 && (_prevGroupBy$group = prevGroupBy.group) !== null && _prevGroupBy$group !== void 0 && _prevGroupBy$group.length) {
44
44
  _data.columns = _data.columns.concat(prevGroupBy.group.map(function (v) {
45
45
  return _objectSpread(_objectSpread({}, v), {}, {
46
- name: v.name || v.quotes,
46
+ name: v.fieldAlias || v.name || v.quotes,
47
47
  name_zh: v.name_zh || v.quotes,
48
48
  id: v.id || v.fieldId,
49
+ fieldAlias: '',
49
50
  database_type: (v === null || v === void 0 ? void 0 : v.database_type) || SQL_COLUMN_TYPE.FLOAT,
50
51
  sql: v.sql,
51
52
  special_type: '',
@@ -56,9 +57,10 @@ var Filter = function Filter(props) {
56
57
  if (prevGroupBy !== null && prevGroupBy !== void 0 && (_prevGroupBy$by = prevGroupBy.by) !== null && _prevGroupBy$by !== void 0 && _prevGroupBy$by.length) {
57
58
  _data.columns = _data.columns.concat(prevGroupBy.by.map(function (v) {
58
59
  return _objectSpread(_objectSpread({}, v), {}, {
59
- name: v.name || v.quotes,
60
+ name: v.fieldAlias || v.name || v.quotes,
60
61
  name_zh: v.name_zh || v.quotes,
61
62
  id: v.id || v.fieldId,
63
+ fieldAlias: '',
62
64
  database_type: (v === null || v === void 0 ? void 0 : v.database_type) || SQL_COLUMN_TYPE.FLOAT,
63
65
  sql: v.sql,
64
66
  special_type: '',
@@ -11,13 +11,12 @@ import cx from 'classnames';
11
11
  import { Button, Tooltip, Input, Modal2, Modal, Toast, Dropdown, InputNumber } from '@gingkoo/pandora';
12
12
  import { FfPlus, FfLine, Repeat, RelatedWork } from '@gingkoo/pandora-icons';
13
13
  import Metabase from '../../index';
14
- import isEqual from 'lodash/isEqual';
15
14
  import cloneDeep from 'lodash/cloneDeep';
16
- import { uuidv4, getAlias, mergeNameAlias, isSameColumnByIdFirst, isSameTableByIdFirst } from '../../utils/helper';
15
+ import { uuidv4, getAlias, mergeNameAlias, isSameTableByIdFirst } from '../../utils/helper';
17
16
  import { getTopLayerZIndex } from '../../utils/helper-dom';
18
17
  import { getConstantLabel } from '../dialog/formula-list/utils';
19
18
  import { __ } from '../../locale';
20
- import { findIndex, getSubColumns } from '../../utils';
19
+ import { findIndex, getSubColumns, mergeSubQueryColumns, syncSubQueryFieldReferences } from '../../utils';
21
20
  import { TypeEnum, ColumnsPopupThemeEnum, JoinEnum, SQL_COLUMN_TYPE } from '../../store/enum';
22
21
  import { AtomsTypeEnum, ChangeType, FormulaTypeEnum } from '../../store/types';
23
22
  import { SummarizeAlias } from '../../store/helper';
@@ -33,7 +32,19 @@ import { SelectTable, SelectJoin, SelectColumnMultiple, SelectColumn, Expression
33
32
  import { ExclamationCircleFill } from '@gingkoo/pandora-icons';
34
33
  import { useStore } from '../../hooks/use-provider';
35
34
  var PrevResult = 'Previous results';
36
- var operators = ['AND', 'OR', '(', ')', ',', '+', '-', '*', '/', '!=', '<=', '>=', '<', '>', '='];
35
+ var normalizeJoinOperator = function normalizeJoinOperator(val) {
36
+ return String(val).toUpperCase() === 'OR' ? 'OR' : 'AND';
37
+ };
38
+ var isJoinOperator = function isJoinOperator(item) {
39
+ return (item === null || item === void 0 ? void 0 : item.type) === AtomsTypeEnum.OPERATOR;
40
+ };
41
+ var menuJoinOperator = [{
42
+ key: 'AND',
43
+ label: 'AND'
44
+ }, {
45
+ key: 'OR',
46
+ label: 'OR'
47
+ }];
37
48
  var FlagLocation;
38
49
  (function (FlagLocation) {
39
50
  FlagLocation["TABLE_1"] = "table1";
@@ -45,7 +56,7 @@ var ExpressionsEnum;
45
56
  ExpressionsEnum["RIGHT"] = "rhs";
46
57
  })(ExpressionsEnum || (ExpressionsEnum = {}));
47
58
  var getText = function getText(v) {
48
- return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, AtomsTypeEnum.JOIN_DEFAULT, 'joinData.default'), AtomsTypeEnum.EXPRESSION, 'joinData.expression'), AtomsTypeEnum.OPERATOR, 'customColumn.operator'), AtomsTypeEnum.COLLECTION, 'customColumn.collection')[v];
59
+ return _defineProperty(_defineProperty(_defineProperty({}, AtomsTypeEnum.JOIN_DEFAULT, 'joinData.default'), AtomsTypeEnum.EXPRESSION, 'joinData.expression'), AtomsTypeEnum.COLLECTION, 'customColumn.collection')[v];
49
60
  };
50
61
  var menuTypes = [{
51
62
  key: AtomsTypeEnum.JOIN_DEFAULT,
@@ -53,19 +64,10 @@ var menuTypes = [{
53
64
  }, {
54
65
  key: AtomsTypeEnum.EXPRESSION,
55
66
  label: __(getText(AtomsTypeEnum.EXPRESSION))
56
- }, {
57
- key: AtomsTypeEnum.OPERATOR,
58
- label: __(getText(AtomsTypeEnum.OPERATOR))
59
67
  }, {
60
68
  key: AtomsTypeEnum.COLLECTION,
61
69
  label: __(getText(AtomsTypeEnum.COLLECTION))
62
70
  }];
63
- var menuOperator = operators.map(function (v) {
64
- return {
65
- key: v,
66
- label: v
67
- };
68
- });
69
71
  var JoinData = function JoinData(props) {
70
72
  var _meta$subquery, _meta$subquery2, _meta$subquery3, _meta$table4, _meta$table5, _meta$table7, _meta$table8, _meta$table9, _meta$table0, _meta$expressions, _meta$table1, _meta$table10, _meta$expressions2, _store$showMainColumn, _store$showMainColumn2;
71
73
  var meta = props.meta,
@@ -126,6 +128,26 @@ var JoinData = function JoinData(props) {
126
128
  tableIsDel();
127
129
  }, 0);
128
130
  }, [meta]);
131
+ useEffect(function () {
132
+ var expressions = meta.expressions || [];
133
+ var hasInvalidOperator = expressions.some(function (item) {
134
+ return item.type === AtomsTypeEnum.OPERATOR && item.val !== normalizeJoinOperator(item.val);
135
+ });
136
+ if (!hasInvalidOperator) {
137
+ return;
138
+ }
139
+ var currentIndex = findIndex(store.metaList[groupIndex].list, meta);
140
+ var newMeta = store.metaList[groupIndex].list.slice();
141
+ newMeta[currentIndex].expressions = expressions.map(function (item) {
142
+ if (item.type !== AtomsTypeEnum.OPERATOR) {
143
+ return item;
144
+ }
145
+ return _objectSpread(_objectSpread({}, item), {}, {
146
+ val: normalizeJoinOperator(item.val)
147
+ });
148
+ });
149
+ store.setMeta(newMeta, groupIndex);
150
+ }, [meta.expressions]);
129
151
  var getTableColumns = function getTableColumns() {
130
152
  var data = [];
131
153
  var _getHelper2 = getHelper(store.metaList[groupIndex].list, meta),
@@ -952,7 +974,6 @@ var JoinData = function JoinData(props) {
952
974
  var childModalZIndex = getTopLayerZIndex() + 1;
953
975
  var newMeta = cloneDeep(store.metaList[groupIndex].list);
954
976
  var index = findIndex(store.metaList[groupIndex].list, meta);
955
- var oldList = cloneDeep(newMeta[index].subquery);
956
977
  var _toolbar = subToolbar || toolbar;
957
978
  var o = Modal2.openModal({
958
979
  zIndex: childModalZIndex,
@@ -968,21 +989,22 @@ var JoinData = function JoinData(props) {
968
989
  onOk: function onOk(newList) {
969
990
  try {
970
991
  var _store$preProps3;
971
- if (isEqual(newList, oldList)) {
972
- o.close();
973
- return;
974
- }
975
992
  newMeta[index].subquery = newList;
976
993
  newMeta[index].table2 = _objectSpread(_objectSpread({}, newList[0].table), {}, {
977
994
  alias: newMeta[index].table2.alias || getAlias(newList[0].table.name, newMeta, ((_store$preProps3 = store.preProps) === null || _store$preProps3 === void 0 ? void 0 : _store$preProps3.notExistsColumns) || [])
978
995
  });
979
996
  var items = getSubColumns(newList);
997
+ var subQueryTable = newMeta[index].table2;
980
998
  var newColumns = items.flatMap(function (item) {
981
999
  return item.columns.map(function (column) {
1000
+ var columnMeta = column;
982
1001
  return _objectSpread(_objectSpread({}, column), {}, {
983
- tableId: column.tableId || column.id || item.id,
984
- tableUuid: column.tableUuid || item.tableUuid,
985
- datasourceId: column.datasourceId || item.datasourceId || ''
1002
+ tableId: subQueryTable.id || item.id || column.tableId || column.id,
1003
+ tableUuid: subQueryTable.tableUuid || item.tableUuid || column.tableUuid,
1004
+ tableName: subQueryTable.name || item.name || columnMeta.tableName || '',
1005
+ tableAlias: subQueryTable.alias || item.alias || columnMeta.tableAlias || '',
1006
+ datasourceId: subQueryTable.datasourceId || item.datasourceId || column.datasourceId || '',
1007
+ datasourceName: subQueryTable.datasourceName || item.datasourceName || column.datasourceName || ''
986
1008
  });
987
1009
  });
988
1010
  }).map(function (v) {
@@ -990,15 +1012,10 @@ var JoinData = function JoinData(props) {
990
1012
  select: false
991
1013
  });
992
1014
  });
993
- newMeta[index].columns = newColumns.map(function (newCol) {
994
- var existingCol = newMeta[index].columns.find(function (oldCol) {
995
- return isSameColumnByIdFirst(oldCol, newCol);
996
- });
997
- var _column = existingCol ? existingCol : newCol;
998
- _column.select = store.isSelectFields || _column.select;
999
- return _column;
1000
- });
1015
+ var oldColumns = newMeta[index].columns;
1016
+ newMeta[index].columns = mergeSubQueryColumns(newColumns, oldColumns, store.isSelectFields);
1001
1017
  initExpressions(newMeta);
1018
+ newMeta = syncSubQueryFieldReferences(newMeta, newMeta[index].table2, newMeta[index].columns, oldColumns);
1002
1019
  store.setMeta(newMeta, groupIndex, {
1003
1020
  obj: newList,
1004
1021
  type: ChangeType.subQuery
@@ -1065,10 +1082,26 @@ var JoinData = function JoinData(props) {
1065
1082
  onCancel: function onCancel() {}
1066
1083
  });
1067
1084
  };
1085
+ var cleanJoinOperators = function cleanJoinOperators(expressions) {
1086
+ for (var i = expressions.length - 1; i >= 0; i--) {
1087
+ if (isJoinOperator(expressions[i]) && (i === 0 || i === expressions.length - 1 || isJoinOperator(expressions[i - 1]))) {
1088
+ expressions.splice(i, 1);
1089
+ }
1090
+ }
1091
+ };
1068
1092
  var changeOperator = function changeOperator(key, i) {
1069
1093
  var index = findIndex(store.metaList[groupIndex].list, meta);
1070
1094
  var newMeta = store.metaList[groupIndex].list.slice();
1071
- newMeta[index].expressions[i].val = key;
1095
+ var operator = newMeta[index].expressions[i];
1096
+ operator.val = normalizeJoinOperator(key);
1097
+ store.setMeta(newMeta, groupIndex);
1098
+ };
1099
+ var removeExpression = function removeExpression(i) {
1100
+ var index = findIndex(store.metaList[groupIndex].list, meta);
1101
+ var newMeta = store.metaList[groupIndex].list.slice();
1102
+ var expressions = newMeta[index].expressions;
1103
+ expressions.splice(i, 1);
1104
+ cleanJoinOperators(expressions);
1072
1105
  store.setMeta(newMeta, groupIndex);
1073
1106
  };
1074
1107
  var changeExpression = function changeExpression(e, i) {
@@ -1134,19 +1167,15 @@ var JoinData = function JoinData(props) {
1134
1167
  }],
1135
1168
  type: type
1136
1169
  };
1137
- store.setMeta(newMeta, groupIndex);
1138
- }
1139
- if (type === AtomsTypeEnum.OPERATOR) {
1140
- item = {
1141
- val: 'AND',
1142
- type: type
1143
- };
1144
1170
  }
1145
1171
  if (!item) {
1146
1172
  return;
1147
1173
  }
1148
1174
  if (i >= 0) {
1149
- newMeta[index].expressions.splice(i + 1, 0, item);
1175
+ newMeta[index].expressions.splice(i + 1, 0, {
1176
+ val: 'AND',
1177
+ type: AtomsTypeEnum.OPERATOR
1178
+ }, item);
1150
1179
  } else {
1151
1180
  newMeta[index].expressions.push(item);
1152
1181
  }
@@ -1461,16 +1490,14 @@ var JoinData = function JoinData(props) {
1461
1490
  className: 'mr-2 operator-icon',
1462
1491
  icon: _jsx(FfLine, {}),
1463
1492
  onClick: function onClick() {
1464
- var index = findIndex(store.metaList[groupIndex].list, meta);
1465
- var newMeta = store.metaList[groupIndex].list.slice();
1466
- newMeta[index].expressions.splice(i, 1);
1467
- store.setMeta(newMeta, groupIndex);
1493
+ removeExpression(i);
1468
1494
  }
1469
1495
  })
1470
1496
  })]
1471
1497
  }, i);
1472
1498
  }
1473
1499
  if (v.type === AtomsTypeEnum.OPERATOR) {
1500
+ var operator = normalizeJoinOperator(v.val);
1474
1501
  return _jsxs("div", {
1475
1502
  className: cx("Sqb-where block"),
1476
1503
  children: [i == 0 && _jsx("span", {
@@ -1481,41 +1508,12 @@ var JoinData = function JoinData(props) {
1481
1508
  onChange: function onChange(key) {
1482
1509
  changeOperator(key, i);
1483
1510
  },
1484
- droplist: menuOperator,
1511
+ droplist: menuJoinOperator,
1485
1512
  children: _jsx(Button, {
1513
+ disabled: meta.readonly,
1486
1514
  className: 'ml-2 mr-4 text-gray-500',
1487
1515
  type: 'link',
1488
- children: v.val
1489
- })
1490
- }), _jsx(Tooltip, {
1491
- title: __('SqlQueryBuilder.add'),
1492
- children: _jsx(Dropdown, {
1493
- trigger: ['click'],
1494
- onChange: function onChange(key) {
1495
- addExpression(key, i);
1496
- },
1497
- droplist: menuTypes,
1498
- children: _jsx(Button, {
1499
- disabled: meta.readonly,
1500
- primary: true,
1501
- ghost: true,
1502
- className: 'mr-2 operator-icon',
1503
- icon: _jsx(FfPlus, {})
1504
- })
1505
- })
1506
- }), _jsx(Tooltip, {
1507
- title: __('SqlQueryBuilder.del'),
1508
- children: _jsx(Button, {
1509
- disabled: meta.readonly,
1510
- danger: true,
1511
- className: 'mr-2 operator-icon',
1512
- icon: _jsx(FfLine, {}),
1513
- onClick: function onClick() {
1514
- var index = findIndex(store.metaList[groupIndex].list, meta);
1515
- var newMeta = store.metaList[groupIndex].list.slice();
1516
- newMeta[index].expressions.splice(i, 1);
1517
- store.setMeta(newMeta, groupIndex);
1518
- }
1516
+ children: operator
1519
1517
  })
1520
1518
  })]
1521
1519
  }, i);
@@ -1561,10 +1559,7 @@ var JoinData = function JoinData(props) {
1561
1559
  className: 'mr-2 operator-icon',
1562
1560
  icon: _jsx(FfLine, {}),
1563
1561
  onClick: function onClick() {
1564
- var index = findIndex(store.metaList[groupIndex].list, meta);
1565
- var newMeta = store.metaList[groupIndex].list.slice();
1566
- newMeta[index].expressions.splice(i, 1);
1567
- store.setMeta(newMeta, groupIndex);
1562
+ removeExpression(i);
1568
1563
  }
1569
1564
  })
1570
1565
  })]
@@ -1615,10 +1610,7 @@ var JoinData = function JoinData(props) {
1615
1610
  className: 'mr-2 operator-icon',
1616
1611
  icon: _jsx(FfLine, {}),
1617
1612
  onClick: function onClick() {
1618
- var index = findIndex(store.metaList[groupIndex].list, meta);
1619
- var newMeta = store.metaList[groupIndex].list.slice();
1620
- newMeta[index].expressions.splice(i, 1);
1621
- store.setMeta(newMeta, groupIndex);
1613
+ removeExpression(i);
1622
1614
  }
1623
1615
  })
1624
1616
  })]
@@ -12,12 +12,11 @@ import { __ } from '../../locale';
12
12
  import { ChangeType } from '../../store/types';
13
13
  import { Tooltip, Button, Modal, Modal2, Input, Toast } from '@gingkoo/pandora';
14
14
  import Metabase from '../../index';
15
- import { uuidv4, getAlias, isSameColumnByIdFirst, isSameTableByIdFirst } from '../../utils/helper';
15
+ import { uuidv4, getAlias, isSameTableByIdFirst } from '../../utils/helper';
16
16
  import { getTopLayerZIndex } from '../../utils/helper-dom';
17
- import { changeTableAlias, getSubColumns, isValidSQLAlias, isExistsError } from '../../utils';
17
+ import { changeTableAlias, getSubColumns, mergeSubQueryColumns, isValidSQLAlias, isExistsError, syncSubQueryFieldReferences } from '../../utils';
18
18
  import { RelatedWork } from '@gingkoo/pandora-icons';
19
19
  import cloneDeep from 'lodash/cloneDeep';
20
- import isEqual from 'lodash/isEqual';
21
20
  import ItemName from './components/item-name';
22
21
  import { SelectTable, SelectColumn } from '../dialog';
23
22
  import Wrapper from './components/Wrapper';
@@ -189,7 +188,6 @@ var TableData = function TableData(props) {
189
188
  var childModalZIndex = getTopLayerZIndex() + 1;
190
189
  var newMetaList = store.metaList[groupIndex].list.slice()[0];
191
190
  var newMeta = store.metaList[groupIndex].list.slice();
192
- var oldList = cloneDeep(newMetaList.subquery);
193
191
  var _toolbar = subToolbar || toolbar;
194
192
  var o = Modal2.openModal({
195
193
  zIndex: childModalZIndex,
@@ -204,22 +202,23 @@ var TableData = function TableData(props) {
204
202
  value: cloneDeep(val),
205
203
  onOk: function onOk(newList) {
206
204
  try {
207
- if (isEqual(newList, oldList)) {
208
- o.close();
209
- return;
210
- }
211
205
  newMetaList.subquery = newList;
212
206
  newMetaList.table = _objectSpread(_objectSpread({}, newList[0].table), {}, {
213
207
  alias: newMetaList.table.alias || getAlias(newList[0].table.name, newMeta),
214
208
  tableUuid: newMetaList.table.tableUuid || uuidv4('table')
215
209
  });
216
210
  var items = getSubColumns(newList);
211
+ var subQueryTable = newMetaList.table;
217
212
  var newColumns = items.flatMap(function (item) {
218
213
  return item.columns.map(function (column) {
214
+ var columnMeta = column;
219
215
  return _objectSpread(_objectSpread({}, column), {}, {
220
- tableId: column.tableId || column.id || item.id,
221
- tableUuid: column.tableUuid || item.tableUuid,
222
- datasourceId: column.datasourceId || item.datasourceId || ''
216
+ tableId: subQueryTable.id || item.id || column.tableId || column.id,
217
+ tableUuid: subQueryTable.tableUuid || item.tableUuid || column.tableUuid,
218
+ tableName: subQueryTable.name || item.name || columnMeta.tableName || '',
219
+ tableAlias: subQueryTable.alias || item.alias || columnMeta.tableAlias || '',
220
+ datasourceId: subQueryTable.datasourceId || item.datasourceId || column.datasourceId || '',
221
+ datasourceName: subQueryTable.datasourceName || item.datasourceName || column.datasourceName || ''
223
222
  });
224
223
  });
225
224
  }).map(function (v) {
@@ -227,16 +226,11 @@ var TableData = function TableData(props) {
227
226
  select: false
228
227
  });
229
228
  });
230
- newMetaList.columns = newColumns.map(function (newCol) {
231
- var existingCol = newMetaList.columns.find(function (oldCol) {
232
- return isSameColumnByIdFirst(oldCol, newCol);
233
- });
234
- var _column = existingCol ? existingCol : newCol;
235
- _column.select = store.isSelectFields || _column.select;
236
- return _column;
237
- });
229
+ var oldColumns = newMetaList.columns;
230
+ newMetaList.columns = mergeSubQueryColumns(newColumns, oldColumns, store.isSelectFields);
238
231
  var newMetas = store.metaList[groupIndex].list.slice();
239
232
  newMetas[0] = newMetaList;
233
+ newMetas = syncSubQueryFieldReferences(newMetas, newMetaList.table, newMetaList.columns, oldColumns);
240
234
  store.setMeta(newMetas, groupIndex, {
241
235
  obj: newList,
242
236
  type: ChangeType.subQuery
package/lib/es/utils.d.ts CHANGED
@@ -21,6 +21,8 @@ interface HelperResultType {
21
21
  };
22
22
  }
23
23
  export declare const getHelper: (list: MetaListType[], item: MetaListType) => HelperResultType;
24
+ export declare const mergeSubQueryColumns: (columns: MetaData_ColumnsType[], oldColumns?: MetaData_ColumnsType[], isSelectFields?: boolean) => MetaData_ColumnsType[];
25
+ export declare const syncSubQueryFieldReferences: (list: MetaListType[], table: MetaData_TableType, columns: MetaData_ColumnsType[], oldColumns?: MetaData_ColumnsType[]) => MetaListType[];
24
26
  export declare const getSubColumns: (metaList: any) => DataType[];
25
27
  export declare function isValidSQLAlias(str: string): boolean;
26
28
  export declare const changeTableAlias: (list: MetaListType[] | [], curObj: Pick<MetaData_TableType, "tableUuid" | "alias">) => MetaListType[];
@@ -31,7 +33,7 @@ export declare const getObjTem: (arr: any[]) => any;
31
33
  export declare const patchData: (metas: MetaListType[], constantList?: any, formulaTemplates?: any) => MetaListType[];
32
34
  export declare function reassembleByUnion(target?: any[]): any[];
33
35
  export declare const buildSqlQuery: (data: MetaListType[] | undefined, type: string) => string;
34
- export declare const isError: (item: AtomsItem | AtomsItem[], data: DataType[]) => boolean;
36
+ export declare const isError: (item: AtomsItem | AtomsItem[], data?: DataType[]) => boolean;
35
37
  export declare function containsSubset(root: unknown, target: Record<string, unknown>): boolean;
36
38
  export declare const isExistsError: (meta: any[], errorList: any[]) => boolean;
37
39
  export declare const changeCopyField: (items: AtomsItem[], data: any[]) => AtomsItem[];