Beispiel #1
0
        private Csv(Stream stream, CsvOptions options, bool canDisposeStream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (!stream.CanRead)
            {
                throw new ArgumentException($"Cannot read from a stream of type: {stream.GetType().FullName}.", nameof(stream));
            }

            if (!stream.CanSeek)
            {
                throw new ArgumentException($"Cannot seek in a stream of type: {stream.GetType().FullName}.", nameof(stream));
            }

            this.stream           = stream;
            this.options          = options ?? throw new ArgumentNullException(nameof(options));
            this.canDisposeStream = canDisposeStream;

            reader   = new CsvReader(this.stream, options);
            accessor = new RowAccessor(this);

            if (options.HasHeaderRow)
            {
                HeaderRow = reader.GetHeaderRow();
            }
            else
            {
                HeaderRow = new string[0];
            }
        }
Beispiel #2
0
        public void Write(RowAccessor row)
        {
            var currentRowIndex = filledRowsCount;

            object[] rowData;
            if (currentRowIndex < rows.Count)
            {
                rowData = rows[currentRowIndex];
            }
            else
            {
                rows.Add(rowData = new object[columns.Length]);
            }
            row.GetValues(rowData);
            filledRowsCount++;
            if (filledRowsCount == batchSize)
            {
                Flush();
            }
        }
Beispiel #3
0
        internal static void Execute(QuerySource[] sources, string queryText, IWriter writer,
                                     ParallelOptions options, bool dumpSql, Dictionary <string, object> parameters)
        {
            RowAccessor rowAccessor  = null;
            var         locker       = new object();
            var         runTimestamp = DateTime.Now;
            var         writeStarted = false;

            try
            {
                Parallel.ForEach(sources, options, (source, state) =>
                {
                    try
                    {
                        if (state.ShouldExitCurrentIteration)
                        {
                            return;
                        }
                        var sql = Translate(source, queryText, runTimestamp);
                        if (dumpSql)
                        {
                            Console.Out.WriteLine("\r\n[{0}]\r\n{1}\r\n====>\r\n{2}",
                                                  source.ConnectionString, queryText, sql);
                        }
                        if (state.ShouldExitCurrentIteration)
                        {
                            return;
                        }
                        var db = new PostgreeSqlDatabase(source.ConnectionString);
                        db.Execute(sql, parameters, c =>
                        {
                            if (state.ShouldExitCurrentIteration)
                            {
                                return;
                            }
                            using (var reader = (NpgsqlDataReader)c.ExecuteReader())
                            {
                                if (state.ShouldExitCurrentIteration)
                                {
                                    return;
                                }
                                var columns = DatabaseHelpers.GetColumns(reader);
                                lock (locker)
                                    if (rowAccessor == null)
                                    {
                                        rowAccessor = new RowAccessor(columns);
                                        writer.BeginWrite(columns);
                                        writeStarted = true;
                                    }
                                    else
                                    {
                                        DatabaseHelpers.CheckColumnsAreEqual(rowAccessor.Columns, "original", columns, "current");
                                    }
                                if (state.ShouldExitCurrentIteration)
                                {
                                    return;
                                }
                                while (reader.Read())
                                {
                                    if (state.ShouldExitCurrentIteration)
                                    {
                                        return;
                                    }
                                    lock (locker)
                                    {
                                        rowAccessor.Reader = reader;
                                        writer.Write(rowAccessor);
                                    }
                                }
                            }
                        });
                    }
                    catch (Exception e)
                    {
                        const string messageFormat = "error has occurred for database [{0}]";
                        throw new InvalidOperationException(
                            string.Format(messageFormat, source.ConnectionString), e);
                    }
                });
            }
            finally
            {
                if (writeStarted)
                {
                    writer.EndWrite();
                }
            }
        }