/// <summary> /// Obtiene la cadena de comparación de campos /// </summary> private string GetSqlCompareFields(ConnectionTableModel table, ScriptsValidationOptions options) { string sql = string.Empty; // Añade la comparación de campos foreach (ConnectionTableFieldModel field in table.Fields) { string nullValue = GetDefaultNullValue(field.Type); string sourceField = $"IfNull({Provider.SqlHelper.FormatName("Target", field.Name)}, {nullValue})"; string targetField = $"IfNull({GetSqlReplaceField("Test", field, options)}, {nullValue})"; // Añade el operador si es necesario if (!string.IsNullOrWhiteSpace(sql)) { sql += "\t\t\t\tAND "; } // Si estamos comparando texto, en caso que queramos sólo comparar caracteres alfanuméricos o dígitos le añade el RegEx if (field.Type == ConnectionTableFieldModel.Fieldtype.String && options.CompareOnlyAlphaAndDigits) { sourceField = GetSqlRegexAlphaAndDigits(sourceField); targetField = GetSqlRegexAlphaAndDigits(targetField); } // Añade la comparación sql += $"{sourceField} = {targetField}" + Environment.NewLine; } // Devuelve la cadena SQL return(sql); }
/// <summary> /// Obtiene el tipo decimal /// </summary> private string GetDecimalType(ScriptsValidationOptions options) { if (string.IsNullOrWhiteSpace(options.DecimalType)) { return("float"); } else { return(options.DecimalType); } }
/// <summary> /// Obtiene la cadena de validación sobre un archivo /// </summary> internal string GetSqlValidateFile(ConnectionTableModel table, bool countRecords, ScriptsValidationOptions options) { string sql = string.Empty; // Crea la cadena SQL de comparación si hay algún campo en la tabla if (table.Fields.Count > 0) { if (countRecords) { sql = "\tSELECT COUNT(*) AS Number"; } else { sql = "\t" + GetSqlHeaderCompare(table, "Test", "Target"); } sql += Environment.NewLine + $"\t\tFROM {GetFileNameTable(options.MountPathVariable, options.SubpathValidate, table.Name, options.FormatType, options.TablePrefixes)} AS Test"; sql += Environment.NewLine + "\t\tFULL OUTER JOIN {{" + options.DataBaseComputeVariable + "}}." + Provider.SqlHelper.FormatName(table.Name) + " AS Target"; sql += Environment.NewLine + "\t\t\tON " + GetSqlCompareFields(table, options).Trim(); sql += Environment.NewLine + $"\t\tWHERE {Provider.SqlHelper.FormatName("Target", table.Fields[0].Name)} IS NULL"; sql += Environment.NewLine + $"\t\t\tOR {Provider.SqlHelper.FormatName("Test", table.Fields[0].Name)} IS NULL"; sql += Environment.NewLine; } // Devuelve la cadena SQL return(sql); }
/// <summary> /// Cuando se compara por cadenas, obtiene la cadena de reemplazo junto al nombre de campo /// </summary> private string GetSqlReplaceField(string tableAlias, ConnectionTableFieldModel field, ScriptsValidationOptions options) { string sql = Provider.SqlHelper.FormatName(tableAlias, field.Name); // Si son cadenas, hace la conversión adecuada if (options.CompareString) { switch (field.Type) { case ConnectionTableFieldModel.Fieldtype.Date: if (!string.IsNullOrWhiteSpace(options.DateFormat)) { sql = $"to_date({sql}, '{options.DateFormat}')"; } break; case ConnectionTableFieldModel.Fieldtype.Decimal: if (!string.IsNullOrWhiteSpace(options.DecimalSeparator)) { sql = $"CAST(REPLACE({sql}, '{options.DecimalSeparator}', '.') AS {GetDecimalType(options)})"; } break; case ConnectionTableFieldModel.Fieldtype.Integer: if (IsFieldBit(field.Name, options.BitFields)) { sql = $"ABS({sql})"; } break; } } // Devuelve la cadena SQL return(sql); }
public ScriptsValidationGenerator(SolutionManager manager, ScriptsValidationOptions options) { Manager = manager; Options = options; }