
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title> mysql格式化 </title> <script> function mysqlformet(obj){ var textVa = obj.value; //獲取帶問號的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 parameterStarIndex = textVa.indexOf('Parameters: '); var parameterEndIndex = textVa.length -1 ; for(var i =parameterStarIndex; i<textVa.length;i++){ if(textVa[i] == '\n'){ parameterEndIndex =i; break; }else{ console.log(textVa[i]); } } var parametersStr=textVa.substring(parameterStarIndex+"Parameters: ".length,parameterEndIndex); 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); document.getElementById("d1").innerHTML=statementStr; return textVa; } function copySQL(){ var SQL = document.getElementById("d1"); SQL.select();//選擇對象 document.execCommand("Copy"); var msg = document.getElementById("msg"); msg.innerHTML="已複製到剪貼板"; setTimeout(function(){ msg.innerHTML=""; },3000); } function clearLog(obj){ obj.select(); obj.value=""; } </script> </head> <body> <h2><font color="#00bfff">輸入mybatis sql日誌:</font></h2> <textarea style="font-size: 20px;" id="sqlLog" cols="140" rows="13"></textarea> <div style="border: 0px deepskyblue solid; width: 1425px; height: 50px;text-align: right;"> <button style="color: mediumblue; width: 100px;height: 60px;" type="button" onclick="clearLog(document.getElementById('sqlLog'))">清空</button> <button style="color: mediumblue;width: 100px; height:60px " type="submit" onclick="mysqlformet(document.getElementById('sqlLog'))">解析SQL</button> </div> <h2><font color="#32出的2">解析為可執行SQL:</font></h2> <textarea style="font-size: 20px;" id="d1" cols="140" rows="13"></textarea> <div style="border: 0px deepskyblue solid; width: 1425px; height: 50px;text-align: right;"> <button style="color: mediumblue; width: 100px;height: 60px;" type="button" onclick="copySQL()">複製SQL</button> </div> <div id="msg" style="color: cornflowerblue; border: 0px black solid;width: 800px; height: 20px; text-align: right; font-style: initial; font-size: large;"></div> </body></html>
转发原文:https://blog.csdn.net/x541211190/article/details/107410409?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-6-107410409.pc_agg_new_rank&utm_term=parameters+sql%E5%A1%AB%E5%85%85&spm=1000.2123.3001.4430