function() {
// 0: 从服务到文件;1:从文件到服务
var type = ~~form("type") ? 1 : 0;
// if(form("clonePass") != sys.clonePass) return { err: "密码错误" };
// 源库,目标
var conn = [
{ str: "localhost; Database=konishi_net; charset=utf8; username=konishi_net; password=jbjjbh@", type: "MySql.Data.Client" },
{ str: "App_Data/konishi.sdf", type: "SqlLite" }
];
if(type) conn.reverse();
conn[0].db = db(conn[0].str, conn[0].type);
conn[1].db = db(conn[1].str, conn[1].type);
// 清空目标库
var tables = conn[1].db.query("select table_name as tname from information_schema.tables");
tables.forEach(function(x) { conn[1].db.query("drop table " + x.tname); });
sys.dbPath = conn[1].str; sys.dbType = conn[1].type;
this.initdb();
dbg().trace("开始从 " + conn[0].type + " 克隆数据到 " + conn[1].type);
function clone(tbl, col) {
var rows = conn[0].db.table(tbl).query();
if(!rows.length) return;
rows.forEach(function(x) {
// 删掉 null 值
for(var y in x) if(x[y] === null) delete x[y];
});
// 处理主键问题的方法:sqlce 主键默认是不能插入值的,只能自动生成
// 设置 identity_insert on 后才能插入,插入完成后还需要关闭
conn[1].db.query("set identity_insert " + tbl + " on");
conn[1].db.insert(tbl, rows);
conn[1].db.query("set identity_insert " + tbl + " off");
var maxId = -~conn[0].db.scalar("select max(" + col + ") from " + tbl);
if(conn[1].type == "MySql") conn[1].db.query("dbcc checkident('" + tbl + "', reseed, " + (maxId - 1) + ")");
else conn[1].db.query("alter table " + tbl + " alter column " + col + " identity(" + maxId + ", 1)");
dbg().trace("从 " + tbl + " 克隆了 " + rows.length + " 条数据,identity 设置为:" + maxId + "。");
}
clone("catalog", "id");
clone("comment", "id");
clone("config", "data");
clone("info", "id");
clone("manager", "id");
clone("message", "id");
clone("users", "id");
clone("wxpaylog", "id");
return { msg: "克隆完成" };
}