Merge branch 'dump' of http://gitea.cf/MES/MES-Toolkit into dump

This commit is contained in:
xief 2022-01-24 15:12:29 +08:00
commit 330793aa25
2 changed files with 100 additions and 6 deletions

View File

@ -181,6 +181,9 @@ namespace Chenfeng.MES.Archiver.Commands
object newVal;
switch (val)
{
case System.DBNull b:
newVal = "null";
break;
case bool b:
newVal = b ? 1 : 0;
break;
@ -204,6 +207,7 @@ namespace Chenfeng.MES.Archiver.Commands
}
}
valString.Append(')');
valString.Append(',');
//valString.Append($"({string.Join(",", row)}),");
}
if (valString.Length == 0) break;

View File

@ -1,10 +1,13 @@
using CliFx;
using CliFx.Attributes;
using CliFx.Infrastructure;
using Dapper;
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Chenfeng.MES.Archiver.Commands
@ -12,15 +15,102 @@ namespace Chenfeng.MES.Archiver.Commands
[Command("import")]
public class ImportSqlCommand : ICommand
{
[CommandParameter(0, Description = "路径")]
public string Source { get; set; } = "";
[CommandParameter(0, Description = "数据库连接")]
public string Connection { get; set; } = "";
[CommandOption("source", Description = "导入文件路径")]
public string Source { get; set; } = "./output";
public Action<string> Print { get; set; } = (text) => { };
public MySqlConnection? Db { get; private set; }
[CommandOption("clear", Description = "导入前清除表数据")]
public bool clear { get; set; } =true;
public ValueTask ExecuteAsync(IConsole console)
{
throw new NotImplementedException();
Print = (text) => console.Output.WriteLine($"{DateTime.Now.ToLongTimeString()} {text}");
Print("开始导入!");
var connBuilder = new MySqlConnectionStringBuilder(this.Connection);
connBuilder.SslMode = MySqlSslMode.None;
connBuilder.CharacterSet = "utf8";
try
{
Db = new MySqlConnection(connBuilder.ConnectionString);
Db.Open();
var dir = new System.IO.DirectoryInfo(Source);
var files = dir.GetFiles();
var result=Db.QueryFirst<(string,string)>("show VARIABLES like '%FOREIGN_KEY_CHECKS'");
if (this.clear&& result.Item2=="ON")
{
Db.Execute("SET FOREIGN_KEY_CHECKS=0");
}
foreach (var file in files)
{
Print($"导入文件:{file.Name}!");
var startTime = DateTime.Now;
ImportFile(file);
console.Output.WriteLine("执行耗时:" + (DateTime.Now - startTime).TotalMilliseconds + "ms");
}
if (this.clear && result.Item2 == "ON")
{
Db.Execute("SET FOREIGN_KEY_CHECKS=1");
}
Print("操作成功");
}
catch (Exception ex)
{
console.Output.WriteLine(ex.ToString());
}
finally
{
Db?.Close();
}
return default;
}
private void ImportFile(FileInfo fileInfo)
{
var reg = new Regex("(.+?)\\-p(.+?)\\.(.+?)\\.sql");
var matchs=reg.Matches(fileInfo.Name);
if (matchs.Count == 0|| matchs[0].Groups.Count != 4)
{
return;
}
var type = matchs[0].Groups[3].ToString();
var table = matchs[0].Groups[1].ToString();
if (this.clear)
{
Db.Execute($"TRUNCATE TABLE `{table}`");
}
var fileStream = File.OpenRead(fileInfo.FullName);
Stream deCompressStream;
switch (type)
{
case "gz":
case "gzip":
deCompressStream = new System.IO.Compression.GZipStream(fileStream,System.IO.Compression.CompressionMode.Decompress);
break;
case "deflate":
deCompressStream = new System.IO.Compression.DeflateStream(fileStream, System.IO.Compression.CompressionMode.Decompress);
break;
case "br":
deCompressStream = new System.IO.Compression.BrotliStream(fileStream, System.IO.Compression.CompressionMode.Decompress);
break;
case "none": // 不压缩
default:
deCompressStream = fileStream;
break;
}
using (deCompressStream)
{
using (StreamReader streamReader = new StreamReader(deCompressStream))
{
var sql=streamReader.ReadToEnd();
this.Db.Execute(sql);
//streamWriter.Flush();
}
}
}
}
}