Skip to content

Commit

Permalink
fix sql
Browse files Browse the repository at this point in the history
  • Loading branch information
DWDBE committed Dec 17, 2024
1 parent aacedab commit bc62906
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/OrderSourceReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void LoadReader(string whereSql)
if (_columnMappings.Count == 0)
return;

string sql = $"SELECT {GetColumns()} FROM {GetFromTables()} ";
string sql = $"SELECT * FROM (SELECT {GetColumns()} FROM {GetFromTables()}) AS innerTable";

if (!string.IsNullOrEmpty(whereSql))
sql = sql + " where " + whereSql;
Expand All @@ -72,6 +72,7 @@ private void LoadReader(string whereSql)
throw new Exception("Failed to open sqlSourceReader. Reason: " + ex.Message, ex);
}
}

protected override string GetColumns()
{
string columns = GetDistinctColumnsFromMapping(new string[] { "OrderCustomerAccessUserExternalId", "OrderLineCalculatedDiscountPercentage" });
Expand All @@ -80,7 +81,7 @@ protected override string GetColumns()
{
case "EcomOrders":
columns = columns + ", [AccessUserExternalId] as [OrderCustomerAccessUserExternalId]";
if (!columns.Split(',').Any(c => c.Trim(new char[] { ' ', '[', ']' }).Equals("OrderId", StringComparison.OrdinalIgnoreCase)))
if (!columns.Split(',').Any(c => c.Trim([' ', '[', ']']).Equals("OrderId", StringComparison.OrdinalIgnoreCase)))
{
columns += ", [OrderId]";
}
Expand All @@ -91,10 +92,30 @@ protected override string GetColumns()

}

string[] standardConditionColumns = ["OrderIsExported", "OrderIntegrationOrderId", "OrderCart", "OrderComplete"];

if (mapping.Conditionals.Any())
{
columns += $", {GetColumnsFromMappingConditions()}";
columns = columns[..^2];
var columnsToSkip = columns.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Union(standardConditionColumns)
.Union(["OrderCustomerAccessUserExternalId", "OrderLineCalculatedDiscountPercentage"])
.Select(c => c.Trim([' ', '[', ']']));
var conditionColumns = GetColumnsFromMappingConditions(columnsToSkip.ToArray());
if (!string.IsNullOrEmpty(conditionColumns))
{
columns += $", {conditionColumns}";
columns = columns[..^2];
}
}

// Add Order source provider standard conditional columns
var splittedColumns = columns.Split(',');
foreach (var column in standardConditionColumns)
{
if (!splittedColumns.Any(c => c.Trim(new char[] { ' ', '[', ']' }).Equals(column, StringComparison.OrdinalIgnoreCase)))
{
columns += $", [{column}]";
}
}

return columns;
Expand Down Expand Up @@ -181,7 +202,7 @@ public static void UpdateExportedOrdersInDb(string orderStateIDAfterExport, SqlC
if (connection.State.ToString() != "Open")
connection.Open();

command.Transaction = connection.BeginTransaction("OrderProviderTransaction");
command.Transaction = connection.BeginTransaction("OrderProviderTransaction");

string sql = "UPDATE EcomOrders SET OrderIsExported = 1";

Expand Down

0 comments on commit bc62906

Please sign in to comment.