Skip to content

Commit ee12472

Browse files
committed
Refatorar gerenciamento de colunas na replicação SQL
As colunas selecionadas e ignoradas na classe `SQLInteraction` agora são gerenciadas por um `Dictionary<string, ColumnMapping>`, permitindo um mapeamento mais flexível entre colunas de origem e destino, além de indicar se uma coluna deve ser ignorada. A função `GetSelectedColumns` na classe `ConfigHelper` foi atualizada para retornar o novo tipo de dados, refletindo a nova estrutura de configuração. No arquivo `UserConfig.xml`, a estrutura das colunas foi alterada para incluir atributos `source`, `destination` e `ignore`, proporcionando uma configuração mais detalhada. Várias partes do código foram comentadas para indicar que o sistema anterior de colunas selecionadas e ignoradas não está mais em uso.
1 parent f0945b2 commit ee12472

2 files changed

Lines changed: 52 additions & 35 deletions

File tree

‎SPOtoSQL-Snapshots/ConsoleApp1/Sqlserver/SQLInteraction.cs‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ internal class SQLInteraction
2828
public string CurrentTime { get; set; }
2929

3030
// Store selected and ignored columns from configuration
31-
private HashSet<string> SelectedColumns { get; set; }
31+
// private HashSet<string> SelectedColumns { get; set; }
3232
private HashSet<string> IgnoredColumns { get; set; }
33+
private Dictionary<string, ColumnMapping> ColumnMappings { get; set; }
3334

