public ImmutableColumn(string name, IValueFormat valueFormat, int columnOrdinal, bool?convert = null, string destinationName = "",
                               bool ignore         = false, int part = cPartDefault,
                               string partSplitter = cPartSplitterDefault, bool partToEnd = cPartToEnd, string timePart = "", string timePartFormat = "",
                               string timeZonePart = "")
        {
            Name = name ?? throw new System.ArgumentNullException(nameof(name));
            if (valueFormat is null)
            {
                throw new System.ArgumentNullException(nameof(valueFormat));
            }
            ColumnOrdinal   = columnOrdinal;
            Convert         = convert ?? valueFormat.DataType != DataType.String;
            DestinationName = destinationName;
            Ignore          = ignore;

            Part           = part;
            PartSplitter   = (partSplitter ?? string.Empty).WrittenPunctuation();
            PartToEnd      = partToEnd;
            TimePart       = timePart;
            TimePartFormat = timePartFormat;
            TimeZonePart   = timeZonePart;

            ValueFormat = valueFormat is ImmutableValueFormat immutable
        ? immutable
        : new ImmutableValueFormat(valueFormat.DataType, valueFormat.DateFormat, valueFormat.DateSeparator,
                                   valueFormat.TimeSeparator, valueFormat.NumberFormat, valueFormat.GroupSeparator, valueFormat.DecimalSeparator, valueFormat.True,
                                   valueFormat.False, valueFormat.DisplayNullAs);
        }
Exemple #2
0
 public Column(IColumn source, IValueFormat format)
 {
     m_ColumnOrdinal    = source.ColumnOrdinal;
     m_Convert          = source.Convert;
     m_DestinationName  = source.DestinationName;
     m_Ignore           = source.Ignore;
     m_Name             = source.Name;
     m_Part             = source.Part;
     m_PartSplitter     = source.PartSplitter;
     m_PartToEnd        = source.PartToEnd;
     m_TimePart         = source.TimePart;
     m_TimePartFormat   = source.TimePartFormat;
     m_TimeZonePart     = source.TimeZonePart;
     ValueFormatMutable = new ValueFormatMutable(format);
 }
Exemple #3
0
        public static string GetTypeAndFormatDescription([NotNull] this IValueFormat one)
        {
            var sbText = new StringBuilder(one.DataType.DataTypeDisplay());

            var shortDesc = one.GetFormatDescription();

            if (shortDesc.Length <= 0)
            {
                return(sbText.ToString());
            }
            sbText.Append(" (");
            sbText.Append(shortDesc);
            sbText.Append(")");

            return(sbText.ToString());
        }
Exemple #4
0
        /// <summary>
        ///   Determines whether the specified expected column is matching this column.
        /// </summary>
        /// <param name="one"></param>
        /// <param name="other">The expected column format.</param>
        /// <returns>
        ///   <c>true</c> if the current format would be acceptable for the expected data type.
        /// </returns>
        /// <remarks>
        ///   Is matching only looks at data type and some formats, it is assumed that we do not
        ///   distinguish between numeric formats, it is O.K. to expect a money value but have a integer
        /// </remarks>
        public static bool IsMatching([NotNull] this IValueFormat one, [NotNull] IValueFormat other)
        {
            if (other.DataType == one.DataType)
            {
                return(true);
            }

            // if one is integer but we expect numeric or vice versa, assume its OK, one of the sides does
            // not have a decimal separator
            if ((other.DataType == DataType.Numeric || other.DataType == DataType.Double ||
                 other.DataType == DataType.Integer) &&
                one.DataType == DataType.Integer)
            {
                return(true);
            }

            // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
            switch (other.DataType)
            {
            case DataType.Integer when one.DataType == DataType.Numeric || one.DataType == DataType.Double ||
                one.DataType == DataType.Integer:
                return(true);

            // if we have dates, check the formats
            case DataType.DateTime when one.DataType == DataType.DateTime:
                return(other.DateFormat.Equals(one.DateFormat, StringComparison.Ordinal) &&
                       (one.DateFormat.IndexOf('/') == -1 ||
                        other.DateSeparator.Equals(one.DateSeparator, StringComparison.Ordinal)) &&
                       (one.DateFormat.IndexOf(':') == -1 ||
                        other.TimeSeparator.Equals(one.TimeSeparator, StringComparison.Ordinal)));
            }

            // if we have decimals, check the formats
            if ((other.DataType == DataType.Numeric || other.DataType == DataType.Double) &&
                (one.DataType == DataType.Numeric || one.DataType == DataType.Double))
            {
                return(other.NumberFormat.Equals(one.NumberFormat, StringComparison.Ordinal) &&
                       other.DecimalSeparator.Equals(one.DecimalSeparator) &&
                       other.GroupSeparator.Equals(one.GroupSeparator));
            }
            // For everything else assume its wrong
            return(false);
        }
