参数自动插到sql相应位置
从log中打出的sql和参数,连接成可执行的sql。
有种情况不行,参数是数组的!
<html>
<style>
textarea{
border: 0px solid #fff;
height:20%;
}
.w100{
width:100%;
}
.textH{
}
.resultH{
height:40%;
}
.operarions{
height:36px;
display: flex;
/*实现垂直居中*/
align-items: center;
/*实现水平居中*/
justify-content: center;
text-align: justify;
}
.operarions input{
margin: 0 8 0 0;
}
</style>
<script>
function toBind(){
var sql = form1.sql0txt.value;
var params = form1.paramtxt.value;
params =params.replace(' ','').replace('[','').replace(']','');
var paramList =[];
if(params){
paramList = params.split(',');
}
var newParams=[]
for(i=0;i<paramList.length;i++){
var p = paramList[i];
newParams.push(p.trim());
}
newParams.push("");
var sqlList = sql.split('?')
console.log(sqlList ,newParams)
if(newParams.length!=sqlList.length){
alert('参数个数不一致');
return;
}
var resultSql ="";
for(i=0;i<sqlList.length;i++){
resultSql += sqlList[i] +"'"+newParams[i]+"' ";
}
var words = [',' ,' from',' and',' or',' where',' group ',' order ',' having',' left',' join',' union',' on']
for(i=0;i<words.length;i++){
var w = words[i];
resultSql = resultSql.replaceAll(w,'\r\n\t'+' '+w)
console.log(resultSql)
}
resultSql= resultSql.substring(0,resultSql.length-3);
form1.sql1txt.value=resultSql;
form1.sql1txt.select();
document.execCommand("copy");
}
function toClear(){
form1.sql0txt.value='';
form1.paramtxt.value='';
form1.sql1txt.value='';
}
</script>
<body>
<form action="/" id="form1">
<fieldset>
<legend>原SQL:</legend>
<div class="w100">
<textarea id="sql0txt" class="w100 textH"></textarea>
</div>
</fieldset>
<fieldset>
<legend>原参数:</legend>
<div class="w100">
<textarea id="paramtxt" class="w100 textH"></textarea>
</div>
</fieldset>
<div class="w100 operarions">
<input type="button" value="绑定" onClick="toBind()" />
<input type="button" value="清空" onclick="toClear()"/>
</div>
<fieldset>
<legend>处理结果:</legend>
<div class="w100">
<textarea id="sql1txt" class="w100 resultH"></textarea>
</div>
</fieldset>
</form>
</body>
</html>