Beispiel #1
0
            public void Build(ISqExtensionBuilder builder)
            {
                var part      = builder.GetValue <DateParts>(0);
                var startDate = builder.GetExpression(1);
                var endDate   = builder.GetExpression(2);

                var secondsExpr = builder.Mul <int>(builder.Sub <int>(
                                                        new SqlFunction(typeof(int), "Days", endDate),
                                                        new SqlFunction(typeof(int), "Days", startDate)),
                                                    new SqlValue(86400));

                var midnight = builder.Sub <int>(
                    new SqlFunction(typeof(int), "MIDNIGHT_SECONDS", endDate),
                    new SqlFunction(typeof(int), "MIDNIGHT_SECONDS", startDate));

                var resultExpr = builder.Add <int>(secondsExpr, midnight);

                switch (part)
                {
                case DateParts.Day: resultExpr = builder.Div(resultExpr, 86400); break;

                case DateParts.Hour: resultExpr = builder.Div(resultExpr, 3600);  break;

                case DateParts.Minute: resultExpr = builder.Div(resultExpr, 60);    break;

                case DateParts.Second: break;

                case DateParts.Millisecond:
                    resultExpr = builder.Add <int>(
                        builder.Mul(resultExpr, 1000),
                        builder.Div(
                            builder.Sub <int>(
                                new SqlFunction(typeof(int), "MICROSECOND", endDate),
                                new SqlFunction(typeof(int), "MICROSECOND", startDate)),
                            1000));
                    break;

                default:
                    throw new InvalidOperationException($"Unexpected datepart: {part}");
                }

                builder.ResultExpression = resultExpr;
            }
Beispiel #2
0
            public void Build(ISqExtensionBuilder builder)
            {
                var part   = builder.GetValue <DateParts>("part");
                var date   = builder.GetExpression("date");
                var number = builder.GetExpression("number", true);

                string expStr;

                switch (part)
                {
                case DateParts.Year: expStr = "{0} * Interval '1 Year'";         break;

                case DateParts.Quarter: expStr = "{0} * Interval '1 Month' * 3";    break;

                case DateParts.Month: expStr = "{0} * Interval '1 Month'";        break;

                case DateParts.DayOfYear:
                case DateParts.WeekDay:
                case DateParts.Day: expStr = "{0} * Interval '1 Day'";          break;

                case DateParts.Week: expStr = "{0} * Interval '1 Day' * 7";      break;

                case DateParts.Hour: expStr = "{0} * Interval '1 Hour'";         break;

                case DateParts.Minute: expStr = "{0} * Interval '1 Minute'";       break;

                case DateParts.Second: expStr = "{0} * Interval '1 Second'";       break;

                case DateParts.Millisecond: expStr = "{0} * Interval '1 Millisecond'";  break;

                default:
                    throw new InvalidOperationException($"Unexpected datepart: {part}");
                }

                builder.ResultExpression = builder.Add(
                    date,
                    new SqlExpression(typeof(TimeSpan?), expStr, Precedence.Multiplicative, number),
                    typeof(DateTime?));
            }
Beispiel #3
0
            public void Build(ISqExtensionBuilder builder)
            {
                var part   = builder.GetValue <DateParts>("part");
                var date   = builder.GetExpression("date");
                var number = builder.GetExpression("number", true);

                string expStr;

                switch (part)
                {
                case DateParts.Year: expStr = "{0} Year";                 break;

                case DateParts.Quarter: expStr = "({0} * 3) Month";          break;

                case DateParts.Month: expStr = "{0} Month";                break;

                case DateParts.DayOfYear:
                case DateParts.WeekDay:
                case DateParts.Day: expStr = "{0} Day";                  break;

                case DateParts.Week: expStr = "({0} * 7) Day";            break;

                case DateParts.Hour: expStr = "{0} Hour";                 break;

                case DateParts.Minute: expStr = "{0} Minute";               break;

                case DateParts.Second: expStr = "{0} Second";               break;

                case DateParts.Millisecond: expStr = "({0} / 1000.0) Second";    break;

                default:
                    throw new InvalidOperationException($"Unexpected datepart: {part}");
                }

                builder.ResultExpression = builder.Add(
                    date,
                    new SqlExpression(typeof(TimeSpan?), expStr, Precedence.Primary, number),
                    typeof(DateTime?));
            }
Beispiel #4
0
            public void Build(ISqExtensionBuilder builder)
            {
                var part   = builder.GetValue <DateParts>("part");
                var date   = builder.GetExpression("date");
                var number = builder.GetExpression("number", true);

                string expStr;

                switch (part)
                {
                case DateParts.Year: expStr = "{0} * INTERVAL '1' YEAR"; break;

                case DateParts.Quarter: expStr = "{0} * INTERVAL '3' MONTH"; break;

                case DateParts.Month: expStr = "{0} * INTERVAL '1' MONTH"; break;

                case DateParts.DayOfYear:
                case DateParts.WeekDay:
                case DateParts.Day: expStr = "{0} * INTERVAL '1' DAY"; break;

                case DateParts.Week: expStr = "{0} * INTERVAL '7' DAY"; break;

                case DateParts.Hour: expStr = "{0} * INTERVAL '1' HOUR"; break;

                case DateParts.Minute: expStr = "{0} * INTERVAL '1' MINUTE"; break;

                case DateParts.Second: expStr = "{0} * INTERVAL '1' SECOND"; break;

                case DateParts.Millisecond: expStr = "{0} * INTERVAL '0.001' SECOND"; break;

                default:
                    throw new InvalidOperationException($"Unexpected datepart: {part}");
                }

                builder.ResultExpression = builder.Add(
                    date,
                    new SqlExpression(typeof(TimeSpan?), expStr, Precedence.Multiplicative, number),
                    typeof(DateTime?));
            }