|
@@ -7,25 +7,27 @@ export function useModelConfig() {
|
|
|
* */
|
|
|
function checkSelectAll(sql) {
|
|
|
let flag = false;
|
|
|
- const parts = sql.split(/from/i);
|
|
|
- if (parts.length > 1) {
|
|
|
- const selectPart = parts[0];
|
|
|
- const fields = selectPart.split(",");
|
|
|
- if (fields.length > 1){
|
|
|
- fields.forEach(field=>{
|
|
|
- if (!flag){
|
|
|
- if (field.includes('*')){
|
|
|
- const regex = /[\w\W]*\([\w\W]*\*[\w\W]*\)[\w\W]*/
|
|
|
- if (!regex.test(field)){
|
|
|
- flag = true
|
|
|
+ try {
|
|
|
+ const parts = sql.split(/from/i);
|
|
|
+ if (parts.length > 1) {
|
|
|
+ const selectPart = parts[0];
|
|
|
+ const fields = selectPart.split(",");
|
|
|
+ if (fields.length > 1){
|
|
|
+ fields.forEach(field=>{
|
|
|
+ if (!flag){
|
|
|
+ if (field.includes('*')){
|
|
|
+ const regex = /[\w\W]*\([\w\W]*\*[\w\W]*\)[\w\W]*/
|
|
|
+ if (!regex.test(field)){
|
|
|
+ flag = true
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- })
|
|
|
- }else {
|
|
|
- return sql.includes('*')
|
|
|
+ })
|
|
|
+ }else {
|
|
|
+ return sql.includes('*')
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
+ }catch(e){}
|
|
|
return flag;
|
|
|
}
|
|
|
|
|
@@ -38,66 +40,70 @@ export function useModelConfig() {
|
|
|
* }
|
|
|
* */
|
|
|
const toFields = (sql) => {
|
|
|
- const regexf = /from/i;
|
|
|
- const regexas = /\s+AS\s+/i;
|
|
|
- const regexs = /select/gi;
|
|
|
- const regext = /FROM\s+([\w]+)/i;
|
|
|
- const regexc = /[^,]*\([^)]*\) AS [^,]*/gi;
|
|
|
- const regexcs = /\)\s+as\s+([\w]+)/i;
|
|
|
- const regexjoin = /\b(LEFT\s+JOIN|RIGHT\s+JOIN|INNER\s+JOIN)\b/i;
|
|
|
-
|
|
|
-
|
|
|
- const matchf = sql.match(regexf);
|
|
|
- const fromIndex = matchf?.index || 0;
|
|
|
- let matchcStr = sql.slice(0, fromIndex).replace(regexs,'');
|
|
|
-
|
|
|
-
|
|
|
- const matchc = matchcStr.match(regexc);
|
|
|
- let fieldc = [];
|
|
|
- if (matchc){
|
|
|
- matchcStr = matchcStr.replace(regexc, '');
|
|
|
- fieldc = matchc.map(c=>c.match(regexcs)[1]);
|
|
|
- }
|
|
|
+ try {
|
|
|
+ const regexf = /from/i;
|
|
|
+ const regexas = /\s+AS\s+/i;
|
|
|
+ const regexs = /select/gi;
|
|
|
+ const regext = /FROM\s+([\w]+)/i;
|
|
|
+ const regexc = /[^,]*\([^)]*\) AS [^,]*/gi;
|
|
|
+ const regexcs = /\)\s+as\s+([\w]+)/i;
|
|
|
+ const regexjoin = /\b(LEFT\s+JOIN|RIGHT\s+JOIN|INNER\s+JOIN)\b/i;
|
|
|
|
|
|
-
|
|
|
- const fieldArr = matchcStr.split(',');
|
|
|
-
|
|
|
- const sqlRelation = sql.slice(fromIndex);
|
|
|
-
|
|
|
- const isJoin = regexjoin.test(sqlRelation.toUpperCase());
|
|
|
-
|
|
|
- const matchStr = sqlRelation.match(regext);
|
|
|
- const singleTable = ( matchStr && matchStr[1] ) || "";
|
|
|
+
|
|
|
+ const matchf = sql.match(regexf);
|
|
|
+ const fromIndex = matchf?.index || 0;
|
|
|
+ let matchcStr = sql.slice(0, fromIndex).replace(regexs,'');
|
|
|
|
|
|
-
|
|
|
- return fieldArr.concat(fieldc).map(item=> {
|
|
|
- let str=item,table=singleTable,field,aliasStr;
|
|
|
-
|
|
|
- const hasAs = regexas.test(item);
|
|
|
- if (hasAs){
|
|
|
- const asStr = item.match(regexas)[0];
|
|
|
- str = item.split(asStr)[0];
|
|
|
- aliasStr = item.split(asStr)[1].trim();
|
|
|
+
|
|
|
+ const matchc = matchcStr.match(regexc);
|
|
|
+ let fieldc = [];
|
|
|
+ if (matchc){
|
|
|
+ matchcStr = matchcStr.replace(regexc, '');
|
|
|
+ fieldc = matchc.map(c=>c.match(regexcs)[1]);
|
|
|
}
|
|
|
-
|
|
|
- str = str.replaceAll(/^\s+|\s+$/g, '').replaceAll(/\s+/g, '');
|
|
|
|
|
|
-
|
|
|
- if (str.includes('.')){
|
|
|
- const split = str.split('.');
|
|
|
- table = split[0]
|
|
|
- field = split[1]
|
|
|
- }
|
|
|
+
|
|
|
+ const fieldArr = matchcStr.split(',');
|
|
|
+
|
|
|
+ const sqlRelation = sql.slice(fromIndex);
|
|
|
+
|
|
|
+ const isJoin = regexjoin.test(sqlRelation.toUpperCase());
|
|
|
+
|
|
|
+ const matchStr = sqlRelation.match(regext);
|
|
|
+ const singleTable = ( matchStr && matchStr[1] ) || "";
|
|
|
+
|
|
|
+
|
|
|
+ return fieldArr.concat(fieldc).map(item=> {
|
|
|
+ let str=item,table=singleTable,field,aliasStr;
|
|
|
+
|
|
|
+ const hasAs = regexas.test(item);
|
|
|
+ if (hasAs){
|
|
|
+ const asStr = item.match(regexas)[0];
|
|
|
+ str = item.split(asStr)[0];
|
|
|
+ aliasStr = item.split(asStr)[1].trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ str = str.replaceAll(/^\s+|\s+$/g, '').replaceAll(/\s+/g, '');
|
|
|
|
|
|
-
|
|
|
- return isJoin
|
|
|
- ? {table,field,alias:aliasStr}
|
|
|
- : {
|
|
|
- table:table?table:singleTable,
|
|
|
- field:field?field:str,
|
|
|
- alias:aliasStr
|
|
|
+
|
|
|
+ if (str.includes('.')){
|
|
|
+ const split = str.split('.');
|
|
|
+ table = split[0]
|
|
|
+ field = split[1]
|
|
|
}
|
|
|
- });
|
|
|
+
|
|
|
+
|
|
|
+ return isJoin
|
|
|
+ ? {table,field,alias:aliasStr}
|
|
|
+ : {
|
|
|
+ table:table?table:singleTable,
|
|
|
+ field:field?field:str,
|
|
|
+ alias:aliasStr
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }catch(e){
|
|
|
+ return []
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -111,32 +117,39 @@ export function useModelConfig() {
|
|
|
* */
|
|
|
const initSqlModel=(model,sql,fields,bizs)=>{
|
|
|
let tableData = [];
|
|
|
- const regex1 = /from/i;
|
|
|
- const match = sql.match(regex1);
|
|
|
- const fromIndex = match?.index || 0;
|
|
|
- const sqlRelation = sql.slice(fromIndex);
|
|
|
- let SQLFields = "SELECT \r\n"
|
|
|
- fields.forEach(({bizName,tableName,fieldName,dataType,comment},index) => {
|
|
|
-
|
|
|
- const isLast = index===fields.length-1
|
|
|
- SQLFields += `\t${tableName}.${fieldName}${!isLast&&","}\r\n`
|
|
|
- const initObj = bizs.find(item=>item.name===fieldName);
|
|
|
- tableData.push({
|
|
|
- id:Math.random(),
|
|
|
- comment:comment,
|
|
|
- bizName: bizName.replace(model+"_",""),
|
|
|
- dataType: dataType,
|
|
|
- fieldName: fieldName,
|
|
|
- tableName: tableName,
|
|
|
- isKey:DIM_TYPE_ENUM.IDENTIFY===initObj?.type,
|
|
|
- common:DIM_TYPE_ENUM.CATEGORICAL===initObj?.type,
|
|
|
- datetime:DIM_TYPE_ENUM.DATETIME===initObj?.type,
|
|
|
- measure:!initObj?.type,
|
|
|
+ try {
|
|
|
+ const regex1 = /from/i;
|
|
|
+ const match = sql.match(regex1);
|
|
|
+ const fromIndex = match?.index || 0;
|
|
|
+ const sqlRelation = sql.slice(fromIndex);
|
|
|
+ let SQLFields = "SELECT \r\n"
|
|
|
+ fields.forEach(({bizName,tableName,fieldName,dataType,comment},index) => {
|
|
|
+
|
|
|
+ const isLast = index===fields.length-1
|
|
|
+ SQLFields += `\t${tableName}.${fieldName}${!isLast&&","}\r\n`
|
|
|
+ const initObj = bizs.find(item=>item.name===fieldName);
|
|
|
+ tableData.push({
|
|
|
+ id:Math.random(),
|
|
|
+ comment:comment,
|
|
|
+ bizName: bizName.replace(model+"_",""),
|
|
|
+ dataType: dataType,
|
|
|
+ fieldName: fieldName,
|
|
|
+ tableName: tableName,
|
|
|
+ isKey:DIM_TYPE_ENUM.IDENTIFY===initObj?.type,
|
|
|
+ common:DIM_TYPE_ENUM.CATEGORICAL===initObj?.type,
|
|
|
+ datetime:DIM_TYPE_ENUM.DATETIME===initObj?.type,
|
|
|
+ measure:!initObj?.type,
|
|
|
+ })
|
|
|
})
|
|
|
- })
|
|
|
- return {
|
|
|
- tableData,
|
|
|
- sql:SQLFields+sqlRelation,
|
|
|
+ return {
|
|
|
+ tableData,
|
|
|
+ sql:SQLFields+sqlRelation,
|
|
|
+ }
|
|
|
+ }catch (e) {
|
|
|
+ return {
|
|
|
+ tableData:[],
|
|
|
+ sql:"",
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -214,9 +227,9 @@ export function useModelConfig() {
|
|
|
* @param table 表名
|
|
|
* @param fields 字段数组
|
|
|
* */
|
|
|
- const fieldsToTable=(table,fields)=>(fields||[])
|
|
|
- .map(field=>{
|
|
|
- return {
|
|
|
+ const fieldsToTable=(table,fields)=>{
|
|
|
+ try {
|
|
|
+ return fields.map(field=>({
|
|
|
id:Math.random(),
|
|
|
bizName: field?.nameEn,
|
|
|
comment: field?.comment,
|
|
@@ -227,9 +240,11 @@ export function useModelConfig() {
|
|
|
common:false,
|
|
|
datetime:false,
|
|
|
measure:false,
|
|
|
- }
|
|
|
+ }))
|
|
|
+ }catch (e) {
|
|
|
+ return []
|
|
|
}
|
|
|
- )
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|