public static string ToTextAll <TRec>(IEnumerable <TRec> records, ChoFixedLengthRecordConfiguration configuration = null, TraceSwitch traceSwitch = null) where TRec : class { if (records == null) { return(null); } if (typeof(DataTable).IsAssignableFrom(typeof(TRec))) { StringBuilder text = new StringBuilder(); foreach (var dt in records.Take(1)) { configuration = configuration == null ? new ChoFixedLengthRecordConfiguration().Configure(c => c.WithFirstLineHeader()) : configuration; using (var w = new ChoFixedLengthWriter(text, configuration)) w.Write(dt); } return(text.ToString()); } else if (typeof(IDataReader).IsAssignableFrom(typeof(TRec))) { StringBuilder text = new StringBuilder(); foreach (var dt in records.Take(1)) { configuration = configuration == null ? new ChoFixedLengthRecordConfiguration().Configure(c => c.WithFirstLineHeader()) : configuration; using (var w = new ChoFixedLengthWriter(text, configuration)) w.Write(dt); } return(text.ToString()); } using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoFixedLengthWriter <TRec>(writer, configuration) { TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitch : traceSwitch }) { parser.Write(records); writer.Flush(); stream.Position = 0; return(reader.ReadToEnd()); } }
public static string ToText <TRec>(IEnumerable <TRec> records, ChoFixedLengthRecordConfiguration configuration = null) where TRec : class { using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoFixedLengthWriter <TRec>(writer)) { parser.Write(records); writer.Flush(); stream.Position = 0; return(reader.ReadToEnd()); } }
public static void Write(ChoFixedLengthWriter <dynamic> w, IDataReader dr) { ChoGuard.ArgumentNotNull(w, "Writer"); ChoGuard.ArgumentNotNull(dr, "DataReader"); DataTable schemaTable = dr.GetSchemaTable(); w.Configuration.UseNestedKeyFormat = false; w.Configuration.FixedLengthRecordFieldConfigurations.Clear(); //int ordinal = 0; if (w.Configuration.FixedLengthRecordFieldConfigurations.IsNullOrEmpty()) { string colName = null; Type colType = null; int startIndex = 0; int fieldLength = 0; foreach (DataRow row in schemaTable.Rows) { colName = row["ColumnName"].CastTo <string>(); colType = row["DataType"] as Type; //if (!colType.IsSimple()) continue; fieldLength = ChoFixedLengthFieldDefaultSizeConfiguation.Instance.GetSize(colType); var obj = new ChoFixedLengthRecordFieldConfiguration(colName, startIndex, fieldLength); w.Configuration.FixedLengthRecordFieldConfigurations.Add(obj); startIndex += fieldLength; } } var ordinals = w.Configuration.FixedLengthRecordFieldConfigurations.ToDictionary(c => c.Name, c => dr.HasColumn(c.Name) ? dr.GetOrdinal(c.Name) : -1); while (dr.Read()) { dynamic expando = new ExpandoObject(); var expandoDic = (IDictionary <string, object>)expando; foreach (var fc in ordinals) { expandoDic.Add(fc.Key, fc.Value == -1 ? null : dr[fc.Value]); } w.Write(expando); } }
public static string ToTextAll <TRec>(IEnumerable <TRec> records, ChoFixedLengthRecordConfiguration configuration = null, TraceSwitch traceSwitch = null) where TRec : class { using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoFixedLengthWriter <TRec>(writer, configuration) { TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitch : traceSwitch }) { parser.Write(records); writer.Flush(); stream.Position = 0; return(reader.ReadToEnd()); } }
public static void Write(ChoFixedLengthWriter <dynamic> w, DataTable dt) { ChoGuard.ArgumentNotNull(w, "Writer"); ChoGuard.ArgumentNotNull(dt, "DataTable"); DataTable schemaTable = dt; w.Configuration.UseNestedKeyFormat = false; if (w.Configuration.FixedLengthRecordFieldConfigurations.IsNullOrEmpty()) { string colName = null; Type colType = null; int startIndex = 0; int fieldLength = 0; foreach (DataColumn col in schemaTable.Columns) { colName = col.ColumnName; colType = col.DataType; //if (!colType.IsSimple()) continue; fieldLength = ChoFixedLengthFieldDefaultSizeConfiguation.Instance.GetSize(colType); var obj = new ChoFixedLengthRecordFieldConfiguration(colName, startIndex, fieldLength); w.Configuration.FixedLengthRecordFieldConfigurations.Add(obj); startIndex += fieldLength; } } foreach (DataRow row in dt.Rows) { dynamic expando = new ExpandoObject(); var expandoDic = (IDictionary <string, object>)expando; foreach (var fc in w.Configuration.FixedLengthRecordFieldConfigurations) { expandoDic.Add(fc.Name, row[fc.Name]); } w.Write(expando); } }
public override IEnumerable <object> WriteTo(object writer, IEnumerable <object> records, Func <object, bool> predicate = null) { TextWriter sw = writer as TextWriter; ChoGuard.ArgumentNotNull(sw, "TextWriter"); if (records == null) { yield break; } if (!RaiseBeginWrite(sw)) { yield break; } CultureInfo prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture; string recText = String.Empty; Type recType; try { foreach (object record in records) { _index++; recType = record.GetType(); if (record is IChoETLNameableObject) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(((IChoETLNameableObject)record).Name)); } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(_index)); } recText = String.Empty; if (record != null) { if (predicate == null || predicate(record)) { //Discover and load Manifold columns from first record if (!_configCheckDone) { //string[] fieldNames = null; //if (record is ExpandoObject) //{ // var dict = record as IDictionary<string, Object>; // fieldNames = dict.Keys.ToArray(); //} //else //{ // fieldNames = ChoTypeDescriptor.GetProperties<ChoManifoldRecordFieldAttribute>(record.GetType()).Select(pd => pd.Name).ToArray(); // if (fieldNames.Length == 0) // { // fieldNames = ChoType.GetProperties(record.GetType()).Select(p => p.Name).ToArray(); // } //} //Configuration.Validate(fieldNames); //WriteHeaderLine(sw); _configCheckDone = true; } if (!RaiseBeforeRecordWrite(record, _index, ref recText)) { yield break; } if (recText == null) { continue; } else if (recText.Length > 0) { sw.Write("{1}{0}", recText, Configuration.FileHeaderConfiguration.HasHeaderRecord ? Configuration.EOLDelimiter : ""); continue; } try { var config = GetConfiguration(record.GetType()); if (config == null) { throw new ChoParserException("No writer found to write record."); } if (config.GetType() == typeof(ChoCSVRecordConfiguration)) { recText = ChoCSVWriter.ToText(record, config as ChoCSVRecordConfiguration, Configuration.Encoding, Configuration.BufferSize, TraceSwitch); } else if (config.GetType() == typeof(ChoFixedLengthRecordConfiguration)) { recText = ChoFixedLengthWriter.ToText(record, config as ChoFixedLengthRecordConfiguration, Configuration.Encoding, Configuration.BufferSize, TraceSwitch); } else { throw new ChoParserException("Unsupported record found to write."); } if (recText != null) { if (_index == 1) { sw.Write("{1}{0}", recText, Configuration.FileHeaderConfiguration.HasHeaderRecord ? Configuration.EOLDelimiter : ""); } else { sw.Write("{1}{0}", recText, Configuration.EOLDelimiter); } if (!RaiseAfterRecordWrite(record, _index, recText)) { yield break; } } } catch (ChoParserException pEx) { throw new ChoParserException($"Failed to write line for '{recType}' object.", pEx); } catch (Exception ex) { ChoETLFramework.HandleException(ex); if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue) { } else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordWriteError(record, _index, recText, ex)) { throw new ChoWriterException($"Failed to write line for '{recType}' object.", ex); } } else { throw new ChoWriterException($"Failed to write line for '{recType}' object.", ex); } } } } yield return(record); if (Configuration.NotifyAfter > 0 && _index % Configuration.NotifyAfter == 0) { if (RaisedRowsWritten(_index)) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Abort requested."); yield break; } } } } finally { System.Threading.Thread.CurrentThread.CurrentCulture = prevCultureInfo; } RaiseEndWrite(sw); }