/// <summary>
        ///   Handles the time zone for a date time column
        /// </summary>
        /// <param name="dataObject">The data object.</param>
        /// <param name="columnInfo">The column information.</param>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        protected DateTime HandleTimeZone(DateTime dataObject, [NotNull] WriterColumn columnInfo,
                                          [NotNull] IDataRecord reader)
        {
            if (columnInfo is null)
            {
                throw new ArgumentNullException(nameof(columnInfo));
            }
            if (reader is null)
            {
                throw new ArgumentNullException(nameof(reader));
            }
            if (columnInfo.ColumnOrdinalTimeZone > -1)
            {
                var destinationTimeZoneId = reader.GetString(columnInfo.ColumnOrdinalTimeZone);
                if (string.IsNullOrEmpty(destinationTimeZoneId))
                {
                    HandleWarning(columnInfo.Name, "Time zone is empty, value not converted");
                }
                else
                {
                    // ReSharper disable once PossibleInvalidOperationException
                    return(FunctionalDI.AdjustTZExport(dataObject, destinationTimeZoneId, Columns.IndexOf(columnInfo),
                                                       (columnNo, msg) => HandleWarning(Columns[columnNo].Name, msg)).Value);
                }
            }
            else if (!string.IsNullOrEmpty(columnInfo.ConstantTimeZone))
            {
                // ReSharper disable once PossibleInvalidOperationException
                return(FunctionalDI.AdjustTZExport(dataObject, columnInfo.ConstantTimeZone,
                                                   Columns.IndexOf(columnInfo), (columnNo, msg) => HandleWarning(Columns[columnNo].Name, msg)).Value);
            }

            return(dataObject);
        }
