public IEnumerable <IRow> Read()
        {
            _context.Debug(() => "Reading file stream.");

            var start = _context.Connection.Start;
            var end   = 0;

            if (_context.Entity.IsPageRequest())
            {
                start += (_context.Entity.Page * _context.Entity.Size) - _context.Entity.Size;
                end    = start + _context.Entity.Size;
            }

            var current = _context.Connection.Start;

            var engine = FileHelpersEngineFactory.Create(_context);

            using (engine.BeginReadStream(new StreamReader(_stream))) {
                foreach (var record in engine)
                {
                    if (end == 0 || current.Between(start, end))
                    {
                        var values = engine.LastRecordValues;
                        var row    = _rowFactory.Create();
                        for (var i = 0; i < _context.InputFields.Length; i++)
                        {
                            var field = _context.InputFields[i];
                            if (field.Type == "string")
                            {
                                row[field] = values[i] as string;
                            }
                            else
                            {
                                row[field] = field.Convert(values[i]);
                            }
                        }
                        yield return(row);
                    }
                    ++current;
                    if (current == end)
                    {
                        break;
                    }
                }
            }

            if (engine.ErrorManager.HasErrors)
            {
                foreach (var error in engine.ErrorManager.Errors)
                {
                    _context.Error(error.ExceptionInfo.Message);
                }
            }
        }
Beispiel #2
0
        public void Write(IEnumerable <IRow> rows)
        {
            var engine = FileHelpersEngineFactory.Create(_context);
            var file   = Path.Combine(_context.Connection.Folder, _context.Connection.File ?? _context.Entity.OutputTableName(_context.Process.Name));

            _context.Debug(() => $"Writing {file}.");

            using (engine.BeginWriteFile(file)) {
                foreach (var row in rows)
                {
                    var i = 0;
                    foreach (var field in _context.OutputFields)
                    {
                        switch (field.Type)
                        {
                        case "byte[]":
                            engine[i] = Convert.ToBase64String((byte[])row[field]);
                            //engine[i] = Utility.BytesToHexString((byte[]) row[field]);
                            //engine[i] = Encoding.UTF8.GetString((byte[])row[field]);
                            break;

                        case "string":
                            engine[i] = row[field];
                            break;

                        case "datetime":
                            engine[i] = field.Format == string.Empty ? ((DateTime)row[field]).ToString("o") : ((DateTime)row[field]).ToString(field.Format);
                            break;

                        default:
                            engine[i] = row[field].ToString();
                            break;
                        }
                        ++i;
                    }

                    engine.WriteNextValues();
                }
                if (engine.ErrorManager.HasErrors)
                {
                    var errorFile = Path.Combine(Path.GetDirectoryName(_context.Connection.File) ?? string.Empty, Path.GetFileNameWithoutExtension(_context.Connection.File) + ".errors.txt");
                    _context.Error($"File writer had {engine.ErrorManager.ErrorCount} error{engine.ErrorManager.ErrorCount.Plural()}. See {errorFile}.");
                    engine.ErrorManager.SaveErrors(errorFile);
                }
            }
        }
Beispiel #3
0
        public IEnumerable <IRow> Read()
        {
            _context.Debug(() => $"Reading {_fileInfo.Name}.");

            var start = _context.Connection.Start;
            var end   = 0;

            if (_context.Entity.IsPageRequest())
            {
                start += (_context.Entity.Page * _context.Entity.Size) - _context.Entity.Size;
                end    = start + _context.Entity.Size;
            }

            var current = _context.Connection.Start;
            var engine  = FileHelpersEngineFactory.Create(_context);

            IDisposable reader;

            try {
                reader = engine.BeginReadFile(_fileInfo.FullName);
            } catch (Exception ex) {
                _context.Error(ex.Message);
                yield break;
            }

            using (reader) {
                foreach (var record in engine)
                {
                    if (end == 0 || current.Between(start, end))
                    {
                        var values = engine.LastRecordValues;
                        var row    = _rowFactory.Create();
                        for (var i = 0; i < _context.InputFields.Length; i++)
                        {
                            var field = _context.InputFields[i];
                            row[field] = values[i];
                        }

                        if (_fileField != null)
                        {
                            row[_fileField] = _context.Connection.File;
                        }
                        yield return(row);
                    }
                    ++current;
                    if (current == end)
                    {
                        break;
                    }
                }
            }

            if (engine.ErrorManager.HasErrors)
            {
                foreach (var error in engine.ErrorManager.Errors)
                {
                    _context.Error(error.ExceptionInfo.Message);
                    _context.Error($"Error processing line {error.LineNumber} in {_context.Connection.File}.");
                    _context.Warn(error.RecordString.Replace("{", "{{").Replace("}", "}}"));
                }
            }
        }
Beispiel #4
0
        public void Write(IEnumerable <IRow> rows)
        {
            _context.Debug(() => "Writing to stream.");

            var engine = FileHelpersEngineFactory.Create(_context);
            var writer = new StreamWriter(_stream);

            using (engine.BeginWriteStream(writer)) {
                foreach (var row in rows)
                {
                    for (var i = 0; i < _context.OutputFields.Length; i++)
                    {
                        var field = _context.OutputFields[i];
                        switch (field.Type)
                        {
                        case "byte[]":
                            engine[i] = Convert.ToBase64String((byte[])row[field]);
                            break;

                        case "string":
                            engine[i] = row[field];
                            break;

                        case "datetime":
                            var format = field.Format == string.Empty ? "o" : field.Format.Replace("AM/PM", "tt");
                            engine[i] = row[field] is DateTime ? ((DateTime)row[field]).ToString(format) : Convert.ToDateTime(row[field]).ToString(format);
                            break;

                        case "float":
                        case "decimal":
                        case "single":
                        case "double":
                            if (field.Format == string.Empty)
                            {
                                engine[i] = row[field].ToString();
                            }
                            else
                            {
                                switch (field.Type)
                                {
                                case "single":
                                case "float":
                                    engine[i] = row[field] is float?((float)row[field]).ToString(field.Format) : Convert.ToSingle(row[field]).ToString(field.Format);
                                    break;

                                case "decimal":
                                    engine[i] = row[field] is decimal ? ((decimal)row[field]).ToString(field.Format) : Convert.ToDecimal(row[field]).ToString(field.Format);
                                    break;

                                case "double":
                                    engine[i] = row[field] is double?((double)row[field]).ToString(field.Format) : Convert.ToDouble(row[field]).ToString(field.Format);
                                    break;

                                default:
                                    engine[i] = row[field].ToString();
                                    break;
                                }
                            }
                            break;

                        default:
                            engine[i] = row[field].ToString();
                            break;
                        }
                    }
                    engine.WriteNextValues();
                }
            }
        }