using System; using System.Linq; namespace SqlSugar.MySqlConnector { public class MySqlExpressionContext : ExpressionContext, ILambdaExpressions { public SqlSugarProvider Context { get; set; } public MySqlExpressionContext() { base.DbMehtods = new MySqlMethod(); } public override string SqlTranslationLeft { get { return "`"; } } public override string SqlTranslationRight { get { return "`"; } } } public class MySqlMethod : DefaultDbMethod, IDbMethods { public override string DateDiff(MethodCallExpressionModel model) { var parameter = model.Args[0]; var parameter2 = model.Args[1]; var parameter3 = model.Args[2]; return string.Format(" TIMESTAMPDIFF({0},{1},{2}) ", parameter.MemberValue?.ToString().ToSqlFilter(), parameter2.MemberName, parameter3.MemberName); } public override string DateValue(MethodCallExpressionModel model) { var parameter = model.Args[0]; var parameter2 = model.Args[1]; if (parameter.MemberName != null && parameter.MemberName is DateTime) { return string.Format(" {0}('{1}') ", parameter2.MemberValue, parameter.MemberName); } else { return string.Format(" {0}({1}) ", parameter2.MemberValue, parameter.MemberName); } } public override string Contains(MethodCallExpressionModel model) { var parameter = model.Args[0]; var parameter2 = model.Args[1]; return string.Format(" ({0} like concat('%',{1},'%')) ", parameter.MemberName, parameter2.MemberName ); } public override string StartsWith(MethodCallExpressionModel model) { var parameter = model.Args[0]; var parameter2 = model.Args[1]; return string.Format(" ({0} like concat({1},'%')) ", parameter.MemberName, parameter2.MemberName); } public override string EndsWith(MethodCallExpressionModel model) { var parameter = model.Args[0]; var parameter2 = model.Args[1]; return string.Format(" ({0} like concat('%',{1}))", parameter.MemberName,parameter2.MemberName); } public override string DateIsSameDay(MethodCallExpressionModel model) { var parameter = model.Args[0]; var parameter2 = model.Args[1]; return string.Format(" (TIMESTAMPDIFF(day,date({0}),date({1}))=0) ", parameter.MemberName, parameter2.MemberName); ; } public override string DateIsSameByType(MethodCallExpressionModel model) { var parameter = model.Args[0]; var parameter2 = model.Args[1]; var parameter3 = model.Args[2]; return string.Format(" (TIMESTAMPDIFF({2},{0},{1})=0) ", parameter.MemberName, parameter2.MemberName, parameter3.MemberValue); } public override string DateAddByType(MethodCallExpressionModel model) { var parameter = model.Args[0]; var parameter2 = model.Args[1]; var parameter3 = model.Args[2]; if (parameter3.MemberValue.ObjToString() == "Millisecond") { parameter3.MemberValue = "Second"; return string.Format(" (DATE_ADD({1} , INTERVAL {2}/1000 {0})) ", parameter3.MemberValue, parameter.MemberName, parameter2.MemberName); } return string.Format(" (DATE_ADD({1} , INTERVAL {2} {0})) ", parameter3.MemberValue, parameter.MemberName, parameter2.MemberName); } public override string DateAddDay(MethodCallExpressionModel model) { var parameter = model.Args[0]; var parameter2 = model.Args[1]; return string.Format(" (DATE_ADD({0}, INTERVAL {1} day)) ", parameter.MemberName, parameter2.MemberName); } public override string ToInt32(MethodCallExpressionModel model) { var parameter = model.Args[0]; return string.Format(" CAST({0} AS SIGNED)", parameter.MemberName); } public override string ToInt64(MethodCallExpressionModel model) { var parameter = model.Args[0]; return string.Format(" CAST({0} AS SIGNED)", parameter.MemberName); } public override string ToString(MethodCallExpressionModel model) { var parameter = model.Args[0]; return string.Format(" CAST({0} AS CHAR)", parameter.MemberName); } public override string ToGuid(MethodCallExpressionModel model) { var parameter = model.Args[0]; return string.Format(" CAST({0} AS CHAR)", parameter.MemberName); } public override string ToDouble(MethodCallExpressionModel model) { var parameter = model.Args[0]; return string.Format(" CAST({0} AS DECIMAL(18,4))", parameter.MemberName); } public override string ToBool(MethodCallExpressionModel model) { var parameter = model.Args[0]; return string.Format(" CAST({0} AS SIGNED)", parameter.MemberName); } public override string ToDecimal(MethodCallExpressionModel model) { var parameter = model.Args[0]; return string.Format(" CAST({0} AS DECIMAL(18,4))", parameter.MemberName); } public override string Length(MethodCallExpressionModel model) { var parameter = model.Args[0]; return string.Format(" LENGTH({0})", parameter.MemberName); } public override string MergeString(params string[] strings) { return " concat("+string.Join(",", strings) + ") "; } public override string IsNull(MethodCallExpressionModel model) { var parameter = model.Args[0]; var parameter1 = model.Args[1]; return string.Format("IFNULL({0},{1})", parameter.MemberName, parameter1.MemberName); } public override string GetDate() { return "NOW()"; } public override string GetRandom() { return "rand()"; } public override string CharIndex(MethodCallExpressionModel model) { return string.Format("instr ({0},{1})", model.Args[0].MemberName, model.Args[1].MemberName); } } }