Beispiel #2
0
        public SourceAccess([NotNull] string fileName, bool isReading = true, [CanBeNull] string id = null,
                            [CanBeNull] string recipient = null, bool keepEnencyrpted = false)
        {
            FullPath        = fileName;
            Reading         = isReading;
            Identifier      = id ?? FileSystemUtils.GetShortDisplayFileName(fileName, 40);
            Recipient       = recipient ?? string.Empty;
            KeepEnencyrpted = keepEnencyrpted;
            LeaveOpen       = false;
            FileType        = FromExtension(fileName);
            switch (FileType)
            {
            case FileTypeEnum.Zip when !isReading:
                IdentifierInContainer = FileSystemUtils.GetFileName(fileName).ReplaceCaseInsensitive(".zip", "");
                break;

            // for PGP we need a password/ passphrase for Zip we might need one later
            case FileTypeEnum.Pgp when isReading:
                EncryptedPassphrase = FunctionalDI.GetEncryptedPassphraseForFile(fileName);
                break;
            }
            if (!isReading && KeepEnencyrpted)
            {
                // remove entension
                var split = FileSystemUtils.SplitPath(fileName);
                var fn    = Path.Combine(split.DirectoryName, split.FileNameWithoutExtension);
                m_OpenStream = GetOpenStreamFunc(fn, false);
            }
            else
            {
                m_OpenStream = GetOpenStreamFunc(fileName, isReading);
            }
        }
        public async Task <long> WriteAsync([CanBeNull] IFileReader reader, CancellationToken token)
        {
            if (reader == null)
            {
                return(-1);
            }
            HandleWriteStart();
            m_SetMaxProcess?.Invoke(-1);

            try
            {
                var sourceAccess = new SourceAccess(m_FullPath, false, recipient: m_Recipient, keepEnencyrpted: m_KeepUnencrypted);
                if (!string.IsNullOrEmpty(m_IdentifierInContainer))
                {
                    sourceAccess.IdentifierInContainer = m_IdentifierInContainer;
                }

                using (var improvedStream = FunctionalDI.OpenStream(sourceAccess))
                    await WriteReaderAsync(reader, improvedStream as Stream, token).ConfigureAwait(false);
            }
            catch (Exception exc)
            {
                Logger.Error(exc, "Could not write file {filename}", FileSystemUtils.GetShortDisplayFileName(m_FullPath));
                throw new FileWriterException($"Could not write file '{FileSystemUtils.GetShortDisplayFileName(m_FullPath)}'\n{exc.SourceExceptionMessage()}",
                                              exc);
            }
            finally
            {
                Logger.Debug("Finished writing {filesetting} Records: {records}", m_FileSettingDisplay, Records);
                WriteFinished?.Invoke(this, null);
            }

            return(Records);
        }
        /// <summary>
        ///   Resets the position and buffer to the first line, excluding headers, use
        ///   ResetPositionToStart if you want to go to first data line
        /// </summary>
        private void ResetPositionToStartOrOpen()
        {
            if (SelfOpenedStream)
            {
                m_ImprovedStream?.Dispose();
                m_ImprovedStream = FunctionalDI.OpenStream(new SourceAccess(FullPath));
            }
            else
            {
                m_ImprovedStream.Seek(0, SeekOrigin.Begin);
            }

            // in case we can not seek need to reopen the stream reader
            m_StreamReader?.Close();
            m_StreamReader = new StreamReader(m_ImprovedStream as Stream ?? throw new InvalidOperationException(), Encoding.UTF8, true, 4096, true);

            // End Line should be at 1, later on as the line is read the start line s set to this value
            StartLineNumber = 1;
            EndLineNumber   = 1;
            RecordNumber    = 0;

            EndOfFile = m_StreamReader.EndOfStream;

            m_JsonTextReader?.Close();
            m_JsonTextReader = new JsonTextReader(m_StreamReader)
            {
                SupportMultipleContent = true
            };
        }
 private async void GuessNewline_Click(object sender, EventArgs e)
 {
     await buttonNewLine.RunWithHourglassAsync(async() =>
     {
         using (var improvedStream = FunctionalDI.OpenStream(new SourceAccess(m_ViewSettings)))
             cboRecordDelimiter.SelectedValue = (int)await improvedStream.GuessNewline(m_ViewSettings.CodePageId, m_ViewSettings.SkipRows, m_ViewSettings.FileFormat.FieldQualifier, m_CancellationTokenSource.Token);
     });
 }
 private async void ButtonSkipLine_ClickAsync(object sender, EventArgs e)
 {
     await buttonSkipLine.RunWithHourglassAsync(async() =>
     {
         using (var improvedStream = FunctionalDI.OpenStream(new SourceAccess(m_ViewSettings)))
             m_ViewSettings.SkipRows = await improvedStream.GuessStartRow(m_ViewSettings.CodePageId, m_ViewSettings.FileFormat.FieldDelimiter, m_ViewSettings.FileFormat.FieldQualifier, m_ViewSettings.FileFormat.CommentLine, m_CancellationTokenSource.Token);
     });
 }
 private async void ButtonGuessHeader_Click(object sender, EventArgs e)
 {
     await buttonGuessHeader.RunWithHourglassAsync(async() =>
     {
         using (var improvedStream = FunctionalDI.OpenStream(new SourceAccess(m_ViewSettings)))
         {
             var res = await improvedStream.GuessHasHeader(m_ViewSettings.CodePageId, m_ViewSettings.SkipRows, m_ViewSettings.FileFormat.CommentLine, m_ViewSettings.FileFormat.FieldDelimiter, m_CancellationTokenSource.Token);
             m_ViewSettings.HasFieldHeader = res.Item1;
             fileSettingBindingSource.ResetBindings(false);
             _MessageBox.Show(res.Item2, "Checking headers");
         }
     });
 }
        private async void ButtonGuessDelimiter_ClickAsync(object sender, EventArgs e)
        {
            await buttonGuessDelimiter.RunWithHourglassAsync(async() =>
            {
                using (var improvedStream = FunctionalDI.OpenStream(new SourceAccess(m_ViewSettings)))
                {
                    await improvedStream.GuessDelimiter(m_ViewSettings.CodePageId, m_ViewSettings.SkipRows, m_ViewSettings.FileFormat.EscapeCharacter, m_CancellationTokenSource.Token);
                }
            });

            // GuessDelimiterAsync does set the values, refresh them
            fileFormatBindingSource.ResetBindings(false);
        }
        private async void ButtonGuessCP_ClickAsync(object sender, EventArgs e)
        {
            await buttonGuessCP.RunWithHourglassAsync(async() =>
            {
                using (var improvedStream = FunctionalDI.OpenStream(new SourceAccess(m_ViewSettings)))
                {
                    var(codepage, bom)           = await improvedStream.GuessCodePage(m_CancellationTokenSource.Token);
                    m_ViewSettings.CodePageId    = codepage;
                    m_ViewSettings.ByteOrderMark = bom;
                }
            });

            fileSettingBindingSource.ResetBindings(false);
        }
        private async void ButtonGuessTextQualifier_Click(object sender, EventArgs e)
        {
            var qualifier = string.Empty;
            await buttonGuessTextQualifier.RunWithHourglassAsync(async() =>
            {
                using (var improvedStream = FunctionalDI.OpenStream(new SourceAccess(m_ViewSettings)))
                    qualifier = await improvedStream.GuessQualifier(m_ViewSettings.CodePageId, m_ViewSettings.SkipRows, m_ViewSettings.FileFormat.FieldDelimiter, m_CancellationTokenSource.Token);
            });

            if (qualifier != null)
            {
                m_ViewSettings.FileFormat.FieldQualifier = qualifier;
            }
            else
            {
                _MessageBox.Show("No Column Qualifier found", "Qualifier", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Beispiel #11
0
        public static async Task <long> WriteAsync([NotNull] this IFileWriter writer, [NotNull] string sqlStatement,
                                                   int timeout, Action <string> reportProgress, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(sqlStatement))
            {
                return(0);
            }

            if (FunctionalDI.SQLDataReader == null)
            {
                throw new ArgumentException("No SQL Reader set");
            }
            using (var sqlReader = await FunctionalDI
                                   .SQLDataReader(sqlStatement, (sender, s) => reportProgress?.Invoke(s.Text), timeout, cancellationToken)
                                   .ConfigureAwait(false))
            {
                await sqlReader.OpenAsync(cancellationToken).ConfigureAwait(false);

                return(await writer.WriteAsync(sqlReader, cancellationToken).ConfigureAwait(false));
            }
        }
        public async Task StartAsync([NotNull] IFileSetting fileSetting, bool includeError, TimeSpan durationInitial,
                                     [NotNull] IProcessDisplay processDisplay, [CanBeNull] EventHandler <WarningEventArgs> addWarning)
        {
            m_FileReader = FunctionalDI.GetFileReader(fileSetting, TimeZoneInfo.Local.Id, processDisplay);
            if (m_FileReader == null)
            {
                throw new FileReaderException($"Could not get reader for {fileSetting}");
            }

            RowErrorCollection warningList = null;

            if (addWarning != null)
            {
                warningList           = new RowErrorCollection(m_FileReader);
                m_FileReader.Warning += addWarning;
                m_FileReader.Warning -= warningList.Add;
            }

            Logger.Information("Reading data for display");
            await m_FileReader.OpenAsync(processDisplay.CancellationToken).ConfigureAwait(false);

            if (addWarning != null)
            {
                warningList.HandleIgnoredColumns(m_FileReader);
                warningList.PassWarning += addWarning;
            }

            m_DataReaderWrapper = new DataReaderWrapper(m_FileReader, fileSetting.RecordLimit, includeError,
                                                        fileSetting.DisplayStartLineNo, fileSetting.DisplayRecordNo, fileSetting.DisplayEndLineNo);

            m_ActionBegin?.Invoke();
            await GetBatchByTimeSpan(durationInitial, includeError, processDisplay, m_SetDataTable).ConfigureAwait(false);

            m_SetLoadNextBatchAsync?.Invoke(process => GetBatchByTimeSpan(TimeSpan.MaxValue, includeError, process, (dt) => m_GetDataTable().Merge(dt)));

            m_ActionFinished?.Invoke(m_DataReaderWrapper);
        }