| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Text; |
| | 7 | | using System.Xml; |
| | 8 | |
|
| | 9 | | namespace Azure.Data.Tables.Queryable |
| | 10 | | { |
| | 11 | | internal static class WebConvert |
| | 12 | | { |
| | 13 | | private const string HexValues = "0123456789ABCDEF"; |
| | 14 | |
|
| | 15 | | internal static string ConvertByteArrayToKeyString(byte[] byteArray) |
| | 16 | | { |
| 36 | 17 | | StringBuilder hexBuilder = new StringBuilder(3 + (byteArray.Length * 2)); |
| 36 | 18 | | hexBuilder.Append(XmlConstants.XmlBinaryPrefix); |
| 36 | 19 | | hexBuilder.Append("'"); |
| 304 | 20 | | for (int i = 0; i < byteArray.Length; i++) |
| | 21 | | { |
| 116 | 22 | | hexBuilder.Append(HexValues[byteArray[i] >> 4]); |
| 116 | 23 | | hexBuilder.Append(HexValues[byteArray[i] & 0x0F]); |
| | 24 | | } |
| | 25 | |
|
| 36 | 26 | | hexBuilder.Append("'"); |
| 36 | 27 | | return hexBuilder.ToString(); |
| | 28 | | } |
| | 29 | |
|
| | 30 | | internal static bool IsKeyTypeQuoted(Type type) |
| | 31 | | { |
| | 32 | | Debug.Assert(type != null, "type != null"); |
| 520 | 33 | | return type == typeof(System.Xml.Linq.XElement) || type == typeof(string); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | internal static bool TryKeyPrimitiveToString(object value, out string result) |
| | 37 | | { |
| | 38 | | Debug.Assert(value != null, "value != null"); |
| 808 | 39 | | Type t = value.GetType(); |
| 808 | 40 | | if (t == typeof(byte[])) |
| | 41 | | { |
| 36 | 42 | | result = ConvertByteArrayToKeyString((byte[])value); |
| | 43 | | } |
| | 44 | | else |
| | 45 | | { |
| 772 | 46 | | if (!TryXmlPrimitiveToString(value, out result)) |
| | 47 | | { |
| 0 | 48 | | return false; |
| | 49 | | } |
| | 50 | |
|
| | 51 | | Debug.Assert(result != null, "result != null"); |
| 772 | 52 | | if (t == typeof(DateTime)) |
| | 53 | | { |
| 60 | 54 | | result = XmlConstants.LiteralPrefixDateTime + "'" + result + "'"; |
| | 55 | | } |
| 712 | 56 | | else if (t == typeof(decimal)) |
| | 57 | | { |
| 0 | 58 | | result += XmlConstants.XmlDecimalLiteralSuffix; |
| | 59 | | } |
| 712 | 60 | | else if (t == typeof(Guid)) |
| | 61 | | { |
| 20 | 62 | | result = XmlConstants.LiteralPrefixGuid + "'" + result + "'"; |
| | 63 | | } |
| 692 | 64 | | else if (t == typeof(long)) |
| | 65 | | { |
| 136 | 66 | | result += XmlConstants.XmlInt64LiteralSuffix; |
| | 67 | | } |
| 556 | 68 | | else if (t == typeof(float)) |
| | 69 | | { |
| 0 | 70 | | result += XmlConstants.XmlSingleLiteralSuffix; |
| | 71 | | } |
| 556 | 72 | | else if (t == typeof(double)) |
| | 73 | | { |
| 36 | 74 | | result = AppendDecimalMarkerToDouble(result); |
| | 75 | | } |
| 520 | 76 | | else if (IsKeyTypeQuoted(t)) |
| | 77 | | { |
| 304 | 78 | | result = "'" + result.Replace("'", "''") + "'"; |
| | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| 808 | 82 | | return true; |
| | 83 | | } |
| | 84 | |
|
| | 85 | | internal static bool TryXmlPrimitiveToString(object value, out string result) |
| | 86 | | { |
| | 87 | | Debug.Assert(value != null, "value != null"); |
| | 88 | |
|
| 772 | 89 | | Type valueType = value.GetType(); |
| 772 | 90 | | valueType = Nullable.GetUnderlyingType(valueType) ?? valueType; |
| | 91 | |
|
| 772 | 92 | | if (typeof(string) == valueType) |
| | 93 | | { |
| 304 | 94 | | result = (string)value; |
| | 95 | | } |
| 468 | 96 | | else if (typeof(bool) == valueType) |
| | 97 | | { |
| 72 | 98 | | result = XmlConvert.ToString((bool)value); |
| | 99 | | } |
| 396 | 100 | | else if (typeof(byte) == valueType) |
| | 101 | | { |
| 0 | 102 | | result = XmlConvert.ToString((byte)value); |
| | 103 | | } |
| 396 | 104 | | else if (typeof(DateTime) == valueType) |
| | 105 | | { |
| 60 | 106 | | result = XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.RoundtripKind); |
| | 107 | | } |
| 336 | 108 | | else if (typeof(decimal) == valueType) |
| | 109 | | { |
| 0 | 110 | | result = XmlConvert.ToString((decimal)value); |
| | 111 | | } |
| 336 | 112 | | else if (typeof(double) == valueType) |
| | 113 | | { |
| 36 | 114 | | result = XmlConvert.ToString((double)value); |
| | 115 | | } |
| 300 | 116 | | else if (typeof(Guid) == valueType) |
| | 117 | | { |
| 20 | 118 | | result = value.ToString(); |
| | 119 | | } |
| 280 | 120 | | else if (typeof(short) == valueType) |
| | 121 | | { |
| 0 | 122 | | result = XmlConvert.ToString((short)value); |
| | 123 | | } |
| 280 | 124 | | else if (typeof(int) == valueType) |
| | 125 | | { |
| 144 | 126 | | result = XmlConvert.ToString((int)value); |
| | 127 | | } |
| 136 | 128 | | else if (typeof(long) == valueType) |
| | 129 | | { |
| 136 | 130 | | result = XmlConvert.ToString((long)value); |
| | 131 | | } |
| 0 | 132 | | else if (typeof(sbyte) == valueType) |
| | 133 | | { |
| 0 | 134 | | result = XmlConvert.ToString((sbyte)value); |
| | 135 | | } |
| 0 | 136 | | else if (typeof(float) == valueType) |
| | 137 | | { |
| 0 | 138 | | result = XmlConvert.ToString((float)value); |
| | 139 | | } |
| 0 | 140 | | else if (typeof(byte[]) == valueType) |
| | 141 | | { |
| 0 | 142 | | byte[] byteArray = (byte[])value; |
| 0 | 143 | | result = Convert.ToBase64String(byteArray); |
| | 144 | | } |
| 0 | 145 | | else if (ClientConvert.IsBinaryValue(value)) |
| | 146 | | { |
| 0 | 147 | | return ClientConvert.TryKeyBinaryToString(value, out result); |
| | 148 | | } |
| 0 | 149 | | else if (typeof(System.Xml.Linq.XElement) == valueType) |
| | 150 | | { |
| 0 | 151 | | result = ((System.Xml.Linq.XElement)value).ToString(System.Xml.Linq.SaveOptions.None); |
| | 152 | | } |
| | 153 | | else |
| | 154 | | { |
| 0 | 155 | | result = null; |
| 0 | 156 | | return false; |
| | 157 | | } |
| | 158 | |
|
| | 159 | | Debug.Assert(result != null, "result != null"); |
| 772 | 160 | | return true; |
| | 161 | | } |
| | 162 | |
|
| | 163 | | private static string AppendDecimalMarkerToDouble(string input) |
| | 164 | | { |
| 188 | 165 | | foreach (char c in input) |
| | 166 | | { |
| 76 | 167 | | if (!char.IsDigit(c)) |
| | 168 | | { |
| 36 | 169 | | return input; |
| | 170 | | } |
| | 171 | | } |
| | 172 | |
|
| 0 | 173 | | return input + ".0"; |
| | 174 | | } |
| | 175 | | } |
| | 176 | | } |