// [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 FilterDefinition <T> Compile <T>(RqlExpression expression) { this.exp = expression; this.sb = new StringBuilder(); Visit(exp); string s = sb.ToString(); FilterDefinition <T> filter = null; if (!String.IsNullOrEmpty(s)) { try { filter = new BsonDocumentFilterDefinition <T>(BsonSerializer.Deserialize <BsonDocument>(s)); } catch { throw new RqlToMongoException("Invalid query specified"); } } this.sb = null; return(filter); }
private RqlExpression VisitOperatorAndOr(RqlFunctionCallExpression node) { sb.Append("{"); sb.Append("$"); sb.Append(node.Name); sb.Append(": ["); for (int i = 0; i < node.Arguments.Count; i++) { RqlExpression argument = node.Arguments[i]; if (argument.ExpressionType != RqlExpressionType.FunctionCall || (argument.ExpressionType == RqlExpressionType.Constant && argument.Token.Data.GetType() != typeof(bool))) { ThrowError(argument, "Argument must be boolean constant or expression"); } Visit(argument); if (i < node.Arguments.Count - 1) { sb.Append(","); } } sb.Append("]"); sb.Append("}"); return(node); }
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 TestExpressionVisitor(RqlExpression exp) { this.exp = exp; }
private void ThrowError(RqlExpression node, string format, params object[] args) { string s = String.Format("Offset {0}: {1}", node.Token.Offset, format); throw new RqlToMongoException(String.Format(s, args)); }