예제 #1
0
 /// <summary>
 /// convert to JSON
 /// </summary>
 /// <param name="responce">object ot convert</param>
 /// <returns>json</returns>
 public static string ToJson(UtilityResponce responce)
 {
     return(Newtonsoft.Json.JsonConvert.SerializeObject(
                responce,
                new Newtonsoft.Json.JsonSerializerSettings()
     {
         Formatting = Newtonsoft.Json.Formatting.None
     }));
 }
        /// <summary>
        /// Convert the sql results to UtilityResponce
        /// </summary>
        /// <param name="sqlReadResults">the source</param>
        /// <param name="result">the target.</param>
        public static void ConvertReadResultsToResponce(List <SqlReadResult> sqlReadResults, UtilityResponce result)
        {
            int kind          = 0;
            var stringBuilder = new System.Text.StringBuilder();

            if (sqlReadResults.Count > 0)
            {
                var sqlReadResult = sqlReadResults[0];
                if ((sqlReadResult.FieldCount == 1) &&
                    (sqlReadResult.Rows.Count > 0) &&
                    ((string.Equals(sqlReadResult.FieldNames[0], "kind", StringComparison.OrdinalIgnoreCase)) ||
                     (string.Equals(sqlReadResult.FieldNames[0], "mimetype", StringComparison.OrdinalIgnoreCase))))
                {
                    var cell00 = sqlReadResult.Rows[0][0] as string;
                    if ((string.Equals(cell00, "text", StringComparison.OrdinalIgnoreCase)) ||
                        (string.Equals(cell00, Consts.MimeTypeText, StringComparison.OrdinalIgnoreCase)) ||
                        (string.Equals(cell00, Consts.MimeTypeJavascript, StringComparison.OrdinalIgnoreCase)))
                    {
                        result.Kind = Consts.MimeTypeText;
                        kind        = 1;
                        sqlReadResults.RemoveAt(0);
                    }
                    else if ((string.Equals(cell00, Consts.MimeTypeHTML, StringComparison.OrdinalIgnoreCase)) ||
                             (string.Equals(cell00, "html", StringComparison.OrdinalIgnoreCase)))
                    {
                        result.Kind = Consts.MimeTypeHTML;
                        kind        = 2;
                        sqlReadResults.RemoveAt(0);
                    }
                    else if ((string.Equals(cell00, "json", StringComparison.OrdinalIgnoreCase)) ||
                             (string.Equals(cell00, Consts.MimeTypeJSON, StringComparison.OrdinalIgnoreCase)))
                    {
                        result.Kind = Consts.MimeTypeJSON;
                        kind        = 3;
                        sqlReadResults.RemoveAt(0);
                    }
                    else if ((string.Equals(cell00, Consts.MimeTypeXML, StringComparison.OrdinalIgnoreCase)) ||
                             (string.Equals(cell00, Consts.MimeTypeTextXML, StringComparison.OrdinalIgnoreCase)))
                    {
                        result.Kind = cell00;
                        kind        = 4;
                        sqlReadResults.RemoveAt(0);
                    }
                    else
                    {
                        result.Kind = cell00;
                        kind        = 1;
                    }
                }
                else if (sqlReadResult.FieldCount >= 1)
                {
                    var fieldName0 = sqlReadResult.FieldNames[0] as string;
                    if ((string.Equals(fieldName0, "text", StringComparison.OrdinalIgnoreCase)) ||
                        (string.Equals(fieldName0, Consts.MimeTypeText, StringComparison.OrdinalIgnoreCase)))
                    {
                        result.Kind = Consts.MimeTypeText;
                        kind        = 1;
                    }
                    else if ((string.Equals(fieldName0, Consts.MimeTypeHTML, StringComparison.OrdinalIgnoreCase)) ||
                             (string.Equals(fieldName0, "html", StringComparison.OrdinalIgnoreCase)))
                    {
                        result.Kind = Consts.MimeTypeHTML;
                        kind        = 2;
                    }
                    else if ((string.Equals(fieldName0, "json", StringComparison.OrdinalIgnoreCase)) ||
                             (string.Equals(fieldName0, Consts.MimeTypeJSON, StringComparison.OrdinalIgnoreCase)))
                    {
                        result.Kind = Consts.MimeTypeJSON;
                        kind        = 3;
                    }
                    else if ((string.Equals(fieldName0, Consts.MimeTypeXML, StringComparison.OrdinalIgnoreCase)) ||
                             (string.Equals(fieldName0, Consts.MimeTypeTextXML, StringComparison.OrdinalIgnoreCase)))
                    {
                        result.Kind = fieldName0;
                        kind        = 4;
                    }
                    else
                    {
                        result.Kind = "application/json";
                        kind        = 3;
                    }
                }
            }
            if (kind == 1)
            {
                foreach (var sqlReadResult in sqlReadResults)
                {
                    foreach (var row in sqlReadResult.Rows)
                    {
                        for (int fieldIndex = 0; fieldIndex < sqlReadResult.FieldCount; fieldIndex++)
                        {
                            var value = row[fieldIndex];
                            if (value == null)
                            {
                                continue;
                            }
                            stringBuilder.Append(value);
                        }
                    }
                }
            }
            else if (kind == 2)
            {
                foreach (var sqlReadResult in sqlReadResults)
                {
                    foreach (var row in sqlReadResult.Rows)
                    {
                        for (int fieldIndex = 0; fieldIndex < sqlReadResult.FieldCount; fieldIndex++)
                        {
                            var value = row[fieldIndex];
                            if (value == null)
                            {
                                continue;
                            }
                            stringBuilder.Append(value);
                        }
                    }
                }
            }
            else if (kind == 3)
            {
                using (System.IO.StringWriter stringWriter = new System.IO.StringWriter(stringBuilder)) {
                    using (Newtonsoft.Json.JsonTextWriter jsonTextWriter = new Newtonsoft.Json.JsonTextWriter(stringWriter)) {
                        jsonTextWriter.WriteStartArray();
                        foreach (var sqlReadResult in sqlReadResults)
                        {
                            jsonTextWriter.WriteStartArray();
                            foreach (var row in sqlReadResult.Rows)
                            {
                                jsonTextWriter.WriteStartObject();
                                for (int fieldIndex = 0; fieldIndex < sqlReadResult.FieldCount; fieldIndex++)
                                {
                                    var fieldName = sqlReadResult.FieldNames[fieldIndex];
                                    var value     = row[fieldIndex];
                                    jsonTextWriter.WritePropertyName(fieldName);
                                    jsonTextWriter.WriteValue(value);
                                }
                                jsonTextWriter.WriteEndObject();
                            }
                            jsonTextWriter.WriteEndArray();
                            if ((sqlReadResult.MeassureMessage.Count > 0) && (sqlReadResult.MeassureError.Count > 0))
                            {
                                if (sqlReadResult.MeassureMessage.Count > 0)
                                {
                                    jsonTextWriter.WriteStartArray();
                                    foreach (var meassureMessage in sqlReadResult.MeassureMessage)
                                    {
                                        jsonTextWriter.WriteStartObject();
                                        jsonTextWriter.WritePropertyName("MeassureMessage");
                                        jsonTextWriter.WriteValue(meassureMessage);
                                        jsonTextWriter.WriteEndObject();
                                    }
                                    jsonTextWriter.WriteEndArray();
                                }
                                if (sqlReadResult.MeassureError.Count > 0)
                                {
                                    jsonTextWriter.WriteStartArray();
                                    foreach (var meassureError in sqlReadResult.MeassureError)
                                    {
                                        jsonTextWriter.WriteStartObject();
                                        jsonTextWriter.WritePropertyName("MeassureError");
                                        jsonTextWriter.WriteValue(meassureError);
                                        jsonTextWriter.WriteEndObject();
                                    }
                                    jsonTextWriter.WriteEndArray();
                                }
                            }
                        }
                        jsonTextWriter.WriteEndArray();
                    }
                }
            }
            else if (kind == 4)
            {
                using (System.IO.StringWriter stringWriter = new System.IO.StringWriter(stringBuilder)) {
                    using (var xmlWriter = new System.Xml.XmlTextWriter(stringWriter)) {
                        xmlWriter.Formatting = System.Xml.Formatting.None;
                        xmlWriter.WriteStartDocument();
                        xmlWriter.WriteStartElement("data");
                        foreach (var sqlReadResult in sqlReadResults)
                        {
                            xmlWriter.WriteStartElement("result");
                            foreach (var row in sqlReadResult.Rows)
                            {
                                xmlWriter.WriteStartElement("row");
                                for (int fieldIndex = 0; fieldIndex < sqlReadResult.FieldCount; fieldIndex++)
                                {
                                    var fieldName = sqlReadResult.FieldNames[fieldIndex];
                                    var value     = row[fieldIndex];
                                    xmlWriter.WriteStartElement(fieldName);
                                    if (value == null)
                                    {
                                        // ignore
                                    }
                                    else if (value == DBNull.Value)
                                    {
                                        // ignore
                                    }
                                    else if (value is string)
                                    {
                                        xmlWriter.WriteValue((string)value);
                                    }
                                    else if (value is bool)
                                    {
                                        xmlWriter.WriteValue((bool)value);
                                    }
                                    else if (value is DateTime)
                                    {
                                        xmlWriter.WriteValue((DateTime)value);
                                    }
                                    else if (value is decimal)
                                    {
                                        xmlWriter.WriteValue((decimal)value);
                                    }
                                    else if (value is float)
                                    {
                                        xmlWriter.WriteValue((float)value);
                                    }
                                    else if (value is double)
                                    {
                                        xmlWriter.WriteValue((double)value);
                                    }
                                    else if (value is int)
                                    {
                                        xmlWriter.WriteValue((int)value);
                                    }
                                    else if (value is long)
                                    {
                                        xmlWriter.WriteValue((long)value);
                                    }
                                    else
                                    {
                                        xmlWriter.WriteElementString(fieldName, value.ToString());
                                    }
                                    xmlWriter.WriteEndElement(/*fieldName*/);
                                }
                                xmlWriter.WriteEndElement(/*row*/);
                            }
                            xmlWriter.WriteEndElement(/*result*/);
                        }
                        xmlWriter.WriteEndElement(/*data*/);
                        xmlWriter.WriteEndDocument();
                    }
                    using (Newtonsoft.Json.JsonTextWriter jsonTextWriter = new Newtonsoft.Json.JsonTextWriter(stringWriter)) {
                        jsonTextWriter.WriteStartArray();
                        foreach (var sqlReadResult in sqlReadResults)
                        {
                            jsonTextWriter.WriteStartArray();
                            foreach (var row in sqlReadResult.Rows)
                            {
                                jsonTextWriter.WriteStartObject();
                                for (int fieldIndex = 0; fieldIndex < sqlReadResult.FieldCount; fieldIndex++)
                                {
                                    var fieldName = sqlReadResult.FieldNames[fieldIndex];
                                    var value     = row[fieldIndex];
                                    jsonTextWriter.WritePropertyName(fieldName);
                                    jsonTextWriter.WriteValue(value);
                                }
                                jsonTextWriter.WriteEndObject();
                            }
                            jsonTextWriter.WriteEndArray();
                        }
                        jsonTextWriter.WriteEndArray();
                    }
                }
            }
            result.Value = stringBuilder.ToString();
        }