Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for querying by guid #26

Merged
merged 2 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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