支持导出sql

This commit is contained in:
xief 2022-01-19 16:20:26 +08:00
parent c0c17abea1
commit a606282499

View File

@ -18,7 +18,7 @@ namespace Chenfeng.MES.Archiver.Commands
[Command("copy")]
public class CopyTableCommand : ICommand,IArchiveReader
{
[CommandParameter(0, Description = "数据库连接")]
[CommandOption("connection", Description = "数据库连接")]
public string Connection { get; set; } = "";
public string TableSuffix { get; set; } = "new";
@ -42,26 +42,25 @@ namespace Chenfeng.MES.Archiver.Commands
[CommandOption("timeout", Description = "sql命令超时时间")]
public int Timeout { get; set; } = 3;
[CommandOption("script", Description = "是否生成执行脚本")]
public bool Script { get; set; }
public ValueTask ExecuteAsync(IConsole console)
{
Print = (text) => console.Output.WriteLine($"{DateTime.Now.ToLongTimeString()} {text}");
var connBuilder = new MySqlConnectionStringBuilder(this.Connection);
connBuilder.SslMode = MySqlSslMode.None;
connBuilder.CharacterSet = "utf8";
var starMonth = DateTime.Now.AddMonths(-12).ToString("yyyyMM");
SqlMapper.Settings.CommandTimeout = 60 * Timeout;
var executeType = "copy";
if (RemoveBackup)
{
executeType = "rm_bak";
console.Output.WriteLine("删除备份表");
}
else if (Restore)
{
executeType = "restore";
console.Output.WriteLine("还原备份表");
}
else
@ -71,9 +70,15 @@ namespace Chenfeng.MES.Archiver.Commands
try
{
Db = new MySqlConnection(connBuilder.ConnectionString);
Db.Open();
if (this.Connection.Length > 0)
{
var connBuilder = new MySqlConnectionStringBuilder(this.Connection);
connBuilder.SslMode = MySqlSslMode.None;
connBuilder.CharacterSet = "utf8";
Db = new MySqlConnection(connBuilder.ConnectionString);
}
Db?.Open();
if (Legacy)
{
TableQueryHelper.MesLegacyTables(this,starMonth);
@ -83,6 +88,21 @@ namespace Chenfeng.MES.Archiver.Commands
TableQueryHelper.MesTables(this,starMonth);
}
if (Script)
{
if (Directory.Exists("./scripts")==false)
{
Directory.CreateDirectory("./scripts");
}
using (var file = File.OpenWrite($"./scripts/mes{(Legacy?"-legacy":"")}-table-{executeType}.sql"))
{
using (var writer = new StreamWriter(file))
{
writer.Write(sqlScripts);
}
}
}
Print("操作成功");
@ -101,12 +121,32 @@ namespace Chenfeng.MES.Archiver.Commands
public void ExecuteData(string table, string where, object param)
{
Db.Execute($"INSERT INTO `{TableDic[table]}` SELECT * FROM `{table}` WHERE {where}", param);
ExecuteSql($"INSERT INTO `{TableDic[table]}` SELECT * FROM `{table}` WHERE {where}", param);
}
public void ExecuteChildData(string table, string parentTable, string condition)
{
Db.Execute($"INSERT INTO `{TableDic[table]}` SELECT child.* FROM `{TableDic[parentTable]}` parent join `{table}` child on {condition} ");
ExecuteSql($"INSERT INTO `{TableDic[table]}` SELECT child.* FROM `{TableDic[parentTable]}` parent join `{table}` child on {condition} ");
}
private StringBuilder sqlScripts = new StringBuilder();
private void ExecuteSql(string sql, object param = null)
{
Db?.Execute(sql,param);
if (Script)
{
var fullSql = sql + ";";
if(param != null)
{
foreach (var property in param.GetType().GetProperties())
{
var val = property.GetValue(param)?.ToString();
fullSql =fullSql.Replace("@" + property.Name, val);
}
}
sqlScripts.AppendLine(fullSql);
}
}
@ -124,19 +164,19 @@ namespace Chenfeng.MES.Archiver.Commands
{
if (RemoveBackup)
{
Db.Execute($"DROP TABLE IF EXISTS `{item.Table}_bak`");
ExecuteSql($"DROP TABLE IF EXISTS `{item.Table}_bak`");
}
else if (Restore)
{
Db.Execute($"DROP TABLE IF EXISTS `{item.Table}`");
Db.Execute($"RENAME TABLE `{item.Table}_bak` TO `{item.Table}`");
ExecuteSql($"DROP TABLE IF EXISTS `{item.Table}`");
ExecuteSql($"RENAME TABLE `{item.Table}_bak` TO `{item.Table}`");
}
else
{
TableDic[item.Table] = item.NewTable;
TableDic[item.Table] = item.NewTable;
Db.Execute($"DROP TABLE IF EXISTS `{item.NewTable}`");
Db.Execute($"CREATE TABLE `{item.NewTable}` LIKE `{item.Table}`"); // IF NOT EXISTS
ExecuteSql($"DROP TABLE IF EXISTS `{item.NewTable}`");
ExecuteSql($"CREATE TABLE `{item.NewTable}` LIKE `{item.Table}`"); // IF NOT EXISTS
Print(item.NewTable + " 创建完成");
action(item.Table);
@ -144,8 +184,8 @@ namespace Chenfeng.MES.Archiver.Commands
if (Replace)
{
Db.Execute($"RENAME TABLE `{item.Table}` TO `{item.Table}_bak`");
Db.Execute($"RENAME TABLE `{item.NewTable}` TO `{item.Table}`");
ExecuteSql($"RENAME TABLE `{item.Table}` TO `{item.Table}_bak`");
ExecuteSql($"RENAME TABLE `{item.NewTable}` TO `{item.Table}`");
}
}
}