Esempio n. 1
0
 public static void RegisterOnConverterFactory(DefaultExpressionConverterFactory expressionConverterFactory)
 {
     expressionConverterFactory.RegisterTemplateMapping <Guid>(
         () => Guid.NewGuid(),
         () => new Term()
     {
         type = Term.TermType.UUID
     });
 }
        public static void RegisterOnConverterFactory(DefaultExpressionConverterFactory expressionConverterFactory)
        {
            expressionConverterFactory.RegisterTemplateMapping <string, string>(
                s => s.ToLowerInvariant(),
                s => new Term()
            {
                type = Term.TermType.DOWNCASE, args = { s }
            });

            expressionConverterFactory.RegisterTemplateMapping <string, string>(
                s => s.ToUpperInvariant(),
                s => new Term()
            {
                type = Term.TermType.UPCASE, args = { s }
            });

            expressionConverterFactory.RegisterTemplateMapping <string, string, MatchResponse>(
                (@string, regexp) => ReQLExpression.Match(@string, regexp),
                (@string, regexp) => new Term()
            {
                type = Term.TermType.MATCH, args = { @string, regexp }
            });
        }
Esempio n. 3
0
        public static void RegisterOnConverterFactory(DefaultExpressionConverterFactory expressionConverterFactory)
        {
            expressionConverterFactory.RegisterTemplateMapping <Dictionary <string, object>, string, bool>(
                (d, k) => d.ContainsKey(k),
                (d, k) => new Term()
            {
                type = Term.TermType.HAS_FIELDS, args = { d, k }
            });

            expressionConverterFactory.RegisterTemplateMapping <Dictionary <string, object>, string, object>(
                (d, k) => d[k],
                (d, k) => new Term()
            {
                type = Term.TermType.GET_FIELD, args = { d, k }
            });

            expressionConverterFactory.RegisterTemplateMapping <Dictionary <string, object>, string, object, Dictionary <string, object> >(
                (d, k, v) => d.SetValue(k, v),
                (d, k, v) => new Term()
            {
                type = Term.TermType.MERGE,
                args = { d, new Term()
                         {
                             type = Term.TermType.OBJECT,args                               = { k, v }
                         } }
            });

            expressionConverterFactory.RegisterTemplateMapping <Dictionary <string, object>, Dictionary <string, object> .KeyCollection>(
                (d) => d.Keys,
                (d) => new Term()
            {
                type = Term.TermType.KEYS, args = { d }
            });

            // There's no RethinkDB command to get the "values" of an object; so, return the actual dictionary when
            // accessing .Values and assume the datum converter will do the right thing for this type.
            expressionConverterFactory.RegisterTemplateMapping <Dictionary <string, object>, Dictionary <string, object> .ValueCollection>(
                (d) => d.Values,
                (d) => d);

            expressionConverterFactory.RegisterTemplateMapping <Dictionary <string, object>, string, Dictionary <string, object> >(
                (d, k) => d.Without(k),
                (d, k) => new Term()
            {
                type = Term.TermType.WITHOUT,
                args = { d, k }
            });
        }
 public static void RegisterOnConverterFactory(DefaultExpressionConverterFactory expressionConverterFactory)
 {
     expressionConverterFactory.RegisterTemplateMapping <int?, bool>(
         v => v.HasValue,
         v => new Term()
     {
         type = Term.TermType.NE,
         args =
         {
             v,
             new Term()
             {
                 type = Term.TermType.DATUM,datum                               = new Datum()
                 {
                     type = Datum.DatumType.R_NULL
                 }
             }
         }
     }
         );
 }
        public static void RegisterOnConverterFactory(DefaultExpressionConverterFactory expressionConverterFactory)
        {
            var appendDelegate = (AppendDelegate <int>)ReQLExpression.Append;

            expressionConverterFactory.RegisterMethodCallMapping(appendDelegate.Method, ConvertAppendToTerm);

            var whereDelegate = (WhereDelegate <int>)Enumerable.Where;

            expressionConverterFactory.RegisterMethodCallMapping(whereDelegate.Method, ConvertEnumerableWhereToTerm);

            var anyDelegate = (AnyDelegate <int>)Enumerable.Any;

            expressionConverterFactory.RegisterMethodCallMapping(anyDelegate.Method, ConvertEnumerableAnyToTerm);

            expressionConverterFactory.RegisterTemplateMapping <IEnumerable <int>, int, bool>(
                (list, arg) => list.Contains(arg),
                (list, arg) => new Term()
            {
                type = Term.TermType.CONTAINS,
                args = { list, arg }
            });

            expressionConverterFactory.RegisterTemplateMapping <IList <int>, int, bool>(
                (list, arg) => list.Contains(arg),
                (list, arg) => new Term()
            {
                type = Term.TermType.CONTAINS,
                args = { list, arg }
            });

            expressionConverterFactory.RegisterTemplateMapping <List <int>, int, bool>(
                (list, arg) => list.Contains(arg),
                (list, arg) => new Term()
            {
                type = Term.TermType.CONTAINS,
                args = { list, arg }
            });

            expressionConverterFactory.RegisterTemplateMapping <IEnumerable <int>, int>(
                list => list.Count(),
                list => Count(list)
                );
            expressionConverterFactory.RegisterTemplateMapping <List <int>, int>(
                list => list.Count,
                list => Count(list)
                );
            expressionConverterFactory.RegisterTemplateMapping <ICollection <int>, int>(
                list => list.Count,
                list => Count(list)
                );

            expressionConverterFactory.RegisterTemplateMapping <int[], int, int[]>(
                (list, startIndex) => ReQLExpression.Slice(list, startIndex),
                (list, startIndex) => new Term()
            {
                type = Term.TermType.SLICE,
                args = { list, startIndex }
            });
            expressionConverterFactory.RegisterTemplateMapping <int[], int, int, int[]>(
                (list, startIndex, endIndex) => ReQLExpression.Slice(list, startIndex, endIndex),
                (list, startIndex, endIndex) => new Term()
            {
                type = Term.TermType.SLICE,
                args = { list, startIndex, endIndex }
            });
            expressionConverterFactory.RegisterTemplateMapping <int[], int, int, Bound, Bound, int[]>(
                (list, startIndex, endIndex, leftBound, rightBound) => ReQLExpression.Slice(list, startIndex, endIndex, leftBound, rightBound),
                (list, startIndex, endIndex, leftBound, rightBound) => new Term()
            {
                type    = Term.TermType.SLICE,
                args    = { list, startIndex, endIndex },
                optargs =
                {
                    new Term.AssocPair()
                    {
                        key = "left_bound",
                        val = leftBound,
                    },
                    new Term.AssocPair()
                    {
                        key = "right_bound",
                        val = rightBound,
                    }
                }
            });

            expressionConverterFactory.RegisterTemplateMapping <string, int>(
                (errorMessage) => ReQLExpression.Error <int>(errorMessage),
                (errorMessage) => new Term()
            {
                type = Term.TermType.ERROR,
                args = { errorMessage }
            });
        }
