Exemplo n.º 1
0
        /// <summary>
        /// 返回时间加减的表达式1:DAY 2:HOUR 3:MINUTE 4:SECOND
        /// </summary>
        /// <param name="connType"></param>
        /// <param name="dateType"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static string GetDateAddString(this DbConnType connType, DATEPART dateType, int Value)
        {
            if (dateType == DATEPART.MS)
            {
                throw new NotSupportedException("DATEPART.MS");
            }
            string sym = "+";

            if (0 > Value)//减
            {
                Value *= -1;
                sym    = "-";
            }
            string result = "";

            switch (dateType)
            {
            case  DATEPART.DAY:     //天数
                if (connType.IsMySQL())
                {
                    result = $"{sym} INTERVAL {Value} DAY";
                }
                else if (connType == DbConnType.POSTGRESQL)
                {
                    result = $"{sym} interval '{Value} days'";
                }
                else
                {
                    result = $"{sym}{Value}";
                }
                break;

            case DATEPART.HOUR:     //小时
                if (connType.IsMySQL())
                {
                    result = $"{sym} INTERVAL {Value} HOUR";
                }
                else if (connType == DbConnType.POSTGRESQL)
                {
                    result = $"{sym} interval '{Value} hours'";
                }
                else
                {
                    result = $"{sym}{Value}/24.0";
                }
                break;

            case DATEPART.MINUTE:     //分时
                if (connType.IsMySQL())
                {
                    result = $"{sym} INTERVAL {Value} MINUTE";
                }
                else if (connType == DbConnType.POSTGRESQL)
                {
                    result = $"{sym} interval '{Value} minutes'";
                }
                else
                {
                    result = $"{sym}{Value}/1440.0";
                }
                break;

            case DATEPART.SECOND:
                if (connType.IsMySQL())
                {
                    result = $"{sym} INTERVAL {Value} seconds";
                }
                else if (connType == DbConnType.POSTGRESQL)
                {
                    result = $"{sym} interval '{Value} seconds'";
                }
                else
                {
                    result = $"{sym}{Value}/86400.0";
                }
                break;
            }
            return(result);
        }
Exemplo n.º 2
0
 public static string GetDateAddString(this DbContext connInfo, DATEPART dateType, int Value)
 {
     return(connInfo.DbType.GetDateAddString(dateType, Value));
 }