diff --git a/FIAS.Core/API/FIASClient.cs b/FIAS.Core/API/FIASClient.cs index 7b1ba6a..3f7677f 100644 --- a/FIAS.Core/API/FIASClient.cs +++ b/FIAS.Core/API/FIASClient.cs @@ -26,9 +26,9 @@ public FIASClient() /// public async Task> GetAllDownloadFileInfo() { - using (var HRM = await Client.GetAsync("GetAllDownloadFileInfo")) + using (var HRM = await Client.GetAsync("GetAllDownloadFileInfo").ConfigureAwait(false)) { - var Content = await HRM.Content.ReadAsStringAsync(); + var Content = await HRM.Content.ReadAsStringAsync().ConfigureAwait(false); var Info = JsonConvert.DeserializeObject>(Content); return Info; } @@ -41,7 +41,7 @@ public async Task> GetAllDownloadFileInfo() /// public async Task> GetAllDownloadFileInfo(DateTime date) { - var Info = await GetAllDownloadFileInfo(); + var Info = await GetAllDownloadFileInfo().ConfigureAwait(false); return Info.Where(I => I.Date > date).ToList(); } @@ -52,7 +52,7 @@ public async Task> GetAllDownloadFileInfo(DateTime date) /// public async Task> GetAllDownloadFileInfo(int version) { - var Info = await GetAllDownloadFileInfo(); + var Info = await GetAllDownloadFileInfo().ConfigureAwait(false); return Info.Where(I => I.VersionId > version).ToList(); } diff --git a/FIAS.Core/API/FIASInfo.cs b/FIAS.Core/API/FIASInfo.cs index 0d1201c..4bc8d7d 100644 --- a/FIAS.Core/API/FIASInfo.cs +++ b/FIAS.Core/API/FIASInfo.cs @@ -62,6 +62,11 @@ public class FIASInfo /// public string GarXMLDeltaURL { get; set; } + /// + /// Дата выгрузки + /// + public DateTime ExpDate { get; set; } + /// /// Дата выгрузки /// diff --git a/FIAS.Core/Models/FIASTableInfo.cs b/FIAS.Core/Models/FIASTableInfo.cs index 8355071..183af95 100644 --- a/FIAS.Core/Models/FIASTableInfo.cs +++ b/FIAS.Core/Models/FIASTableInfo.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Data; +using System.Linq; namespace FIAS.Core.Models { @@ -26,6 +28,8 @@ protected FIASTableInfo(DataRow row) public decimal UnusedMB { get; } public decimal UsedMB { get; } + public static List Parse(DataTable table) => table.Rows.Cast().Select(Parse).ToList(); + public static FIASTableInfo Parse(DataRow row) => new FIASTableInfo(row); public override string ToString() => Name; diff --git a/FIAS.Core/Stores/FIASDatabaseStore.cs b/FIAS.Core/Stores/FIASDatabaseStore.cs index d18c196..9fd5776 100644 --- a/FIAS.Core/Stores/FIASDatabaseStore.cs +++ b/FIAS.Core/Stores/FIASDatabaseStore.cs @@ -70,7 +70,7 @@ public void SetSubjects(List regions) public List TablesInfo() { using (var DT = UP_TablesInfo()) - return DT.Rows.Cast().Select(R => FIASTableInfo.Parse(R)).ToList(); + return FIASTableInfo.Parse(DT); } #region SQL diff --git a/FIASUpdate/Database/DBClient.cs b/FIASUpdate/Database/DBClient.cs new file mode 100644 index 0000000..dcc12eb --- /dev/null +++ b/FIASUpdate/Database/DBClient.cs @@ -0,0 +1,55 @@ +using Microsoft.Data.SqlClient; +using Microsoft.SqlServer.Management.Common; +using Microsoft.SqlServer.Management.Smo; +using System; + +namespace FIASUpdate +{ + internal abstract class DBClient : IDisposable + { + protected readonly Database DB; + protected readonly string DBName = FIASProperties.DBName; + + protected DBClient() + { + SqlConnection Connection = NewConnection(); + Server Server = new Server(new ServerConnection(Connection)); + DB = Server.Databases[DBName]; + if (DB == null) { throw new InvalidOperationException($"База данных {DBName} не найдена"); } + DB.Refresh(); + } + + protected static SqlConnection NewConnection() => NewConnection("master"); + + protected static SqlConnection NewConnection(string Database) + { + var SCSB = new SqlConnectionStringBuilder(FIASProperties.SQLConnection) + { + InitialCatalog = Database, + Encrypt = false + }; + var connection = new SqlConnection(SCSB.ToString()); + connection.Open(); + return connection; + } + + #region IDisposable + private bool disposedValue; + + public void Dispose() => Dispose(true); + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + DB.Parent.ConnectionContext.Disconnect(); + } + disposedValue = true; + } + } + + #endregion IDisposable + } +} \ No newline at end of file diff --git a/FIASUpdate/DBCreate.cs b/FIASUpdate/Database/DBCreate.cs similarity index 75% rename from FIASUpdate/DBCreate.cs rename to FIASUpdate/Database/DBCreate.cs index e0b59fe..2912744 100644 --- a/FIASUpdate/DBCreate.cs +++ b/FIASUpdate/Database/DBCreate.cs @@ -1,6 +1,4 @@ using JANL; -using Microsoft.Data.SqlClient; -using Microsoft.SqlServer.Management.Common; using Microsoft.SqlServer.Management.Smo; using System; using System.Collections.Generic; @@ -8,40 +6,25 @@ using System.IO; using System.Text.RegularExpressions; using System.Threading; -using Settings = FIASUpdate.Properties.Settings; namespace FIASUpdate { [Obsolete("Заменён на FIAS_GAR")] - internal class DBCreate : IDisposable + internal class DBCreate : DBClient { - private static readonly Settings Settings = Settings.Default; private readonly Dictionary DataSets = new Dictionary(); - private readonly Database DB; - private readonly string DBName; private readonly Regex R = new Regex("AS_(?[a-zA-Z_]+)_"); - //Status Progress + // Status Progress private readonly IProgress SP; - public DBCreate() : this(new Progress()) { } + public DBCreate() : this(default) { } - public DBCreate(IProgress TaskProgress) + public DBCreate(IProgress TaskProgress) : base() { - DBName = Settings.DBName; SP = TaskProgress; - - SqlConnection Connection = SQL.NewConnection(); - Server Server = new Server(new ServerConnection(Connection)); - DB = Server.Databases[DBName]; - if (DB == null) { throw new InvalidOperationException($"База данных {DBName} не найдена"); } - DB.Refresh(); - var I = Server.Information; } - private static string GAR => Settings.XMLPath; - private static string GAR_XSD => GAR + @"\gar_schemas"; - public void Create() { ReadSchemas(); @@ -106,7 +89,7 @@ private void DropTables() private void ReadSchemas() { - foreach (var XSD in Directory.EnumerateFiles(GAR_XSD)) + foreach (var XSD in Directory.EnumerateFiles(FIASProperties.GAR_XSD)) { var Name = R.Match(XSD).Groups["name"].Value; SP.Report(new TaskProgress($"Чтение схемы:{Name}")); @@ -128,24 +111,22 @@ private void ReadSchemas() } } - #region IDisposable Support + #region IDisposable private bool disposedValue; - public void Dispose() => Dispose(true); - - protected virtual void Dispose(bool disposing) + protected override void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { foreach (var DS in DataSets.Values) { DS.Dispose(); } - DB.Parent.ConnectionContext.Disconnect(); } disposedValue = true; } + base.Dispose(disposing); } - #endregion IDisposable Support + #endregion IDisposable } } \ No newline at end of file diff --git a/FIASUpdate/DBImport.cs b/FIASUpdate/Database/DBImport.cs similarity index 50% rename from FIASUpdate/DBImport.cs rename to FIASUpdate/Database/DBImport.cs index 68d874d..768a158 100644 --- a/FIASUpdate/DBImport.cs +++ b/FIASUpdate/Database/DBImport.cs @@ -1,9 +1,6 @@ using FIAS.Core.Stores; using FIASUpdate.Models; using JANL; -using Microsoft.Data.SqlClient; -using Microsoft.SqlServer.Management.Common; -using Microsoft.SqlServer.Management.Smo; using System; using System.Collections.Generic; using System.IO; @@ -12,10 +9,8 @@ namespace FIASUpdate { - internal abstract class DBImport : IDisposable + internal abstract class DBImport : DBClient { - protected readonly Database DB; - protected readonly string DBName = FIASProperties.DBName; protected readonly SyncEvent Events; protected readonly List Tables = new List(); protected readonly FIASDatabaseStore Store = new FIASDatabaseStore(FIASProperties.SQLConnection); @@ -23,15 +18,9 @@ internal abstract class DBImport : IDisposable protected IProgress SP; protected CancellationToken Token; - protected DBImport() + protected DBImport() : base() { Events = new SyncEvent(this); - - SqlConnection Connection = NewConnection(); - Server Server = new Server(new ServerConnection(Connection)); - DB = Server.Databases[DBName]; - if (DB == null) { throw new InvalidOperationException($"База данных {DBName} не найдена"); } - DB.Refresh(); } protected abstract string ScanPath { get; } @@ -42,20 +31,6 @@ protected DBImport() public abstract void Import(IProgress progress, CancellationToken token); - protected static SqlConnection NewConnection() => NewConnection("master"); - - protected static SqlConnection NewConnection(string Database) - { - var SCSB = new SqlConnectionStringBuilder(FIASProperties.SQLConnection) - { - InitialCatalog = Database, - Encrypt = false - }; - var connection = new SqlConnection(SCSB.ToString()); - connection.Open(); - return connection; - } - protected void ScanFiles() { Tables.Clear(); @@ -75,24 +50,5 @@ protected void ScanFiles() Tables.AddRange(tables); } - - #region IDisposable - private bool disposedValue; - - public void Dispose() => Dispose(true); - - protected virtual void Dispose(bool disposing) - { - if (!disposedValue) - { - if (disposing) - { - DB.Parent.ConnectionContext.Disconnect(); - } - disposedValue = true; - } - } - - #endregion IDisposable } } \ No newline at end of file diff --git a/FIASUpdate/DBImportDelta.cs b/FIASUpdate/Database/DBImportDelta.cs similarity index 100% rename from FIASUpdate/DBImportDelta.cs rename to FIASUpdate/Database/DBImportDelta.cs diff --git a/FIASUpdate/DBImportFull.cs b/FIASUpdate/Database/DBImportFull.cs similarity index 100% rename from FIASUpdate/DBImportFull.cs rename to FIASUpdate/Database/DBImportFull.cs diff --git a/FIASUpdate/FIASProperties.cs b/FIASUpdate/FIASProperties.cs index ba2e469..82e9f6e 100644 --- a/FIASUpdate/FIASProperties.cs +++ b/FIASUpdate/FIASProperties.cs @@ -7,6 +7,7 @@ internal static class FIASProperties public static string DBName => Settings.Default.DBName; public static string GAR_Delta => $@"{GAR_Common}\gar_delta_xml"; public static string GAR_Full => $@"{GAR_Common}\gar_xml"; + public static string GAR_XSD => $@"{GAR_Common}\gar_schemas"; public static string SQLConnection => Settings.Default.SQLConnection; private static string GAR_Common => Settings.Default.XMLPath; } diff --git a/FIASUpdate/FIASUpdate.csproj b/FIASUpdate/FIASUpdate.csproj index 57cd6a2..70c4d77 100644 --- a/FIASUpdate/FIASUpdate.csproj +++ b/FIASUpdate/FIASUpdate.csproj @@ -367,9 +367,10 @@ UC_DatabaseInfo.cs - - - + + + + @@ -408,7 +409,7 @@ - + Form @@ -431,7 +432,6 @@ - diff --git a/FIASUpdate/SQL.cs b/FIASUpdate/SQL.cs deleted file mode 100644 index a4c924f..0000000 --- a/FIASUpdate/SQL.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FIASUpdate.Properties; -using Microsoft.Data.SqlClient; - -namespace FIASUpdate -{ - internal static class SQL - { - private static readonly Settings Settings = Settings.Default; - - public static SqlConnection NewConnection() => NewConnection("master"); - - public static SqlConnection NewConnection(string Database) - { - var SCSB = new SqlConnectionStringBuilder(Settings.SQLConnection) - { - InitialCatalog = Database, - Encrypt = false - }; - var connection = new SqlConnection(SCSB.ToString()); - connection.Open(); - return connection; - } - } -} \ No newline at end of file diff --git a/README.md b/README.md index 3d5e60f..abc0f17 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ [build-src]: https://img.shields.io/github/actions/workflow/status/Virenbar/FIAS_GAR/build-artifact.yml?label=Build&logo=github -[build-href]: ttps://github.com/Virenbar/FIAS_GAR/actions/workflows/build-artifact.yml +[build-href]: https://github.com/Virenbar/FIAS_GAR/actions/workflows/build-artifact.yml [codacy-src]: https://app.codacy.com/project/badge/Grade/d9c5e3f57c914aed83166e72af7ba936 [codacy-href]: https://app.codacy.com/gh/Virenbar/FIAS_GAR/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade