public Dictionary(IConvertibleToBsonDocument document) { if (document == null) { throw new ArgumentNullException("document"); } _document = document.ToBsonDocument(); }
// public for tests public static Expression GetExpression(IConvertibleToBsonDocument update, IConvertibleToBsonDocument query, bool insert) { var compiler = new UpdateCompiler(); Expression expression = Expression.Constant(compiler, typeof(UpdateCompiler)); if (update == null) { return(expression); } if (insert) { expression = UpdateFromQueryExpression(expression, query); } var document = update.ToBsonDocument(); bool isName = false; bool isOper = false; var names = new List <string>(); foreach (var element in document) { var name = element.Name; if (name[0] == '$') { if (isName) { ThrowMixedOperatorsAndNames(); } isOper = true; foreach (var fieldElement in element.Value.AsBsonDocument) { var fieldName = fieldElement.Name; ValidateFieldName(fieldName, names); expression = OperatorExpression(expression, name, Expression.Constant(fieldName, typeof(string)), fieldElement.Value, insert); } } else //_131103_204607 { if (isOper) { ThrowMixedOperatorsAndNames(); } isName = true; ValidateFieldName(name, names); expression = SetExpression(expression, Expression.Constant(name, typeof(string)), element.Value); } } return(expression); }
// public for tests public static Expression GetExpression(IConvertibleToBsonDocument query) { if (query == null) return Expression.Constant(true, typeof(bool)); var document = query.ToBsonDocument(); switch (document.ElementCount) { case 1: return ElementExpression(document.GetElement(0)); case 0: return Expression.Constant(true, typeof(bool)); default: return DocumentExpression(document); } }
// public for tests public static Expression GetExpression(IConvertibleToBsonDocument update, IConvertibleToBsonDocument query, bool insert) { var compiler = new UpdateCompiler(); Expression expression = Expression.Constant(compiler, typeof(UpdateCompiler)); if (update == null) return expression; if (insert) expression = UpdateFromQueryExpression(expression, query); var document = update.ToBsonDocument(); bool isName = false; bool isOper = false; var names = new List<string>(); foreach (var element in document) { var name = element.Name; if (name[0] == '$') { if (isName) ThrowMixedOperatorsAndNames(); isOper = true; foreach (var fieldElement in element.Value.AsBsonDocument) { var fieldName = fieldElement.Name; ValidateFieldName(fieldName, names); expression = OperatorExpression(expression, name, Expression.Constant(fieldName, typeof(string)), fieldElement.Value, insert); } } else //_131103_204607 { if (isOper) ThrowMixedOperatorsAndNames(); isName = true; ValidateFieldName(name, names); expression = SetExpression(expression, Expression.Constant(name, typeof(string)), element.Value); } } return expression; }
// public for tests public static Expression GetExpression(IConvertibleToBsonDocument query) { if (query == null) { return(Expression.Constant(true, typeof(bool))); } var document = query.ToBsonDocument(); switch (document.ElementCount) { case 1: return(ElementExpression(document.GetElement(0))); case 0: return(Expression.Constant(true, typeof(bool))); default: return(DocumentExpression(document)); } }
static Expression UpdateFromQueryExpression(Expression that, IConvertibleToBsonDocument query) { if (query == null) { return(that); } foreach (var element in query.ToBsonDocument()) { // v2.6 fails if (element.Name[0] == '$') { throw new ArgumentException(string.Format(null, "Unknown top level query operator: ({0}).", element.Name)); } var selector = element.Value; switch (selector.BsonType) { case BsonType.RegularExpression: continue; case BsonType.Document: //TODO _140223_223449 var document = selector.AsBsonDocument; if (document.ElementCount > 0) { var name = document.GetElement(0).Name; if (name[0] == '$' && name != "$ref") { continue; } } break; } that = UpdateCompiler.SetExpression(that, Expression.Constant(element.Name, typeof(string)), selector); } return(that); }
public Dictionary(IConvertibleToBsonDocument document) { if (document == null) throw new ArgumentNullException("document"); _document = document.ToBsonDocument(); }
static Expression UpdateFromQueryExpression(Expression that, IConvertibleToBsonDocument query) { if (query == null) return that; foreach (var element in query.ToBsonDocument()) { if (element.Name[0] == '$') continue; var selector = element.Value; switch(selector.BsonType) { case BsonType.RegularExpression: continue; case BsonType.Document: var document = selector.AsBsonDocument; if (document.ElementCount > 0) { var name = document.GetElement(0).Name; if (name[0] == '$' && name != "$ref") continue; } break; } that = UpdateCompiler.SetExpression(that, Expression.Constant(element.Name, typeof(string)), selector); } return that; }