コード例 #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;
            }
コード例 #2
0
            public void Build(ISqExtensionBuilder builder)
            {
                string exprStr;
                var    part = builder.GetValue <DateParts>("part");

                switch (part)
                {
                case DateParts.Year: exprStr = "Year({date})";          break;

                case DateParts.Quarter:
                {
                    builder.Expression       = "Month({date})";
                    builder.ResultExpression =
                        builder.Inc(builder.Div(builder.Dec(builder.ConvertToSqlExpression(Precedence.Primary)), 3));
                    return;
                }

                case DateParts.Month: exprStr = "Month({date})";         break;

                case DateParts.DayOfYear:
                {
                    var param = builder.GetExpression("date");
                    builder.ResultExpression = builder.Inc(
                        builder.Sub <int>(
                            new SqlFunction(typeof(DateTime?), "Mdy",
                                            new SqlFunction(typeof(int?), "Month", param),
                                            new SqlFunction(typeof(int?), "Day", param),
                                            new SqlFunction(typeof(int?), "Year", param)),
                            new SqlFunction(typeof(DateTime?), "Mdy",
                                            new SqlValue(1),
                                            new SqlValue(1),
                                            new SqlFunction(typeof(int?), "Year", param)))
                        );
                    return;
                }

                case DateParts.Day: exprStr = "Day({date})";           break;

                case DateParts.Week: exprStr = "((Extend({date}, year to day) - (Mdy(12, 31 - WeekDay(Mdy(1, 1, year({date}))), Year({date}) - 1) + Interval(1) day to day)) / 7 + Interval(1) day to day)::char(10)::int"; break;

                case DateParts.WeekDay:
                {
                    builder.Expression       = "weekDay({date})";
                    builder.ResultExpression = builder.Inc(builder.ConvertToSqlExpression(Precedence.Primary));
                    return;
                }

                case DateParts.Hour: exprStr = "({date}::datetime Hour to Hour)::char(3)::int";     break;

                case DateParts.Minute: exprStr = "({date}::datetime Minute to Minute)::char(3)::int"; break;

                case DateParts.Second: exprStr = "({date}::datetime Second to Second)::char(3)::int"; break;

                case DateParts.Millisecond: exprStr = "Millisecond({date})";                               break;

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

                builder.Expression = exprStr;
            }