diff --git a/rethinkdb-net-test/Integration/BlankTests.cs b/rethinkdb-net-test/Integration/BlankTests.cs index 2b526e1..9b4c4fe 100644 --- a/rethinkdb-net-test/Integration/BlankTests.cs +++ b/rethinkdb-net-test/Integration/BlankTests.cs @@ -309,5 +309,14 @@ public void ExpressionDateTimeWithMilliseconds() var datetimems = connection.Run(Query.Expr(() => new DateTime(2014, 2, 3, 7, 30, 15, 500))); Assert.That(datetimems, Is.EqualTo(new DateTime(2014, 2, 3, 7, 30, 15, 500))); } + + [Test] + public void RegexpMatch() + { + var match = connection.Run(Query.Expr(() => "hello".Match("l+"))); + match.MatchedString.Should().Be("ll"); + match.Start.Should().Be(2); + match.End.Should().Be(4); + } } } diff --git a/rethinkdb-net-test/Integration/MultiObjectTests.cs b/rethinkdb-net-test/Integration/MultiObjectTests.cs index 342d82a..a1cf4fa 100644 --- a/rethinkdb-net-test/Integration/MultiObjectTests.cs +++ b/rethinkdb-net-test/Integration/MultiObjectTests.cs @@ -1121,5 +1121,17 @@ public void ToUpperInvariant() connection.Run(testTable.Insert(new TestObject() { Id = "1", Name = "Has a Name" })); connection.Run(testTable.Select(o => o.Name.ToUpperInvariant())).Single().Should().Be("HAS A NAME"); } + + [Test] + public void FilterByRegexpMatches() + { + int count = 0; + foreach (var row in connection.Run(testTable.Filter(o => o.Name.Match("^3$") != null))) + { + row.Name.Should().Be("3"); + count += 1; + } + count.Should().Be(1); + } } } diff --git a/rethinkdb-net/Expressions/StringExpressionConverters.cs b/rethinkdb-net/Expressions/StringExpressionConverters.cs index 1510061..ab3988f 100644 --- a/rethinkdb-net/Expressions/StringExpressionConverters.cs +++ b/rethinkdb-net/Expressions/StringExpressionConverters.cs @@ -15,6 +15,10 @@ public static void RegisterOnConverterFactory(DefaultExpressionConverterFactory expressionConverterFactory.RegisterTemplateMapping( s => s.ToUpperInvariant(), s => new Term() { type = Term.TermType.UPCASE, args = { s } }); + + expressionConverterFactory.RegisterTemplateMapping( + (@string, regexp) => ReQLExpression.Match(@string, regexp), + (@string, regexp) => new Term() { type = Term.TermType.MATCH, args = { @string, regexp } }); } } } diff --git a/rethinkdb-net/MatchGroupResponse.cs b/rethinkdb-net/MatchGroupResponse.cs new file mode 100644 index 0000000..bd42a26 --- /dev/null +++ b/rethinkdb-net/MatchGroupResponse.cs @@ -0,0 +1,17 @@ +using System.Runtime.Serialization; + +namespace RethinkDb +{ + [DataContract] + public class MatchGroupResponse + { + [DataMember(Name = "start")] + public uint Start; + + [DataMember(Name = "end")] + public uint End; + + [DataMember(Name = "str")] + public string MatchedString; + } +} diff --git a/rethinkdb-net/MatchResponse.cs b/rethinkdb-net/MatchResponse.cs new file mode 100644 index 0000000..eeb71d3 --- /dev/null +++ b/rethinkdb-net/MatchResponse.cs @@ -0,0 +1,20 @@ +using System.Runtime.Serialization; + +namespace RethinkDb +{ + [DataContract] + public class MatchResponse + { + [DataMember(Name = "start")] + public uint Start; + + [DataMember(Name = "end")] + public uint End; + + [DataMember(Name = "str")] + public string MatchedString; + + [DataMember(Name = "groups")] + public MatchGroupResponse[] Groups; + } +} diff --git a/rethinkdb-net/ReQLExpression.cs b/rethinkdb-net/ReQLExpression.cs index adaca1d..5a33bf9 100644 --- a/rethinkdb-net/ReQLExpression.cs +++ b/rethinkdb-net/ReQLExpression.cs @@ -28,5 +28,10 @@ public static T Error(string errorText) { throw new NotSupportedException("This method cannot be invoked directly, it can only be used as part of an expression tree."); } + + public static MatchResponse Match(this string @string, string regexp) + { + throw new NotSupportedException("This method cannot be invoked directly, it can only be used as part of an expression tree."); + } } } diff --git a/rethinkdb-net/rethinkdb-net.csproj b/rethinkdb-net/rethinkdb-net.csproj index fa4bc9f..c5e3230 100644 --- a/rethinkdb-net/rethinkdb-net.csproj +++ b/rethinkdb-net/rethinkdb-net.csproj @@ -223,6 +223,8 @@ + +