Esempio n. 6
0
        public static void RegisterTimeSpanConstructors(DefaultExpressionConverterFactory expressionConverterFactory)
        {
            expressionConverterFactory.RegisterTemplateMapping <long, TimeSpan>(
                (ticks) => new TimeSpan(ticks),
                (ticks) => TicksToSeconds(ticks)
                );
            expressionConverterFactory.RegisterTemplateMapping <int, int, int, TimeSpan>(
                (hours, minutes, seconds) => new TimeSpan(hours, minutes, seconds),
                (hours, minutes, seconds) =>
                Add(
                    HoursToSeconds(hours),
                    MinutesToSeconds(minutes),
                    seconds
                    )
                );
            expressionConverterFactory.RegisterTemplateMapping <int, int, int, int, TimeSpan>(
                (days, hours, minutes, seconds) => new TimeSpan(days, hours, minutes, seconds),
                (days, hours, minutes, seconds) =>
                Add(
                    DaysToSeconds(days),
                    HoursToSeconds(hours),
                    MinutesToSeconds(minutes),
                    seconds
                    )
                );
            expressionConverterFactory.RegisterTemplateMapping <int, int, int, int, int, TimeSpan>(
                (days, hours, minutes, seconds, milliseconds) => new TimeSpan(days, hours, minutes, seconds, milliseconds),
                (days, hours, minutes, seconds, milliseconds) =>
                Add(
                    DaysToSeconds(days),
                    HoursToSeconds(hours),
                    MinutesToSeconds(minutes),
                    seconds,
                    MillisecondsToSeconds(milliseconds)
                    )
                );

            expressionConverterFactory.RegisterTemplateMapping <double, TimeSpan>(
                (days) => TimeSpan.FromDays(days),
                DaysToSeconds
                );
            expressionConverterFactory.RegisterTemplateMapping <double, TimeSpan>(
                (hours) => TimeSpan.FromHours(hours),
                HoursToSeconds
                );
            expressionConverterFactory.RegisterTemplateMapping <double, TimeSpan>(
                (milliseconds) => TimeSpan.FromMilliseconds(milliseconds),
                MillisecondsToSeconds
                );
            expressionConverterFactory.RegisterTemplateMapping <double, TimeSpan>(
                (minutes) => TimeSpan.FromMinutes(minutes),
                MinutesToSeconds
                );
            expressionConverterFactory.RegisterTemplateMapping <double, TimeSpan>(
                (seconds) => TimeSpan.FromSeconds(seconds),
                term => term
                );
            expressionConverterFactory.RegisterTemplateMapping <long, TimeSpan>(
                (ticks) => TimeSpan.FromTicks(ticks),
                TicksToSeconds
                );
        }
Esempio n. 7
0
        public static void RegisterDateTimeAccessors(DefaultExpressionConverterFactory expressionConverterFactory)
        {
            expressionConverterFactory.RegisterTemplateMapping <DateTime, int>(
                dt => dt.Year,
                dt => new Term()
            {
                type = Term.TermType.YEAR, args = { dt }
            }
                );
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, int>(
                dt => dt.Year,
                dt => new Term()
            {
                type = Term.TermType.YEAR, args = { dt }
            }
                );

            expressionConverterFactory.RegisterTemplateMapping <DateTime, int>(
                dt => dt.Month,
                dt => new Term()
            {
                type = Term.TermType.MONTH, args = { dt }
            }
                );
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, int>(
                dt => dt.Month,
                dt => new Term()
            {
                type = Term.TermType.MONTH, args = { dt }
            }
                );

            expressionConverterFactory.RegisterTemplateMapping <DateTime, int>(
                dt => dt.Day,
                dt => new Term()
            {
                type = Term.TermType.DAY, args = { dt }
            }
                );
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, int>(
                dt => dt.Day,
                dt => new Term()
            {
                type = Term.TermType.DAY, args = { dt }
            }
                );

            expressionConverterFactory.RegisterTemplateMapping <DateTime, int>(
                dt => dt.Hour,
                dt => new Term()
            {
                type = Term.TermType.HOURS, args = { dt }
            }
                );
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, int>(
                dt => dt.Hour,
                dt => new Term()
            {
                type = Term.TermType.HOURS, args = { dt }
            }
                );

            expressionConverterFactory.RegisterTemplateMapping <DateTime, int>(
                dt => dt.Minute,
                dt => new Term()
            {
                type = Term.TermType.MINUTES, args = { dt }
            }
                );
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, int>(
                dt => dt.Minute,
                dt => new Term()
            {
                type = Term.TermType.MINUTES, args = { dt }
            }
                );

            expressionConverterFactory.RegisterTemplateMapping <DateTime, int>(
                dt => dt.Second,
                dt => new Term()
            {
                type = Term.TermType.SECONDS, args = { dt }
            }
                );
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, int>(
                dt => dt.Second,
                dt => new Term()
            {
                type = Term.TermType.SECONDS, args = { dt }
            }
                );

            expressionConverterFactory.RegisterTemplateMapping <DateTime, DayOfWeek>(
                dt => dt.DayOfWeek,
                dt => new Term()
            {
                type = Term.TermType.DAY_OF_WEEK, args = { dt }
            }
                );
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, DayOfWeek>(
                dt => dt.DayOfWeek,
                dt => new Term()
            {
                type = Term.TermType.DAY_OF_WEEK, args = { dt }
            }
                );

            expressionConverterFactory.RegisterTemplateMapping <DateTime, int>(
                dt => dt.DayOfYear,
                dt => new Term()
            {
                type = Term.TermType.DAY_OF_YEAR, args = { dt }
            }
                );
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, int>(
                dt => dt.DayOfYear,
                dt => new Term()
            {
                type = Term.TermType.DAY_OF_YEAR, args = { dt }
            }
                );
        }
