diff --git a/src/Archiver/Commands/ExportTableCommand.cs b/src/Archiver/Commands/ExportTableCommand.cs index b5f6e4e..38afe1a 100644 --- a/src/Archiver/Commands/ExportTableCommand.cs +++ b/src/Archiver/Commands/ExportTableCommand.cs @@ -173,6 +173,9 @@ namespace Chenfeng.MES.Archiver.Commands switch (val) { + case System.DBNull b: + row.Add("null"); + break; case bool b: row.Add(b ? 1 : 0); break; diff --git a/src/Archiver/Commands/ImportSqlCommand.cs b/src/Archiver/Commands/ImportSqlCommand.cs index 031fa71..5a124c1 100644 --- a/src/Archiver/Commands/ImportSqlCommand.cs +++ b/src/Archiver/Commands/ImportSqlCommand.cs @@ -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 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(); + } + } + } } }