Skip to content

Commit

Permalink
Updated to .Net 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pmachapman committed Nov 9, 2021
1 parent f1fb044 commit 8ca0c0d
Show file tree
Hide file tree
Showing 7 changed files with 496 additions and 498 deletions.
6 changes: 3 additions & 3 deletions Conglomo.DataPump.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<Company>Conglomo</Company>
<Authors>Peter Chapman</Authors>
Expand All @@ -12,8 +12,8 @@
<ApplicationIcon>Resources\GASPUMP.ICO</ApplicationIcon>
<StartupObject>Conglomo.DataPump.Program</StartupObject>
<NeutralLanguage>en-NZ</NeutralLanguage>
<AssemblyVersion>1.1.3.0</AssemblyVersion>
<Version>1.1.3</Version>
<AssemblyVersion>1.1.4.0</AssemblyVersion>
<Version>1.1.4</Version>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
Expand Down
41 changes: 20 additions & 21 deletions Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,30 @@
// </copyright>
// -----------------------------------------------------------------------

namespace Conglomo.DataPump
namespace Conglomo.DataPump;

/// <summary>
/// The database type.
/// </summary>
public enum Database
{
/// <summary>
/// The database type.
/// No database selected.
/// </summary>
public enum Database
{
/// <summary>
/// No database selected.
/// </summary>
None = 0,
None = 0,

/// <summary>
/// The Firebird database.
/// </summary>
Firebird = 1,
/// <summary>
/// The Firebird database.
/// </summary>
Firebird = 1,

/// <summary>
/// The Microsoft SQL Server database.
/// </summary>
MSSQL = 2,
/// <summary>
/// The Microsoft SQL Server database.
/// </summary>
MSSQL = 2,

/// <summary>
/// The MySQL database.
/// </summary>
MySQL = 3,
}
/// <summary>
/// The MySQL database.
/// </summary>
MySQL = 3,
}
249 changes: 125 additions & 124 deletions ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,154 +4,155 @@
// </copyright>
// -----------------------------------------------------------------------