Esempio n. 8
0
        public static void RegisterDateTimeConstructors(DefaultExpressionConverterFactory expressionConverterFactory)
        {
            expressionConverterFactory.RegisterTemplateMapping <DateTime>(
                () => DateTime.UtcNow,
                () => new Term()
            {
                type = Term.TermType.NOW
            });
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset>(
                () => DateTimeOffset.UtcNow,
                () => new Term()
            {
                type = Term.TermType.NOW
            });

            expressionConverterFactory.RegisterTemplateMapping <int, int, int, DateTime>(
                (year, month, day) => new DateTime(year, month, day),
                (year, month, day) => new Term()
            {
                type = Term.TermType.TIME, args = { year, month, day, String("Z") }
            });
            expressionConverterFactory.RegisterTemplateMapping <int, int, int, int, int, int, DateTime>(
                (year, month, day, hour, minute, second) => new DateTime(year, month, day, hour, minute, second),
                (year, month, day, hour, minute, second) => new Term()
            {
                type = Term.TermType.TIME,
                args = { year, month, day, hour, minute, second, String("Z") }
            });
            expressionConverterFactory.RegisterTemplateMapping <int, int, int, int, int, int, int, DateTime>(
                (year, month, day, hour, minute, second, millisecond) => new DateTime(year, month, day, hour, minute, second, millisecond),
                (year, month, day, hour, minute, second, millisecond) => new Term()
            {
                type = Term.TermType.TIME,
                args = { year, month, day, hour, minute, Add(second, Binary(millisecond, Term.TermType.DIV, 1000)), String("Z") }
            });

            // new DateTimeOffset in .NET creates a DateTimeOffset with either local time offset, or UTC offset,
            // depending upon the DateTime's Kind property.  We can't really support that because we're not actually
            // working with DateTime objects on the RethinkDB side; we're working with ReQL times that already have
            // offsets associated with them.  So... this is basically a NOOP.
            expressionConverterFactory.RegisterTemplateMapping <DateTime, DateTimeOffset>(
                (dt) => new DateTimeOffset(dt),
                (dt) => dt);

            // But creating a DateTimeOffset with a specific timezone isn't a NOOP.  This is re-interpreting the given
            // time at the given timezone (not converting it).
            expressionConverterFactory.RegisterTemplateMapping <DateTime, TimeSpan, DateTimeOffset>(
                (dt, offset) => new DateTimeOffset(dt, offset),
                (dt, offset) => new Term()
            {
                type = Term.TermType.TIME,
                // Well, this is awkward, but... break the time up and construct it again with the specified
                // TZ offset.
                args =
                {
                    new Term()
                    {
                        type = Term.TermType.YEAR, args ={ dt                                       }
                    },
                    new Term()
                    {
                        type = Term.TermType.MONTH, args ={ dt                                       }
                    },
                    new Term()
                    {
                        type = Term.TermType.DAY, args ={ dt                                       }
                    },
                    new Term()
                    {
                        type = Term.TermType.HOURS, args ={ dt                                       }
                    },
                    new Term()
                    {
                        type = Term.TermType.MINUTES, args ={ dt                                       }
                    },
                    new Term()
                    {
                        type = Term.TermType.SECONDS, args ={ dt                                       }
                    },
                    TimeSpanToOffset(offset),
                }
            });

            // epoch = (UtcTicks - 621355968000000000) / 10000000.0
            expressionConverterFactory.RegisterTemplateMapping <long, TimeSpan, DateTimeOffset>(
                (ticks, offset) => new DateTimeOffset(ticks, offset),
                (ticks, offset) => new Term()
            {
                type = Term.TermType.IN_TIMEZONE,
                args =
                {
                    new Term()
                    {
                        type = Term.TermType.EPOCH_TIME,
                        args =
                        {
                            Binary(
                                Binary(
                                    Binary(ticks,      Term.TermType.SUB, 621355968000000000),
                                    Term.TermType.DIV,
                                    10000000
                                    ),
                                Term.TermType.SUB,
                                offset
                                )
                        }
                    },
                    TimeSpanToOffset(offset)
                }
            });
            expressionConverterFactory.RegisterTemplateMapping <int, int, int, int, int, int, TimeSpan, DateTimeOffset>(
                (year, month, day, hour, minute, second, offset) => new DateTimeOffset(year, month, day, hour, minute, second, offset),
                (year, month, day, hour, minute, second, offset) => new Term()
            {
                type = Term.TermType.TIME,
                args = { year, month, day, hour, minute, second, TimeSpanToOffset(offset) }
            });
            expressionConverterFactory.RegisterTemplateMapping <int, int, int, int, int, int, int, TimeSpan, DateTimeOffset>(
                (year, month, day, hour, minute, second, millisecond, offset) => new DateTimeOffset(year, month, day, hour, minute, second, millisecond, offset),
                (year, month, day, hour, minute, second, millisecond, offset) => new Term()
            {
                type = Term.TermType.TIME,
                args = { year, month, day, hour, minute, Add(second, Binary(millisecond, Term.TermType.DIV, 1000)), TimeSpanToOffset(offset) }
            });
        }
