예제 #1
0
        private IEnumerable <IRow> PreRead()
        {
            _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(_streamReader)) {
                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++)
                        {
                            row[_context.InputFields[i]] = values[i];
                        }
                        yield return(row);
                    }
                    ++current;
                    if (current == end)
                    {
                        break;
                    }
                }
            }

            _streamReader.Close();

            if (engine.ErrorManager.HasErrors)
            {
                foreach (var error in engine.ErrorManager.Errors)
                {
                    _context.Error(error.ExceptionInfo.Message);
                }
            }
        }
예제 #2
0
        public void Write(IEnumerable <IRow> rows)
        {
            var engine   = FileHelpersEngineFactory.Create(_context);
            var fileInfo = new FileInfo(Path.Combine(_context.Connection.Folder, _context.Connection.File ?? _context.Entity.OutputTableName(_context.Process.Name)));
            var fields   = _context.Entity.GetAllOutputFields().Where(f => !f.System).ToArray();

            _context.Info($"Writing {fileInfo.FullName}.");

            using (engine.BeginWriteFile(fileInfo.FullName)) {
                foreach (var row in rows)
                {
                    var i = 0;
                    foreach (var field in fields)
                    {
                        switch (field.Type)
                        {
                        case "byte[]":
                            engine[i] = Convert.ToBase64String((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();
                    _context.Entity.Inserts++;
                }
                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);
                }
            }
        }
예제 #3
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] is string?row[field] : row[field].ToString();
                            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();
                }
                engine.Flush();
            }
        }