/// <summary> /// Fills the Column Format for writer fileSettings /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <param name="fileSettings">The file settings.</param> /// <param name="all">if set to <c>true</c> event string columns are added.</param> public static void FillGuessColumnFormatWriter(this IFileSetting fileSettings, bool all, CancellationToken cancellationToken) { var fileWriter = fileSettings.GetFileWriter(cancellationToken); if (string.IsNullOrEmpty(fileSettings.SqlStatement)) { throw new ApplicationException("No SQL Statement given"); } using (var dataReader = ApplicationSetting.SQLDataReader(fileSettings.SqlStatement, cancellationToken)) { // Put the information into the list foreach (DataRow schemaRow in dataReader.GetSchemaTable().Rows) { var header = schemaRow[SchemaTableColumn.ColumnName].ToString(); var colType = ((Type)schemaRow[SchemaTableColumn.DataType]).GetDataType(); if (!all && (colType == DataType.String)) { continue; } var fsColumn = new Column { Name = header, DataType = colType }; fileSettings.ColumnAdd(fsColumn); } } }
/// <summary> /// Gets the writer source columns. /// </summary> /// <param name="fileSettings">The file settings.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static IEnumerable <ColumnInfo> GetWriterSourceColumns(IFileSetting fileSettings, CancellationToken cancellationToken) { Contract.Requires(fileSettings != null); var writer = fileSettings.GetFileWriter(cancellationToken); return(writer.GetColumnInformation(writer.GetSchemaReader())); }
/// <summary> /// Writes the file ans displays performance information /// </summary> /// <param name="fileSetting">The file setting.</param> /// <param name="showSummary"> /// Flag indicating that a message Box should be displayed. If <c>true</c> a message box will be /// shown /// </param> /// <param name="cancellationToken">A CancellationToken</param> /// <param name="fileSettingSourcesCurrent">The file setting sources current.</param> /// <param name="settingLaterThanSources">The setting later than sources.</param> /// <param name="ask">if set to <c>true</c> [ask].</param> /// <returns> /// Number of record written to file /// </returns> public static long WriteFileWithInfo(this IFileSetting fileSetting, bool showSummary, FileSettingChecker fileSettingSourcesCurrent, bool ask, bool onlyOlder, CancellationToken cancellationToken) { if (fileSetting == null) { return(0); } long written = 0; var fi = FileSystemUtils.FileInfo(fileSetting.FullPath); var dir = FileSystemUtils.GetDirectoryName(fi.FullName); if (!FileSystemUtils.DirectoryExists(dir)) { if (_MessageBox.Show(null, $"The directory {dir.RemovePrefix()} does not exist, should it be created?", "Directory", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { FileSystemUtils.CreateDirectory(dir); } else { return(0); } } var fileInfo = new FileInfo(fileSetting.FullPath); if (fileInfo.Exists) { fileSetting.FileLastWriteTimeUtc = fi.LastWriteTimeUtc; } var stringBuilder = new System.Text.StringBuilder(); using (var processDisplay = fileSetting.GetProcessDisplay(null, true, cancellationToken)) { fileSettingSourcesCurrent?.Invoke(fileSetting, processDisplay); if (onlyOlder && fileSetting.SettingLaterThanSources(processDisplay.CancellationToken)) { return(0); } fileSetting.FullPath.DeleteFileQuestion(ask); var errors = new RowErrorCollection(50); var writer = fileSetting.GetFileWriter(processDisplay.CancellationToken); writer.ProcessDisplay = processDisplay; writer.Warning += errors.Add; written = writer.Write(); var hasIssues = !string.IsNullOrEmpty(writer.ErrorMessage) || (errors.CountRows > 0); if (showSummary || hasIssues) { fi = FileSystemUtils.FileInfo(fileSetting.FullPath); // if all source settings are file settings, get the latest file time and set this fileTime var latest = DateTime.MinValue; fileSetting.GetSourceFileSettings(delegate(IFileSetting setting) { if (!(setting is IFileSettingRemoteDownload)) { if (latest < setting.FileLastWriteTimeUtc) { latest = setting.FileLastWriteTimeUtc; } } else { var fiSrc = FileSystemUtils.FileInfo(setting.FullPath); if (fiSrc.Exists && latest < fiSrc.LastWriteTimeUtc) { latest = fiSrc.LastWriteTimeUtc; } } return(false); }, cancellationToken); stringBuilder.Append($"Finished writing file\r\rRecords: {written:N0}\rFile size: {fi.Length / 1048576.0:N} MB"); if (latest < DateTime.MaxValue && latest > DateTime.MinValue) { stringBuilder.Append($"\rTime adjusted to latest source file: {latest.ToLocalTime():D}"); fi.LastWriteTimeUtc = latest; fileSetting.FileLastWriteTimeUtc = latest; } if (hasIssues) { stringBuilder.Append("\rIssues:\r"); } if (!string.IsNullOrEmpty(writer.ErrorMessage)) { stringBuilder.Append(writer.ErrorMessage); } if (errors.CountRows > 0) { stringBuilder.Append(errors.DisplayByRecordNumber); } } } if (stringBuilder.Length > 0) { _MessageBox.Show(null, stringBuilder.ToString(), FileSystemUtils.GetShortDisplayFileName(fileSetting.FileName, 80), MessageBoxButtons.OK); } return(written); }