Skip to content

Commit

Permalink
chore: небольшой рефактор
Browse files Browse the repository at this point in the history
  • Loading branch information
Virenbar committed Feb 27, 2024
1 parent 6ab6e85 commit 03c5612
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 109 deletions.
8 changes: 4 additions & 4 deletions FIAS.Core/API/FIASClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public FIASClient()
/// <returns></returns>
public async Task<List<FIASInfo>> 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<List<FIASInfo>>(Content);
return Info;
}
Expand All @@ -41,7 +41,7 @@ public async Task<List<FIASInfo>> GetAllDownloadFileInfo()
/// <returns></returns>
public async Task<List<FIASInfo>> GetAllDownloadFileInfo(DateTime date)
{
var Info = await GetAllDownloadFileInfo();
var Info = await GetAllDownloadFileInfo().ConfigureAwait(false);
return Info.Where(I => I.Date > date).ToList();
}

Expand All @@ -52,7 +52,7 @@ public async Task<List<FIASInfo>> GetAllDownloadFileInfo(DateTime date)
/// <returns></returns>
public async Task<List<FIASInfo>> GetAllDownloadFileInfo(int version)
{
var Info = await GetAllDownloadFileInfo();
var Info = await GetAllDownloadFileInfo().ConfigureAwait(false);
return Info.Where(I => I.VersionId > version).ToList();
}

Expand Down
5 changes: 5 additions & 0 deletions FIAS.Core/API/FIASInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public class FIASInfo
/// </summary>
public string GarXMLDeltaURL { get; set; }

/// <summary>
/// Дата выгрузки
/// </summary>
public DateTime ExpDate { get; set; }

/// <summary>
/// Дата выгрузки
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions FIAS.Core/Models/FIASTableInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace FIAS.Core.Models
{
Expand All @@ -26,6 +28,8 @@ protected FIASTableInfo(DataRow row)
public decimal UnusedMB { get; }
public decimal UsedMB { get; }

public static List<FIASTableInfo> Parse(DataTable table) => table.Rows.Cast<DataRow>().Select(Parse).ToList();

public static FIASTableInfo Parse(DataRow row) => new FIASTableInfo(row);

public override string ToString() => Name;
Expand Down
2 changes: 1 addition & 1 deletion FIAS.Core/Stores/FIASDatabaseStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void SetSubjects(List<string> regions)
public List<FIASTableInfo> TablesInfo()
{
using (var DT = UP_TablesInfo())
return DT.Rows.Cast<DataRow>().Select(R => FIASTableInfo.Parse(R)).ToList();
return FIASTableInfo.Parse(DT);
}

#region SQL
Expand Down
55 changes: 55 additions & 0 deletions FIASUpdate/Database/DBClient.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
37 changes: 9 additions & 28 deletions FIASUpdate/DBCreate.cs → FIASUpdate/Database/DBCreate.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,30 @@
using JANL;
using Microsoft.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System;
using System.Collections.Generic;
using System.Data;
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<string, DataSet> DataSets = new Dictionary<string, DataSet>();
private readonly Database DB;
private readonly string DBName;
private readonly Regex R = new Regex("AS_(?<name>[a-zA-Z_]+)_");

//Status Progress
// Status Progress
private readonly IProgress<TaskProgress> SP;

public DBCreate() : this(new Progress<TaskProgress>()) { }
public DBCreate() : this(default) { }

public DBCreate(IProgress<TaskProgress> TaskProgress)
public DBCreate(IProgress<TaskProgress> 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();
Expand Down Expand Up @@ -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}"));
Expand All @@ -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
}
}
48 changes: 2 additions & 46 deletions FIASUpdate/DBImport.cs → FIASUpdate/Database/DBImport.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,26 +9,18 @@

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<FIASTable> Tables = new List<FIASTable>();
protected readonly FIASDatabaseStore Store = new FIASDatabaseStore(FIASProperties.SQLConnection);

protected IProgress<TaskProgress> 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; }
Expand All @@ -42,20 +31,6 @@ protected DBImport()

public abstract void Import(IProgress<TaskProgress> 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();
Expand All @@ -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
}
}
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions FIASUpdate/FIASProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions FIASUpdate/FIASUpdate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,10 @@
<Compile Include="Controls\UC_DatabaseInfo.Designer.cs">
<DependentUpon>UC_DatabaseInfo.cs</DependentUpon>
</Compile>
<Compile Include="DBCreate.cs" />
<Compile Include="DBImport.cs" />
<Compile Include="DBImportDelta.cs" />
<Compile Include="Database\DBClient.cs" />
<Compile Include="Database\DBCreate.cs" />
<Compile Include="Database\DBImport.cs" />
<Compile Include="Database\DBImportDelta.cs" />
<Compile Include="Extensions\DataExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Forms\FormDictionaryView.cs">
Expand Down Expand Up @@ -408,7 +409,7 @@
<Compile Include="FIASProperties.cs" />
<Compile Include="Properties\Settings.cs" />
<Compile Include="Readers\FIASReader.cs" />
<Compile Include="DBImportFull.cs" />
<Compile Include="Database\DBImportFull.cs" />
<Compile Include="Forms\FormAddressSearch.cs">
<SubType>Form</SubType>
</Compile>
Expand All @@ -431,7 +432,6 @@
<Compile Include="Models\FIASFile.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SQL.cs" />
<Compile Include="SyncEvent.cs" />
<Compile Include="Readers\XMLDataReader.cs" />
<EmbeddedResource Include="Controls\UC_Database.resx">
Expand Down
24 changes: 0 additions & 24 deletions FIASUpdate/SQL.cs

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<!-- Badges -->
[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

0 comments on commit 03c5612

Please sign in to comment.