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

                string funcName;

                switch (part)
                {
                case DateParts.Day: funcName = "Days_Between";                     break;

                case DateParts.Hour: funcName = "Seconds_Between"; divider = 3600;  break;

                case DateParts.Minute: funcName = "Seconds_Between"; divider = 60;    break;

                case DateParts.Second: funcName = "Seconds_Between";                  break;

                case DateParts.Millisecond: funcName = "Nano100_Between"; divider = 10000; break;

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

                ISqlExpression func = new SqlFunction(typeof(int), funcName, startdate, endDate);

                if (divider != 1)
                {
                    func = builder.Div(func, divider);
                }

                builder.ResultExpression = func;
            }
Example #2
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;
            }
Example #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 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 #4
0
            public void Build(ISqExtensionBuilder builder)
            {
                string?partStr = null;
                var    part    = builder.GetValue <DateParts>("part");

                switch (part)
                {
                case DateParts.Year: partStr = "Y"; break;

                case DateParts.Quarter:
                    builder.Expression       = "Cast(strFTime('%m', {date}) as int)";
                    builder.ResultExpression = builder.Inc(builder.Div(builder.Dec(builder.ConvertToSqlExpression(Precedence.Primary)), 3));
                    return;

                case DateParts.Month: partStr = "m"; break;

                case DateParts.DayOfYear: partStr = "j"; break;

                case DateParts.Day: partStr = "d"; break;

                case DateParts.Week: partStr = "W"; break;

                case DateParts.WeekDay:
                    builder.Expression       = "Cast(strFTime('%w', {date}) as int)";
                    builder.ResultExpression = builder.Inc(builder.ConvertToSqlExpression(Precedence.Primary));
                    return;

                case DateParts.Hour: partStr = "H"; break;

                case DateParts.Minute: partStr = "M"; break;

                case DateParts.Second: partStr = "S"; break;

                case DateParts.Millisecond:
                    builder.Expression           = "Cast(strFTime('%f', {date}) * 1000 as int) % 1000";
                    builder.Extension.Precedence = Precedence.Multiplicative;
                    break;

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

                if (partStr != null)
                {
                    builder.AddExpression("part", partStr);
                }
            }
Example #5
0
            public void Build(ISqExtensionBuilder builder)
            {
                string partStr;
                var    part = builder.GetValue <DateParts>("part");

                switch (part)
                {
                case DateParts.Year: partStr = "year";        break;

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

                case DateParts.Month: partStr = "month";       break;

                case DateParts.DayOfYear: partStr = "yearday";     break;

                case DateParts.Day: partStr = "day";         break;

                case DateParts.Week: partStr = "week";        break;

                case DateParts.WeekDay: partStr = "weekday";     break;

                case DateParts.Hour: partStr = "hour";        break;

                case DateParts.Minute: partStr = "minute";      break;

                case DateParts.Second: partStr = "second";      break;

                case DateParts.Millisecond: partStr = "millisecond"; break;

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

                builder.AddExpression("part", partStr);

                switch (part)
                {
                case DateParts.DayOfYear:
                case DateParts.WeekDay:
                    builder.ResultExpression = builder.Inc(builder.ConvertToSqlExpression(Precedence.Primary));
                    break;
                }
            }
Example #6
0
            public void Build(ISqExtensionBuilder builder)
            {
                string partStr;
                var    part = builder.GetValue <DateParts>("part");

                switch (part)
                {
                case DateParts.Year: partStr = "To_Number(To_Char({date}, 'YYYY'))";                  break;

                case DateParts.Quarter: partStr = "To_Number(To_Char({date}, 'Q'))";                     break;

                case DateParts.Month: partStr = "To_Number(To_Char({date}, 'MM'))";                    break;

                case DateParts.DayOfYear: partStr = "To_Number(To_Char({date}, 'DDD'))";                   break;

                case DateParts.Day: partStr = "To_Number(To_Char({date}, 'DD'))";                    break;

                case DateParts.Week: partStr = "To_Number(To_Char({date}, 'WW'))";                    break;

                case DateParts.WeekDay: partStr = "DayOfWeek({date})";                                   break;

                case DateParts.Hour: partStr = "To_Number(To_Char({date}, 'HH24'))";                  break;

                case DateParts.Minute: partStr = "To_Number(To_Char({date}, 'MI'))";                    break;

                case DateParts.Second: partStr = "To_Number(To_Char({date}, 'SS'))";                    break;

                case DateParts.Millisecond:
                {
                    builder.Expression       = "To_Number(To_Char({date}, 'FF'))";
                    builder.ResultExpression = builder.Div(builder.ConvertToSqlExpression(Precedence.Primary), 1000);
                    return;
                }

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

                builder.Expression = partStr;
            }
Example #7
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;
            }