/// <summary> /// 获取 Time 类型的 SQL 片断 /// </summary> /// <param name="value">值</param> /// <param name="dbType">数据类型</param> /// <param name="scale">小数位</param> /// <returns></returns> protected override string GetSqlValueOfTime(object value, object dbType, int?scale) { // the range is '-838:59:59.000000' to '838:59:59.000000' new TimeSpan(-34, -22, -59, -59)~new TimeSpan(34, 22, 59, 59); // https://dev.mysql.com/doc/refman/8.0/en/time.html TimeSpan ts = (TimeSpan)value; int hours = (int)ts.TotalHours; // 默认精度为7 string format = @"mm\:ss\.ffffff"; if (MySqlUtils.IsTime(dbType)) { string s = string.Empty; if (scale != null && scale.Value > 0) { s = string.Empty.PadLeft(scale.Value > 6 ? 6 : scale.Value, 'f'); } if (!string.IsNullOrEmpty(s)) { format = string.Format(@"mm\:ss\.{0}", s); } } string result = ts.ToString(format); result = string.Format("{0}:{1}", hours, result); result = this.EscapeQuote(result, false, false); return(result); }
/// <summary> /// 获取 DatetTime 类型的 SQL 片断 /// </summary> /// <param name="value">值</param> /// <param name="dbType">数据类型</param> /// <param name="scale">小数位</param> /// <returns></returns> protected override string GetSqlValueOfDateTime(object value, object dbType, int?scale) { // 默认精度为0 string format = "yyyy-MM-dd HH:mm:ss"; if (MySqlUtils.IsDate(dbType)) { format = "yyyy-MM-dd"; } else if (MySqlUtils.IsDateTime(dbType)) { string s = string.Empty; if (scale != null && scale.Value > 0) { s = string.Empty.PadLeft(scale.Value > 6 ? 6 : scale.Value, 'f'); } if (!string.IsNullOrEmpty(s)) { format = string.Format("yyyy-MM-dd HH:mm:ss.{0}", s); } } string result = this.EscapeQuote(((DateTime)value).ToString(format), false, false); return(result); }
/// <summary> /// 访问 EndWidth 方法 /// </summary> /// <param name="m">方法表达式</param> protected override Expression VisitEndsWith(MethodCallExpression m) { _visitor.Visit(m.Object); if (this.NotOperands != null && this.NotOperands.Contains(m)) { _builder.Append(" NOT"); } _builder.Append(" LIKE "); if (m.Arguments[0].CanEvaluate()) { ColumnAttribute column = null; bool isUnicode = MySqlUtils.IsUnicode(_visitedMark.Current, out column); string value = _constor.GetSqlValue(m.Arguments[0].Evaluate().Value, _builder.TranslateContext, column); if (!_builder.Parameterized && value != null) { value = value.TrimStart('N').Trim('\''); } if (_builder.Parameterized) { _builder.Append("CONCAT("); _builder.Append("'%',"); _builder.Append(value); _builder.Append(')'); } else { if (isUnicode) { _builder.Append('N'); } _builder.Append("'%"); _builder.Append(value); _builder.Append("'"); } } else { _builder.Append("CONCAT('%',"); _visitor.Visit(m.Arguments[0]); _builder.Append(")"); } return(m); }
/// <summary> /// 设置命令参数对象的 DbType属性 /// </summary> /// <param name="parameter">命令参数对象</param> /// <param name="dbType">DbType属性</param> public static void DbType(this MySqlParameter parameter, object dbType) { if (dbType != null) { if (dbType is DbType) { parameter.DbType = (DbType)dbType; } else if (dbType is MySqlDbType) { parameter.MySqlDbType = (MySqlDbType)dbType; } else { MySqlUtils.ThrowException(dbType); } } }
/// <summary> /// 是否日期+时间类型 /// </summary> /// <param name="dbType">DbType属性</param> /// <returns></returns> public static bool IsDateTime(object dbType) { if (dbType == null) { return(false); } else if (dbType is DbType) { return(((DbType)dbType) == System.Data.DbType.DateTime); } else if (dbType is MySqlDbType) { return(((MySqlDbType)dbType) == MySqlDbType.DateTime); } else { return(MySqlUtils.ThrowException(dbType)); } }
///// <summary> ///// 检查字段或属性成员声明的 DbType 是否为 Unicode 数据类型 ///// </summary> ///// <param name="m">将要检查的字段或属性成员</param> ///// <returns></returns> //public static bool IsUnicode(MemberVisitedStack.VisitedMember m) //{ // ColumnAttribute column = null; // return DbTypeUtils.IsUnicode(m, out column); //} /// <summary> /// 检查字段或属性成员声明的 DbType 是否为 Unicode 数据类型 /// </summary> /// <param name="m">将要检查的字段或属性成员</param> /// <param name="column">字段或属性成员显示声明的列特性</param> /// <returns></returns> public static bool IsUnicode(MemberVisitedStack.VisitedMember m, out ColumnAttribute column) { column = m != null?TypeUtils.GetColumnAttribute(m.Member, m.ReflectedType) : null; return(MySqlUtils.IsUnicode(column == null ? null : column.DbType)); }
/// <summary> /// 是否日期+时间+精度类型 /// </summary> /// <param name="dbType">DbType属性</param> /// <returns></returns> public static bool IsDateTime2(object dbType) { return(MySqlUtils.IsDateTime(dbType)); }
/// <summary> /// 获取 DateTimeOffset 类型的 SQL 片断 /// </summary> /// <param name="value">值</param> /// <param name="dbType">数据类型</param> /// <param name="scale">小数位</param> /// <returns></returns> protected override string GetSqlValueOfDateTimeOffset(object value, object dbType, int?scale = null) { MySqlUtils.IsDateTimeOffset(dbType); return(null); }