Skip to content

Commit

Permalink
Merge pull request #26 from matteobortolazzo/GuidQuerySupport
Browse files Browse the repository at this point in the history
Support for querying by guid
  • Loading branch information
Matteo Bortolazzo authored Apr 2, 2019
2 parents e032153 + 65b0d84 commit e6603d4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/CouchDB.Driver/Translators/ConstantExpressionTranslator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;

Expand Down Expand Up @@ -41,16 +42,21 @@ protected override Expression VisitConstant(ConstantExpression c)
VisitIEnumerable(c.Value as IList<int>);
else if (c.Value is IList<long>)
VisitIEnumerable(c.Value as IList<long>);
else if(c.Value is IList<decimal>)
else if (c.Value is IList<decimal>)
VisitIEnumerable(c.Value as IList<decimal>);
else if (c.Value is IList<float>)
VisitIEnumerable(c.Value as IList<float>);
else if (c.Value is IList<double>)
VisitIEnumerable(c.Value as IList<double>);
else if(c.Value is IList<string>)
else if (c.Value is IList<string>)
VisitIEnumerable(c.Value as IList<string>);
else if (c.Value is Guid)
_sb.Append(JsonConvert.SerializeObject(c.Value));
else
throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", c.Value));
{
Debug.WriteLine($"The constant for '{c.Value}' not ufficially supported.");
_sb.Append(JsonConvert.SerializeObject(c.Value));
}
break;
default:
_sb.Append(c.Value);
Expand Down Expand Up @@ -85,7 +91,7 @@ string VisitConst(object o)
case TypeCode.String:
return $"\"{o}\"";
case TypeCode.Object:
throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", o));
throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", o));
default:
return o.ToString();
}
Expand Down
14 changes: 11 additions & 3 deletions tests/CouchDB.Driver.UnitTests/Find/Find_Selector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public void ToList_EmptySelector()
[Fact]
public void ComplexQuery()
{
var json = rebels.Where(r =>
r.Age == 19 &&
(r.Name == "Luke" || r.Name == "Leia") &&
var json = rebels.Where(r =>
r.Age == 19 &&
(r.Name == "Luke" || r.Name == "Leia") &&
r.Skills.Contains("force")).ToString();
Assert.Equal(@"{""selector"":{""$and"":[{""age"":19},{""$or"":[{""name"":""Luke""},{""name"":""Leia""}]},{""skills"":{""$all"":[""force""]}}]}}", json);
}
Expand Down Expand Up @@ -65,5 +65,13 @@ public void Enum()
var json = rebels.Where(r => r.Species == Species.Human).ToString();
Assert.Equal(@"{""selector"":{""species"":0}}", json);
}
[Fact]
public void GuidQuery()
{
var guidString = "83c79283-f634-41e3-8aab-674bdbae3413";
var guid = Guid.Parse(guidString);
var json = rebels.Where(r => r.Guid == guid).ToString();
Assert.Equal(@"{""selector"":{""guid"":""83c79283-f634-41e3-8aab-674bdbae3413""}}", json);
}
}
}
1 change: 1 addition & 0 deletions tests/CouchDB.Driver.UnitTests/_Models/Rebel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Rebel : CouchDocument
public string Surname { get; set; }
public int Age { get; set; }
public Species Species { get; set; }
public Guid Guid { get; set; }
public List<string> Skills { get; set; }
public List<Battle> Battles { get; set; }
}
Expand Down

0 comments on commit e6603d4

Please sign in to comment.