Ejemplo n.º 1
0
        /// <summary>
        ///     Reads a reader as comma separated values.
        /// </summary>
        /// <param name="context">
        ///     The context.
        /// </param>
        /// <param name="rowBuilder">
        ///     The data Set.
        /// </param>
        /// <param name="reader">
        ///     The stream.
        /// </param>
        /// <param name="fields">
        ///     The fields, or <c>null</c> to retrieve all fields.
        /// </param>
        /// <returns>
        ///     The rows.
        /// </returns>
        public IEnumerable <Row> Read([NotNull] IFileFormatExecutionContext context, IRowBuilder rowBuilder, StreamReader reader, HashSet <string> fields)
        {
            var separator = context.GetDefault("SEPARATOR", false) as string ?? ",";
            var splitter  = CsvFileFormat.GetSplitter(separator);
            var headers   = CsvFileFormat.GetHeaders(splitter, reader, separator);

            if (headers.Length == 1 && string.IsNullOrEmpty(headers[0]))
            {
                yield break;
            }

            var idx = 0L;

            do
            {
                var line = splitter.Matches($"{reader.ReadLine()}{separator}")
                           .Cast <Match>()
                           .Select(match => match.Groups[1].Value)
                           .Select(value => value.Trim())
                           .Select(value => CsvFileFormat.EscapedString.IsMatch(value) ? value.Substring(1, value.Length - 2).Replace("\"\"", "\"") : value)
                           .ToArray();

                if (line.Length == headers.Length)
                {
                    // ReSharper disable once AccessToDisposedClosure
                    yield return(rowBuilder.CreateRow(idx++, headers.Select((header, i) => new KeyValuePair <string, object>(header, line[i]))));
                }
            }while (!reader.EndOfStream);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Writes rows to the file.
        /// </summary>
        /// <param name="context">
        ///     The context.
        /// </param>
        /// <param name="writer">
        ///     The stream.
        /// </param>
        /// <param name="rows">
        ///     The rows to write.
        /// </param>
        /// <param name="upsert">
        ///     True to upsert, false to insert.
        /// </param>
        /// <returns>
        ///     The number of rows that were written.
        /// </returns>
        public long WriteRows(IFileFormatExecutionContext context, StreamWriter writer, [NotNull] IEnumerable <Row> rows, bool upsert)
        {
            var count = 0L;

            foreach (var row in rows)
            {
                writer.WriteLine(string.Join(",", row.ColumnNames.Select(c => CsvFileFormat.Escape(row[c]))));
                count++;
            }

            return(count);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Gets the descriptor for this data source.
        /// </summary>
        /// <param name="alias">
        ///     The alias.
        /// </param>
        /// <param name="context">
        ///     The execution context.
        /// </param>
        /// <param name="reader">
        ///     The reader.
        /// </param>
        /// <returns>
        ///     The <see cref="System.Threading.Tasks.Task" />.
        /// </returns>
        public Task <IDataSourceDescriptor> GetDataSourceDescriptorAsync(string alias, [NotNull] IFileFormatExecutionContext context, StreamReader reader)
        {
            var separator = context.GetDefault("SEPARATOR", false) as string ?? ",";

            return(Task.FromResult(Descriptor.ForDataSource(alias, CsvFileFormat.GetHeaders(CsvFileFormat.GetSplitter(separator), reader, separator).Where(header => header.Length > 0).Select(column => Descriptor.ForColumn(column, typeof(string))))));
        }