@gingkoo/pandora-metabase 1.0.0-alpha.19 → 1.0.0-alpha.20

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.
package/lib/es/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @gingkoo/pandora-metabase v1.0.0-alpha.19
2
+ * @gingkoo/pandora-metabase v1.0.0-alpha.20
3
3
  */
4
4
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
5
5
  import * as React from 'react';
@@ -340,7 +340,8 @@ const summarizeToSql = (arr, record) => {
340
340
  });
341
341
  as = as + (count ? '_' + (count + 1) : '');
342
342
  return {
343
- sql: sql + ' AS ' + as,
343
+ sql: sql,
344
+ // + ' AS ' + as,
344
345
  fieldAlias: as
345
346
  };
346
347
  };
@@ -1012,26 +1013,91 @@ function reassembleByUnion(target = []) {
1012
1013
  }
1013
1014
  return result;
1014
1015
  }
1016
+ // export const buildSqlQuery = (data?: MetaListType[]): string => {
1017
+ // if (!data || data.length === 0) return '';
1018
+ // let sqlClauses: string[] = ['not exitis ( '];
1019
+ // data.forEach((item) => {
1020
+ // if (item.type === TypeEnum.data) {
1021
+ // const meta = item as MetaData;
1022
+ // const tableName = meta.table.name;
1023
+ // const alias = meta.table.alias ? `AS ${meta.table.alias}` : '';
1024
+ // const dataSource = meta.table.datasourceName;
1025
+ // sqlClauses.push(`SELECT 1 FROM ${dataSource}.${tableName} ${alias}`);
1026
+ // }
1027
+ // if (item.type === TypeEnum.joinData) {
1028
+ // const meta = item as MetaJoin;
1029
+ // const table1 = meta.table1.alias ? meta.table1.alias : meta.table1.name;
1030
+ // const table2 = `${meta.table2.datasourceName}.${meta.table2.name}`;
1031
+ // const alias2 = meta.table2.alias ? meta.table2.alias : '';
1032
+ // joins.push(`LEFT JOIN ${table2} ${alias2} ON ${table1}.${meta.columns[0]?.name} = ${alias2}.${meta.columns[0]?.name}`);
1033
+ // }
1034
+ // if (item.type === TypeEnum.filter) {
1035
+ // const filterStrings = (item as MetaFilter).filter.map((f) => f.quotes).filter(Boolean); // 排除空条件
1036
+ // if (filterStrings.length > 0) {
1037
+ // sqlClauses.push(`WHERE ${filterStrings.join(' AND ')}`);
1038
+ // }
1039
+ // }
1040
+ // });
1041
+ // // 简单拼接 SQL 片段
1042
+ // return sqlClauses.join(' ') + ' )' + ';';
1043
+ // };
1015
1044
  const buildSqlQuery = data => {
1016
1045
  if (!data || data.length === 0) return '';
1017
- let sqlClauses = ['not exitis ( '];
1046
+ let sqlClauses = ['NOT EXISTS ('];
1047
+ let tables = [];
1048
+ let joins = [];
1049
+ let wheres = [];
1018
1050
  data.forEach(item => {
1019
1051
  if (item.type === TypeEnum.data) {
1020
1052
  const meta = item;
1021
- const tableName = meta.table.name;
1022
- const alias = meta.table.alias ? `AS ${meta.table.alias}` : '';
1023
- const dataSource = meta.table.datasourceName;
1024
- sqlClauses.push(`SELECT ${tableName} ${alias} FROM ${dataSource}`);
1053
+ const tableName = `${meta.table.name}`;
1054
+ const alias = meta.table.alias ? ` AS ${meta.table.alias}` : '';
1055
+ tables.push(`${tableName}${alias}`);
1056
+ }
1057
+ if (item.type === TypeEnum.joinData) {
1058
+ const meta = item;
1059
+ const table2Name = `${meta.table2.name}`;
1060
+ const alias2 = meta.table2.alias ? ` AS ${meta.table2.alias}` : '';
1061
+ const table2 = `${meta.table2.alias || meta.table2.name}`;
1062
+ const table1 = `${meta.table1.alias || meta.table1.name}`;
1063
+ joins.push(`LEFT JOIN ${table2Name}${alias2} ON ${table1}.${meta.table1.fieldAlias} = ${table2}.${meta.table2.fieldAlias}`);
1064
+ if (Array.isArray(item.expressions) && item.expressions.length > 0) {
1065
+ item.expressions.forEach(expression => {
1066
+ let left = '',
1067
+ right = '';
1068
+ if (expression.left_type === 'field') {
1069
+ left = `${table1}.${expression.left_fieldAlias}`;
1070
+ } else {
1071
+ left = expression.left_string;
1072
+ }
1073
+ if (expression.right_type === 'field') {
1074
+ right = `${table2}.${expression.right_fieldAlias}`;
1075
+ } else {
1076
+ right = expression.right_string;
1077
+ }
1078
+ joins.push(`AND ${left} = ${right}`);
1079
+ });
1080
+ }
1025
1081
  }
1026
1082
  if (item.type === TypeEnum.filter) {
1027
- const filterStrings = item.filter.map(f => f.quotes).filter(Boolean); // 排除空条件
1083
+ const filterStrings = item.filter.map(f => f.quotes).filter(Boolean);
1028
1084
  if (filterStrings.length > 0) {
1029
- sqlClauses.push(`WHERE ${filterStrings.join(' AND ')}`);
1085
+ wheres.push(...filterStrings);
1030
1086
  }
1031
1087
  }
1032
1088
  });
1033
- // 简单拼接 SQL 片段
1034
- return sqlClauses.join(' ') + ' )' + ';';
1089
+ // 构建子查询
1090
+ if (tables.length > 0) {
1091
+ sqlClauses.push(`SELECT 1 FROM ${tables[0]}`);
1092
+ if (joins.length > 0) {
1093
+ sqlClauses.push(joins.join(' '));
1094
+ }
1095
+ if (wheres.length > 0) {
1096
+ sqlClauses.push(`WHERE ${wheres.join(' AND ')}`);
1097
+ }
1098
+ }
1099
+ sqlClauses.push(')');
1100
+ return sqlClauses.join(' ') + ';';
1035
1101
  };
