Skip to content

Commit c70daab

Browse files
committed
Atualiza gerenciamento de configuração e mapeamento de colunas
Implementa suporte para especificar caminho de configuração via linha de comando em `RefreshSPOLists.cs`. Altera a obtenção da URL do contexto em `RefreshSQLLists.cs` para usar `config.SharepointList`. Modifica a lógica de definição de tipos SQL em `SQLInteraction.cs` para incluir mapeamento de colunas com tipos de dados definidos no XML. Transforma `ConfigurationReader` em uma classe estática em `ConfigHelper.cs` e atualiza o caminho do arquivo de configuração. Adiciona novo atributo `datatype` em `UserConfig.xml` para permitir a especificação de tipos de dados nas colunas.
1 parent 7c2b703 commit c70daab

4 files changed

Lines changed: 45 additions & 19 deletions

File tree

SPOtoSQL-Snapshots/ConsoleApp1/SPODataQuality/RefreshSPOLists.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ internal class RefreshSPOLists
1414
// Main entry point of the application, executed when the program starts
1515
private static void Main(string[] args)
1616
{
17+
string configPath = "SPO_to_SQL_config.xml"; // Default path for the configuration file
18+
19+
foreach (var arg in args)
20+
{
21+
if (arg.StartsWith("--config="))
22+
{
23+
configPath = arg.Substring("--config=".Length);
24+
}
25+
}
26+
27+
// 2. Configure o caminho do XML antes de qualquer uso do ConfigurationReader
28+
Bring.XmlConfig.ConfigurationReader.SetConfigPath(configPath);
29+
1730
try
1831
{
1932
Console.WriteLine("DEBUG: Starting Main");

SPOtoSQL-Snapshots/ConsoleApp1/Sqlserver/RefreshSQLLists.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static void SPOtoSQLUpdate(bool daily)
4949
var config = kvp.Value;
5050
if (config.Ignore) continue; // Ignores the list if "ignore="true\""
5151

52-
string ctxURL = config.Context;
52+
string ctxURL = config.SharepointList;
5353
Console.WriteLine($"SPOtoSQLUpdate: Processing list: {listName} with URL: {ctxURL}");
5454
try
5555
{

SPOtoSQL-Snapshots/ConsoleApp1/Sqlserver/SQLInteraction.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,13 @@ private void CreateTable()
312312

313313
foreach (var fn in this.FNDictionary)
314314
{
315-
string sqlType = this.SQLFieldType(fn.Value);
315+
string sqlType = null;
316+
// Verifica se há DataType definido no mapeamento
317+
if (this.ColumnMappings != null && this.ColumnMappings.TryGetValue(fn.Value.InternalName, out var mapping) && !string.IsNullOrEmpty(mapping.DataType))
318+
sqlType = mapping.DataType;
319+
else
320+
sqlType = this.SQLFieldType(fn.Value);
321+
316322
if (sqlType != null)
317323
stringBuilder.AppendLine($"[{fn.Key}] {sqlType} NULL,");
318324
}
@@ -343,18 +349,20 @@ private void UpdateTableDesign()
343349
{
344350
try
345351
{
346-
string sqlType = this.SQLFieldType(fn.Value);
352+
string sqlType = null;
353+
if (this.ColumnMappings != null && this.ColumnMappings.TryGetValue(fn.Value.InternalName, out var mapping) && !string.IsNullOrEmpty(mapping.DataType))
354+
sqlType = mapping.DataType;
355+
else
356+
sqlType = this.SQLFieldType(fn.Value);
357+
347358
string colName = fn.Key;
348359

349-
// Verifica se a coluna já existe na tabela
350360
this.Command.CommandText = $"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{this.TableName}' AND COLUMN_NAME = '{colName}'";
351361
if ((int)this.Command.ExecuteScalar() == 0)
352362
{
353-
// Se não existe, cria a coluna automaticamente
354363
this.Command.CommandText = $"ALTER TABLE [{this.TableName}] ADD [{colName}] {sqlType} NULL";
355364
this.Command.ExecuteNonQuery();
356365
updatedColumns++;
357-
LogInfo("UpdateTableDesign", $"Column '{colName}' created automatically.");
358366
}
359367
}
360368
catch (Exception ex)

SPOtoSQL-Snapshots/ConsoleApp1/XmlConfig/ConfigHelper.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ namespace Bring.XmlConfig
99
/// Provides methods to read SharePoint credentials, SQL connection strings,
1010
/// and configuration settings from an XML configuration file.
1111
/// </summary>
12-
public class ConfigurationReader
12+
public static class ConfigurationReader
1313
{
14-
// Path to the XML configuration file relative to application root
15-
private static readonly string CONFIG_PATH = "XmlConfig/UserConfig.xml";
16-
17-
// Singleton XmlDocument instance, loaded on first access
14+
private static string _configPath = "SPO_to_SQL_config.xml"; // valor padrão
1815
private static XmlDocument _xmlDoc;
1916

17+
public static void SetConfigPath(string path)
18+
{
19+
_configPath = path;
20+
_xmlDoc = null; // força recarregar se já estava carregado
21+
}
22+
2023
/// <summary>
2124
/// Loads the XML configuration document if it hasn't been loaded yet.
2225
/// Throws an exception if the file cannot be read.
@@ -30,8 +33,8 @@ private static void LoadConfig()
3033
try
3134
{
3235
_xmlDoc = new XmlDocument();
33-
_xmlDoc.Load(CONFIG_PATH);
34-
Console.WriteLine("Configuration file loaded successfully.");
36+
_xmlDoc.Load(_configPath);
37+
Console.WriteLine($"Configuration file loaded successfully from {_configPath}.");
3538
}
3639
catch (Exception ex)
3740
{
@@ -342,15 +345,15 @@ public static Dictionary<string, ListConfiguration> GetListConfigurations()
342345
foreach (XmlNode listNode in listNodes)
343346
{
344347
var nameAttr = listNode.Attributes["name"];
345-
var contextAttr = listNode.Attributes["context"];
348+
var contextAttr = listNode.Attributes["sharepointlist"];
346349
var ignoreAttr = listNode.Attributes["ignore"];
347350

348351
if (nameAttr != null)
349352
{
350353
var listConfig = new ListConfiguration
351354
{
352355
Name = nameAttr.Value,
353-
Context = contextAttr?.Value,
356+
SharepointList = contextAttr?.Value,
354357
Ignore = ignoreAttr != null && bool.Parse(ignoreAttr.Value),
355358
Columns = GetListColumns(listNode) // Get list-specific column mappings
356359
};
@@ -370,7 +373,6 @@ public static Dictionary<string, ListConfiguration> GetListConfigurations()
370373

371374
private static Dictionary<string, ColumnMapping> GetListColumns(XmlNode listNode)
372375
{
373-
// Use relative XPath to find columns within this specific list
374376
var columnNodes = listNode.SelectNodes(".//Columns/column");
375377
if (columnNodes == null || columnNodes.Count == 0)
376378
return null;
@@ -382,14 +384,16 @@ private static Dictionary<string, ColumnMapping> GetListColumns(XmlNode listNode
382384
var sourceAttr = node.Attributes["source"];
383385
var destAttr = node.Attributes["destination"];
384386
var ignoreAttr = node.Attributes["ignore"];
387+
var datatypeAttr = node.Attributes["datatype"];
385388

386389
if (sourceAttr != null)
387390
{
388391
var mapping = new ColumnMapping
389392
{
390393
Source = sourceAttr.Value,
391-
Destination = destAttr?.Value ?? sourceAttr.Value, // Default to source if no destination specified
392-
Ignore = ignoreAttr != null && bool.Parse(ignoreAttr.Value)
394+
Destination = destAttr?.Value ?? sourceAttr.Value,
395+
Ignore = ignoreAttr != null && bool.Parse(ignoreAttr.Value),
396+
DataType = datatypeAttr?.Value
393397
};
394398

395399
columnMappings[mapping.Source] = mapping;
@@ -418,13 +422,14 @@ public class ColumnMapping
418422
public string Source { get; set; }
419423
public string Destination { get; set; }
420424
public bool Ignore { get; set; }
425+
public string DataType { get; set; }
421426
}
422427

423428
// List-specific configuration that can override global settings
424429
public class ListConfiguration
425430
{
426431
public string Name { get; set; }
427-
public string Context { get; set; }
432+
public string SharepointList { get; set; }
428433
public bool Ignore { get; set; }
429434
public Dictionary<string, ColumnMapping> Columns { get; set; }
430435
}

0 commit comments

Comments
 (0)