Example #1
0
 public IntoProvider(IProvider provider, FileOptionsIntoClause fileOptions, CultureInfo cultureInfo)
 {
     this.provider = provider;
     this.fileOptions = fileOptions;
     this.cultureInfo = cultureInfo;
     columnDelimiter = fileOptions.ColumnDelimiter;
     if (columnDelimiter == null) {
         if (fileOptions.Format == FileOptionsIntoClause.FormatEnum.Csv)
             columnDelimiter = ",";
         else
             columnDelimiter = "\t";
     }
 }
Example #2
0
        public static void DumpProviderToStream(IProvider provider, Stream outputStream, GqlQueryState gqlQueryState, 
            string columnDelimiter, string recordDelimiter, GqlEngineState.HeadingEnum heading,
            FileOptionsIntoClause.FormatEnum format,
            CultureInfo cultureInfo)
        {
            using (TextWriter writer = new StreamWriter (outputStream, System.Text.Encoding.GetEncoding (0))) {
                writer.NewLine = recordDelimiter;

                DumpProviderToStream (
                    provider,
                    writer,
                    gqlQueryState,
                    columnDelimiter,
                    heading,
                    0,
                    format,
                    cultureInfo
                );
            }
        }
Example #3
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 ();
            }
        }
Example #4
0
 private string GetNewLine(FileOptionsIntoClause.NewLineEnum lineEnd)
 {
     if (fileOptions.NewLine == FileOptionsIntoClause.NewLineEnum.Dos)
         return "\r\n";
     else if (fileOptions.NewLine == FileOptionsIntoClause.NewLineEnum.Unix)
         return "\n";
     else if (fileOptions.NewLine == FileOptionsIntoClause.NewLineEnum.Mac)
         return "\r";
     else
         return Environment.NewLine;
 }