Exemple #5
0
        public static string GetFormatDescription([NotNull] this IValueFormat one)
        {
            switch (one.DataType)
            {
            case DataType.Integer:
                return(one.NumberFormat.Replace(CultureInfo.InvariantCulture.NumberFormat.NumberGroupSeparator,
                                                one.GroupSeparator.ToString()));

            case DataType.DateTime:
                return(one.DateFormat.ReplaceDefaults(CultureInfo.InvariantCulture.DateTimeFormat.DateSeparator,
                                                      one.DateSeparator,
                                                      CultureInfo.InvariantCulture.DateTimeFormat.TimeSeparator, one.TimeSeparator));

            case DataType.Numeric:
            case DataType.Double:
                return(one.NumberFormat.ReplaceDefaults(CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator,
                                                        one.DecimalSeparator.ToString(),
                                                        CultureInfo.InvariantCulture.NumberFormat.NumberGroupSeparator, one.GroupSeparator.ToString()));

            default:
                return(string.Empty);
            }
        }
Exemple #6
0
        public CsvFileWriter([NotNull] string id, [NotNull] string fullPath, bool hasFieldHeader, [CanBeNull] IValueFormat valueFormat = null, [CanBeNull] IFileFormat fileFormat = null,
                             int codePageId   = 65001, bool byteOrderMark = true, [CanBeNull] string fileSettingDisplay           = null, [CanBeNull] IEnumerable <IColumn> columnDefinition = null, [CanBeNull] string recipient = null,
                             bool unencyrpted = false, [CanBeNull] string identifierInContainer = null, [CanBeNull] string header = null, [CanBeNull] string footer = null,
                             [CanBeNull] IProcessDisplay processDisplay = null)
            : base(id, fullPath, hasFieldHeader, valueFormat, fileFormat, recipient, unencyrpted, identifierInContainer, footer, header, columnDefinition, fileSettingDisplay, processDisplay)
        {
            m_CodePageId     = codePageId;
            m_ByteOrderMark  = byteOrderMark;
            m_FieldQualifier = fileFormat.FieldQualifierChar.ToStringHandle0();
            m_FieldDelimiter = fileFormat.FieldDelimiterChar.ToStringHandle0();

            if (fileFormat.EscapeChar != '\0')
            {
                m_QualifyCharArray      = new[] { (char)0x0a, (char)0x0d };
                m_FieldQualifierEscaped = fileFormat.EscapeChar + m_FieldQualifier;
                m_FieldDelimiterEscaped = fileFormat.EscapeChar + m_FieldDelimiter;
            }
            else
            {
                // Delimiters are not escaped
                m_FieldDelimiterEscaped = m_FieldDelimiter;
                // but require quoting
                m_QualifyCharArray = new[] { (char)0x0a, (char)0x0d, fileFormat.FieldDelimiterChar };

                // the Qualifier is repeated to so it can be recognized as not to be end the quoting
                m_FieldQualifierEscaped = m_FieldQualifier + m_FieldQualifier;
            }
        }
        protected BaseFileWriter([NotNull] string id,
                                 [NotNull] string fullPath, bool hasFieldHeader, [CanBeNull] IValueFormat valueFormatGeneral = null, [CanBeNull] IFileFormat fileFormat = null, [CanBeNull] string recipient = null,
                                 bool unencrypted          = false, [CanBeNull] string identifierInContainer = null,
                                 [CanBeNull] string footer = null, [CanBeNull] string header                 = null,
                                 [CanBeNull] IEnumerable <IColumn> columnDefinition = null, [NotNull] string fileSettingDisplay = null,
                                 [CanBeNull] IProcessDisplay processDisplay         = null)
        {
            m_FullPath = fullPath;
            var fileName = FileSystemUtils.GetFileName(fullPath);

            ColumnHeader = hasFieldHeader;
            if (valueFormatGeneral != null)
            {
                ValueFormatGeneral = new ImmutableValueFormat(valueFormatGeneral.DataType, valueFormatGeneral.DateFormat, valueFormatGeneral.DateSeparator,
                                                              valueFormatGeneral.TimeSeparator, valueFormatGeneral.NumberFormat, valueFormatGeneral.GroupSeparator, valueFormatGeneral.DecimalSeparator, valueFormatGeneral.True,
                                                              valueFormatGeneral.False, valueFormatGeneral.DisplayNullAs);
            }
            else
            {
                ValueFormatGeneral = new ImmutableValueFormat();
            }

            if (fileFormat != null)
            {
                FileFormat = new ImmutableFileFormat(fileFormat.IsFixedLength, fileFormat.QualifyAlways,
                                                     fileFormat.QualifyOnlyIfNeeded, fileFormat.EscapeChar, fileFormat.FieldDelimiterChar, fileFormat.DelimiterPlaceholder,
                                                     fileFormat.FieldQualifierChar, fileFormat.QuotePlaceholder, fileFormat.NewLine, fileFormat.NewLinePlaceholder);
            }
            else
            {
                FileFormat = new ImmutableFileFormat();
            }

            ColumnDefinition = columnDefinition?.Select(col => col is ImmutableColumn immutableColumn ? immutableColumn : new ImmutableColumn(col.Name, col.ValueFormat, col.ColumnOrdinal, col.Convert, col.DestinationName, col.Ignore, col.Part, col.PartSplitter, col.PartToEnd, col.TimePart, col.TimePartFormat, col.TimeZonePart)).ToList() ??
                               new List <ImmutableColumn>();
            NewLine = fileFormat.NewLine.NewLineString();
            if (!string.IsNullOrEmpty(header))
            {
                Header = ReplacePlaceHolder(StringUtils.HandleCRLFCombinations(header, NewLine), fileFormat.FieldDelimiterChar.GetDescription(),
                                            fileName, id);
            }
            else
            {
                Header = string.Empty;
            }
            if (!string.IsNullOrEmpty(footer))
            {
                m_Footer = ReplacePlaceHolder(StringUtils.HandleCRLFCombinations(footer, NewLine), fileFormat.FieldDelimiterChar.GetDescription(),
                                              fileName, id);
            }
            else
            {
                m_Footer = string.Empty;
            }
            m_FileSettingDisplay    = fileSettingDisplay ?? string.Empty;
            m_Recipient             = recipient ?? string.Empty;
            m_KeepUnencrypted       = unencrypted;
            m_IdentifierInContainer = identifierInContainer ?? string.Empty;

            Logger.Debug("Created Writer for {filesetting}", m_FileSettingDisplay);
            if (processDisplay == null)
            {
                return;
            }
            m_ReportProgress = t => processDisplay.SetProcess(t, 0, true);
            if (!(processDisplay is IProcessDisplayTime processDisplayTime))
            {
                return;
            }
            processDisplayTime.Maximum = 0;
            m_SetMaxProcess            = l => processDisplayTime.Maximum = l;
        }
 public WriterColumn(string name, int colNum, IValueFormat valueFormat, int fieldLength = 0, string constantTimeZone = "", int columnOrdinalTimeZone = -1) : base(name, valueFormat, colNum)
 {
     FieldLength           = fieldLength;
     ConstantTimeZone      = constantTimeZone ?? string.Empty;
     ColumnOrdinalTimeZone = columnOrdinalTimeZone;
 }
        /// <summary>
        ///   Gets the column information based on the SQL Source, but overwritten with the definitions
        /// </summary>
        /// <param name="generalFormat">
        ///   general value format for not explicitly specified columns format
        /// </param>
        /// <param name="columnDefinitions"></param>
        /// <param name="sourceSchemaDataReader">The reader for the source.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">reader</exception>
        public static IEnumerable <IColumn> GetColumnInformation(IValueFormat generalFormat,
                                                                 IReadOnlyCollection <IColumn> columnDefinitions, DataTable schemaTable)
        {
            if (schemaTable == null)
            {
                throw new ArgumentNullException(nameof(schemaTable));
            }
            var result = new List <WriterColumn>();

            var colName = new BiDirectionalDictionary <int, string>();

            // Make names unique and fill the dictionary
            foreach (DataRow schemaRow in schemaTable.Rows)
            {
                var colNo   = (int)schemaRow[SchemaTableColumn.ColumnOrdinal];
                var newName =
                    StringUtils.MakeUniqueInCollection(colName.Values, schemaRow[SchemaTableColumn.ColumnName].ToString());

                colName.Add(colNo, newName);
            }

            foreach (DataRow schemaRow in schemaTable.Rows)
            {
                var colNo  = (int)schemaRow[SchemaTableColumn.ColumnOrdinal];
                var column = columnDefinitions.FirstOrDefault(x => x.Name.Equals(colName[colNo], StringComparison.OrdinalIgnoreCase));

                if (column != null && column.Ignore)
                {
                    continue;
                }

                // Based on the data Type in the reader defined and the general format create the value format
                var valueFormat = column?.ValueFormat ?? new ImmutableValueFormat(
                    ((Type)schemaRow[SchemaTableColumn.DataType]).GetDataType(), generalFormat.DateFormat,
                    generalFormat.DateSeparator,
                    generalFormat.TimeSeparator, generalFormat.NumberFormat, generalFormat.GroupSeparator,
                    generalFormat.DecimalSeparator, generalFormat.True,
                    generalFormat.False, generalFormat.DisplayNullAs);

                var fieldLength = Math.Max((int)schemaRow[SchemaTableColumn.ColumnSize], 0);
                switch (valueFormat.DataType)
                {
                case DataType.Integer:
                    fieldLength = 10;
                    break;

                case DataType.Boolean:
                {
                    var lenTrue  = valueFormat.True.Length;
                    var lenFalse = valueFormat.False.Length;
                    fieldLength = lenTrue > lenFalse ? lenTrue : lenFalse;
                    break;
                }

                case DataType.Double:
                case DataType.Numeric:
                    fieldLength = 28;
                    break;

                case DataType.DateTime:
                    fieldLength = valueFormat.DateFormat.Length;
                    break;

                case DataType.Guid:
                    fieldLength = 36;
                    break;

                case DataType.String:
                case DataType.TextToHtml:
                case DataType.TextToHtmlFull:
                case DataType.TextPart:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                var constantTimeZone            = string.Empty;
                var columnOrdinalTimeZoneReader = -1;

                // the timezone information
                if (column != null)
                {
                    var tz = column.TimeZonePart;
                    if (!string.IsNullOrEmpty(tz))
                    {
                        var tzInfo = tz.GetPossiblyConstant();
                        if (tzInfo.Item2)
                        {
                            constantTimeZone = tzInfo.Item1;
                        }
                        else
                        {
                            if (colName.TryGetByValue(tzInfo.Item1, out var ordinal))
                            {
                                columnOrdinalTimeZoneReader = ordinal;
                            }
                        }
                    }
                }
                var ci = new WriterColumn(colName[colNo], colNo, valueFormat, fieldLength, constantTimeZone, columnOrdinalTimeZoneReader);
                result.Add(ci);

                // add an extra column for the time, reading columns they get combined, writing them they
                // get separated again

                if (column == null || string.IsNullOrEmpty(column.TimePart) || colName.ContainsValue(column.TimePart))
                {
                    continue;
                }

                if (ci.ValueFormat.DateFormat.IndexOfAny(new[] { 'h', 'H', 'm', 's' }) != -1)
                {
                    Logger.Warning(
                        $"'{ci.Name}' will create a separate time column '{column.TimePart}' but seems to write time itself '{ci.ValueFormat.DateFormat}'");
                }

                // In case we have a split column, add the second column (unless the column is also present
                result.Add(new WriterColumn(column.TimePart, colNo, new ImmutableValueFormat(DataType.DateTime, column.TimePartFormat, timeSeparator: column.ValueFormat.TimeSeparator), column.TimePartFormat.Length, constantTimeZone, columnOrdinalTimeZoneReader));
            }

            return(result);
        }
Exemple #10
0
 public Column(string name, IValueFormat valueFormat)
 {
     m_Name             = name;
     ValueFormatMutable = new ValueFormatMutable(valueFormat);
 }
 public ValueFormatMutable([NotNull] IValueFormat other) => CopyFrom(other);