Example #1
0
        public static void DumpProviderToStream(IProvider provider, TextWriter outputWriter, GqlQueryState gqlQueryState, 
            string columnDelimiter, GqlEngineState.HeadingEnum heading, int autoSize,
            FileOptionsIntoClause.FormatEnum format,
            CultureInfo cultureInfo)
        {
            try {
                provider.Initialize (gqlQueryState);

                List<string[]> list = new List<string[]> ();
                if (heading != GqlEngineState.HeadingEnum.Off) {
                    ColumnName[] columnTitles = provider.GetColumnNames ();
                    if (columnTitles.Length > 0) {
                        string[] columnTitleStrings = columnTitles.Select (p => p.ToStringWithoutBrackets ()).ToArray ();
                        list.Add (columnTitleStrings);
                        if (heading == GqlEngineState.HeadingEnum.OnWithRule) {
                            string[] columnTitlesRule = new string[columnTitles.Length];
                            for (int i = 0; i < columnTitles.Length; i++)
                                columnTitlesRule [i] = new string ('=', columnTitleStrings [i].ToString ().Length);
                            list.Add (columnTitlesRule);
                        }
                    }
                }

                for (int record = 0; (autoSize == -1 || record < autoSize); record++) {
                    try {
                        if (!provider.GetNextRecord ())
                            break;
                    } catch (WarningException x) {
                        gqlQueryState.Warnings.Add (new LineIgnoredException (x));
                        record--;
                        continue;
                    }
                    list.Add (provider.Record.Columns.Select (p => p.ToString ()).ToArray ());
                }

                FormatColumnsFunction formatColumnListFunction;
                if (autoSize == 0) {
                    if (format == FileOptionsIntoClause.FormatEnum.Csv)
                        formatColumnListFunction = new FormatCsvFunction (columnDelimiter);
                    else
                        formatColumnListFunction = new FormatColumnListFunction (columnDelimiter);
                } else {
                    int[] max = new int[list [0].Length];
                    foreach (string[] item in list) {
                        for (int col = 0; col < max.Length; col++)
                            max [col] = Math.Max (item [col].Length, max [col]);
                    }
                    Type[] types = provider.GetColumnTypes ();
                    for (int col = 0; col < max.Length; col++)
                        if (types [col] != typeof(DataString))
                            max [col] = -max [col];
                    formatColumnListFunction = new FormatColumnListFunction (columnDelimiter, max);
                }

                foreach (var item in list) {
                    outputWriter.WriteLine (formatColumnListFunction.Evaluate (item));
                }

                do {
                    try {
                        if (!provider.GetNextRecord ())
                            break;
                        outputWriter.WriteLine (formatColumnListFunction.Evaluate (provider.Record.Columns.Select (p => p.ToDataString (cultureInfo).Value)));
                    } catch (WarningException x) {
                        gqlQueryState.Warnings.Add (new LineIgnoredException (x));
                    }
                } while (true);
            } finally {
                provider.Uninitialize ();
            }
        }