namespace Conglomo.DataPump
{
using System;
using System.IO;
using System.Text;
namespace Conglomo.DataPump;

using System;
using System.IO;
using System.Text;

/// <summary>
/// Extension methods.
/// </summary>
public static class ExtensionMethods
{
/// <summary>
/// Extension methods.
/// Encodes a string for a CSV field.
/// </summary>
public static class ExtensionMethods
/// <param name="field">The field to encode.</param>
/// <param name="separator">The separator (defaults to a comma).</param>
/// <returns>
/// A string suitable to output to a CSV file.
/// </returns>
public static string EncodeCsvField(this string field, char separator = ',')
{
/// <summary>
/// Encodes a string for a CSV field.
/// </summary>
/// <param name="field">The field to encode.</param>
/// <param name="separator">The separator (defaults to a comma).</param>
/// <returns>
/// A string suitable to output to a CSV file.
/// </returns>
public static string EncodeCsvField(this string field, char separator = ',')
{
// Set up the string builder
StringBuilder sb = new StringBuilder(field);
// Set up the string builder
StringBuilder sb = new StringBuilder(field);

// Fields with leading/trailing whitespace must be embedded in double quotes
bool embedInQuotes = sb.Length > 0 && (sb[0] == ' ' || sb[0] == '\t' || sb[^1] == ' ' || sb[^1] == '\t');
// Fields with leading/trailing whitespace must be embedded in double quotes
bool embedInQuotes = sb.Length > 0 && (sb[0] == ' ' || sb[0] == '\t' || sb[^1] == ' ' || sb[^1] == '\t');

// If we have not yet found a reason to embed in quotes
if (!embedInQuotes)
// If we have not yet found a reason to embed in quotes
if (!embedInQuotes)
{
for (int i = 0; i < sb.Length; i++)
{
for (int i = 0; i < sb.Length; i++)
// Embed in quotes to preserve commas, line-breaks etc.
if (sb[i] == separator || sb[i] == '\r' || sb[i] == '\n' || sb[i] == '"')
{
// Embed in quotes to preserve commas, line-breaks etc.
if (sb[i] == separator || sb[i] == '\r' || sb[i] == '\n' || sb[i] == '"')
{
embedInQuotes = true;
break;
}
embedInQuotes = true;
break;
}
}
}

// If the field itself has quotes, they must each be represented by a pair of consecutive quotes.
if (embedInQuotes)
{
sb.Replace("\"", "\"\"");
return $"\"{sb}\"";
}
// If the field itself has quotes, they must each be represented by a pair of consecutive quotes.
if (embedInQuotes)
{
sb.Replace("\"", "\"\"");
return $"\"{sb}\"";
}

// No quotes required
return sb.ToString();
}

// No quotes required
return sb.ToString();
/// <summary>
/// Parses the command line arguments.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="args">The command line arguments.</param>
/// <returns>
/// The pump configuration completed with the command line arguments.
/// </returns>
public static PumpConfiguration ParseArguments(this PumpConfiguration configuration, string[] args)
{
if (configuration == default || args == default)
{
return configuration ?? new PumpConfiguration();
}

/// <summary>
/// Parses the command line arguments.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="args">The command line arguments.</param>
/// <returns>
/// The pump configuration completed with the command line arguments.
/// </returns>
public static PumpConfiguration ParseArguments(this PumpConfiguration configuration, string[] args)
foreach (string arg in args)
{
if (configuration != default && args != default)
try
{
foreach (string arg in args)
if (arg != default)
{
try
if (Enum.TryParse(arg, true, out Database database))
{
configuration.Database = database;
}
else if (Enum.TryParse(arg, true, out FileType fileType))
{
if (arg != default)
{
if (Enum.TryParse(arg, true, out Database database))
{
configuration.Database = database;
}
else if (Enum.TryParse(arg, true, out FileType fileType))
{
configuration.FileType = fileType;
}
else if (arg.EndsWith(".sql", StringComparison.OrdinalIgnoreCase))
{
configuration.SqlFile = arg;
}
else if (arg.EndsWith(".csv", StringComparison.OrdinalIgnoreCase))
{
configuration.FileType = FileType.CSV;
configuration.OutputFile = arg;
}
else if (arg.EndsWith(".xls", StringComparison.OrdinalIgnoreCase))
{
configuration.FileType = FileType.XLS;
configuration.OutputFile = arg;
}
else if (arg.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase))
{
configuration.FileType = FileType.XLSX;
configuration.OutputFile = arg;
}
else if (arg.Contains(".gdb", StringComparison.OrdinalIgnoreCase)
|| arg.Contains(".fdb", StringComparison.OrdinalIgnoreCase))
{
// Firebird connection string
configuration.ConnectionString = arg;
configuration.Database = Database.Firebird;
}
else if (arg.Contains(".mdf", StringComparison.OrdinalIgnoreCase))
{
// Microsoft SQL Server connection string
configuration.ConnectionString = arg;
configuration.Database = Database.MSSQL;
}
else if (string.IsNullOrWhiteSpace(configuration.ConnectionString))
{
// SQL connection string
configuration.ConnectionString = arg;
}
}
configuration.FileType = fileType;
}
catch (Exception ex)
else if (arg.EndsWith(".sql", StringComparison.OrdinalIgnoreCase))
{
// Ignore errors
if (!(ex is ArgumentException
|| ex is ArgumentNullException
|| ex is InvalidOperationException))
{
throw;
}
configuration.SqlFile = arg;
}
else if (arg.EndsWith(".csv", StringComparison.OrdinalIgnoreCase))
{
configuration.FileType = FileType.CSV;
configuration.OutputFile = arg;
}
else if (arg.EndsWith(".xls", StringComparison.OrdinalIgnoreCase))
{
configuration.FileType = FileType.XLS;
configuration.OutputFile = arg;
}
else if (arg.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase))
{
configuration.FileType = FileType.XLSX;
configuration.OutputFile = arg;
}
else if (arg.Contains(".gdb", StringComparison.OrdinalIgnoreCase)
|| arg.Contains(".fdb", StringComparison.OrdinalIgnoreCase))
{
// Firebird connection string
configuration.ConnectionString = arg;
configuration.Database = Database.Firebird;
}
else if (arg.Contains(".mdf", StringComparison.OrdinalIgnoreCase))
{
// Microsoft SQL Server connection string
configuration.ConnectionString = arg;
configuration.Database = Database.MSSQL;
}
else if (string.IsNullOrWhiteSpace(configuration.ConnectionString))
{
// SQL connection string
configuration.ConnectionString = arg;
}
}
}

return configuration ?? new PumpConfiguration();
catch (Exception ex)
{
// Ignore errors
if (!(ex is ArgumentException
|| ex is ArgumentNullException
|| ex is InvalidOperationException))
{
throw;
}
}
}

/// <summary>
/// Returns true if the pump configuration is valid.
/// </summary>
/// <param name="configuration">The pump configuration.</param>
/// <returns>
/// <c>true</c> if the specified pump configuration is valid; otherwise, <c>false</c>.
/// </returns>
/// <remarks>Checks for empty values or an invalid SQL file.</remarks>
public static bool IsValid(this PumpConfiguration configuration)
=> configuration != default
&& configuration.Database != Database.None
&& !string.IsNullOrWhiteSpace(configuration.ConnectionString)
&& configuration.FileType != FileType.None
&& !string.IsNullOrWhiteSpace(configuration.OutputFile)
&& !string.IsNullOrWhiteSpace(configuration.SqlFile)
&& File.Exists(configuration.SqlFile);
return configuration ?? new PumpConfiguration();
}

/// <summary>
/// Returns true if the pump configuration is valid.
/// </summary>
/// <param name="configuration">The pump configuration.</param>
/// <returns>
/// <c>true</c> if the specified pump configuration is valid; otherwise, <c>false</c>.
/// </returns>
/// <remarks>Checks for empty values or an invalid SQL file.</remarks>
public static bool IsValid(this PumpConfiguration configuration)
=> configuration != default
&& configuration.Database != Database.None
&& !string.IsNullOrWhiteSpace(configuration.ConnectionString)
&& configuration.FileType != FileType.None
&& !string.IsNullOrWhiteSpace(configuration.OutputFile)
&& !string.IsNullOrWhiteSpace(configuration.SqlFile)
&& File.Exists(configuration.SqlFile);
}
Loading

0 comments on commit 8ca0c0d

Please sign in to comment.