Skip to content

Commit

Permalink
Visual studio suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasSort committed Nov 28, 2024
1 parent 37cbba8 commit a1afe12
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 42 deletions.
19 changes: 9 additions & 10 deletions src/OrderDestinationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ public OrderDestinationWriter(Mapping mapping, SqlConnection connection, ILogger
SkipFailingRows = skipFailingRows;
DiscardDuplicates = discardDuplicates;
TempTablePrefix = $"TempTableForBulkImport{mapping.GetId()}";
SqlBulkCopier = new SqlBulkCopy(connection);
SqlBulkCopier.DestinationTableName = mapping.DestinationTable.Name + TempTablePrefix;
SqlBulkCopier.BulkCopyTimeout = 0;
SqlBulkCopier = new SqlBulkCopy(connection)
{
DestinationTableName = mapping.DestinationTable.Name + TempTablePrefix,
BulkCopyTimeout = 0
};
Initialize();
if (connection.State != ConnectionState.Open)
connection.Open();
}

public new void Initialize()
{
List<SqlColumn> destColumns = new();
List<SqlColumn> destColumns = [];
var columnMappings = Mapping.GetColumnMappings();
foreach (ColumnMapping columnMapping in columnMappings.DistinctBy(obj => obj.DestinationColumn.Name))
{
Expand Down Expand Up @@ -112,9 +114,9 @@ internal int MoveDataToMainTable(SqlTransaction sqlTransaction, bool updateOnlyE
// if 10k write table to db, empty table
if (TableToWrite.Rows.Count >= 1000)
{
RowsToWriteCount = RowsToWriteCount + TableToWrite.Rows.Count;
RowsToWriteCount += TableToWrite.Rows.Count;
SkippedFailedRowsCount = SqlBulkCopierWriteToServer(SqlBulkCopier, TableToWrite, SkipFailingRows, Mapping, Logger);
RowsToWriteCount = RowsToWriteCount - SkippedFailedRowsCount;
RowsToWriteCount -= SkippedFailedRowsCount;
TableToWrite.Clear();
if (RowsToWriteCount >= LastLogRowsCount + 10000)
{
Expand All @@ -131,9 +133,6 @@ internal int MoveDataToMainTable(SqlTransaction sqlTransaction, bool updateOnlyE
SqlCommand.CommandText = "if exists (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'" + text + "') AND type in (N'U')) drop table " + text;
SqlCommand.ExecuteNonQuery();
((IDisposable)SqlBulkCopier).Dispose();
if (duplicateRowsHandler != null)
{
duplicateRowsHandler.Dispose();
}
duplicateRowsHandler?.Dispose();
}
}
37 changes: 12 additions & 25 deletions src/OrderProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class OrderProvider : BaseSqlProvider, IParameterOptions, ISource, IDesti
private SqlConnection connection;
private SqlConnection Connection
{
get { return connection ?? (connection = (SqlConnection)Database.CreateConnection()); }
get { return connection ??= (SqlConnection)Database.CreateConnection(); }
set { connection = value; }
}

Expand Down Expand Up @@ -318,8 +318,8 @@ public override void UpdateDestinationSettings(IDestination destination)

public override string Serialize()
{
XDocument document = new XDocument(new XDeclaration("1.0", "utf-8", string.Empty));
XElement root = new XElement("Parameters");
XDocument document = new(new XDeclaration("1.0", "utf-8", string.Empty));
XElement root = new("Parameters");
root.Add(CreateParameterNode(GetType(), "Export not yet exported Orders", ExportNotExportedOrders.ToString(CultureInfo.CurrentCulture)));
root.Add(CreateParameterNode(GetType(), "Only export orders without externalID", ExportOnlyOrdersWithoutExtID.ToString(CultureInfo.CurrentCulture)));
root.Add(CreateParameterNode(GetType(), "Export completed orders only", DoNotExportCarts.ToString(CultureInfo.CurrentCulture)));
Expand Down Expand Up @@ -419,8 +419,7 @@ public override bool RunJob(Job job)
msg += GetFailedSourceRowMessage(sourceRow);

Logger.Log("Import job failed: " + msg);
if (sqlTransaction != null)
sqlTransaction.Rollback();
sqlTransaction?.Rollback();

TotalRowsAffected = 0;

Expand Down Expand Up @@ -708,15 +707,11 @@ private void ProcessOrderLineRow(ColumnMappingCollection columnMappings, Diction
string integrationOrderId = Convert.ToString(value);
if (!string.IsNullOrEmpty(integrationOrderId))
{
orderId = ExistingOrdersWithOrderIntegrationOrderId.ContainsKey(integrationOrderId) ? ExistingOrdersWithOrderIntegrationOrderId[integrationOrderId] : integrationOrderId;
orderId = ExistingOrdersWithOrderIntegrationOrderId.TryGetValue(integrationOrderId, out string existingIntegrationOrderId) ? existingIntegrationOrderId : integrationOrderId;
}
}
}
if (!row.ContainsKey(SourceColumnNameForDestinationOrderIntegrationOrderId))
{
row.Add(SourceColumnNameForDestinationOrderIntegrationOrderId, orderId);
}
else
if (!row.TryAdd(SourceColumnNameForDestinationOrderIntegrationOrderId, orderId))
{
row[SourceColumnNameForDestinationOrderIntegrationOrderId] = orderId;
}
Expand Down Expand Up @@ -748,17 +743,13 @@ private void ProcessOrderDeliveryAddress(ColumnMappingCollection columnMappings,
if (row.ContainsKey(OrderDeliveryAddressExternalIdMapping.SourceColumn.Name))
{
string externalID = Convert.ToString(row[OrderDeliveryAddressExternalIdMapping.SourceColumn.Name]);
if (!string.IsNullOrEmpty(externalID) && ExistingAddresses.ContainsKey(externalID))
if (!string.IsNullOrEmpty(externalID) && ExistingAddresses.TryGetValue(externalID, out string value))
{
addressId = ExistingAddresses[externalID];
addressId = value;
}
}
}
if (!row.ContainsKey(SourceColumnNameForDestinationOrderDeliveryAddressId))
{
row.Add(SourceColumnNameForDestinationOrderDeliveryAddressId, addressId);
}
else
if (!row.TryAdd(SourceColumnNameForDestinationOrderDeliveryAddressId, addressId))
{
row[SourceColumnNameForDestinationOrderDeliveryAddressId] = addressId;
}
Expand All @@ -776,17 +767,13 @@ private void ProcessOrderCustomerAccessUser(ColumnMappingCollection columnMappin
if (row.ContainsKey(OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name))
{
string externalID = Convert.ToString(row[OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name]);
if (!string.IsNullOrEmpty(externalID) && ExistingUsers.ContainsKey(externalID))
if (!string.IsNullOrEmpty(externalID) && ExistingUsers.TryGetValue(externalID, out string value))
{
accessUserId = ExistingUsers[externalID];
accessUserId = value;
}
}
}
if (!row.ContainsKey(SourceColumnNameForDestinationOrderCustomerAccessUserId))
{
row.Add(SourceColumnNameForDestinationOrderCustomerAccessUserId, accessUserId);
}
else
if (!row.TryAdd(SourceColumnNameForDestinationOrderCustomerAccessUserId, accessUserId))
{
row[SourceColumnNameForDestinationOrderCustomerAccessUserId] = accessUserId;
}
Expand Down
14 changes: 7 additions & 7 deletions src/OrderSourceReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected override string GetColumns()
{
string columns = GetDistinctColumnsFromMapping(["OrderCustomerAccessUserExternalId", "OrderDeliveryAddressExternalId", "OrderDeliveryAddressLocationCode",
"OrderDeliveryAddressShipmentMethodCode", "OrderDeliveryAddressShippingAgentCode", "OrderDeliveryAddressShippingAgentServiceCode", "OrderLineCalculatedDiscountPercentage"]);
columns = columns.Substring(0, columns.Length - 2);
columns = columns[..^2];
switch (mapping.SourceTable.Name)
{
case "EcomOrders":
Expand All @@ -95,7 +95,7 @@ protected override string GetColumns()
}
break;
case "EcomOrderLines":
columns = columns + ", (-1 * OrderLineTotalDiscountWithVAT) / NULLIF(OrderLinePriceWithVat, 0) * 100 as [OrderLineCalculatedDiscountPercentage]";
columns += ", (-1 * OrderLineTotalDiscountWithVAT) / NULLIF(OrderLinePriceWithVat, 0) * 100 as [OrderLineCalculatedDiscountPercentage]";
break;

}
Expand All @@ -111,11 +111,11 @@ protected override string GetColumns()

private string GetWhereSql(bool exportNotExportedOrders, bool exportOnlyOrdersWithoutExtID, bool doNotExportCarts)
{
List<SqlParameter> parameters = new List<SqlParameter>();
List<SqlParameter> parameters = [];
string conditionalsSql = MappingExtensions.GetConditionalsSql(out parameters, mapping.Conditionals, false, false);
if (conditionalsSql != "")
{
conditionalsSql = conditionalsSql.Substring(0, conditionalsSql.Length - 4);
conditionalsSql = conditionalsSql[..^4];
foreach (SqlParameter p in parameters)
_command.Parameters.Add(p);
}
Expand Down Expand Up @@ -147,7 +147,7 @@ protected override string GetFromTables()
switch (mapping.SourceTable.Name)
{
case "EcomOrderLines":
result = result + " inner join EcomOrders on EcomOrderLines.OrderLineOrderID = EcomOrders.OrderID";
result += " inner join EcomOrders on EcomOrderLines.OrderLineOrderID = EcomOrders.OrderID";
break;
case "EcomOrders":
result = "[dbo].[EcomOrders] left join dbo.AccessUser on OrderCustomerAccessUserID = AccessUserID left join dbo.AccessUserAddress on OrderDeliveryAddressId = AccessUserAddressId";
Expand Down Expand Up @@ -196,7 +196,7 @@ public static void UpdateExportedOrdersInDb(string orderStateIDAfterExport, SqlC

if (!string.IsNullOrEmpty(orderStateIDAfterExport))
{
sql = sql + string.Format(", OrderStateID = '{0}'", orderStateIDAfterExport);
sql += string.Format(", OrderStateID = '{0}'", orderStateIDAfterExport);
}
if (_ordersToExport.Count > 100)
{
Expand All @@ -212,7 +212,7 @@ public static void UpdateExportedOrdersInDb(string orderStateIDAfterExport, SqlC
command.ExecuteNonQuery();
ClearOrderCache(idsCollection);
}
taken = taken + step;
taken += step;
}
}
else
Expand Down

0 comments on commit a1afe12

Please sign in to comment.