public void Build(Sql.ISqExtensionBuilder builder) { string?partStr = null; var part = builder.GetValue <Sql.DateParts>("part"); switch (part) { case Sql.DateParts.Year: partStr = "Y"; break; case Sql.DateParts.Quarter: builder.Expression = "Cast(strFTime('%m', {date}) as int)"; builder.ResultExpression = builder.Inc(builder.Div(builder.Dec(builder.ConvertToSqlExpression(Precedence.Primary)), 3)); break; case Sql.DateParts.Month: partStr = "m"; break; case Sql.DateParts.DayOfYear: partStr = "j"; break; case Sql.DateParts.Day: partStr = "d"; break; case Sql.DateParts.Week: partStr = "W"; break; case Sql.DateParts.WeekDay: builder.Expression = "Cast(strFTime('%w', {date}) as int)"; builder.ResultExpression = builder.Inc(builder.ConvertToSqlExpression(Precedence.Primary)); break; case Sql.DateParts.Hour: partStr = "H"; break; case Sql.DateParts.Minute: partStr = "M"; break; case Sql.DateParts.Second: partStr = "S"; break; case Sql.DateParts.Millisecond: builder.Expression = "Cast(strFTime('%f', {date}) * 1000 as int) % 1000"; builder.Extension.Precedence = Precedence.Multiplicative; break; default: throw new ArgumentOutOfRangeException(nameof(part), part, null); } if (partStr != null) { builder.AddExpression("part", partStr); } }
public void Build(Sql.ISqExtensionBuilder builder) { string partStr; var part = builder.GetValue <Sql.DateParts>("part"); switch (part) { case Sql.DateParts.Year: partStr = "year"; break; case Sql.DateParts.Quarter: builder.Expression = "Extract(Month from {date})"; builder.ResultExpression = builder.Inc(builder.Div(builder.Dec(builder.ConvertToSqlExpression(Precedence.Primary)), 3)); return; case Sql.DateParts.Month: partStr = "month"; break; case Sql.DateParts.DayOfYear: partStr = "yearday"; break; case Sql.DateParts.Day: partStr = "day"; break; case Sql.DateParts.Week: partStr = "week"; break; case Sql.DateParts.WeekDay: partStr = "weekday"; break; case Sql.DateParts.Hour: partStr = "hour"; break; case Sql.DateParts.Minute: partStr = "minute"; break; case Sql.DateParts.Second: partStr = "second"; break; case Sql.DateParts.Millisecond: partStr = "millisecond"; break; default: throw new ArgumentOutOfRangeException(); } builder.AddExpression("part", partStr); switch (part) { case Sql.DateParts.DayOfYear: case Sql.DateParts.WeekDay: builder.ResultExpression = builder.Inc(builder.ConvertToSqlExpression(Precedence.Primary)); break; } }
public void Build(Sql.ISqExtensionBuilder builder) { string exprStr; var part = builder.GetValue <Sql.DateParts>("part"); switch (part) { case Sql.DateParts.Year: exprStr = "Year({date})"; break; case Sql.DateParts.Quarter: { builder.Expression = "Month({date})"; builder.ResultExpression = builder.Inc(builder.Div(builder.Dec(builder.ConvertToSqlExpression(Precedence.Primary)), 3)); return; } case Sql.DateParts.Month: exprStr = "Month({date})"; break; case Sql.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 Sql.DateParts.Day: exprStr = "Day({date})"; break; case Sql.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 Sql.DateParts.WeekDay: { builder.Expression = "weekDay({date})"; builder.ResultExpression = builder.Inc(builder.ConvertToSqlExpression(Precedence.Primary)); return; } case Sql.DateParts.Hour: exprStr = "({date}::datetime Hour to Hour)::char(3)::int"; break; case Sql.DateParts.Minute: exprStr = "({date}::datetime Minute to Minute)::char(3)::int"; break; case Sql.DateParts.Second: exprStr = "({date}::datetime Second to Second)::char(3)::int"; break; case Sql.DateParts.Millisecond: exprStr = "Millisecond({date})"; break; default: throw new ArgumentOutOfRangeException(); } builder.Expression = exprStr; }