<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>sharding toolkit</title>
<style>
#input{width: 1200px; height: 500px;}
#template{width: 1200px;}
#result{font-size:12px;}
.introduce{font-size:12px; color:#00F;}
</style>
</head>
<body>
<h4>sharding toolkit</h4>
<p>
<div class="introduce">数据,从excel拷贝出来,按\t分割的多列数据</div>
<textarea id="input"></textarea>
</p>
<p>
<div class="introduce">模板,$x代表一般变量,#x代表sharding变量,x=列序号(0开始),如update table_#0 set a = '$1' where sku_code = '$0'; </div>
<input type="text" id="template"></input>
</p>
<p>
<div class="introduce">分表,算法 = Math.abs(字符串.hashCode()) % shardingCount</div>
<select id="sharding">
<option value="4">4</option>
<option value="8">8</option>
<option value="16">16</option>
<option value="32">32</option>
</select>
</p>
<p>
<button id="generate">按模板生成SQL</button>
<div id="result"></div>
</p>
<script type="application/javascript" language="javascript">
String.prototype.hashCode = function(){
var hash = 0;
if (this.length == 0)
return hash;
for (i = 0; i < this.length; i++) {
char = this.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
function sharding(str, shardingCount){
return Math.abs(str.hashCode()) % shardingCount;
}
function merge(template, columnArray, shardingCount){
for(var i = 0; i < columnArray.length; i++){
template = template.replaceAll('$' + i, columnArray[i]);
template = template.replaceAll('#' + i, sharding(columnArray[i], shardingCount));
}
return template;
}
document.getElementById('generate').onclick = function(){
var input = document.getElementById('input').value;
var template = document.getElementById('template').value;
var shardingInput = document.getElementById('sharding');
var shardingCount = shardingInput.options[shardingInput.selectedIndex].value;
if(input == ''){
alert('输入数据不能为空');
return;
}
if(template == ''){
alert('模板不能为空');
return;
}
var inputArray = input.split('\n')
var result = [];
for(var i = 0; i < inputArray.length; i++){
var row = inputArray[i];
if(row != ''){
var column = row.split('\t');
result.push(merge(template, column, shardingCount));
}
}
document.getElementById('result').innerHTML = result.join('<br/>');
}
</script>
</body>
</html>
excel生成分表sql小工具
最新推荐文章于 2023-12-26 19:52:22 发布