3435
/// <summary>
3536
/// Initializes the SQL table for the specified SharePoint list,
@@ -42,10 +43,10 @@ public void Build()
4243
try
4344
{
4445
// Load selected and ignored columns from configuration
45-
this.SelectedColumns = ConfigurationReader.GetSelectedColumns();
46+
// this.SelectedColumns = ConfigurationReader.GetSelectedColumns();
4647
this.IgnoredColumns = ConfigurationReader.GetIgnoredColumns();
4748

48-
LogInfo("Build", $"Selected columns from config: {(this.SelectedColumns == null ? "All" : string.Join(", ", this.SelectedColumns))}");
49+
// LogInfo("Build", $"Selected columns from config: {(this.SelectedColumns == null ? "All" : string.Join(", ", this.SelectedColumns))}");
4950
LogInfo("Build", $"Ignored columns from config: {(this.IgnoredColumns == null ? "None" : string.Join(", ", this.IgnoredColumns))}");
5051

5152
this.TableName = this.ToPascalCase(this.List.Name, false);
@@ -224,24 +225,37 @@ private void BuildDictionary()
224225
int skippedFields = 0;
225226
int ignoredFields = 0;
226227

228+
this.ColumnMappings = ConfigurationReader.GetSelectedColumns();
229+
this.IgnoredColumns = ConfigurationReader.GetIgnoredColumns();
230+
227231
foreach (Field field in this.List.Fields)
228232
{
229233
if (field.TypeAsString != "Computed")
230234
{
231235
try
232236
{
233-
string columnName = this.GetActualColName(field);
237+
string columnName = field.InternalName;
234238

235-
// Skip if the column is in the ignored list
236-
if (this.IgnoredColumns != null && this.IgnoredColumns.Contains(columnName))
239+
if (this.ColumnMappings != null && this.ColumnMappings.TryGetValue(columnName, out var mapping))
240+
{
241+
if (mapping.Ignore)
242+
{
243+
ignoredFields++;
244+
LogInfo("BuildDictionary", $"Ignored field (by mapping): {columnName}");
245+
continue;
246+
}
247+
248+
string destinationName = mapping.Destination;
249+
this.FNDictionary.Add(this.GetKeyName(destinationName, 1), field);
250+
processedFields++;
251+
LogInfo("BuildDictionary", $"Added mapped field: {columnName} -> {destinationName}");
252+
}
253+
else if (this.IgnoredColumns != null && this.IgnoredColumns.Contains(columnName))
237254
{
238255
ignoredFields++;
239-
LogInfo("BuildDictionary", $"Ignored field: {columnName}");
240-
continue;
256+
LogInfo("BuildDictionary", $"Ignored field (global): {columnName}");
241257
}
242-
243-
// Only add the field if it's selected in configuration or if no specific columns are selected
244-
if (this.SelectedColumns == null || this.SelectedColumns.Contains(columnName))
258+
else if (this.ColumnMappings == null)
245259
{
246260
this.FNDictionary.Add(this.GetKeyName(columnName, 1), field);
247261
processedFields++;
@@ -250,7 +264,7 @@ private void BuildDictionary()
250264
else
251265
{
252266
skippedFields++;
253-
LogInfo("BuildDictionary", $"Skipped field (not selected): {columnName}");
267+
LogInfo("BuildDictionary", $"Skipped field (not mapped): {columnName}");
254268
}
255269
}
256270
catch (Exception ex)
@@ -259,10 +273,6 @@ private void BuildDictionary()
259273
LogError("BuildDictionary", $"Failed to process field: {field.Title}", ex);
260274
}
261275
}
262-
else
263-
{
264-
skippedFields++;
265-
}
266276
}
267277

268278
LogInfo("BuildDictionary",

‎SPOtoSQL-Snapshots/ConsoleApp1/XmlConfig/ConfigHelper.cs‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,45 +44,45 @@ private static void LoadConfig()
4444
/// Gets the list of columns that should be included in the replication process.
4545
/// </summary>
4646
/// <returns>
47-
/// A HashSet of column names to include, or null if all columns should be included.
47+
/// A Dictionary of column mappings, or null if all columns should be included.
4848
/// </returns>
49-
public static HashSet<string> GetSelectedColumns()
49+
public static Dictionary<string, ColumnMapping> GetSelectedColumns()
5050
{
5151
LoadConfig();
5252

5353
try
5454
{
55-
var columnNodes = _xmlDoc.SelectNodes("//Configuration/ReplicationConfiguration/SelectColumns/Column");
55+
var columnNodes = _xmlDoc.SelectNodes("//Configuration/ReplicationConfiguration/SelectColumns/column");
5656

57-
// If no column nodes exist, return null to indicate all columns should be included
5857
if (columnNodes == null || columnNodes.Count == 0)
5958
{
6059
Console.WriteLine("No specific columns configured. All columns will be included.");
6160
return null;
6261
}
6362

64-
// Create case-insensitive HashSet for column names
65-
var selectedColumns = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
63+
var columnMappings = new Dictionary<string, ColumnMapping>(StringComparer.OrdinalIgnoreCase);
6664

6765
foreach (XmlNode node in columnNodes)
6866
{
69-
if (!string.IsNullOrWhiteSpace(node.InnerText))
67+
var sourceAttr = node.Attributes["source"];
68+
var destAttr = node.Attributes["destination"];
69+
var ignoreAttr = node.Attributes["ignore"];
70+
71+
if (sourceAttr != null)
7072
{
71-
string columnName = node.InnerText.Trim();
72-
selectedColumns.Add(columnName);
73-
Console.WriteLine($"Added selected column: {columnName}");
73+
var mapping = new ColumnMapping
74+
{
75+
Source = sourceAttr.Value,
76+
Destination = destAttr?.Value ?? sourceAttr.Value,
77+
Ignore = ignoreAttr != null && bool.Parse(ignoreAttr.Value)
78+
};
79+
80+
columnMappings[mapping.Source] = mapping;
81+
Console.WriteLine($"Added column mapping: {mapping.Source} -> {mapping.Destination} (Ignore: {mapping.Ignore})");
7482
}
7583
}
7684

77-
// If no valid columns were added, return null to include all columns
78-
if (selectedColumns.Count == 0)
79-
{
80-
Console.WriteLine("No valid columns specified. All columns will be included.");
81-
return null;
82-
}
83-
84-
Console.WriteLine($"Total selected columns: {selectedColumns.Count}");
85-
return selectedColumns;
85+
return columnMappings.Count > 0 ? columnMappings : null;
8686
}
8787
catch (Exception ex)
8888
{
@@ -311,4 +311,11 @@ public class SharePointListConfig
311311
public bool AutoAddNewColumns { get; set; }
312312
public bool Disabled { get; set; }
313313
}
314+
315+
public class ColumnMapping
316+
{
317+
public string Source { get; set; }
318+
public string Destination { get; set; }
319+
public bool Ignore { get; set; }
320+
}
314321
}

0 commit comments

Comments
 (0)