|
1 | 1 | using Newtonsoft.Json; |
2 | 2 | using System; |
| 3 | +using System.Collections; |
3 | 4 | using System.Collections.Generic; |
4 | 5 | using System.Diagnostics; |
5 | 6 | using System.Linq; |
6 | 7 | using System.Linq.Expressions; |
7 | 8 |
|
8 | 9 | namespace CouchDB.Driver |
9 | 10 | { |
10 | | -#pragma warning disable IDE0058 // Expression value is never used |
11 | 11 | internal partial class QueryTranslator |
12 | 12 | { |
13 | 13 | protected override Expression VisitConstant(ConstantExpression c) |
14 | 14 | { |
15 | | - if (c.Value is IQueryable) |
| 15 | + HandleConstant(c.Value); |
| 16 | + |
| 17 | + return c; |
| 18 | + } |
| 19 | + |
| 20 | + private void HandleConstant(object constant) |
| 21 | + { |
| 22 | + if (constant is IQueryable) |
16 | 23 | { |
17 | 24 | // assume constant nodes w/ IQueryables are table references |
18 | 25 | // q.ElementType.Name |
19 | 26 | } |
20 | | - else if (c.Value == null) |
| 27 | + else if (constant == null) |
21 | 28 | { |
22 | 29 | _sb.Append("null"); |
23 | 30 | } |
24 | 31 | else |
25 | 32 | { |
26 | | - switch (Type.GetTypeCode(c.Value.GetType())) |
| 33 | + switch (Type.GetTypeCode(constant.GetType())) |
27 | 34 | { |
28 | 35 | case TypeCode.Boolean: |
29 | | - _sb.Append(((bool)c.Value) ? "true" : "false"); |
| 36 | + _sb.Append(((bool)constant) ? "true" : "false"); |
30 | 37 | break; |
31 | 38 | case TypeCode.String: |
32 | | - _sb.Append($"\"{c.Value}\""); |
| 39 | + _sb.Append($"\"{constant}\""); |
33 | 40 | break; |
34 | 41 | case TypeCode.DateTime: |
35 | | - _sb.Append(JsonConvert.SerializeObject(c.Value)); |
| 42 | + _sb.Append(JsonConvert.SerializeObject(constant)); |
36 | 43 | break; |
37 | 44 | case TypeCode.Object: |
38 | | - if (c.Value is IList<bool>) |
39 | | - { |
40 | | - VisitIEnumerable(c.Value as IList<bool>); |
41 | | - } |
42 | | - else if (c.Value is IList<int>) |
43 | | - { |
44 | | - VisitIEnumerable(c.Value as IList<int>); |
45 | | - } |
46 | | - else if (c.Value is IList<long>) |
47 | | - { |
48 | | - VisitIEnumerable(c.Value as IList<long>); |
49 | | - } |
50 | | - else if (c.Value is IList<decimal>) |
51 | | - { |
52 | | - VisitIEnumerable(c.Value as IList<decimal>); |
53 | | - } |
54 | | - else if (c.Value is IList<float>) |
55 | | - { |
56 | | - VisitIEnumerable(c.Value as IList<float>); |
57 | | - } |
58 | | - else if (c.Value is IList<double>) |
| 45 | + if (constant is IEnumerable enumerable) |
59 | 46 | { |
60 | | - VisitIEnumerable(c.Value as IList<double>); |
| 47 | + VisitIEnumerable(enumerable); |
61 | 48 | } |
62 | | - else if (c.Value is IList<string>) |
| 49 | + else if (constant is Guid) |
63 | 50 | { |
64 | | - VisitIEnumerable(c.Value as IList<string>); |
65 | | - } |
66 | | - else if (c.Value is Guid) |
67 | | - { |
68 | | - _sb.Append(JsonConvert.SerializeObject(c.Value)); |
| 51 | + _sb.Append(JsonConvert.SerializeObject(constant)); |
69 | 52 | } |
70 | 53 | else |
71 | 54 | { |
72 | | - Debug.WriteLine($"The constant for '{c.Value}' not ufficially supported."); |
73 | | - _sb.Append(JsonConvert.SerializeObject(c.Value)); |
| 55 | + Debug.WriteLine($"The constant for '{constant}' not ufficially supported."); |
| 56 | + _sb.Append(JsonConvert.SerializeObject(constant)); |
74 | 57 | } |
75 | 58 | break; |
76 | 59 | default: |
77 | | - _sb.Append(c.Value); |
| 60 | + _sb.Append(constant); |
78 | 61 | break; |
79 | 62 | } |
80 | 63 | } |
81 | 64 |
|
82 | | - return c; |
83 | 65 | } |
84 | 66 |
|
85 | | - private void VisitIEnumerable<T>(IList<T> list) |
| 67 | + private void VisitIEnumerable(IEnumerable list) |
86 | 68 | { |
87 | | - if (list.Count < 1) |
88 | | - { |
89 | | - return; |
90 | | - } |
91 | | - if (list.Count == 1) |
92 | | - { |
93 | | - _sb.Append(VisitConst(list[0])); |
94 | | - } |
95 | | - else |
| 69 | + _sb.Append("["); |
| 70 | + bool needsComma = false; |
| 71 | + foreach (var item in list) |
96 | 72 | { |
97 | | - _sb.Append("["); |
98 | | - _sb.Append(string.Join(",", list.Select(e => VisitConst(e)))); |
99 | | - _sb.Append("]"); |
100 | | - } |
101 | | - |
102 | | - string VisitConst(object o) |
103 | | - { |
104 | | - switch (Type.GetTypeCode(o.GetType())) |
| 73 | + if (needsComma) |
105 | 74 | { |
106 | | - case TypeCode.Boolean: |
107 | | - return (bool)o ? "true" : "false"; |
108 | | - case TypeCode.String: |
109 | | - return $"\"{o}\""; |
110 | | - case TypeCode.Object: |
111 | | - throw new NotSupportedException($"The constant for '{o}' is not supported"); |
112 | | - default: |
113 | | - return o.ToString(); |
| 75 | + _sb.Append(","); |
114 | 76 | } |
| 77 | + HandleConstant(item); |
| 78 | + needsComma = true; |
115 | 79 | } |
| 80 | + _sb.Append("]"); |
116 | 81 | } |
117 | 82 | } |
118 | | -#pragma warning restore IDE0058 // Expression value is never used |
119 | 83 | } |
0 commit comments