|
|
@ -19,35 +19,55 @@ export function doGenerateSQL(json: InputJSON) {
|
|
|
|
const rootInvokeTreeNode = { ...initTreeNode };
|
|
|
|
const rootInvokeTreeNode = { ...initTreeNode };
|
|
|
|
const context = json;
|
|
|
|
const context = json;
|
|
|
|
const resultSQL = generateSQL(
|
|
|
|
const resultSQL = generateSQL(
|
|
|
|
context.main,
|
|
|
|
"main",
|
|
|
|
context,
|
|
|
|
context,
|
|
|
|
context.main?.params,
|
|
|
|
context.main?.params,
|
|
|
|
rootInvokeTreeNode
|
|
|
|
rootInvokeTreeNode
|
|
|
|
);
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
resultSQL,
|
|
|
|
resultSQL,
|
|
|
|
invokeTree: rootInvokeTreeNode,
|
|
|
|
invokeTree: rootInvokeTreeNode.children[0], // 取第一个作为根节点
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 递归生成 SQL
|
|
|
|
* 递归生成 SQL
|
|
|
|
* @param currentNode
|
|
|
|
* @param key
|
|
|
|
* @param context
|
|
|
|
* @param context
|
|
|
|
* @param params
|
|
|
|
* @param params
|
|
|
|
* @param invokeTreeNode
|
|
|
|
* @param invokeTreeNode
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
function generateSQL(
|
|
|
|
function generateSQL(
|
|
|
|
currentNode: InputJSONValue,
|
|
|
|
key: string,
|
|
|
|
context: InputJSON,
|
|
|
|
context: InputJSON,
|
|
|
|
params?: Record<string, string>,
|
|
|
|
params?: Record<string, string>,
|
|
|
|
invokeTreeNode?: InvokeTreeNode
|
|
|
|
invokeTreeNode?: InvokeTreeNode
|
|
|
|
): string {
|
|
|
|
): string {
|
|
|
|
|
|
|
|
const currentNode = context[key];
|
|
|
|
if (!currentNode) {
|
|
|
|
if (!currentNode) {
|
|
|
|
return "";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const result = replaceParams(currentNode, context, params, invokeTreeNode);
|
|
|
|
let childInvokeTreeNode: InvokeTreeNode | undefined;
|
|
|
|
return replaceSubSql(result, context, invokeTreeNode);
|
|
|
|
if (invokeTreeNode) {
|
|
|
|
|
|
|
|
childInvokeTreeNode = {
|
|
|
|
|
|
|
|
title: key,
|
|
|
|
|
|
|
|
sql: currentNode.sql ?? currentNode,
|
|
|
|
|
|
|
|
params,
|
|
|
|
|
|
|
|
children: [],
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
invokeTreeNode.children?.push(childInvokeTreeNode);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = replaceParams(
|
|
|
|
|
|
|
|
currentNode,
|
|
|
|
|
|
|
|
context,
|
|
|
|
|
|
|
|
params,
|
|
|
|
|
|
|
|
childInvokeTreeNode
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
const resultSQL = replaceSubSql(result, context, childInvokeTreeNode);
|
|
|
|
|
|
|
|
if (childInvokeTreeNode) {
|
|
|
|
|
|
|
|
childInvokeTreeNode.resultSQL = resultSQL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultSQL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -140,23 +160,8 @@ function replaceSubSql(
|
|
|
|
const key = keyValueArray[0].trim();
|
|
|
|
const key = keyValueArray[0].trim();
|
|
|
|
params[key] = keyValueArray[1].trim();
|
|
|
|
params[key] = keyValueArray[1].trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let childInvokeTreeNode;
|
|
|
|
|
|
|
|
if (invokeTreeNode) {
|
|
|
|
|
|
|
|
childInvokeTreeNode = {
|
|
|
|
|
|
|
|
title: subKey,
|
|
|
|
|
|
|
|
sql,
|
|
|
|
|
|
|
|
params,
|
|
|
|
|
|
|
|
children: [],
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
invokeTreeNode.children?.push(childInvokeTreeNode);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 递归解析被替换节点
|
|
|
|
// 递归解析被替换节点
|
|
|
|
const replacement = generateSQL(
|
|
|
|
const replacement = generateSQL(subKey, context, params, invokeTreeNode);
|
|
|
|
replacementNode,
|
|
|
|
|
|
|
|
context,
|
|
|
|
|
|
|
|
params,
|
|
|
|
|
|
|
|
childInvokeTreeNode
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
result = result.replace(regExpMatchArray[0], replacement);
|
|
|
|
result = result.replace(regExpMatchArray[0], replacement);
|
|
|
|
regExpMatchArray = matchSubQuery(result);
|
|
|
|
regExpMatchArray = matchSubQuery(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|