Example #1
0
            public void Build(ISqExtensionBuilder builder)
            {
                var part   = builder.GetValue <DateParts>("part");
                var date   = builder.GetExpression("date");
                var number = builder.GetExpression("number", true);

                switch (part)
                {
                case DateParts.Quarter:
                    part   = DateParts.Month;
                    number = builder.Mul(number, 3);
                    break;

                case DateParts.DayOfYear:
                case DateParts.WeekDay:
                    part = DateParts.Day;
                    break;

                case DateParts.Week:
                    part   = DateParts.Day;
                    number = builder.Mul(number, 7);
                    break;
                }

                var partSql = new SqlExpression(part.ToString());

                builder.ResultExpression = new SqlFunction(typeof(DateTime?), "DateAdd", partSql, number, date);
            }
Example #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 function;

                switch (part)
                {
                case DateParts.Year: function = "Add_Years";   break;

                case DateParts.Quarter:
                    function = "Add_Months";
                    number   = builder.Mul(number, 3);
                    break;

                case DateParts.Month: function = "Add_Months";  break;

                case DateParts.DayOfYear:
                case DateParts.Day:
                case DateParts.WeekDay: function = "Add_Days";    break;

                case DateParts.Week:
                    function = "Add_Days";
                    number   = builder.Mul(number, 7);
                    break;

                case DateParts.Hour:
                    function = "Add_Seconds";
                    number   = builder.Mul(number, 3600);
                    break;

                case DateParts.Minute:
                    function = "Add_Seconds";
                    number   = builder.Mul(number, 60);
                    break;

                case DateParts.Second: function = "Add_Seconds"; break;

                case DateParts.Millisecond:
                    function = "Add_Seconds";
                    number   = builder.Div(number, 1000);
                    break;

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

                builder.ResultExpression = new SqlFunction(typeof(DateTime?), function, date, number);
            }
Example #3
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;
            }