Skip to content

Commit 3509658

Browse files
committed
Support for querying by guid
1 parent de8d7ae commit 3509658

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/CouchDB.Driver/Translators/ConstantExpressionTranslator.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ protected override Expression VisitConstant(ConstantExpression c)
4141
VisitIEnumerable(c.Value as IList<int>);
4242
else if (c.Value is IList<long>)
4343
VisitIEnumerable(c.Value as IList<long>);
44-
else if(c.Value is IList<decimal>)
44+
else if (c.Value is IList<decimal>)
4545
VisitIEnumerable(c.Value as IList<decimal>);
4646
else if (c.Value is IList<float>)
4747
VisitIEnumerable(c.Value as IList<float>);
4848
else if (c.Value is IList<double>)
4949
VisitIEnumerable(c.Value as IList<double>);
50-
else if(c.Value is IList<string>)
50+
else if (c.Value is IList<string>)
5151
VisitIEnumerable(c.Value as IList<string>);
52+
else if (c.Value is Guid)
53+
_sb.Append(JsonConvert.SerializeObject(c.Value));
5254
else
5355
throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", c.Value));
5456
break;
@@ -85,7 +87,7 @@ string VisitConst(object o)
8587
case TypeCode.String:
8688
return $"\"{o}\"";
8789
case TypeCode.Object:
88-
throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", o));
90+
throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", o));
8991
default:
9092
return o.ToString();
9193
}

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)