<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace TSqlFlex.Core
{
    public class XmlSpreadsheetRenderer
    {
        public static void renderAsXMLSpreadsheet(FlexResultSet resultSet, StreamWriter sw)
        {
            //todo: refactor this and FlexResultSet to to share code and have test coverage.
            sw.Write(Utils.GetResourceByName("TSqlFlex.Core.Resources.XMLSpreadsheetTemplateHeader.txt"));
            for (int i = 0; i < resultSet.results.Count; i++)
            {
                FlexResult result = resultSet.results[i];
                int columnCount = result.visibleColumnCount;

                sw.Write(String.Format("<Worksheet ss:Name=\"Sheet{0}\">", i + 1));
                sw.Write(String.Format("<Table ss:ExpandedColumnCount=\"{0}\" ss:ExpandedRowCount=\"{1}\" x:FullColumns=\"1\" x:FullRows=\"1\" ss:DefaultRowHeight=\"15\">",
                    columnCount,
                    result.data.Count + 1 /* include header row */)
                    );

                //do header
                sw.Write("<Row>");
                for (int colIndex = 0; colIndex < columnCount; colIndex += 1)
                {
                    sw.Write(String.Format("<Cell ss:StyleID=\"s62\"><Data ss:Type=\"String\">{0}</Data></Cell>", escapeForXML((string)result.schema.Rows[colIndex].ItemArray[(int)FieldScripting.FieldInfo.Name])));
                }
                sw.Write("</Row>");

                //do data rows
                for (int rowIndex = 0; rowIndex < result.data.Count; rowIndex += 1)
                {
                    sw.Write("<Row>");
                    for (int colIndex = 0; colIndex < columnCount; colIndex += 1)
                    {
                        object fieldData = result.data[rowIndex][colIndex];
                        string fieldTypeName = result.schema.Rows[colIndex].ItemArray[(int)FieldScripting.FieldInfo.DataType].ToString();
                        if (fieldData == null || fieldData is DBNull)
                        {
                            sw.Write("<Cell/>");
                        }
                        else if (fieldTypeName == "bigint" || fieldTypeName == "numeric" || fieldTypeName == "smallint" || fieldTypeName == "decimal" || fieldTypeName == "smallmoney" ||
                            fieldTypeName == "int" || fieldTypeName == "tinyint" || fieldTypeName == "float" || fieldTypeName == "real" || fieldTypeName == "money")
                        {
                            sw.Write(String.Format("<Cell><Data ss:Type=\"Number\">{0}</Data></Cell>\r\n", escapeForXML(fieldData.ToString())));
                        }
                        else if (fieldTypeName == "date" || fieldTypeName == "datetime2" || fieldTypeName == "time" || fieldTypeName == "datetime" ||
                            fieldTypeName == "smalldatetime")
                        {
                            sw.Write(String.Format("<Cell ss:StyleID=\"s63\"><Data ss:Type=\"DateTime\">{0}</Data></Cell>\r\n", escapeForXML(
                                ((DateTime)fieldData).ToString("yyyy-MM-ddTHH:mm:ss.fff")
                                )));
                        }
                        else
                        {
                            sw.Write(String.Format("<Cell ss:StyleID=\"s64\"><Data ss:Type=\"String\">{0}</Data></Cell>\r\n", escapeForXML(result.data[rowIndex][colIndex].ToString())));
                        }

                    }
                    sw.Write("</Row>");
                }

                sw.Write("</Table></Worksheet>");
            }
            sw.Write("</Workbook>\r\n");
        }

        private static string escapeForXML(string input)
        {
            return input.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
        }
    }
}<commit_msg>will no longer try to script a worksheet if the schema is null<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace TSqlFlex.Core
{
    public class XmlSpreadsheetRenderer
    {
        public static void renderAsXMLSpreadsheet(FlexResultSet resultSet, StreamWriter sw)
        {
            //todo: refactor this and FlexResultSet to to share code and have test coverage.
            sw.Write(Utils.GetResourceByName("TSqlFlex.Core.Resources.XMLSpreadsheetTemplateHeader.txt"));
            for (int i = 0; i < resultSet.results.Count; i++)
            {
                FlexResult result = resultSet.results[i];
                if (result.schema != null)
                {
                    int columnCount = result.visibleColumnCount;

                    sw.Write(String.Format("<Worksheet ss:Name=\"Sheet{0}\">", i + 1));
                    sw.Write(String.Format("<Table ss:ExpandedColumnCount=\"{0}\" ss:ExpandedRowCount=\"{1}\" x:FullColumns=\"1\" x:FullRows=\"1\" ss:DefaultRowHeight=\"15\">",
                        columnCount,
                        result.data.Count + 1 /* include header row */)
                        );

                    //do header
                    sw.Write("<Row>");
                    for (int colIndex = 0; colIndex < columnCount; colIndex += 1)
                    {
                        sw.Write(String.Format("<Cell ss:StyleID=\"s62\"><Data ss:Type=\"String\">{0}</Data></Cell>", escapeForXML((string)result.schema.Rows[colIndex].ItemArray[(int)FieldScripting.FieldInfo.Name])));
                    }
                    sw.Write("</Row>");

                    //do data rows
                    for (int rowIndex = 0; rowIndex < result.data.Count; rowIndex += 1)
                    {
                        sw.Write("<Row>");
                        for (int colIndex = 0; colIndex < columnCount; colIndex += 1)
                        {
                            object fieldData = result.data[rowIndex][colIndex];
                            string fieldTypeName = result.schema.Rows[colIndex].ItemArray[(int)FieldScripting.FieldInfo.DataType].ToString();
                            if (fieldData == null || fieldData is DBNull)
                            {
                                sw.Write("<Cell/>");
                            }
                            else if (fieldTypeName == "bigint" || fieldTypeName == "numeric" || fieldTypeName == "smallint" || fieldTypeName == "decimal" || fieldTypeName == "smallmoney" ||
                                fieldTypeName == "int" || fieldTypeName == "tinyint" || fieldTypeName == "float" || fieldTypeName == "real" || fieldTypeName == "money")
                            {
                                sw.Write(String.Format("<Cell><Data ss:Type=\"Number\">{0}</Data></Cell>\r\n", escapeForXML(fieldData.ToString())));
                            }
                            else if (fieldTypeName == "date" || fieldTypeName == "datetime2" || fieldTypeName == "time" || fieldTypeName == "datetime" ||
                                fieldTypeName == "smalldatetime")
                            {
                                sw.Write(String.Format("<Cell ss:StyleID=\"s63\"><Data ss:Type=\"DateTime\">{0}</Data></Cell>\r\n", escapeForXML(
                                    ((DateTime)fieldData).ToString("yyyy-MM-ddTHH:mm:ss.fff")
                                    )));
                            }
                            else
                            {
                                sw.Write(String.Format("<Cell ss:StyleID=\"s64\"><Data ss:Type=\"String\">{0}</Data></Cell>\r\n", escapeForXML(result.data[rowIndex][colIndex].ToString())));
                            }

                        }
                        sw.Write("</Row>");
                    }

                    sw.Write("</Table></Worksheet>");
                }
            }
            sw.Write("</Workbook>\r\n");
        }

        private static string escapeForXML(string input)
        {
            return input.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
        }
    }
}
{'fraction_non_alphanumeric': 0.14936492427943332, 'fraction_numerical': 0.006595017098192477, 'mean_word_length': 1.505813953488372, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5440826', 'n_tokens_mistral': 2178, 'n_tokens_neox': 2068, 'n_words': 367}