源码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
function multiTransefer(inputText) {
// 将传入的字符串根据MyBatis的标识拆分成数组
var mybatisSQLTexts = new Array();
while (inputText.lastIndexOf('Preparing: ') > -1) {
// 因为是从尾部截取,所以需要从数组的头部添加
mybatisSQLTexts.unshift(inputText.substring(inputText.lastIndexOf('Preparing: ')));
// console.log('lastIndexOf:'+inputText.lastIndexOf('Preparing: '));
// console.log('substring 0:'+inputText.substring(0, inputText.lastIndexOf('Preparing: ')));
// console.log('substring 1:'+inputText.substring(inputText.lastIndexOf('Preparing: ')));
inputText = inputText.substring(0, inputText.lastIndexOf('Preparing: '));
}
// console.log(mybatisSQLTexts);
var radio = document.getElementById("flag");
var selectvalue = radio.checked; // selectvalue为radio中选中的值
// 将数组中的字符串挨个处理,以数组形式返回
var retStr = null;
var tmpSQL = "";
for (var i = 0; i < mybatisSQLTexts.length; i++) {
retStr = f(mybatisSQLTexts[i]);
if (retStr != null && retStr != "") {
tmpSQL += retStr + '\n\n';
}
}
if (selectvalue) {
if (tmpSQL != null && tmpSQL != "") {
document.getElementById("d1").value += tmpSQL;
}
} else {
document.getElementById("d1").value = '\n' + tmpSQL;
}
}
// 单句的问号生成SQL
function f(textVa){
// 获取带问号的SQL语句
var statementStartIndex = textVa.indexOf('Preparing: ');
var statementEndIndex = textVa.length-1;
for(var i = statementStartIndex; i < textVa.length; i++) {
if(textVa[i] == "\n") {
statementEndIndex = i;
break;
}
}
var statementStr = textVa.substring(statementStartIndex+"Preparing: ".length, statementEndIndex);
// console.log(statementStr);
//获取参数
var parametersStartIndex = textVa.indexOf('Parameters: ');
var parametersEndIndex = textVa.length-1;
for(var i = parametersStartIndex; i < textVa.length; i++) {
if(textVa[i] == "\n") {
parametersEndIndex = i;
break;
} else {
// console.log(textVa[i]);
}
}
var parametersStr = textVa.substring(parametersStartIndex+"Parameters: ".length, parametersEndIndex);
parametersStr = parametersStr.split(",");
// console.log(parametersStr);
for(var i = 0; i < parametersStr.length; i++) {
// 如果数据中带括号将使用其他逻辑
tempStr = parametersStr[i].substring(0, parametersStr[i].indexOf("("));
// 获取括号中内容
typeStr = parametersStr[i].substring(parametersStr[i].indexOf("(")+1,parametersStr[i].indexOf(")"));
// 如果为字符类型
if (typeStr == "String" || typeStr == "Timestamp") {
statementStr = statementStr.replace("?", "'"+tempStr.trim()+"'");
}else{
// 数值类型
statementStr = statementStr.replace("?", tempStr.trim());
}
}
// console.log(statementStr);
if(statementStr != null && statementStr != "") {
return statementStr;
}
return null;
}
</script>
</head>
<body>
<div>
<label>
<input type="checkbox" id="flag" />
继续叠加
</label>
</div>
<div>
<textarea name="getStr" id="1" oninput="multiTransefer(document.getElementById('1').value)" rows="40" style="width: 49%" ></textarea>
<textarea name="getStr" id="d1" rows="40" style="width: 49%" ></textarea>
</div>
</body>
</html>