sqlsugar入門(2)-DateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")源碼修改

1.註釋SqlSugar\ExpressionsToSql\ResolveItems\MethodCallExpressionResolve文件下的GetMethodValue方法

 

case "ToString":
                        //if (model.Args.Count > 1 && model.Args.Last().MemberValue.ObjToString().IsContainsIn("-", "/", ":", "yy", "ms", "hh"))
                        //{
                        //    return GeDateFormat(model.Args.Last().MemberValue.ObjToString(), model.Args.First().MemberName.ObjToString());
                        //}
                        //Check.Exception(model.Args.Count > 1, "ToString (Format) is not supported, Use ToString().If time formatting can be used it.Date.Year+\"-\"+it.Data.Month+\"-\"+it.Date.Day ");
                        return this.Context.DbMehtods.ToString(model);

 

來支持tostring的解析。

2.修改SqlSugar\ExpressionsToSql\DbMethods\DefaultDbMethod 來支持sqlserver的解析

public virtual string ToString(MethodCallExpressionModel model)
        {
            if (model.Args.Count == 1)
            {
                var parameter = model.Args[0];
                return string.Format(" CAST({0} AS NVARCHAR(MAX))", parameter.MemberName);
            }
            else if (model.Args.Count == 2)
            {
                if (model.Args[0].MemberName.ToString().StartsWith("@"))
                {
                    return string.Format("Format(CAST ({0} as datetime),'{1}')", model.Args[0].MemberName, model.Args[1].MemberValue);
                }
                else
                {
                    return string.Format("Format({0},'{1}')", model.Args[0].MemberName, model.Args[1].MemberValue);
                }
            }
            return "";
        }

 3.修改SqlSugar\Realization\MySql\SqlBuilder\MySqlExpressionContext 來支持mysql的解析

public override string ToString(MethodCallExpressionModel model)
        {
            if (model.Args.Count == 1)
            {
                var parameter = model.Args[0];
                return string.Format(" CAST({0} AS CHAR)", parameter.MemberName);
            }
            else if (model.Args.Count == 2)
            {
                var formatstr = model.Args[1].MemberValue.ToString();

                if (formatstr.Contains("mm"))
                {
                    formatstr = formatstr.Replace("mm", "%I");
                }
                else if (formatstr.Contains("m"))
                {
                    formatstr = formatstr.Replace("m", "%i");
                }

                if (formatstr.Contains("yyyy"))
                {
                    formatstr = formatstr.Replace("yyyy", "%Y");
                }
                else if (formatstr.Contains("yy"))
                {
                    formatstr = formatstr.Replace("yy", "%Y");
                }
                if (formatstr.Contains("MM"))
                {
                    formatstr = formatstr.Replace("MM", "%m");
                }
                else if (formatstr.Contains("M"))
                {
                    formatstr = formatstr.Replace("M", "%c");
                }
                if (formatstr.Contains("dd"))
                {
                    formatstr = formatstr.Replace("dd", "%d");
                }
                else if (formatstr.Contains("d"))
                {
                    formatstr = formatstr.Replace("d", "%e");
                }
                if (formatstr.Contains("HH"))
                {
                    formatstr = formatstr.Replace("HH", "%H");
                }
                else if (formatstr.Contains("hh"))
                {
                    formatstr = formatstr.Replace("hh", "%h");
                }
                else if (formatstr.Contains("H"))
                {
                    formatstr = formatstr.Replace("H", "%k");
                }
                else if (formatstr.Contains("h"))
                {
                    formatstr = formatstr.Replace("h", "%l");
                }

                if (formatstr.Contains("ss"))
                {
                    formatstr = formatstr.Replace("ss", "%S");
                }
                else if (formatstr.Contains("s"))
                {
                    formatstr = formatstr.Replace("s", "%s");
                }
                var count = formatstr.ToCharArray().Where(o => o == 'f').Count();
                if (count > 0)
                {
                    var s = "";
                    s = s.PadLeft(count, 'f');
                    formatstr = formatstr.Replace(s, "%f");
                    count = 6 - count;
                }

                if (count > 0)
                {
                    var s = string.Format("DATE_FORMAT({0},'{1}')", model.Args[0].MemberName, formatstr);
                    return string.Format("SUBSTR({0},1,LENGTH({0})-{1})", s, count);
                }
                return string.Format("DATE_FORMAT({0},'{1}')", model.Args[0].MemberName, formatstr);
            }
            return "";
        }

4.修改SqlSugar\Realization\Oracle\SqlBuilder\OracleExpressionContext來支持Oracle的解析

public override string ToString(MethodCallExpressionModel model)
        {
            if (model.Args.Count == 1)
            {
                var parameter = model.Args[0];
                return string.Format(" CAST({0} AS VARCHAR2(4000))", parameter.MemberName);
            }
            else if (model.Args.Count == 2)
            {
                var formatstr = model.Args[1].MemberValue.ToString();
                if (formatstr.Contains("MM"))
                {

                }
                else if (formatstr.Contains("M"))
                {
                    formatstr = formatstr.Replace("M", "FMMM");
                }

                if (formatstr.Contains("dd"))
                {

                }
                else if (formatstr.Contains("d"))
                {
                    formatstr = formatstr.Replace("d", "FMdd");
                }

                if (formatstr.Contains("HH"))
                {
                    formatstr = formatstr.Replace("HH", "HH24");
                }
                else if (formatstr.Contains("H"))
                {
                    formatstr = formatstr.Replace("H", "FMHH24");
                }

                if (formatstr.Contains("hh"))
                {

                }
                else if (formatstr.Contains("h"))
                {
                    formatstr = formatstr.Replace("h", "FMhh");
                }

                if (formatstr.Contains("mm"))
                {
                    formatstr = formatstr.Replace("mm", "mi");
                }
                else if (formatstr.Contains("m"))
                {
                    formatstr = formatstr.Replace("m", "FMmi");
                }

                if (formatstr.Contains("ss"))
                {

                }
                else if (formatstr.Contains("s"))
                {
                    formatstr = formatstr.Replace("s", "FMss");
                }
                return string.Format("to_char({0},'{1}')", model.Args[0].MemberName, formatstr);
            }
            return "";
        }

 

5.查看是否按指定格式進行解析

var ssc = SqlFuncEx.GetDB("server=127.0.0.1;database=SQLSUGAR4XTEST;uid=sa;pwd=123456");
            var sql = ssc.Queryable<Student>()
                .Where(o => o.CreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") == "2020-10-11 18:38:05.820")
                .ToSqlString();

            var sscmy = SqlFuncEx.GetDB("server=127.0.0.1;database=SQLSUGAR4XTEST;uid=sa;pwd=123456", DbType.MySql);
            var sql1 = sscmy.Queryable<Student>()
                .Where(o => o.CreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") == "2020-10-11 18:38:05.820")
                .ToSqlString();

            var ssco = SqlFuncEx.GetDB("server=127.0.0.1;database=SQLSUGAR4XTEST;uid=sa;pwd=123456", DbType.Oracle);
            var sql2 = ssco.Queryable<Student>()
                .Where(o => o.CreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") == "2020-10-11 18:38:05.820")
                .ToSqlString();

6.Demo地址:https://gitee.com/xuanyun2018/sqlsugardemo.git

解析代碼寫的有些簡陋若有紕漏請自行修改。下一章節修改源碼支持多主鍵保存。mysql

相關文章
相關標籤/搜索