Esempio n. 9
0
        public static void RegisterDateTimeAddMethods(DefaultExpressionConverterFactory expressionConverterFactory)
        {
            expressionConverterFactory.RegisterTemplateMapping <DateTime, TimeSpan, DateTime>(
                (dt, timespan) => dt.Add(timespan),
                (dt, timespan) => Add(dt, timespan));
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, TimeSpan, DateTimeOffset>(
                (dt, timespan) => dt.Add(timespan),
                (dt, timespan) => Add(dt, timespan));

            expressionConverterFactory.RegisterTemplateMapping <DateTime, double, DateTime>(
                (dt, minutes) => dt.AddMinutes(minutes),
                (dt, minutes) => Add(dt, MinutesToSeconds(minutes)));
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, double, DateTimeOffset>(
                (dt, minutes) => dt.AddMinutes(minutes),
                (dt, minutes) => Add(dt, MinutesToSeconds(minutes)));

            expressionConverterFactory.RegisterTemplateMapping <DateTime, double, DateTime>(
                (dt, seconds) => dt.AddSeconds(seconds),
                (dt, seconds) => Add(dt, seconds));
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, double, DateTimeOffset>(
                (dt, seconds) => dt.AddSeconds(seconds),
                (dt, seconds) => Add(dt, seconds));

            expressionConverterFactory.RegisterTemplateMapping <DateTime, double, DateTime>(
                (dt, hours) => dt.AddHours(hours),
                (dt, hours) => Add(dt, HoursToSeconds(hours)));
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, double, DateTimeOffset>(
                (dt, hours) => dt.AddHours(hours),
                (dt, hours) => Add(dt, HoursToSeconds(hours)));

            expressionConverterFactory.RegisterTemplateMapping <DateTime, double, DateTime>(
                (dt, ms) => dt.AddMilliseconds(ms),
                (dt, ms) => Add(dt, MillisecondsToSeconds(ms)));
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, double, DateTimeOffset>(
                (dt, ms) => dt.AddMilliseconds(ms),
                (dt, ms) => Add(dt, MillisecondsToSeconds(ms)));

            expressionConverterFactory.RegisterTemplateMapping <DateTime, long, DateTime>(
                (dt, ticks) => dt.AddTicks(ticks),
                (dt, ticks) => Add(dt, TicksToSeconds(ticks)));
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, long, DateTimeOffset>(
                (dt, ticks) => dt.AddTicks(ticks),
                (dt, ticks) => Add(dt, TicksToSeconds(ticks)));

            expressionConverterFactory.RegisterTemplateMapping <DateTime, double, DateTime>(
                (dt, days) => dt.AddDays(days),
                (dt, days) => Add(dt, DaysToSeconds(days)));
            expressionConverterFactory.RegisterTemplateMapping <DateTimeOffset, double, DateTimeOffset>(
                (dt, days) => dt.AddDays(days),
                (dt, days) => Add(dt, DaysToSeconds(days)));
        }