예제 #1
0
        /// <summary>
        /// Write the query response into a file.
        /// </summary>
        public async Task PrintToFileAsync(string path)
        {
            using var output    = File.OpenWrite(path);
            using var tsvWriter = new TsvWriter(output);

            await PrintToTsvWriterAsync(tsvWriter);
        }
예제 #2
0
        /// <summary>
        /// Write the query response into a file.
        /// </summary>
        public void PrintToFile(string path)
        {
            using var output    = File.OpenWrite(path);
            using var tsvWriter = new TsvWriter(output);

            PrintToTsvWriter(tsvWriter);
        }
예제 #3
0
        public void TsvWriter_Basics()
        {
            String8Block block           = new String8Block();
            String8      valueNoEscaping = block.GetCopy("Sample Description");
            String8      valueEscaping   = block.GetCopy("Value\tWith\nIssues");
            String8      t = block.GetCopy("T");

            using (TsvWriter writer = new TsvWriter("TsvWriter.tsv"))
            {
                writer.SetColumns(new string[] { "LineNumber", "IsEven", "Code", "When", "WhenDay", "Count", "Description", "Source" });

                Assert.AreEqual(0, writer.RowCountWritten);

                int sum = 0;
                for (int i = 0; i < 10; ++i)
                {
                    Assert.AreEqual(i, writer.RowCountWritten);

                    sum += i;

                    // Test write for non-String8 types
                    writer.Write(i);
                    writer.Write((i % 2 == 0));
                    writer.Write((byte)'Z');
                    writer.Write(new DateTime(2016, 02, 03, 04, 05, 06, DateTimeKind.Utc));
                    writer.Write(new DateTime(2016, 12, 23, 0, 0, 0, DateTimeKind.Utc));

                    // Test writing partial values
                    writer.WriteValueStart();
                    writer.WriteValuePart((byte)'[');
                    writer.WriteValuePart(sum);
                    writer.WriteValuePart(true);
                    writer.WriteValuePart(t);
                    writer.WriteValuePart((byte)']');
                    writer.WriteValueEnd();

                    // Test String8 writing with and without values to escape
                    writer.Write(valueNoEscaping);
                    writer.Write(valueEscaping);

                    writer.NextRow();
                }
            }

            string tsvContent = File.ReadAllText("TsvWriter.tsv");

            Trace.WriteLine(tsvContent);

            // Verify header is as expected
            Assert.IsTrue(tsvContent.StartsWith("LineNumber\tIsEven\tCode\tWhen\tWhenDay\tCount\tDescription\tSource\r\n"));

            // Verify illegal characters are stripped
            Assert.IsTrue(tsvContent.Contains("ValueWithIssues"));

            // Verify the first row fully
            Assert.IsTrue(tsvContent.Contains("1\tfalse\tZ\t2016-02-03T04:05:06Z\t2016-12-23\t[1trueT]\tSample Description\tValueWithIssues\r\n"));
        }
예제 #4
0
        /// <summary>
        /// Output the query result into a TsvWriter instance
        /// </summary>
        public void PrintToTsvWriter(TsvWriter output)
        {
            output.WriteLine(table.ColumnNames);
            var values = new string[table.ColumnNames.Length];

            foreach (var item in cursor)
            {
                table.GetStringValues(item, values);
                output.WriteLine(values);
            }
        }
예제 #5
0
        /// <summary>
        /// Output the query result into a TsvWriter instance
        /// </summary>
        public async Task PrintToTsvWriterAsync(TsvWriter output)
        {
            await output.WriteLineAsync(table.ColumnNames);

            var values = new string[table.ColumnNames.Length];
            T   item;

            while ((item = await cursor.FetchNextAsync()) != null)
            {
                table.GetStringValues(item, values);
                await output.WriteLineAsync(values);
            }
        }
예제 #6
0
        /// <summary>
        /// Converts the records to TSV representation and
        /// compresses the results.
        /// </summary>
        internal void SerializeCompress()
        {
            using var ms = new MemoryStream();

            using (var gzip = new GZipStream(ms, CompressionMode.Compress))
                using (var tsv = new TsvWriter(gzip)) {
                    tsv.WriteLine(Header);
                    var values = new string[Header.Length];

                    foreach (T record in this.data.Values)
                    {
                        table.GetStringValues(record, values);
                        tsv.WriteLine(values);
                    }
                }
            rawCompressedData = ms.ToArray();
        }
예제 #7
0
        /// <summary>
        /// Output the query result into a Stream instance
        /// </summary>
        public void PrintToStream(Stream stream)
        {
            using var tsvWriter = new TsvWriter(stream);

            PrintToTsvWriter(tsvWriter);
        }
예제 #8
0
        /// <summary>
        /// Print the query result to the standard output
        /// </summary>
        public async Task PrintAsync()
        {
            using var tsvWriter = new TsvWriter(Console.Out);

            await PrintToTsvWriterAsync(tsvWriter);
        }
예제 #9
0
        /// <summary>
        /// Print the query result to the standard output
        /// </summary>
        public void Print()
        {
            using var tsvWriter = new TsvWriter(Console.Out);

            PrintToTsvWriter(tsvWriter);
        }
예제 #10
0
        /// <summary>
        /// Output the query result into a Stream instance
        /// </summary>
        public async Task PrintToStreamAsync(Stream stream)
        {
            using var tsvWriter = new TsvWriter(stream);

            await PrintToTsvWriterAsync(tsvWriter);
        }