Browse Source

Synchronization code

pull/30/head
sunkaixuan 2 years ago
parent
commit
5432bd9901
  1. 9
      Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs
  2. 10
      Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs
  3. 10
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/SugarParameter.cs
  4. 27
      Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs

9
Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs

@ -273,6 +273,7 @@ namespace SqlSugar
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
}
private int GetDbColumnIndex = 0;
public virtual string GetDbColumn(DbColumnInfo columnInfo ,object name)
{
if (columnInfo.InsertServerTime)
@ -283,6 +284,14 @@ namespace SqlSugar
{
return columnInfo.InsertSql;
}
else if (columnInfo.PropertyType.Name == "TimeOnly" && name!=null&&!name.ObjToString().StartsWith(Builder.SqlParameterKeyWord))
{
var timeSpan = UtilMethods.TimeOnlyToTimeSpan(columnInfo.Value);
var pname =Builder.SqlParameterKeyWord+columnInfo.DbColumnName+ "_ts"+ GetDbColumnIndex;
this.Parameters.Add(new SugarParameter(pname, timeSpan));
GetDbColumnIndex++;
return pname;
}
else
{
return name+"";

10
Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs

@ -386,7 +386,7 @@ namespace SqlSugar
var date = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value);
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
}
private int GetDbColumnIndex = 0;
public virtual string GetDbColumn(DbColumnInfo columnInfo, object name)
{
if (columnInfo.UpdateServerTime)
@ -397,6 +397,14 @@ namespace SqlSugar
{
return columnInfo.UpdateSql;
}
else if (columnInfo.PropertyType.Name == "TimeOnly" && name != null && !name.ObjToString().StartsWith(Builder.SqlParameterKeyWord))
{
var timeSpan = UtilMethods.TimeOnlyToTimeSpan(columnInfo.Value);
var pname = Builder.SqlParameterKeyWord + columnInfo.DbColumnName + "_ts" + GetDbColumnIndex;
this.Parameters.Add(new SugarParameter(pname, timeSpan));
GetDbColumnIndex++;
return pname;
}
else
{
return name + "";

10
Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/SugarParameter.cs

@ -162,6 +162,16 @@ namespace SqlSugar
{
this.DbType = System.Data.DbType.UInt16;
}
else if (type.Name == "TimeOnly")
{
this.DbType = System.Data.DbType.Time;
this.Value = UtilMethods.TimeOnlyToTimeSpan(this.Value);
}
else if (type.Name == "DateOnly")
{
this.DbType = System.Data.DbType.Date;
this.Value = UtilMethods.DateOnlyToDateTime(this.Value);
}
}
public SugarParameter(string name, object value, bool isOutput)

27
Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs

@ -116,6 +116,19 @@ namespace SqlSugar
if (destinationType.IsEnum && value is int)
return Enum.ToObject(destinationType, (int)value);
if (destinationType.Name == "TimeOnly"&& sourceType.Name!= "TimeOnly")
{
var type = Type.GetType("System.TimeOnly", true, true);
var method=type.GetMethods().FirstOrDefault(it => it.GetParameters().Length == 1 && it.Name == "FromTimeSpan");
return method.Invoke(null, new object[] { value });
}
if (destinationType.Name == "DateOnly" && sourceType.Name != "DateOnly")
{
var type = Type.GetType("System.DateOnly", true, true);
var method = type.GetMethods().FirstOrDefault(it => it.GetParameters().Length == 1 && it.Name == "FromDateTime");
return method.Invoke(null, new object[] { value });
}
if (!destinationType.IsInstanceOfType(value))
return Convert.ChangeType(value, destinationType, culture);
}
@ -1064,5 +1077,19 @@ namespace SqlSugar
{
return $"[value=sql{UtilConstants.ReplaceKey}]";
}
internal static object TimeOnlyToTimeSpan(object value)
{
if (value == null) return null;
var method = value.GetType().GetMethods().First(it => it.GetParameters().Length == 0 && it.Name == "ToTimeSpan");
return method.Invoke(value, new object[] { });
}
internal static object DateOnlyToDateTime(object value)
{
if (value == null) return null;
var method = value.GetType().GetMethods().First(it => it.GetParameters().Length == 0 && it.Name == "ToShortDateString");
return method.Invoke(value, new object[] { });
}
}
}

Loading…
Cancel
Save