/// <summary> /// Returns all values from array according filter expression or all values (index = MaxValue) /// </summary> public static IEnumerable <BsonValue> ARRAY_FILTER(BsonValue value, int index, BsonExpression filterExpr, BsonDocument root, BsonDocument parameters) { if (!value.IsArray) { yield break; } var arr = value.AsArray; // [*] - index are all values if (index == int.MaxValue) { foreach (var item in arr) { yield return(item); } } // [<expr>] - index are an expression else { // update parameters in expression parameters.CopyTo(filterExpr.Parameters); foreach (var item in arr) { // execute for each child value and except a first bool value (returns if true) var c = filterExpr.ExecuteScalar(new BsonDocument[] { root }, root, item); if (c.IsBoolean && c.AsBoolean == true) { yield return(item); } } } }
/// <summary> /// Returns a single value from array according index or expression parameter /// </summary> public static BsonValue ARRAY_INDEX(BsonValue value, int index, BsonExpression expr, BsonDocument root, BsonDocument parameters) { if (!value.IsArray) { return(BsonValue.Null); } var arr = value.AsArray; // for expr.Type = parameter, just get value as index (fixed position) if (expr.Type == BsonExpressionType.Parameter) { // update parameters in expression parameters.CopyTo(expr.Parameters); // get fixed position based on parameter value (must return int value) var indexValue = expr.ExecuteScalar(root); if (!indexValue.IsNumber) { throw new LiteException(0, "Parameter expression must return number when called inside an array"); } index = indexValue.AsInt32; } var idx = index < 0 ? arr.Count + index : index; if (arr.Count > idx) { return(arr[idx]); } return(BsonValue.Null); }
public static IEnumerable <BsonValue> SORT(IEnumerable <BsonValue> input, BsonExpression sortExpr, BsonDocument root, BsonDocument parameters) { //TODO: implement a sort function here // update parameters in expression parameters.CopyTo(sortExpr.Parameters); foreach (var item in input) { yield return(item); } }
public static IEnumerable <BsonValue> FILTER(IEnumerable <BsonValue> input, BsonExpression filterExpr, BsonDocument root, BsonDocument parameters) { // update parameters in expression parameters.CopyTo(filterExpr.Parameters); foreach (var item in input) { // execute for each child value and except a first bool value (returns if true) var c = filterExpr.ExecuteScalar(new BsonDocument[] { root }, root, item); if (c.IsBoolean && c.AsBoolean == true) { yield return(item); } } }
public static IEnumerable <BsonValue> MAP(IEnumerable <BsonValue> input, BsonExpression mapExpr, BsonDocument root, BsonDocument parameters) { // update parameters in expression parameters.CopyTo(mapExpr.Parameters); foreach (var item in input) { // execute for each child value and except a first bool value (returns if true) var values = mapExpr.Execute(new BsonDocument[] { root }, root, item); foreach (var value in values) { yield return(value); } } }
public static IEnumerable <BsonValue> SORT(BsonDocument root, Collation collation, BsonDocument parameters, IEnumerable <BsonValue> input, BsonExpression sortExpr, BsonValue order) { // update parameters in expression parameters.CopyTo(sortExpr.Parameters); IEnumerable <Tuple <BsonValue, BsonValue> > source() { foreach (var item in input) { var value = sortExpr.ExecuteScalar(new BsonDocument[] { root }, root, item, collation); yield return(new Tuple <BsonValue, BsonValue>(item, value)); } } return((order.IsInt32 && order.AsInt32 > 0) || (order.IsString && order.AsString.Equals("asc", StringComparison.OrdinalIgnoreCase)) ? source().OrderBy(x => x.Item2, collation).Select(x => x.Item1) : source().OrderByDescending(x => x.Item2, collation).Select(x => x.Item1)); }