public FolderReader(IConnectionContext input, IRowFactory rowFactory)
        {
            var readers      = new List <IRead>();
            var searchOption = (SearchOption)Enum.Parse(typeof(SearchOption), input.Connection.SearchOption, true);

            input.Info($"Searching folder: {input.Connection.Folder}");
            var files = new DirectoryInfo(input.Connection.Folder).GetFiles(input.Connection.SearchPattern, searchOption).OrderBy(f => f.CreationTime).ToArray();

            input.Info($"Found {files.Length} files.");
            foreach (var file in files)
            {
                input.Info($"Found file: {file.Name}");
                input.Connection.File = file.FullName;
                var context = new PipelineContext(input.Logger, input.Process, input.Entity, input.Field, input.Operation);

                var fileConnection = input.Connection.Clone();
                fileConnection.Provider = "file";
                fileConnection.File     = file.FullName;

                var fileInput = new InputContext(context)
                {
                    Connection = fileConnection
                };

                if (input.Connection.Delimiter == string.Empty && input.Entity.Fields.Count(f => f.Input) == 1)
                {
                    readers.Add(new FileReader(fileInput, rowFactory));
                }
                else
                {
                    readers.Add(new DelimitedFileReader(fileInput, rowFactory));
                }
            }
            _reader = new CompositeReader(readers);
        }
        public IEnumerable <IRow> Read()
        {
            using (var cn = _cf.GetConnection()) {
                cn.Open();
                var cmd = cn.CreateCommand();

                cmd.CommandTimeout = 0;
                cmd.CommandType    = CommandType.Text;
                cmd.CommandText    = $@"
                    SELECT {string.Join(",", _fields.Select(f => _readFrom == ReadFrom.Output ? _cf.Enclose(f.FieldName()) : _cf.Enclose(f.Name)))} 
                    FROM {_schemaPrefix}{_cf.Enclose(_tableOrView)} {(_connection.Provider == "sqlserver" && _context.Entity.NoLock ? "WITH (NOLOCK)" : string.Empty)}
                    {_filter};";
                _context.Debug(() => cmd.CommandText);

                IDataReader reader;
                try {
                    reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
                } catch (System.Data.Common.DbException e) {
                    _context.Error($"Error reading data from {_connection.Name}, {_tableOrView}.");
                    _context.Error(e.Message);
                    yield break;
                }

                if (_context.Connection.Buffer)
                {
                    var buffer = new List <IRow>();
                    while (reader.Read())
                    {
                        _rowCount++;
                        buffer.Add(_rowCreator.Create(reader, _fields));
                    }
                    foreach (var row in buffer)
                    {
                        yield return(row);
                    }
                }
                else
                {
                    while (reader.Read())
                    {
                        _rowCount++;
                        yield return(_rowCreator.Create(reader, _fields));
                    }
                }

                _context.Info("{0} from {1}", _rowCount, _connection.Name);
            }
        }
Exemple #3
0
        /// <summary>
        /// Converts from .doc to .docx and returns output (.docx) file name
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string Convert(IConnectionContext context)
        {
            var outFile  = Path.GetTempPath() + Guid.NewGuid() + ".docx";
            var fileInfo = new FileInfo(context.Connection.File);

            context.Info("Converting doc to docx");

            using (var reader = new StructuredStorageReader(fileInfo.FullName)) {
                var doc               = new WordDocument(reader);
                var outType           = Converter.DetectOutputType(doc);
                var conformOutputFile = Converter.GetConformFilename(outFile, outType);
                var docx              = WordprocessingDocument.Create(conformOutputFile, outType);
                Converter.Convert(doc, docx);
            }

            return(outFile);
        }
        public IEnumerable <IRow> Read()
        {
            if (_parent.Entities.Sum(e => e.Inserts + e.Updates + e.Deletes) == 0)
            {
                yield break;
            }
            ;

            var batches    = _parent.Entities.Select(e => e.BatchId).ToArray();
            var minBatchId = batches.Min();
            var maxBatchId = batches.Max();

            _output.Info("Batch Range: {0} to {1}.", minBatchId, maxBatchId);

            var threshold = minBatchId - 1;

            var sql = string.Empty;

            if (_cf.AdoProvider == AdoProvider.SqlCe)
            {
                // because SqlCe doesn't support views, re-construct the parent view's definition

                var ctx     = new PipelineContext(_output.Logger, _parent);
                var master  = _parent.Entities.First(e => e.IsMaster);
                var builder = new StringBuilder();

                builder.AppendLine($"SELECT {string.Join(",", _output.Entity.Fields.Where(f => f.Output).Select(f => _cf.Enclose(f.Source.Split('.')[0]) + "." + _cf.Enclose(f.Source.Split('.')[1])))}");
                foreach (var from in ctx.SqlStarFroms(_cf))
                {
                    builder.AppendLine(@from);
                }
                builder.AppendLine($"WHERE {_cf.Enclose(Utility.GetExcelName(master.Index))}.{_cf.Enclose(master.TflBatchId().FieldName())} > @Threshold;");

                sql = builder.ToString();
            }
            else
            {
                sql = $@"
                SELECT {string.Join(",", _output.Entity.Fields.Where(f => f.Output).Select(f => _cf.Enclose(f.Alias)))} 
                FROM {_cf.Enclose(_output.Process.Star)} {(_cf.AdoProvider == AdoProvider.SqlServer ? "WITH (NOLOCK)" : string.Empty)} 
                WHERE {_cf.Enclose(Constants.TflBatchId)} > @Threshold;";
            }

            _output.Debug(() => sql);

            using (var cn = _cf.GetConnection()) {
                cn.Open();

                var cmd = cn.CreateCommand();

                cmd.CommandTimeout = 0;
                cmd.CommandType    = CommandType.Text;
                cmd.CommandText    = sql;

                var min = cmd.CreateParameter();
                min.ParameterName = "@Threshold";
                min.Value         = threshold;
                min.Direction     = ParameterDirection.Input;
                min.DbType        = DbType.Int32;

                cmd.Parameters.Add(min);

                var reader     = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
                var rowCount   = 0;
                var fieldArray = _output.Entity.Fields.ToArray();
                while (reader.Read())
                {
                    rowCount++;
                    _output.Increment();
                    yield return(_rowCreator.Create(reader, fieldArray));
                }
                _output.Info("{0} from {1}", rowCount, _output.Connection.Name);
            }
        }