Skip to content

Commit

Permalink
New procedures, bug fix
Browse files Browse the repository at this point in the history
- new procedure: divide
- new procedure: thousand separator
- fixed duplicate error if column for "copy old" already exists
  • Loading branch information
Kirdock committed Jan 17, 2022
1 parent 892e15b commit 743b577
Show file tree
Hide file tree
Showing 16 changed files with 1,249 additions and 201 deletions.
43 changes: 38 additions & 5 deletions DataTableConverter/Assisstant/DatabaseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ private void Init(bool createMainDatabase)
SQLiteFunction.RegisterFunction(typeof(CustomTrim)); //CUSTOMTRIM(myValue, myCharacters, isDeleteDouble, trimType)
SQLiteFunction.RegisterFunction(typeof(CustomSubstring)); //CUSTOMSUBSTRING(myValue, replaceText, start, end, isReplace, isReverse)
SQLiteFunction.RegisterFunction(typeof(SQL_Functions.Padding)); //PADDING(value, type, count, character)
SQLiteFunction.RegisterFunction(typeof(Divide)); //DIVIDE(value, divisor)
SQLiteFunction.RegisterFunction(typeof(ThousandSeparator)); //THOUSAND_SEPARATOR(value)

if (createMainDatabase)
{
Expand Down Expand Up @@ -829,7 +831,7 @@ internal string AddColumnWithAdditionalIfExists(string columnName, string tableN
{
newAlias = columnName + counter;
}
return AddColumnFixedAlias(newAlias, tableName, defaultValue, destinationTableColumnAliasMapping.Values);
return AddColumnFixedAlias(newAlias, tableName, defaultValue);
}

/// <summary>
Expand All @@ -839,16 +841,16 @@ internal string AddColumnWithAdditionalIfExists(string columnName, string tableN
/// <param name="tableName"></param>
/// <param name="defaultValue"></param>
/// /// <returns>Column name of created column</returns>
internal string AddColumnFixedAlias(string alias, string tableName, string defaultValue = "", IEnumerable<string> columnNames = null)
internal string AddColumnFixedAlias(string alias, string tableName, string defaultValue = "")
{
string columnName = GetValidColumnName(alias, tableName, columnNames);
string columnName = GetValidColumnName(alias, tableName);
AddColumnWithAlias(columnName, alias, tableName, defaultValue);
return columnName;
}

private string GetValidColumnName(string alias, string tableName, IEnumerable<string> columnNames = null)
private string GetValidColumnName(string alias, string tableName)
{
columnNames = columnNames ?? GetColumnNames(tableName, true);
List<string> columnNames = GetColumnNames(tableName, true);
string columnName = alias;
for (int counter = 1; columnNames.Any(col => col.Equals(columnName, StringComparison.OrdinalIgnoreCase)); ++counter)
{
Expand Down Expand Up @@ -2585,5 +2587,36 @@ internal void InsertRowDuplicate(string id, string value, SQLiteCommand command)
command.Parameters[1].Value = value;
command.ExecuteNonQuery();
}

internal void Divide(string[] sourceColumns, string[] destinationColumns, decimal divisor, string tableName)
{
using (SQLiteCommand command = GetConnection(tableName).CreateCommand())
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < sourceColumns.Length; ++i)
{
builder.Append("[").Append(destinationColumns[i]).Append("]=DIVIDE([").Append(sourceColumns[i]).Append("],$divisor), ");
}
builder.Remove(builder.Length - 2, 2);
command.Parameters.AddWithValue("$divisor", divisor);
command.CommandText = $"UPDATE [{tableName}] SET {builder}";
command.ExecuteNonQuery();
}
}

internal void ThousandSeparator(string[] sourceColumns, string[] destinationColumns, string tableName)
{
using (SQLiteCommand command = GetConnection(tableName).CreateCommand())
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < sourceColumns.Length; ++i)
{
builder.Append("[").Append(destinationColumns[i]).Append("]=THOUSAND_SEPARATOR([").Append(sourceColumns[i]).Append("]), ");
}
builder.Remove(builder.Length - 2, 2);
command.CommandText = $"UPDATE [{tableName}] SET {builder}";
command.ExecuteNonQuery();
}
}
}
}
21 changes: 21 additions & 0 deletions DataTableConverter/Assisstant/SQL Functions/Divide.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataTableConverter.Assisstant.SQL_Functions
{
[SQLiteFunction(Name = "DIVIDE", Arguments = 2, FuncType = FunctionType.Scalar)]
class Divide : SQLiteFunction
{

public override object Invoke(object[] args)
{
decimal.TryParse(args[0].ToString(), out decimal dividend);
decimal.TryParse(args[1].ToString(), out decimal divisor);
return divisor != 0 ? (Math.Floor((dividend / divisor) * 100)/100).ToString("0.##") : "0";
}
}
}
21 changes: 21 additions & 0 deletions DataTableConverter/Assisstant/SQL Functions/ThousandSeparator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataTableConverter.Assisstant.SQL_Functions
{
[SQLiteFunction(Name = "THOUSAND_SEPARATOR", Arguments = 1, FuncType = FunctionType.Scalar)]
class ThousandSeparator : SQLiteFunction
{

public override object Invoke(object[] args)
{
decimal.TryParse(args[0].ToString(), out decimal result);
int decimals = BitConverter.GetBytes(decimal.GetBits(result)[3])[2];
return result.ToString($"N{decimals}");
}
}
}
8 changes: 8 additions & 0 deletions DataTableConverter/Assisstant/WorkflowFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ internal static WorkProc CreateWorkProc(int type, int id, int ordinal, string na
newProc = new ProcMergeRows(ordinal, id, name);
break;

case 19:
newProc = new ProcDivide(ordinal, id, name);
break;

case 20:
newProc = new ProcThousandSeparator(ordinal, id, name);
break;

case 1:
default:
newProc = new ProcTrim(ordinal, id, name);
Expand Down
63 changes: 63 additions & 0 deletions DataTableConverter/Classes/WorkProcs/ProcDivide.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using DataTableConverter.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataTableConverter.Classes.WorkProcs
{
[Serializable()]
class ProcDivide: WorkProc
{
internal static readonly string ClassName = "Dividieren";
public decimal Divisor = 1;

public ProcDivide(int ordinal, int id, string name) : base(ordinal, id, name) { }

public ProcDivide(string[] columns, decimal divisor, string newColumn, bool copyOldColumn)
{
Divisor = divisor;
NewColumn = newColumn;
Columns = new DataTable { TableName = "Columnnames" };
Columns.Columns.Add("Spalten", typeof(string));
foreach (string col in columns)
{
Columns.Rows.Add(col);
}
CopyOldColumn = copyOldColumn;
}

public override string[] GetHeaders()
{
return RemoveEmptyHeaders(Columns.AsEnumerable().Select(dr => dr.ItemArray.FirstOrDefault()?.ToString()));
}

public override void RenameHeaders(string oldName, string newName)
{
foreach (DataRow row in Columns.Rows)
{
if (row[0].ToString() == oldName)
{
row[0] = newName;
}
}
}

public override void RemoveHeader(string colName)
{
Columns = Columns.AsEnumerable().Where(row => row[0].ToString() != colName).ToTable(Columns);
}

public override void DoWork(ref string sortingOrder, Case duplicateCase, List<Tolerance> tolerances, Proc procedure, string filename, ContextMenuStrip ctxRow, OrderType orderType, Form1 invokeForm, string tableName)
{
string[] columns = GetHeaders();
if (PrepareMultiple(columns, invokeForm, tableName, out string[] sourceColumns, out string[] destinationColumns))
{
invokeForm.DatabaseHelper.Divide(sourceColumns, destinationColumns, Divisor, tableName);
}
}
}
}
61 changes: 61 additions & 0 deletions DataTableConverter/Classes/WorkProcs/ProcThousandSeparator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using DataTableConverter.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataTableConverter.Classes.WorkProcs
{
[Serializable()]
class ProcThousandSeparator : WorkProc
{
internal static readonly string ClassName = "Tausender Trennzeichen";

public override string[] GetHeaders()
{
return RemoveEmptyHeaders(Columns.AsEnumerable().Select(dr => dr.ItemArray.FirstOrDefault()?.ToString()));
}

public ProcThousandSeparator(int ordinal, int id, string name) : base(ordinal, id, name) { }

public ProcThousandSeparator(string[] columns, string newColumn, bool copyOldColumn)
{
NewColumn = newColumn;
Columns = new DataTable { TableName = "Columnnames" };
Columns.Columns.Add("Spalten", typeof(string));
foreach (string col in columns)
{
Columns.Rows.Add(col);
}
CopyOldColumn = copyOldColumn;
}

public override void RenameHeaders(string oldName, string newName)
{
foreach (DataRow row in Columns.Rows)
{
if (row[0].ToString() == oldName)
{
row[0] = newName;
}
}
}

public override void RemoveHeader(string colName)
{
Columns = Columns.AsEnumerable().Where(row => row[0].ToString() != colName).ToTable(Columns);
}

public override void DoWork(ref string sortingOrder, Case duplicateCase, List<Tolerance> tolerances, Proc procedure, string filename, ContextMenuStrip ctxRow, OrderType orderType, Form1 invokeForm, string tableName)
{
string[] columns = GetHeaders();
if (PrepareMultiple(columns, invokeForm, tableName, out string[] sourceColumns, out string[] destinationColumns))
{
invokeForm.DatabaseHelper.ThousandSeparator(sourceColumns, destinationColumns, tableName);
}
}
}
}
13 changes: 13 additions & 0 deletions DataTableConverter/DataTableConverter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@
<Compile Include="Assisstant\SQL Functions\CustomSubstring.cs" />
<Compile Include="Assisstant\SQL Functions\CustomTrim.cs" />
<Compile Include="Assisstant\SQL Functions\CustomUppercase.cs" />
<Compile Include="Assisstant\SQL Functions\Divide.cs" />
<Compile Include="Assisstant\SQL Functions\GetSplit.cs" />
<Compile Include="Assisstant\SQL Functions\CountString.cs" />
<Compile Include="Assisstant\SQL Functions\NumberToString.cs" />
<Compile Include="Assisstant\SQL Functions\Padding.cs" />
<Compile Include="Assisstant\SQL Functions\Round.cs" />
<Compile Include="Assisstant\SQL Functions\SQLiteSensitive.cs" />
<Compile Include="Assisstant\SQL Functions\ThousandSeparator.cs" />
<Compile Include="Assisstant\StringComparer.cs" />
<Compile Include="Assisstant\UpdateHelper.cs" />
<Compile Include="Assisstant\WorkflowFactory.cs" />
Expand Down Expand Up @@ -150,6 +152,7 @@
<Compile Include="Classes\WorkProcs\ProcAddTableColumns.cs" />
<Compile Include="Classes\WorkProcs\ProcCompare.cs" />
<Compile Include="Classes\WorkProcs\ProcCount.cs" />
<Compile Include="Classes\WorkProcs\ProcDivide.cs" />
<Compile Include="Classes\WorkProcs\ProcMergeRows.cs" />
<Compile Include="Classes\WorkProcs\ProcNumber.cs" />
<Compile Include="Classes\WorkProcs\ProcOrder.cs" />
Expand All @@ -161,6 +164,7 @@
<Compile Include="Classes\WorkProcs\ProcRound.cs" />
<Compile Include="Classes\WorkProcs\ProcSplit.cs" />
<Compile Include="Classes\WorkProcs\ProcSubstring.cs" />
<Compile Include="Classes\WorkProcs\ProcThousandSeparator.cs" />
<Compile Include="Classes\WorkProcs\ProcTrim.cs" />
<Compile Include="Classes\WorkProcs\ProcMerge.cs" />
<Compile Include="Classes\WorkProcs\ProcDuplicate.cs" />
Expand Down Expand Up @@ -256,6 +260,12 @@
<Compile Include="View\SplitFormMain.Designer.cs">
<DependentUpon>SplitFormMain.cs</DependentUpon>
</Compile>
<Compile Include="View\WorkProcViews\DivideForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="View\WorkProcViews\DivideForm.Designer.cs">
<DependentUpon>DivideForm.cs</DependentUpon>
</Compile>
<Compile Include="View\WorkProcViews\ExportCount.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -444,6 +454,9 @@
<EmbeddedResource Include="View\SplitFormMain.resx">
<DependentUpon>SplitFormMain.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="View\WorkProcViews\DivideForm.resx">
<DependentUpon>DivideForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="View\WorkProcViews\ExportCount.resx">
<DependentUpon>ExportCount.cs</DependentUpon>
</EmbeddedResource>
Expand Down
2 changes: 1 addition & 1 deletion DataTableConverter/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.19.0")]
[assembly: AssemblyFileVersion("1.0.20.0")]
Loading

0 comments on commit 743b577

Please sign in to comment.