Skip to content

Commit e6603d4

Browse files
Merge pull request #26 from matteobortolazzo/GuidQuerySupport
Support for querying by guid
2 parents e032153 + 65b0d84 commit e6603d4

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/CouchDB.Driver/Translators/ConstantExpressionTranslator.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Newtonsoft.Json;
22
using System;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.Linq;
56
using System.Linq.Expressions;
67

@@ -41,16 +42,21 @@ protected override Expression VisitConstant(ConstantExpression c)
4142
VisitIEnumerable(c.Value as IList<int>);
4243
else if (c.Value is IList<long>)
4344
VisitIEnumerable(c.Value as IList<long>);
44-
else if(c.Value is IList<decimal>)
45+
else if (c.Value is IList<decimal>)
4546
VisitIEnumerable(c.Value as IList<decimal>);
4647
else if (c.Value is IList<float>)
4748
VisitIEnumerable(c.Value as IList<float>);
4849
else if (c.Value is IList<double>)
4950
VisitIEnumerable(c.Value as IList<double>);
50-
else if(c.Value is IList<string>)
51+
else if (c.Value is IList<string>)
5152
VisitIEnumerable(c.Value as IList<string>);
53+
else if (c.Value is Guid)
54+
_sb.Append(JsonConvert.SerializeObject(c.Value));
5255
else
53-
throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", c.Value));
56+
{
57+
Debug.WriteLine($"The constant for '{c.Value}' not ufficially supported.");
58+
_sb.Append(JsonConvert.SerializeObject(c.Value));
59+
}
5460
break;
5561
default:
5662
_sb.Append(c.Value);
@@ -85,7 +91,7 @@ string VisitConst(object o)
8591
case TypeCode.String:
8692
return $"\"{o}\"";
8793
case TypeCode.Object:
88-
throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", o));
94+
throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", o));
8995
default:
9096
return o.ToString();
9197
}

tests/CouchDB.Driver.UnitTests/Find/Find_Selector.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public void ToList_EmptySelector()
2424
[Fact]
2525
public void ComplexQuery()
2626
{
27-
var json = rebels.Where(r =>
28-
r.Age == 19 &&
29-
(r.Name == "Luke" || r.Name == "Leia") &&
27+
var json = rebels.Where(r =>
28+
r.Age == 19 &&
29+
(r.Name == "Luke" || r.Name == "Leia") &&
3030
r.Skills.Contains("force")).ToString();
3131
Assert.Equal(@"{""selector"":{""$and"":[{""age"":19},{""$or"":[{""name"":""Luke""},{""name"":""Leia""}]},{""skills"":{""$all"":[""force""]}}]}}", json);
3232
}
@@ -65,5 +65,13 @@ public void Enum()
6565
var json = rebels.Where(r => r.Species == Species.Human).ToString();
6666
Assert.Equal(@"{""selector"":{""species"":0}}", json);
6767
}
68+
[Fact]
69+
public void GuidQuery()
70+
{
71+
var guidString = "83c79283-f634-41e3-8aab-674bdbae3413";
72+
var guid = Guid.Parse(guidString);
73+
var json = rebels.Where(r => r.Guid == guid).ToString();
74+
Assert.Equal(@"{""selector"":{""guid"":""83c79283-f634-41e3-8aab-674bdbae3413""}}", json);
75+
}
6876
}
6977
}

tests/CouchDB.Driver.UnitTests/_Models/Rebel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class Rebel : CouchDocument
1111
public string Surname { get; set; }
1212
public int Age { get; set; }
1313
public Species Species { get; set; }
14+
public Guid Guid { get; set; }
1415
public List<string> Skills { get; set; }
1516
public List<Battle> Battles { get; set; }
1617
}

0 commit comments

Comments
 (0)