// [Fact] public void RobustUse() { var rawJson = ""; // Statically parse raw json var(dbExpression, errs) = RqlParser.Parse <TestClass>(rawJson); // Alternatively parse a `RqlExpression` useful for avoiding nasty C# json string literals var rqlExpression = new RqlExpression { Filter = new Dictionary <string, object>() { }, }; (dbExpression, errs) = RqlParser.Parse <TestClass>(rqlExpression); // Alternatively you can use a generic instance IRqlParser <TestClass> genericParser = new RqlParser <TestClass>(); (dbExpression, errs) = genericParser.Parse(rawJson); (dbExpression, errs) = genericParser.Parse(rqlExpression); // Alternatively you can use a non-generic instance var classSpec = new ClassSpecBuilder().Build(typeof(TestClass)); IRqlParser parser = new RqlParser(classSpec); (dbExpression, errs) = parser.Parse(rawJson); (dbExpression, errs) = parser.Parse(rqlExpression); }
public void SimpleAnd() { var raw = @"{ ""filter"": { ""t_Long"": 3, ""t_Int"": 2, ""t_Float"": 3.4, ""t_String"": ""str"", ""t_Bool"": true, ""t_DateTime"" : ""2019-03-20T01:21:25.467589-05:00"", ""t_DateTime2"" : 1553063286 } } "; var(result, errs) = RqlParser.Parse <TestClass>(raw); Assert.True(errs == null); Assert.Equal( "T_Long = @t_Long AND T_Int = @t_Int AND T_Float = @t_Float AND T_String = @t_String AND T_Bool = @t_Bool AND T_DateTime = @t_DateTime AND T_DateTime2 = @t_DateTime2", result.Filter); }
public void QueryExamples() { var rqlExpression = new RqlExpression { Filter = new Dictionary <string, object>() { ["isDone"] = true, ["$or"] = new List <object>() { new Dictionary <string, object>() { ["updatedAt"] = new Dictionary <string, object>() { ["$lt"] = "2020/01/02", ["$gt"] = 1577836800, } } }, }, Limit = 1000, Offset = 0, Sort = new List <string>() { "-updatedAt" }, }; var(result, errs) = RqlParser.Parse <Item>(rqlExpression); Assert.True(errs == null); var expectation = "IsDone = @isDone AND ( UpdatedAt < @updatedAt AND UpdatedAt > @updatedAt2 )"; Assert.Equal(expectation, result.Filter); Assert.True(result.Limit == 1000); Assert.True(result.Offset == 0); Assert.True(result.Sort == "UpdatedAt DESC"); Assert.Equal(result.Parameters["@isDone"], true); Assert.Equal(result.Parameters["@updatedAt"], new DateTime(2020, 01, 02)); Assert.Equal(result.Parameters["@updatedAt2"], new DateTime(2020, 01, 01)); }
public void Complex() { var raw = @"{ ""filter"": { ""t_Long"": 3, ""$or"" : [ {""t_Long"" : {""$gte"" : 1 }}, {""t_Long"" : {""$lte"" : 5 }}, {""t_Long"" : 20 } ], ""t_Bool"": true, ""$and"" : { ""t_String"": {""$like"":""%testing%"", ""$neq"" : ""test""} }, ""t_Int"" : { ""$in"" : [1,2] } } } "; var(result, errs) = RqlParser.Parse <TestClass>(raw); Assert.True(errs == null); var expectation = "T_Long = @t_Long AND ( T_Long >= @t_Long2 OR T_Long <= @t_Long3 OR T_Long = @t_Long4 ) AND T_Bool = @t_Bool AND ( T_String LIKE @t_String AND T_String != @t_String2 ) AND T_Int IN @t_Int"; Console.WriteLine(result.Filter); Assert.Equal(expectation, result.Filter); }