1036
1102
 
1037
1103
  let metaKey = 1;
@@ -4009,7 +4075,7 @@ const SelectJoinColumn = ({
4009
4075
  let quotes = name;
4010
4076
  return onGroup({
4011
4077
  ..._value,
4012
- tableUuid: _value.tableUuid || tableUuid,
4078
+ tableUuid: tableUuid || _value.tableUuid,
4013
4079
  ...v,
4014
4080
  table,
4015
4081
  tableId,
@@ -4023,7 +4089,7 @@ const SelectJoinColumn = ({
4023
4089
  }
4024
4090
  typeof onSelect === 'function' && onSelect({
4025
4091
  ..._value,
4026
- tableUuid: _value.tableUuid || tableUuid,
4092
+ tableUuid: tableUuid || _value.tableUuid,
4027
4093
  ...v,
4028
4094
  table,
4029
4095
  tableId,
@@ -6025,7 +6091,8 @@ const JoinData = props => {
6025
6091
  id = '',
6026
6092
  fieldAlias = '',
6027
6093
  fieldUuid = '',
6028
- quotes
6094
+ quotes,
6095
+ tableUuid
6029
6096
  } = record;
6030
6097
  let newMeta = store.metaList[groupIndex].list.slice();
6031
6098
  if (alias === SummarizeAlias$1) {
@@ -6041,6 +6108,7 @@ const JoinData = props => {
6041
6108
  if (newMeta[index][type].alias !== alias) {
6042
6109
  newMeta[index][type].alias = alias;
6043
6110
  newMeta[index][type].name = table;
6111
+ newMeta[index][type].tableUuid = tableUuid;
6044
6112
  newMeta[index][type].datasourceId = datasourceId;
6045
6113
  newMeta[index][type].datasourceName = datasourceName;
6046
6114
  newMeta[index].expressions = [];